-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve support for nested shadow DOMs
- Loading branch information
Victor Babel
committed
Mar 28, 2022
1 parent
de2edd6
commit 6aa3984
Showing
11 changed files
with
253 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[ | ||
{ | ||
path: "dist/es2015/index.js", | ||
limit: "2.58 kB" | ||
limit: "2.591 kB" | ||
} | ||
] |
This file contains 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[ | ||
{ | ||
"name": "dist/es2015/index.js", | ||
"passed": true, | ||
"size": 2641 | ||
"passed": false, | ||
"size": 2653 | ||
} | ||
] |
This file contains 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 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,114 @@ | ||
import { focusIsHidden, constants } from '../src'; | ||
|
||
describe('focusIsHidden', () => { | ||
const setupTest = () => { | ||
document.body.innerHTML = ` | ||
<div ${constants.FOCUS_ALLOW}> | ||
<button id="focus-hidden"></button> | ||
</div> | ||
<button id="focus-not-hidden"></button> | ||
`; | ||
}; | ||
|
||
it('returns true when the focused element is hidden', () => { | ||
setupTest(); | ||
|
||
const button = document.querySelector('#focus-hidden') as HTMLButtonElement; | ||
|
||
button.focus(); | ||
|
||
expect(focusIsHidden()).toBe(true); | ||
}); | ||
|
||
it('returns false when the focused element is not hidden', () => { | ||
setupTest(); | ||
|
||
const button = document.querySelector('#focus-not-hidden') as HTMLButtonElement; | ||
|
||
button.focus(); | ||
|
||
expect(focusIsHidden()).toBe(false); | ||
}); | ||
|
||
const setupShadowTest = (nested?: boolean) => { | ||
const html = ` | ||
<div id="app"> | ||
<div id="nonshadow"> | ||
<input /> | ||
<button>I am a button</button> | ||
</div> | ||
<div id="shadowdom"></div> | ||
</div>`; | ||
const shadowHtml = ` | ||
<div id="first"></div> | ||
<button id="firstBtn">first button</button> | ||
<div id="last"> | ||
<button id="secondBtn">second button</button> | ||
</div> | ||
`; | ||
document.body.innerHTML = html; | ||
|
||
const shadowContainer = document.getElementById('shadowdom') as HTMLElement; | ||
const root = shadowContainer.attachShadow({ mode: 'open' }); | ||
const shadowDiv = document.createElement('div'); | ||
shadowDiv.innerHTML = shadowHtml; | ||
root.appendChild(shadowDiv); | ||
|
||
if (nested) { | ||
const firstDiv = root.querySelector('#first') as HTMLDivElement; | ||
const nestedRoot = firstDiv.attachShadow({ mode: 'open' }); | ||
const nestedShadowDiv = document.createElement('div'); | ||
|
||
nestedShadowDiv.innerHTML = shadowHtml; | ||
nestedRoot.appendChild(nestedShadowDiv); | ||
} | ||
}; | ||
|
||
it('looks for focus within shadow doms', () => { | ||
setupShadowTest(); | ||
|
||
const shadowHost = document.querySelector('#shadowdom') as HTMLDivElement; | ||
const button = shadowHost.shadowRoot?.querySelector('#firstBtn') as HTMLButtonElement; | ||
|
||
button.focus(); | ||
|
||
expect(focusIsHidden()).toBe(false); | ||
|
||
shadowHost.setAttribute(constants.FOCUS_ALLOW, ''); | ||
|
||
expect(focusIsHidden()).toBe(true); | ||
}); | ||
|
||
it('looks for focus within nested shadow doms', () => { | ||
setupShadowTest(true); | ||
|
||
const shadowHost = document.querySelector('#shadowdom') as HTMLDivElement; | ||
const nestedShadowHost = shadowHost.shadowRoot?.querySelector('#first') as HTMLDivElement; | ||
const button = nestedShadowHost.shadowRoot?.querySelector('#firstBtn') as HTMLButtonElement; | ||
|
||
button.focus(); | ||
|
||
expect(focusIsHidden()).toBe(false); | ||
|
||
shadowHost.setAttribute(constants.FOCUS_ALLOW, ''); | ||
|
||
expect(focusIsHidden()).toBe(true); | ||
}); | ||
|
||
it('does not support marking shadow members as FOCUS_ALLOW', () => { | ||
setupShadowTest(); | ||
|
||
const shadowHost = document.querySelector('#shadowdom') as HTMLDivElement; | ||
const shadowDiv = shadowHost.shadowRoot?.querySelector('#last') as HTMLDivElement; | ||
const button = shadowDiv.children[0] as HTMLButtonElement; | ||
|
||
button.focus(); | ||
|
||
expect(focusIsHidden()).toBe(false); | ||
|
||
// Setting elements within shadow doms as FOCUS_ALLOW not supported | ||
shadowDiv.setAttribute(constants.FOCUS_ALLOW, ''); | ||
|
||
expect(focusIsHidden()).toBe(false); | ||
}); | ||
}); |
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
const getNestedShadowActiveElement = (shadowRoot: ShadowRoot): HTMLElement | undefined => | ||
shadowRoot.activeElement | ||
? shadowRoot.activeElement.shadowRoot | ||
? getNestedShadowActiveElement(shadowRoot.activeElement.shadowRoot) | ||
: (shadowRoot.activeElement as HTMLElement) | ||
: undefined; | ||
|
||
/** | ||
* returns active element from document or from shadowdom | ||
* returns active element from document or from nested shadowdoms | ||
*/ | ||
export const getActiveElement = (): HTMLElement | undefined => { | ||
return ( | ||
document.activeElement | ||
? document.activeElement.shadowRoot | ||
? document.activeElement.shadowRoot.activeElement | ||
? getNestedShadowActiveElement(document.activeElement.shadowRoot) | ||
: document.activeElement | ||
: undefined | ||
) as // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
any; | ||
) as any; // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
}; |
This file contains 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