-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shadow DOM support with ft v6.8.1 (#652)
Adds a demo that uses open and closed shadows. Does NOT add a test for this demo because of lack of support for Shadow DOM in Cypress (see comment in focus-trap-demo.spec.js). Adds a warning to the README about testing in JSDom, and fixes all the JSDom-based tests to use `displayCheck='none'` to get around JSDom limitations with new APIs used by tabbable in v5.3.0.
- Loading branch information
1 parent
c3ef438
commit 659d44e
Showing
10 changed files
with
228 additions
and
41 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'focus-trap-react': minor | ||
--- | ||
|
||
<<<<<<< HEAD | ||
Bumps focus-trap to v6.8.0. The big new feature is opt-in Shadow DOM support in focus-trap (in tabbable), and a new `getShadowRoot` tabbable option exposed in a new `focusTrapOptions.tabbableOptions` configuration option. | ||
======= | ||
Bumps focus-trap to v6.8.1. The big new feature is opt-in Shadow DOM support in focus-trap (in tabbable), and new tabbable options exposed in a new `focusTrapOptions.tabbableOptions` configuration option. | ||
>>>>>>> 57d9caa (Add shadow DOM support with ft v6.8.1) |
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const React = require('react'); | ||
const ReactDOM = require('react-dom'); | ||
const FocusTrap = require('../../dist/focus-trap-react'); | ||
|
||
const createShadow = function (hostEl, isOpen) { | ||
const containerEl = document.createElement('div'); | ||
containerEl.id = 'with-shadow-dom-closed-container'; | ||
containerEl.style = `border: 1px dotted black; margin-top: 10px; padding: 10px; background-color: ${ | ||
isOpen ? 'transparent' : 'rgba(0, 0, 0, 0.05)' | ||
};`; | ||
containerEl.innerHTML = ` | ||
<p style="margin-top: 0; padding-top: 0;"> | ||
This field is inside a <strong>${ | ||
isOpen ? 'opened' : 'closed' | ||
}</strong> Shadow DOM: | ||
</p> | ||
<input id="text-input" type="text" /> | ||
`; | ||
|
||
// use same styles as host | ||
const styleLinkEl = document.createElement('link'); | ||
styleLinkEl.setAttribute('rel', 'stylesheet'); | ||
styleLinkEl.setAttribute('href', 'style.css'); | ||
|
||
const shadowEl = hostEl.attachShadow({ mode: isOpen ? 'open' : 'closed' }); | ||
shadowEl.appendChild(styleLinkEl); | ||
shadowEl.appendChild(containerEl); | ||
|
||
return shadowEl; | ||
}; | ||
|
||
const DemoWithShadowDom = function () { | ||
const [active, setActive] = React.useState(false); | ||
const openedShadowHostRef = React.useRef(null); | ||
const openedShadowRef = React.useRef(null); | ||
const closedShadowHostRef = React.useRef(null); | ||
const closedShadowRef = React.useRef(null); | ||
|
||
const handleTrapActivate = React.useCallback(function () { | ||
setActive(true); | ||
}, []); | ||
|
||
const handleTrapDeactivate = React.useCallback(function () { | ||
setActive(false); | ||
}, []); | ||
|
||
React.useEffect(function () { | ||
if (openedShadowHostRef.current && !openedShadowRef.current) { | ||
openedShadowRef.current = createShadow(openedShadowHostRef.current, true); | ||
} | ||
|
||
if (closedShadowHostRef.current && !closedShadowRef.current) { | ||
closedShadowRef.current = createShadow( | ||
closedShadowHostRef.current, | ||
false | ||
); | ||
} | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<p> | ||
<button | ||
onClick={handleTrapActivate} | ||
aria-describedby="with-shadow-dom-heading" | ||
> | ||
activate trap | ||
</button> | ||
</p> | ||
<FocusTrap | ||
active={active} | ||
focusTrapOptions={{ | ||
onDeactivate: handleTrapDeactivate, | ||
tabbableOptions: { | ||
getShadowRoot(node) { | ||
if (node === closedShadowHostRef.current) { | ||
return closedShadowHostRef.current; | ||
} | ||
}, | ||
}, | ||
}} | ||
> | ||
<div className={`trap ${active ? 'is-active' : ''}`}> | ||
<p> | ||
Here is a focus trap <a href="#">with</a> <a href="#">some</a>{' '} | ||
<a href="#">focusable</a> parts. | ||
</p> | ||
<div id="with-shadow-dom-opened-host" ref={openedShadowHostRef}></div> | ||
<div id="with-shadow-dom-closed-host" ref={closedShadowHostRef}></div> | ||
<p> | ||
<button | ||
onClick={handleTrapDeactivate} | ||
aria-describedby="with-shadow-dom-heading" | ||
> | ||
deactivate trap | ||
</button> | ||
</p> | ||
</div> | ||
</FocusTrap> | ||
</div> | ||
); | ||
}; | ||
|
||
ReactDOM.render( | ||
<DemoWithShadowDom />, | ||
document.getElementById('demo-with-shadow-dom') | ||
); |
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
Oops, something went wrong.