-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
[tabs] Fix Arrow key navigation failing when component is rendered in shadow DOM #47178
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
Open
sai6855
wants to merge
6
commits into
mui:master
Choose a base branch
from
sai6855:fix-tab-shadow
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+212
−2
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import getActiveElement from '@mui/utils/getActiveElement'; | ||
|
|
||
| export default getActiveElement; |
117 changes: 117 additions & 0 deletions
117
packages/mui-utils/src/getActiveElement/getActiveElement.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| import { expect } from 'chai'; | ||
| import { stub } from 'sinon'; | ||
| import getActiveElement from './getActiveElement'; | ||
|
|
||
| describe('getActiveElement', () => { | ||
| it('should return the active element from document', () => { | ||
| const button = document.createElement('button'); | ||
| document.body.appendChild(button); | ||
| button.focus(); | ||
|
|
||
| const activeElement = getActiveElement(document); | ||
| expect(activeElement).to.equal(button); | ||
|
|
||
| document.body.removeChild(button); | ||
| }); | ||
|
|
||
| it('should return null when no element has focus', () => { | ||
| const activeElementStub = stub(document, 'activeElement').get(() => null); | ||
|
|
||
| const activeElement = getActiveElement(document); | ||
| expect(activeElement).to.equal(null); | ||
|
|
||
| activeElementStub.restore(); | ||
| }); | ||
|
|
||
| it('should traverse shadow roots to find the actual focused element', () => { | ||
| // Create a shadow host | ||
| const host = document.createElement('div'); | ||
| document.body.appendChild(host); | ||
| const shadowRoot = host.attachShadow({ mode: 'open' }); | ||
|
|
||
| // Create an element inside the shadow root | ||
| const button = document.createElement('button'); | ||
| shadowRoot.appendChild(button); | ||
| button.focus(); | ||
|
|
||
| // document.activeElement should point to the host | ||
| expect(document.activeElement).to.equal(host); | ||
|
|
||
| // getActiveElement should traverse into the shadow root | ||
| const activeElement = getActiveElement(document); | ||
| expect(activeElement).to.equal(button); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('should handle nested shadow roots', () => { | ||
| // Create outer shadow host | ||
| const outerHost = document.createElement('div'); | ||
| document.body.appendChild(outerHost); | ||
| const outerShadowRoot = outerHost.attachShadow({ mode: 'open' }); | ||
|
|
||
| // Create inner shadow host inside outer shadow root | ||
| const innerHost = document.createElement('div'); | ||
| outerShadowRoot.appendChild(innerHost); | ||
| const innerShadowRoot = innerHost.attachShadow({ mode: 'open' }); | ||
|
|
||
| // Create a button inside the inner shadow root | ||
| const button = document.createElement('button'); | ||
| innerShadowRoot.appendChild(button); | ||
| button.focus(); | ||
|
|
||
| // document.activeElement should point to the outer host | ||
| expect(document.activeElement).to.equal(outerHost); | ||
|
|
||
| // getActiveElement should traverse through both shadow roots | ||
| const activeElement = getActiveElement(document); | ||
| expect(activeElement).to.equal(button); | ||
|
|
||
| document.body.removeChild(outerHost); | ||
| }); | ||
|
|
||
| it('should return the element inside shadow root when it has focus', () => { | ||
| const host = document.createElement('div'); | ||
| document.body.appendChild(host); | ||
| const shadowRoot = host.attachShadow({ mode: 'open' }); | ||
|
|
||
| const button = document.createElement('button'); | ||
| shadowRoot.appendChild(button); | ||
| button.focus(); | ||
|
|
||
| const activeElement = getActiveElement(document); | ||
| expect(activeElement).to.equal(button); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('should work when starting from a shadow root', () => { | ||
| const host = document.createElement('div'); | ||
| document.body.appendChild(host); | ||
| const shadowRoot = host.attachShadow({ mode: 'open' }); | ||
|
|
||
| const button = document.createElement('button'); | ||
| shadowRoot.appendChild(button); | ||
| button.focus(); | ||
|
|
||
| // When called with the shadow root directly | ||
| const activeElement = getActiveElement(shadowRoot); | ||
| expect(activeElement).to.equal(button); | ||
|
|
||
| document.body.removeChild(host); | ||
| }); | ||
|
|
||
| it('should handle shadow root with null activeElement', () => { | ||
| const host = document.createElement('div'); | ||
| document.body.appendChild(host); | ||
| const shadowRoot = host.attachShadow({ mode: 'open' }); | ||
|
|
||
| const activeElementStub = stub(shadowRoot, 'activeElement').get(() => null); | ||
|
|
||
| const activeElement = getActiveElement(shadowRoot); | ||
| expect(activeElement).to.equal(null); | ||
|
|
||
| activeElementStub.restore(); | ||
| document.body.removeChild(host); | ||
| }); | ||
| }); |
33 changes: 33 additions & 0 deletions
33
packages/mui-utils/src/getActiveElement/getActiveElement.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * Gets the actual active element, traversing through shadow roots if necessary. | ||
| * | ||
| * When an element inside a shadow root has focus, `document.activeElement` returns | ||
| * the shadow host element. This function recursively traverses shadow roots to find | ||
| * the actual focused element. | ||
| * | ||
| * @param root - The document or shadow root to start from. Defaults to document. | ||
| * @returns The actual focused element, or null if no element has focus. | ||
| * | ||
| * @example | ||
| * // In a shadow DOM context | ||
| * const activeElement = getActiveElement(document); | ||
| * // Returns the actual focused element inside the shadow root | ||
| * | ||
| * @example | ||
| * // Starting from a specific document | ||
| * const activeElement = getActiveElement(ownerDocument(element)); | ||
| */ | ||
| export default function getActiveElement(root: Document | ShadowRoot = document): Element | null { | ||
| const activeEl = root.activeElement; | ||
|
|
||
| if (!activeEl) { | ||
| return null; | ||
| } | ||
|
|
||
| // If the active element has a shadow root, recursively check inside it | ||
| if (activeEl.shadowRoot && activeEl.shadowRoot.activeElement) { | ||
| return getActiveElement(activeEl.shadowRoot); | ||
| } | ||
|
|
||
| return activeEl; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { default } from './getActiveElement'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Fixed by adding a
getActiveElementutility that recursively traverses shadow roots to find the actual focusedelement. The issue was that
document.activeElementreturns the shadow host instead of the focused element insideshadow DOM, causing keyboard navigation to fail the
role="tab"check.