Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compose: Add types to useMergeRefs #31939

Merged
merged 3 commits into from
May 21, 2021
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
4 changes: 2 additions & 2 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,11 @@ return <div ref={ mergedRefs } />;

_Parameters_

- _refs_ `Array<RefObject|RefCallback>`: The refs to be merged.
- _refs_ `Array<TRef>`: The refs to be merged.

_Returns_

- `RefCallback`: The merged ref callback.
- `import('react').RefCallback<TypeFromRef<TRef>>`: The merged ref callback.

<a name="usePrevious" href="#usePrevious">#</a> **usePrevious**

Expand Down
25 changes: 20 additions & 5 deletions packages/compose/src/hooks/use-merge-refs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,25 @@
*/
import { useRef, useCallback, useLayoutEffect } from '@wordpress/element';

/** @typedef {import('@wordpress/element').RefObject} RefObject */
/** @typedef {import('@wordpress/element').RefCallback} RefCallback */
/* eslint-disable jsdoc/valid-types */
/**
* @template T
* @typedef {T extends import('react').Ref<infer R> ? R : never} TypeFromRef
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need never here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you suggest in place of never, if the template type doesn't extend Ref? never is essentially the error case for this utility type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand, then never makes sense 👍

*/
/* eslint-enable jsdoc/valid-types */

/**
* @template T
* @param {import('react').Ref<T>} ref
* @param {T} value
*/
function assignRef( ref, value ) {
if ( typeof ref === 'function' ) {
ref( value );
} else if ( ref && ref.hasOwnProperty( 'current' ) ) {
ref.current = value;
/* eslint-disable jsdoc/no-undefined-types */
/** @type {import('react').MutableRefObject<T>} */ ( ref ).current = value;
/* eslint-enable jsdoc/no-undefined-types */
}
}

Expand Down Expand Up @@ -52,13 +63,17 @@ function assignRef( ref, value ) {
* return <div ref={ mergedRefs } />;
* ```
*
* @param {Array<RefObject|RefCallback>} refs The refs to be merged.
* @template {import('react').Ref<any>} TRef
* @param {Array<TRef>} refs The refs to be merged.
*
* @return {RefCallback} The merged ref callback.
* @return {import('react').RefCallback<TypeFromRef<TRef>>} The merged ref callback.
*/
export default function useMergeRefs( refs ) {
const element = useRef();
const didElementChange = useRef( false );
/* eslint-disable jsdoc/no-undefined-types */
/** @type {import('react').MutableRefObject<TRef[]>} */
/* eslint-enable jsdoc/no-undefined-types */
const previousRefs = useRef( [] );
const currentRefs = useRef( refs );

Expand Down
1 change: 1 addition & 0 deletions packages/compose/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"src/hooks/use-previous/**/*",
"src/hooks/use-media-query/**/*",
"src/hooks/use-reduced-motion/**/*",
"src/hooks/use-merge-refs/**/*",
"src/utils/**/*"
]
}