Skip to content

Commit d1bbe3b

Browse files
committed
fix: keep portal onEsc fallback without double close
1 parent be59728 commit d1bbe3b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Preview/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,14 +327,21 @@ const Preview: React.FC<PreviewProps> = props => {
327327
}
328328
};
329329

330+
const escClosingRef = useRef(false);
331+
330332
// >>>>> Effect: Keyboard
331333
const onKeyDown = useEvent((event: KeyboardEvent) => {
332334
if (open) {
333335
const { keyCode, key } = event;
334336

335337
if (keyCode === KeyCode.ESC || key === 'Escape') {
338+
escClosingRef.current = true;
336339
event.preventDefault();
337340
onClose?.();
341+
342+
Promise.resolve().then(() => {
343+
escClosingRef.current = false;
344+
});
338345
return;
339346
}
340347

@@ -382,6 +389,12 @@ const Preview: React.FC<PreviewProps> = props => {
382389
}
383390
}, [open]);
384391

392+
const onEsc: PortalProps['onEsc'] = ({ top }) => {
393+
if (top && !escClosingRef.current) {
394+
onClose?.();
395+
}
396+
};
397+
385398
// ========================== Render ==========================
386399
const bodyStyle: React.CSSProperties = {
387400
...styles.body,
@@ -396,6 +409,7 @@ const Preview: React.FC<PreviewProps> = props => {
396409
autoDestroy={false}
397410
getContainer={getContainer}
398411
autoLock={lockScroll}
412+
onEsc={onEsc}
399413
>
400414
<CSSMotion
401415
motionName={motionName}

0 commit comments

Comments
 (0)