-
-
Notifications
You must be signed in to change notification settings - Fork 118
fix: lock focus inside preview when opened via keyboard #505
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -196,7 +196,9 @@ const Preview: React.FC<PreviewProps> = props => { | |||||||||||||
| } = props; | ||||||||||||||
|
|
||||||||||||||
| const imgRef = useRef<HTMLImageElement>(); | ||||||||||||||
| const wrapperRef = useRef<HTMLDivElement>(null); | ||||||||||||||
| const triggerRef = useRef<HTMLElement>(null); | ||||||||||||||
| const [wrapperEl, setWrapperEl] = useState<HTMLDivElement>(null); | ||||||||||||||
|
|
||||||||||||||
| const groupContext = useContext(PreviewGroupContext); | ||||||||||||||
| const showLeftOrRightSwitches = groupContext && count > 1; | ||||||||||||||
| const showOperationsProgress = groupContext && count >= 1; | ||||||||||||||
|
|
@@ -366,6 +368,10 @@ const Preview: React.FC<PreviewProps> = props => { | |||||||||||||
| const onVisibleChanged = (nextVisible: boolean) => { | ||||||||||||||
| if (!nextVisible) { | ||||||||||||||
| setLockScroll(false); | ||||||||||||||
|
|
||||||||||||||
| // Restore focus to the trigger element after leave animation | ||||||||||||||
| triggerRef.current?.focus?.(); | ||||||||||||||
| triggerRef.current = null; | ||||||||||||||
| } | ||||||||||||||
| afterOpenChange?.(nextVisible); | ||||||||||||||
| }; | ||||||||||||||
|
|
@@ -385,7 +391,13 @@ const Preview: React.FC<PreviewProps> = props => { | |||||||||||||
| }; | ||||||||||||||
|
|
||||||||||||||
| // =========================== Focus ============================ | ||||||||||||||
| useLockFocus(open && portalRender, () => wrapperRef.current); | ||||||||||||||
| useEffect(() => { | ||||||||||||||
| if (open) { | ||||||||||||||
| triggerRef.current = document.activeElement as HTMLElement; | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+395
to
+397
Contributor
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. To avoid overwriting the trigger element with an element from inside the preview (which can happen if
Suggested change
Member
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. Check this one
Contributor
Author
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. Thanks for the suggestion! After investigation, I believe this is a false positive in our scenario. When the user presses Escape to close the preview, The actual sequence is:
So there's no window where |
||||||||||||||
| }, [open]); | ||||||||||||||
|
|
||||||||||||||
| useLockFocus(open && !!wrapperEl, () => wrapperEl); | ||||||||||||||
|
|
||||||||||||||
| // ========================== Render ========================== | ||||||||||||||
| const bodyStyle: React.CSSProperties = { | ||||||||||||||
|
|
@@ -423,7 +435,7 @@ const Preview: React.FC<PreviewProps> = props => { | |||||||||||||
|
|
||||||||||||||
| return ( | ||||||||||||||
| <div | ||||||||||||||
| ref={wrapperRef} | ||||||||||||||
| ref={setWrapperEl} | ||||||||||||||
| className={clsx(prefixCls, rootClassName, classNames.root, motionClassName, { | ||||||||||||||
| [`${prefixCls}-movable`]: movable, | ||||||||||||||
| [`${prefixCls}-moving`]: isMoving, | ||||||||||||||
|
|
||||||||||||||
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.
When restoring focus, it's safer to check if the element is still in the document. Additionally, using
{ preventScroll: true }prevents the page from jumping if the trigger element has moved out of the viewport while the preview was open.