Skip to content

Commit cd5a14c

Browse files
committed
fix(Drawer): onClose was being called even when not shown
1 parent 00f5d80 commit cd5a14c

File tree

1 file changed

+7
-3
lines changed
  • packages/orbit-components/src/Drawer

1 file changed

+7
-3
lines changed

packages/orbit-components/src/Drawer/index.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,18 @@ const Drawer = ({
6262

6363
React.useEffect(() => {
6464
const handleKeyDown = (event: KeyboardEvent) => {
65-
if (event.key === "Escape" && onClose) {
65+
if (shown && event.key === "Escape" && onClose) {
6666
onClose();
6767
}
6868
};
6969

7070
document.addEventListener("keydown", handleKeyDown);
7171
return () => document.removeEventListener("keydown", handleKeyDown);
72-
}, [onClose]);
72+
}, [onClose, shown]);
73+
74+
const handleClickOutside = React.useCallback(() => {
75+
if (shown && onClose) onClose();
76+
}, [shown, onClose]);
7377

7478
const vars = {
7579
"--lm-drawer-width": width,
@@ -80,7 +84,7 @@ const Drawer = ({
8084
const onlyIcon = !title && !actions;
8185
const bordered = !!(title || actions);
8286

83-
useClickOutside(drawerRef, () => onClose?.());
87+
useClickOutside(drawerRef, handleClickOutside);
8488

8589
return (
8690
<>

0 commit comments

Comments
 (0)