1
- import { useEffect , useRef } from 'react' ;
1
+ import { useEffect , useRef , MutableRefObject } from 'react' ;
2
2
import useKeyDownHandlers from './useKeyDownHandlers' ;
3
3
4
4
/**
@@ -14,21 +14,24 @@ import useKeyDownHandlers from './useKeyDownHandlers';
14
14
*
15
15
* Returns a ref to attach to the outermost element of the modal.
16
16
*
17
- * @param { () => void } onClose
18
- * @param { React.MutableRefObject<HTMLElement | null> } [passedRef]
19
- * @return { React.MutableRefObject<HTMLElement | null> }
17
+ * @param onClose - Function called when modal should close
18
+ * @param passedRef - Optional ref to the modal element. If not provided, one is created internally.
19
+ * @returns A ref to be attached to the modal DOM element
20
20
*/
21
- export default function useModalClose ( onClose , passedRef ) {
22
- const createdRef = useRef ( null ) ;
23
- const modalRef = passedRef || createdRef ;
21
+ export default function useModalClose (
22
+ onClose : ( ) => void ,
23
+ passedRef ?: MutableRefObject < HTMLElement | null >
24
+ ) : MutableRefObject < HTMLElement | null > {
25
+ const createdRef = useRef < HTMLElement | null > ( null ) ;
26
+ const modalRef = passedRef ?? createdRef ;
24
27
25
28
useEffect ( ( ) => {
26
29
modalRef . current ?. focus ( ) ;
27
30
28
- function handleClick ( e ) {
31
+ function handleClick ( e : MouseEvent ) {
29
32
// ignore clicks on the component itself
30
- if ( modalRef . current && ! modalRef . current . contains ( e . target ) ) {
31
- onClose ?. ( ) ;
33
+ if ( modalRef . current && ! modalRef . current . contains ( e . target as Node ) ) {
34
+ onClose ( ) ;
32
35
}
33
36
}
34
37
0 commit comments