Skip to content

Commit

Permalink
compose: Make useRefEffect generic; also fix docgen for default exp…
Browse files Browse the repository at this point in the history
…orts
  • Loading branch information
sarayourfriend committed May 25, 2021
1 parent 8cb5044 commit 95e250f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
6 changes: 3 additions & 3 deletions packages/compose/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ callback will be called multiple times for the same node.

_Parameters_

- _callback_ `(node: HTMLElement) => (() => void) | undefined`: Callback with ref as argument.
- _dependencies_ `import('react').DependencyList`: Dependencies of the callback.
- _callback_ `( node: TElement ) => ( () => void ) | undefined`: Callback with ref as argument.
- _dependencies_ `DependencyList`: Dependencies of the callback.

_Returns_

- `import('react').RefCallback<HTMLElement | null>`: Ref callback.
- `RefCallback< TElement | null >`: Ref callback.

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/**
* External dependencies
*/
// eslint-disable-next-line no-restricted-imports
import type { DependencyList, RefCallback } from 'react';

/**
* WordPress dependencies
*/
Expand All @@ -17,15 +23,17 @@ import { useCallback, useRef } from '@wordpress/element';
* to be removed. It *is* necessary if you add dependencies because the ref
* callback will be called multiple times for the same node.
*
* @param {(node: HTMLElement) => (() => void) | undefined} callback Callback with ref as argument.
* @param {import('react').DependencyList} dependencies Dependencies of the callback.
* @param callback Callback with ref as argument.
* @param dependencies Dependencies of the callback.
*
* @return {import('react').RefCallback<HTMLElement | null>} Ref callback.
* @return Ref callback.
*/
export default function useRefEffect( callback, dependencies ) {
/** @type {import('react').MutableRefObject<(() => void) | undefined>} */
const cleanup = useRef();
return useCallback( ( /** @type {HTMLElement | null} */ node ) => {
export default function useRefEffect< TElement = Node >(
callback: ( node: TElement ) => ( () => void ) | undefined,
dependencies: DependencyList
): RefCallback< TElement | null > {
const cleanup = useRef< ( () => void ) | undefined >();
return useCallback( ( node: TElement | null ) => {
if ( node ) {
cleanup.current = callback( node );
} else if ( cleanup.current ) {
Expand Down
8 changes: 7 additions & 1 deletion packages/docgen/lib/get-type-annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ function getFunctionTypeAnnotation( typeAnnotation, returnIndicator ) {
typeAnnotation.typeAnnotation.typeAnnotation
);

return `( ${ params } )${ returnIndicator }${ returnType }`;
const paramsWithParens = params.length ? `( ${ params } )` : `()`;

return `${ paramsWithParens }${ returnIndicator }${ returnType }`;
}

/**
Expand Down Expand Up @@ -376,6 +378,10 @@ function getTypeAnnotation( typeAnnotation ) {
*/
function getFunctionToken( token ) {
let resolvedToken = token;
if ( babelTypes.isExportDefaultDeclaration( resolvedToken ) ) {
resolvedToken = resolvedToken.declaration;
}

if ( babelTypes.isExportNamedDeclaration( resolvedToken ) ) {
resolvedToken = resolvedToken.declaration;
}
Expand Down

0 comments on commit 95e250f

Please sign in to comment.