Skip to content

Commit b3c0dc6

Browse files
committed
fix: close preview on Escape in nested portal contexts
1 parent 2825512 commit b3c0dc6

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/Preview/index.tsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,14 @@ const Preview: React.FC<PreviewProps> = props => {
330330
// >>>>> Effect: Keyboard
331331
const onKeyDown = useEvent((event: KeyboardEvent) => {
332332
if (open) {
333-
const { keyCode } = event;
333+
const { keyCode, key } = event;
334+
335+
if (keyCode === KeyCode.ESC || key === 'Escape') {
336+
event.preventDefault();
337+
event.stopPropagation();
338+
onClose?.();
339+
return;
340+
}
334341

335342
if (showLeftOrRightSwitches) {
336343
if (keyCode === KeyCode.LEFT) {
@@ -376,12 +383,6 @@ const Preview: React.FC<PreviewProps> = props => {
376383
}
377384
}, [open]);
378385

379-
const onEsc: PortalProps['onEsc'] = ({ top }) => {
380-
if (top) {
381-
onClose?.();
382-
}
383-
};
384-
385386
// ========================== Render ==========================
386387
const bodyStyle: React.CSSProperties = {
387388
...styles.body,
@@ -396,7 +397,6 @@ const Preview: React.FC<PreviewProps> = props => {
396397
autoDestroy={false}
397398
getContainer={getContainer}
398399
autoLock={lockScroll}
399-
onEsc={onEsc}
400400
>
401401
<CSSMotion
402402
motionName={motionName}

0 commit comments

Comments
 (0)