-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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(material/dialog): focus restoration not working inside shadow dom #21811
Conversation
Related to angular#21796. The dialog focus restoration works by grabbing `document.activeElement` before the dialog is opened and restoring focus to the element on destroy. This won't work if the element is inside the shadow DOM, because the browser will return the shadow root. These changes add a workaround.
// If the `activeElement` is inside a shadow root, `document.activeElement` will | ||
// point to the shadow root so we have to descend into it ourselves. | ||
const activeElement = this._document.activeElement; | ||
return activeElement?.shadowRoot?.activeElement as HTMLElement || activeElement; |
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.
Isn't it possible for the element to be within multiple layers of shadow dom?
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.
It is possible, but my assumption was activeElement
would point to the closest shadow root. That being said, I haven't actually tried it. If that's the case, we might need some reusable utility under cdk/platform
since we have a lot of places that look only at document.activeElement
.
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.
LGTM as a solution for now, then; we can revisit the shadow dom hierarchy if it becomes a problem
…#21811) Related to #21796. The dialog focus restoration works by grabbing `document.activeElement` before the dialog is opened and restoring focus to the element on destroy. This won't work if the element is inside the shadow DOM, because the browser will return the shadow root. These changes add a workaround. (cherry picked from commit be508da)
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
Related to #21796. The dialog focus restoration works by grabbing
document.activeElement
before the dialog is opened and restoring focus to the element on destroy. This won't work if the element is inside the shadow DOM, because the browser will return the shadow root. These changes add a workaround.