Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import type { AngleCircleProps } from './types';

type UseDraggingArgumentType = Parameters< typeof useDragging >[ 0 ];
type UseDraggingCallbackEvent =
| Parameters< UseDraggingArgumentType[ 'onDragStart' ] >[ 0 ]
| Parameters< UseDraggingArgumentType[ 'onDragMove' ] >[ 0 ]
| Parameters< UseDraggingArgumentType[ 'onDragEnd' ] >[ 0 ];
| Parameters< NonNullable< UseDraggingArgumentType[ 'onDragStart' ] > >[ 0 ]
| Parameters< NonNullable< UseDraggingArgumentType[ 'onDragMove' ] > >[ 0 ]
| Parameters< NonNullable< UseDraggingArgumentType[ 'onDragEnd' ] > >[ 0 ];

function AngleCircle( {
value,
Expand Down
80 changes: 42 additions & 38 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,23 +198,23 @@ In Dialogs/modals, the tabbing must be constrained to the content of the wrapper

_Usage_

```js
```ts
import { useConstrainedTabbing } from '@wordpress/compose';

const ConstrainedTabbingExample = () => {
const constrainedTabbingRef = useConstrainedTabbing();
return (
<div ref={ constrainedTabbingRef }>
<Button />
<Button />
</div>
);
};
const constrainedTabbingRef = useConstrainedTabbing()
return (
<div ref={ constrainedTabbingRef }>
<Button />
<Button />
</div>
);
}
```

_Returns_

- `React.RefCallback<Element>`: Element Ref.
- `React.RefCallback< Element >`: Element Ref.

### useCopyOnClick

Expand Down Expand Up @@ -256,12 +256,12 @@ _Related_
_Parameters_

- _fn_ `TFunc`: The function to debounce.
- _wait_ `[number]`: The number of milliseconds to delay.
- _options_ `[import('../../utils/debounce').DebounceOptions]`: The options object.
- _wait_ `number`: The number of milliseconds to delay.
- _options_ `DebounceOptions`: The options object.
Comment thread
im3dabasia marked this conversation as resolved.
Resolved

_Returns_

- `import('../../utils/debounce').DebouncedFunc<TFunc>`: Debounced function.
- `DebouncedFunc< TFunc >`: Debounced function.

### useDebouncedInput

Expand Down Expand Up @@ -374,27 +374,27 @@ Adds the unmount behavior of returning focus to the element which had it previou

_Usage_

```js
```ts
import { useFocusReturn } from '@wordpress/compose';

const WithFocusReturn = () => {
const ref = useFocusReturn();
return (
<div ref={ ref }>
<Button />
<Button />
</div>
);
};
const ref = useFocusReturn()
return (
<div ref={ ref }>
<Button />
<Button />
</div>
);
}
```

_Parameters_

- _onFocusReturn_ `[() => void]`: Overrides the default return behavior.
- _onFocusReturn_ `() => void`: Overrides the default return behavior.
Comment thread
im3dabasia marked this conversation as resolved.
Resolved

_Returns_

- `React.RefCallback<HTMLElement>`: Element Ref.
- `React.RefCallback< HTMLElement >`: Element Ref.

### useInstanceId

Expand Down Expand Up @@ -424,9 +424,13 @@ _Related_

_Parameters_

- _shortcuts_ `string[]|string`: Keyboard Shortcuts.
- _callback_ `(e: Mousetrap.ExtendedKeyboardEvent, combo: string) => void`: Shortcut callback.
- _options_ `WPKeyboardShortcutConfig`: Shortcut options.
- _shortcuts_ `string[] | string`: Keyboard Shortcuts.
- _callback_ `( e: ExtendedKeyboardEvent, combo: string ) => void`: Shortcut callback.
- _options_ `Partial< KeyboardShortcutConfig >`: Shortcut options.
- _options.bindGlobal_ `Partial< KeyboardShortcutConfig >[ 'bindGlobal' ]`:
- _options.eventName_ `Partial< KeyboardShortcutConfig >[ 'eventName' ]`:
- _options.isDisabled_ `Partial< KeyboardShortcutConfig >[ 'isDisabled' ]`:
- _options.target_ `Partial< KeyboardShortcutConfig >[ 'target' ]`:

### useMediaQuery

Expand Down Expand Up @@ -575,42 +579,42 @@ _Related_
_Parameters_

- _fn_ `TFunc`: The function to throttle.
- _wait_ `[number]`: The number of milliseconds to throttle invocations to.
- _options_ `[import('../../utils/throttle').ThrottleOptions]`: The options object. See linked documentation for details.
- _wait_ `number`: The number of milliseconds to throttle invocations to.
- _options_ `ThrottleOptions`: The options object.
Comment thread
im3dabasia marked this conversation as resolved.
Resolved

_Returns_

- `import('../../utils/debounce').DebouncedFunc<TFunc>`: Throttled function.
- `DebouncedFunc< TFunc >`: Throttled function.

### useViewportMatch

Returns true if the viewport matches the given query, or false otherwise.

_Usage_

```js
```ts
useViewportMatch( 'huge', '<' );
useViewportMatch( 'medium' );
```

_Parameters_

- _breakpoint_ `WPBreakpoint`: Breakpoint size name.
- _operator_ `[WPViewportOperator]`: Viewport operator.
- _view_ `[Window|undefined]`: Window instance in which to perform viewport matching.
- _breakpoint_ `Breakpoint`: Breakpoint size name.
- _operator_ `ViewportOperator`: Viewport operator.
- _view_ `Window | undefined`: Window instance in which to perform viewport matching.

_Returns_

- `boolean`: Whether viewport matches query.

### useWarnOnChange

Hook that performs a shallow comparison between the preview value of an object and the new one, if there's a difference, it prints it to the console. this is useful in performance related work, to check why a component re-renders.
Hook that performs a shallow comparison between the previous value of an object and the new one, if there's a difference, it prints it to the console. This is useful in performance related work, to check why a component re-renders.

_Usage_

```jsx
function MyComponent( props ) {
```tsx
function MyComponent( props: Record< string, any > ) {
useWarnOnChange( props );

return 'Something';
Expand All @@ -619,7 +623,7 @@ function MyComponent( props ) {

_Parameters_

- _object_ `object`: Object which changes to compare.
- _object_ `Record< string, any > | any[]`: Object which changes to compare.
- _prefix_ `string`: Just a prefix to show when console logging.

### withGlobalEvents
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import useRefEffect from '../use-ref-effect';
* In Dialogs/modals, the tabbing must be constrained to the content of
* the wrapper element. This hook adds the behavior to the returned ref.
*
* @return {React.RefCallback<Element>} Element Ref.
* @return Element Ref.
*
* @example
* ```js
* ```ts
* import { useConstrainedTabbing } from '@wordpress/compose';
*
* const ConstrainedTabbingExample = () => {
Expand All @@ -29,9 +29,9 @@ import useRefEffect from '../use-ref-effect';
* }
* ```
*/
function useConstrainedTabbing() {
return useRefEffect( ( /** @type {HTMLElement} */ node ) => {
function onKeyDown( /** @type {KeyboardEvent} */ event ) {
function useConstrainedTabbing(): React.RefCallback< Element > {
return useRefEffect( ( node: HTMLElement ) => {
function onKeyDown( event: KeyboardEvent ): void {
const { key, shiftKey, target } = event;

if ( key !== 'Tab' ) {
Expand All @@ -40,19 +40,15 @@ function useConstrainedTabbing() {

const action = shiftKey ? 'findPrevious' : 'findNext';
const nextElement =
focus.tabbable[ action ](
/** @type {HTMLElement} */ ( target )
) || null;
focus.tabbable[ action ]( target as HTMLElement ) || null;

// When the target element contains the element that is about to
// receive focus, for example when the target is a tabbable
// container, browsers may disagree on where to move focus next.
// In this case we can't rely on native browsers behavior. We need
// to manage focus instead.
// See https://github.com/WordPress/gutenberg/issues/46041.
if (
/** @type {HTMLElement} */ ( target ).contains( nextElement )
) {
if ( ( target as HTMLElement ).contains( nextElement ) ) {
event.preventDefault();
nextElement?.focus();
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { useEffect } from '@wordpress/element';
* Internal dependencies
*/
import { debounce } from '../../utils/debounce';
import type { DebounceOptions, DebouncedFunc } from '../../utils/debounce';

/**
* Debounces a function similar to Lodash's `debounce`. A new debounced function will
Expand All @@ -21,14 +22,18 @@ import { debounce } from '../../utils/debounce';
*
* @see https://lodash.com/docs/4#debounce
*
* @template {(...args: any[]) => void} TFunc
* @template TFunc
*
* @param {TFunc} fn The function to debounce.
* @param {number} [wait] The number of milliseconds to delay.
* @param {import('../../utils/debounce').DebounceOptions} [options] The options object.
* @return {import('../../utils/debounce').DebouncedFunc<TFunc>} Debounced function.
* @param fn The function to debounce.
* @param wait The number of milliseconds to delay.
* @param options The options object.
* @return Debounced function.
*/
export default function useDebounce( fn, wait, options ) {
export default function useDebounce< TFunc extends ( ...args: any[] ) => void >(
fn: TFunc,
wait?: number,
options?: DebounceOptions
): DebouncedFunc< TFunc > {
const debounced = useMemoOne(
() => debounce( fn, wait ?? 0, options ),
[ fn, wait, options?.leading, options?.trailing, options?.maxWait ]
Expand Down
4 changes: 3 additions & 1 deletion packages/compose/src/hooks/use-disabled/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import useRefEffect from '../use-ref-effect';
*/
export default function useDisabled( {
isDisabled: isDisabledProp = false,
}: {
isDisabled?: boolean;
} = {} ) {
return useRefEffect(
( node ) => {
Expand All @@ -45,7 +47,7 @@ export default function useDisabled( {
}

/** A variable keeping track of the previous updates in order to restore them. */
const updates: Function[] = [];
const updates: ( () => void )[] = [];
const disable = () => {
node.childNodes.forEach( ( child ) => {
if ( ! ( child instanceof defaultView.HTMLElement ) ) {
Expand Down
74 changes: 0 additions & 74 deletions packages/compose/src/hooks/use-dragging/index.js

This file was deleted.

Loading
Loading