Skip to content

Commit c9d9027

Browse files
committed
useModalClose: update with types
1 parent 1118d08 commit c9d9027

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

client/common/useModalClose.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from 'react';
1+
import { useEffect, useRef, MutableRefObject } from 'react';
22
import useKeyDownHandlers from './useKeyDownHandlers';
33

44
/**
@@ -14,21 +14,24 @@ import useKeyDownHandlers from './useKeyDownHandlers';
1414
*
1515
* Returns a ref to attach to the outermost element of the modal.
1616
*
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
2020
*/
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;
2427

2528
useEffect(() => {
2629
modalRef.current?.focus();
2730

28-
function handleClick(e) {
31+
function handleClick(e: MouseEvent) {
2932
// 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();
3235
}
3336
}
3437

0 commit comments

Comments
 (0)