Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 122
fix: optimize ESC key handling in nested portal scenarios#494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -44,4 +44,5 @@ es/ | ||
| .dumi/tmp | ||
| .dumi/tmp-production | ||
| bun.lockb | ||
| bun.lockb | ||
| .vscode | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,6 +5,7 @@ | ||
| overflow: hidden; | ||
| user-select: none; | ||
| inset: 0; | ||
| z-index: 1055; | ||
| &-mask { | ||
| position: absolute; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| title: nested | ||
| nav: | ||
| title: Demo | ||
| path: /demo | ||
| --- | ||
| <code src="../examples/nested.tsx"></code> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| import React, { useState } from 'react'; | ||
| import Dialog from '@rc-component/dialog'; | ||
| import Image from '@rc-component/image'; | ||
| import '@rc-component/dialog/assets/index.css'; | ||
| import '../../assets/index.less'; | ||
| const App: React.FC = () => { | ||
| const [show, setShow] = useState(false); | ||
| return ( | ||
| <> | ||
| <button | ||
| onClick={() => { | ||
| setShow(true); | ||
| }} | ||
| > | ||
| showModal | ||
| </button> | ||
| <Dialog | ||
| visible={show} | ||
| afterOpenChange={open => { | ||
| setShow(open); | ||
| }} | ||
| onClose={() => { | ||
| setShow(false); | ||
| }} | ||
| footer={ | ||
| <> | ||
| <button | ||
| onClick={() => { | ||
| setShow(false); | ||
| }} | ||
| > | ||
| Cancel | ||
| </button> | ||
| <button | ||
| onClick={() => { | ||
| setShow(false); | ||
| }} | ||
| > | ||
| OK | ||
| </button> | ||
| </> | ||
| } | ||
| > | ||
| <Image | ||
| width={200} | ||
| alt="svg image" | ||
| src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" | ||
| /> | ||
| <Image.PreviewGroup | ||
| preview={{ | ||
| onChange: (current, prev) => | ||
| console.log(`current index: ${current}, prev index: ${prev}`), | ||
| }} | ||
| > | ||
| <Image | ||
| width={200} | ||
| alt="svg image" | ||
| src="https://gw.alipayobjects.com/zos/rmsportal/KDpgvguMpGfqaHPjicRK.svg" | ||
| /> | ||
| <Image | ||
| width={200} | ||
| src="https://gw.alipayobjects.com/zos/antfincdn/aPkFc8Sj7n/method-draw-image.svg" | ||
| /> | ||
| </Image.PreviewGroup> | ||
| </Dialog> | ||
| </> | ||
| ); | ||
| }; | ||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -332,10 +332,6 @@ const Preview: React.FC<PreviewProps> = props => { | ||
| if (open) { | ||
| const { keyCode } = event; | ||
| if (keyCode === KeyCode.ESC) { | ||
| onClose?.(); | ||
| } | ||
| if (showLeftOrRightSwitches) { | ||
| if (keyCode === KeyCode.LEFT) { | ||
| onActive(-1); | ||
| @@ -380,6 +376,12 @@ const Preview: React.FC<PreviewProps> = props => { | ||
| } | ||
| }, [open]); | ||
| const onEsc: PortalProps['onEsc'] = ({ top }) => { | ||
| if (top) { | ||
| onClose?.(); | ||
| } | ||
| }; | ||
| // ========================== Render ========================== | ||
| const bodyStyle: React.CSSProperties = { | ||
| ...styles.body, | ||
| @@ -389,7 +391,13 @@ const Preview: React.FC<PreviewProps> = props => { | ||
| } | ||
| return ( | ||
| <Portal open={portalRender} getContainer={getContainer} autoLock={lockScroll}> | ||
| <Portal | ||
| open={portalRender && open} | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. | ||
| autoDestroy={false} | ||
ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里是为了确保行为与原逻辑一致 | ||
| getContainer={getContainer} | ||
| autoLock={lockScroll} | ||
| onEsc={onEsc} | ||
| > | ||
| <CSSMotion | ||
| motionName={motionName} | ||
| visible={portalRender && open} | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The event handlers in this component can be simplified and deduplicated for better readability and maintainability. You can define separate functions for showing and hiding the modal and reuse them. Also,
afterOpenChangecan directly receive thesetShowfunction.