-
Notifications
You must be signed in to change notification settings - Fork 232
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
Fix: Using focus trap in web components #1338
base: main
Are you sure you want to change the base?
Conversation
…when using web components
@Palkess is attempting to deploy a commit to the Themesberg Team on Vercel. A member of the Team first needs to authorize it. |
WalkthroughThe update enhances the Changes
Possibly related issues
Recent Review DetailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Files skipped from review due to trivial changes (1)
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
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.
Actionable comments posted: 2
let index = tabbable.indexOf(document.activeElement ?? node); | ||
const activeElement = !!document.activeElement?.shadowRoot ? document.activeElement.shadowRoot.activeElement : document.activeElement; | ||
|
||
let index = tabbable.indexOf(activeElement ?? node); |
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.
Consider handling cases where tabbable[index]
might be undefined.
In rare cases, if tabbable
is empty, tabbable[index]
will be undefined, and calling .focus()
on it will throw an error. It might be beneficial to add a guard clause to prevent this:
+ if (tabbable[index]) {
+ tabbable[index].focus();
+ }
This ensures that the focus method is only called if there is a valid element to receive focus.
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
let index = tabbable.indexOf(activeElement ?? node); | |
let index = tabbable.indexOf(activeElement ?? node); | |
if (tabbable[index]) { | |
tabbable[index].focus(); | |
} |
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
Any ETA on when someone can check this PR out? Would be swell to have a solution for our web components without having to do some weird workaround :D |
Closes #
📑 Description
When using Svelte-components as custom elements/web components, they're encapsulated with a shadow-root. When using a focus trapped component, like a Modal,
document.activeElement
incorrectly returns the custom element as the active element instead of the actual element inside the component. This causes the focus trap to never change focus whilst tabbing.The solution I'm suggesting checks if
document.activeElement
has a shadow root and uses the correctdocument.activeElement.shadowRoot.activeElement
to get the actual active element inside the component.Status
✅ Checks
ℹ Additional Information
Summary by CodeRabbit