From 7a900f063a6b84d66ae1c085e0814110a4fa9887 Mon Sep 17 00:00:00 2001 From: linxianxi <904492381@qq.com> Date: Tue, 1 Aug 2023 09:15:49 +0800 Subject: [PATCH] fix: should be closed when press esc after click portal --- src/Operations.tsx | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Operations.tsx b/src/Operations.tsx index 013523f8..64511482 100644 --- a/src/Operations.tsx +++ b/src/Operations.tsx @@ -1,6 +1,7 @@ import Portal from '@rc-component/portal'; import classnames from 'classnames'; import CSSMotion from 'rc-motion'; +import KeyCode from 'rc-util/lib/KeyCode'; import * as React from 'react'; import { useContext } from 'react'; import { PreviewGroupContext } from './context'; @@ -75,6 +76,22 @@ const Operations: React.FC = props => { const { rotateLeft, rotateRight, zoomIn, zoomOut, close, left, right, flipX, flipY } = icons; const toolClassName = `${prefixCls}-operations-operation`; + React.useEffect(() => { + const onKeyDown = (e: KeyboardEvent) => { + if (e.keyCode === KeyCode.ESC) { + onClose(); + } + }; + + if (visible) { + window.addEventListener('keydown', onKeyDown); + } + + return () => { + window.removeEventListener('keydown', onKeyDown); + }; + }, [visible]); + const tools = [ { icon: flipY,