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' ;
22import { useKeyDownHandlers } from './useKeyDownHandlers' ;
33
44/**
@@ -20,10 +20,27 @@ import { useKeyDownHandlers } from './useKeyDownHandlers';
2020 */
2121export function useModalClose < T extends HTMLElement = HTMLElement > (
2222 onClose : ( ) => void ,
23- passedRef ?: MutableRefObject < T | null >
23+ passedRef ?: MutableRefObject < T | null > | ForwardedRef < T >
2424) : MutableRefObject < T | null > {
2525 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+ } ) ( ) ;
2744
2845 useEffect ( ( ) => {
2946 modalRef . current ?. focus ( ) ;
You can’t perform that action at this time.
0 commit comments