File tree Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Expand file tree Collapse file tree 1 file changed +20
-3
lines changed Original file line number Diff line number Diff line change 1
- import { useEffect , useRef , MutableRefObject } from 'react' ;
1
+ import { useEffect , useRef , MutableRefObject , ForwardedRef } from 'react' ;
2
2
import { useKeyDownHandlers } from './useKeyDownHandlers' ;
3
3
4
4
/**
@@ -20,10 +20,27 @@ import { useKeyDownHandlers } from './useKeyDownHandlers';
20
20
*/
21
21
export function useModalClose < T extends HTMLElement = HTMLElement > (
22
22
onClose : ( ) => void ,
23
- passedRef ?: MutableRefObject < T | null >
23
+ passedRef ?: MutableRefObject < T | null > | ForwardedRef < T >
24
24
) : MutableRefObject < T | null > {
25
25
const createdRef = useRef < T | null > ( null ) ;
26
- const modalRef = passedRef ?? createdRef ;
26
+
27
+ // Normalize any ref to a MutableRefObject internally
28
+ const modalRef : MutableRefObject < T | null > = ( ( ) => {
29
+ if ( ! passedRef ) return createdRef ;
30
+ if ( typeof passedRef === 'function' ) {
31
+ // For function refs, write to createdRef and call the function
32
+ return {
33
+ get current ( ) {
34
+ return createdRef . current ;
35
+ } ,
36
+ set current ( value : T | null ) {
37
+ createdRef . current = value ;
38
+ passedRef ( value ) ;
39
+ }
40
+ } ;
41
+ }
42
+ return passedRef ;
43
+ } ) ( ) ;
27
44
28
45
useEffect ( ( ) => {
29
46
modalRef . current ?. focus ( ) ;
You can’t perform that action at this time.
0 commit comments