Skip to content

Commit 7d5fa9d

Browse files
compose: Add types to useFocusReturn (#31949)
* compose: Add types to `useFocusReturn` * Slightly improve types * Use null as default value * Fix missing null type. Co-authored-by: Marco Ciampini <[email protected]> Co-authored-by: Marco Ciampini <[email protected]>
1 parent a33e453 commit 7d5fa9d

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

packages/compose/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,11 +261,11 @@ const WithFocusReturn = () => {
261261

262262
_Parameters_
263263

264-
- _onFocusReturn_ `Function?`: Overrides the default return behavior.
264+
- _onFocusReturn_ `[() => void]`: Overrides the default return behavior.
265265

266266
_Returns_
267267

268-
- `Function`: Element Ref.
268+
- `import('react').RefCallback<HTMLElement>`: Element Ref.
269269

270270
<a name="useInstanceId" href="#useInstanceId">#</a> **useInstanceId**
271271

packages/compose/src/hooks/use-focus-return/index.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import { useRef, useEffect, useCallback } from '@wordpress/element';
99
* previously focused element when closed.
1010
* The current hook implements the returning behavior.
1111
*
12-
* @param {Function?} onFocusReturn Overrides the default return behavior.
13-
* @return {Function} Element Ref.
12+
* @param {() => void} [onFocusReturn] Overrides the default return behavior.
13+
* @return {import('react').RefCallback<HTMLElement>} Element Ref.
1414
*
1515
* @example
1616
* ```js
@@ -28,8 +28,10 @@ import { useRef, useEffect, useCallback } from '@wordpress/element';
2828
* ```
2929
*/
3030
function useFocusReturn( onFocusReturn ) {
31-
const ref = useRef();
32-
const focusedBeforeMount = useRef();
31+
/** @type {import('react').MutableRefObject<null | HTMLElement>} */
32+
const ref = useRef( null );
33+
/** @type {import('react').MutableRefObject<null | Element>} */
34+
const focusedBeforeMount = useRef( null );
3335
const onFocusReturnRef = useRef( onFocusReturn );
3436
useEffect( () => {
3537
onFocusReturnRef.current = onFocusReturn;
@@ -47,11 +49,11 @@ function useFocusReturn( onFocusReturn ) {
4749

4850
focusedBeforeMount.current = node.ownerDocument.activeElement;
4951
} else if ( focusedBeforeMount.current ) {
50-
const isFocused = ref.current.contains(
51-
ref.current.ownerDocument.activeElement
52+
const isFocused = ref.current?.contains(
53+
ref.current?.ownerDocument.activeElement
5254
);
5355

54-
if ( ref.current.isConnected && ! isFocused ) {
56+
if ( ref.current?.isConnected && ! isFocused ) {
5557
return;
5658
}
5759

@@ -62,7 +64,7 @@ function useFocusReturn( onFocusReturn ) {
6264
if ( onFocusReturnRef.current ) {
6365
onFocusReturnRef.current();
6466
} else {
65-
focusedBeforeMount.current.focus();
67+
/** @type {null | HTMLElement} */ ( focusedBeforeMount.current )?.focus();
6668
}
6769
}
6870
}, [] );

packages/compose/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"src/hooks/use-constrained-tabbing/**/*",
1919
"src/hooks/use-debounce/**/*",
2020
"src/hooks/use-instance-id/**/*",
21+
"src/hooks/use-focus-return/**/*",
2122
"src/utils/**/*"
2223
]
2324
}

0 commit comments

Comments
 (0)