|
| 1 | +const React = require('react'); |
| 2 | +const ReactDOM = require('react-dom'); |
| 3 | +const FocusTrap = require('../../dist/focus-trap-react'); |
| 4 | + |
| 5 | +const createShadow = function (hostEl, isOpen) { |
| 6 | + const containerEl = document.createElement('div'); |
| 7 | + containerEl.id = 'with-shadow-dom-closed-container'; |
| 8 | + containerEl.style = `border: 1px dotted black; margin-top: 10px; padding: 10px; background-color: ${ |
| 9 | + isOpen ? 'transparent' : 'rgba(0, 0, 0, 0.05)' |
| 10 | + };`; |
| 11 | + containerEl.innerHTML = ` |
| 12 | + <p style="margin-top: 0; padding-top: 0;"> |
| 13 | + This field is inside a <strong>${ |
| 14 | + isOpen ? 'opened' : 'closed' |
| 15 | + }</strong> Shadow DOM: |
| 16 | + </p> |
| 17 | + <input id="text-input" type="text" /> |
| 18 | + `; |
| 19 | + |
| 20 | + // use same styles as host |
| 21 | + const styleLinkEl = document.createElement('link'); |
| 22 | + styleLinkEl.setAttribute('rel', 'stylesheet'); |
| 23 | + styleLinkEl.setAttribute('href', 'style.css'); |
| 24 | + |
| 25 | + const shadowEl = hostEl.attachShadow({ mode: isOpen ? 'open' : 'closed' }); |
| 26 | + shadowEl.appendChild(styleLinkEl); |
| 27 | + shadowEl.appendChild(containerEl); |
| 28 | + |
| 29 | + return shadowEl; |
| 30 | +}; |
| 31 | + |
| 32 | +const DemoWithShadowDom = function () { |
| 33 | + const [active, setActive] = React.useState(false); |
| 34 | + const openedShadowHostRef = React.useRef(null); |
| 35 | + const openedShadowRef = React.useRef(null); |
| 36 | + const closedShadowHostRef = React.useRef(null); |
| 37 | + const closedShadowRef = React.useRef(null); |
| 38 | + |
| 39 | + const handleTrapActivate = React.useCallback(function () { |
| 40 | + setActive(true); |
| 41 | + }, []); |
| 42 | + |
| 43 | + const handleTrapDeactivate = React.useCallback(function () { |
| 44 | + setActive(false); |
| 45 | + }, []); |
| 46 | + |
| 47 | + React.useEffect(function () { |
| 48 | + if (openedShadowHostRef.current && !openedShadowRef.current) { |
| 49 | + openedShadowRef.current = createShadow(openedShadowHostRef.current, true); |
| 50 | + } |
| 51 | + |
| 52 | + if (closedShadowHostRef.current && !closedShadowRef.current) { |
| 53 | + closedShadowRef.current = createShadow( |
| 54 | + closedShadowHostRef.current, |
| 55 | + false |
| 56 | + ); |
| 57 | + } |
| 58 | + }, []); |
| 59 | + |
| 60 | + return ( |
| 61 | + <div> |
| 62 | + <p> |
| 63 | + <button |
| 64 | + onClick={handleTrapActivate} |
| 65 | + aria-describedby="with-shadow-dom-heading" |
| 66 | + > |
| 67 | + activate trap |
| 68 | + </button> |
| 69 | + </p> |
| 70 | + <FocusTrap |
| 71 | + active={active} |
| 72 | + focusTrapOptions={{ |
| 73 | + onDeactivate: handleTrapDeactivate, |
| 74 | + tabbableOptions: { |
| 75 | + getShadowRoot(node) { |
| 76 | + if (node === closedShadowHostRef.current) { |
| 77 | + return closedShadowHostRef.current; |
| 78 | + } |
| 79 | + }, |
| 80 | + }, |
| 81 | + }} |
| 82 | + > |
| 83 | + <div className={`trap ${active ? 'is-active' : ''}`}> |
| 84 | + <p> |
| 85 | + Here is a focus trap <a href="#">with</a> <a href="#">some</a>{' '} |
| 86 | + <a href="#">focusable</a> parts. |
| 87 | + </p> |
| 88 | + <div id="with-shadow-dom-opened-host" ref={openedShadowHostRef}></div> |
| 89 | + <div id="with-shadow-dom-closed-host" ref={closedShadowHostRef}></div> |
| 90 | + <p> |
| 91 | + <button |
| 92 | + onClick={handleTrapDeactivate} |
| 93 | + aria-describedby="with-shadow-dom-heading" |
| 94 | + > |
| 95 | + deactivate trap |
| 96 | + </button> |
| 97 | + </p> |
| 98 | + </div> |
| 99 | + </FocusTrap> |
| 100 | + </div> |
| 101 | + ); |
| 102 | +}; |
| 103 | + |
| 104 | +ReactDOM.render( |
| 105 | + <DemoWithShadowDom />, |
| 106 | + document.getElementById('demo-with-shadow-dom') |
| 107 | +); |
0 commit comments