Skip to content

Commit c183aff

Browse files
Stop using a forwarded ref for essential/internal functionality
1 parent 04ecb13 commit c183aff

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/components/shared/modals/Modal.tsx

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { forwardRef, PropsWithChildren, useImperativeHandle, useState } from "react";
1+
import { forwardRef, PropsWithChildren, useCallback, useImperativeHandle, useState } from "react";
22
import ReactDOM from "react-dom";
33
import { useHotkeys } from "react-hotkeys-hook";
44
import { availableHotkeys } from "../../../configs/hotkeysConfig";
@@ -40,23 +40,23 @@ export const Modal = forwardRef<ModalHandle, PropsWithChildren<ModalProps>>(({
4040
const { t } = useTranslation();
4141

4242
const [isOpen, setOpen] = useState(open);
43+
const close = useCallback(() => {
44+
if (closeCallback !== undefined && !closeCallback()) {
45+
// Don't close modal
46+
return;
47+
}
48+
setOpen(false);
49+
}, [closeCallback]);
4350

4451
useImperativeHandle(ref, () => ({
4552
isOpen: () => isOpen,
4653
open: () => setOpen(true),
47-
close: () => {
48-
if (closeCallback !== undefined && !closeCallback()) {
49-
// Don't close modal
50-
return;
51-
}
52-
setOpen(false);
53-
},
54-
}), [closeCallback, isOpen]);
54+
close,
55+
}), [close, isOpen]);
5556

5657
useHotkeys(
5758
availableHotkeys.general.CLOSE_MODAL.sequence,
58-
//@ts-expect-error: TODO: Figure out what typescripts problem is
59-
() => ref?.current.close?.(),
59+
close,
6060
{ description: t(availableHotkeys.general.CLOSE_MODAL.description) ?? undefined },
6161
[ref],
6262
);
@@ -72,8 +72,7 @@ export const Modal = forwardRef<ModalHandle, PropsWithChildren<ModalProps>>(({
7272
<header>
7373
<ButtonLikeAnchor
7474
extraClassName="fa fa-times close-modal"
75-
//@ts-expect-error: TODO: Figure out what typescripts problem is
76-
onClick={() => ref?.current.close?.()}
75+
onClick={close}
7776
tabIndex={0}
7877
/>
7978
<h2>

0 commit comments

Comments
 (0)