-
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
fix(VDialog): fix focus trap when tabbing forward #22101
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
Changes from 1 commit
2be922a
6a89543
5376d8d
eb28b82
1bb656b
bd8cc60
e592e18
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 |
---|---|---|
|
@@ -80,15 +80,39 @@ export const VDialog = genericComponent<OverlaySlots>()({ | |
} | ||
} | ||
|
||
function onKeydown (e: KeyboardEvent) { | ||
if (e.key !== 'Tab' || !overlay.value?.contentEl) return | ||
|
||
const focusable = focusableChildren(overlay.value.contentEl) | ||
if (!focusable.length) return | ||
|
||
const firstElement = focusable[0] | ||
const lastElement = focusable[focusable.length - 1] | ||
const active = document.activeElement as HTMLElement | null | ||
|
||
if (e.shiftKey && active === firstElement) { | ||
e.preventDefault() | ||
lastElement.focus() | ||
} else if (!e.shiftKey && active === lastElement) { | ||
e.preventDefault() | ||
firstElement.focus() | ||
} | ||
} | ||
|
||
onBeforeUnmount(() => { | ||
document.removeEventListener('focusin', onFocusin) | ||
document.removeEventListener('keydown', onKeydown) | ||
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.
Lines 69-79 could be replaced with 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. Oh, I see. 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. I have two questions.
screen-capture.24.webmThere 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.
That said, the code between those is not the same because VDialog applied (bugged) focus trap. My suggestion would be to have
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. Thank you for your explanation and sharing the current refactor PR 22106! |
||
}) | ||
|
||
if (IN_BROWSER) { | ||
watch(() => isActive.value && props.retainFocus, val => { | ||
val | ||
? document.addEventListener('focusin', onFocusin) | ||
: document.removeEventListener('focusin', onFocusin) | ||
if (val) { | ||
document.addEventListener('focusin', onFocusin) | ||
document.addEventListener('keydown', onKeydown) | ||
} else { | ||
document.removeEventListener('focusin', onFocusin) | ||
document.removeEventListener('keydown', onKeydown) | ||
} | ||
}, { immediate: true }) | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.