|
| 1 | +/** |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + */ |
| 8 | + |
| 9 | +import type {JSX, ReactNode} from 'react'; |
| 10 | + |
| 11 | +import {useEffect, useRef, useState} from 'react'; |
| 12 | +import {createPortal} from 'react-dom'; |
| 13 | + |
| 14 | +type ShadowDOMWrapperProps = { |
| 15 | + children: ReactNode; |
| 16 | + enabled: boolean; |
| 17 | + className?: string; |
| 18 | +}; |
| 19 | + |
| 20 | +export default function ShadowDOMWrapper({ |
| 21 | + children, |
| 22 | + enabled, |
| 23 | + className, |
| 24 | +}: ShadowDOMWrapperProps): JSX.Element { |
| 25 | + const hostRef = useRef<HTMLDivElement>(null); |
| 26 | + const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null); |
| 27 | + const [stylesAdded, setStylesAdded] = useState(false); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (!enabled || !hostRef.current) { |
| 31 | + setShadowRoot(null); |
| 32 | + setStylesAdded(false); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + const host = hostRef.current; |
| 37 | + |
| 38 | + // Create shadow DOM (should be safe with fresh element due to key prop) |
| 39 | + try { |
| 40 | + const shadow = host.attachShadow({mode: 'open'}); |
| 41 | + setShadowRoot(shadow); |
| 42 | + } catch (error) { |
| 43 | + // If shadow already exists, use existing one |
| 44 | + if (error instanceof DOMException && error.name === 'NotSupportedError') { |
| 45 | + const existingShadow = host.shadowRoot; |
| 46 | + if (existingShadow) { |
| 47 | + setShadowRoot(existingShadow); |
| 48 | + // Clear existing content |
| 49 | + existingShadow.innerHTML = ''; |
| 50 | + } |
| 51 | + } else { |
| 52 | + console.error('Error creating shadow DOM:', error); |
| 53 | + return; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const shadow = host.shadowRoot; |
| 58 | + if (!shadow) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + // Copy all document styles to shadow DOM |
| 63 | + const documentStyles = Array.from( |
| 64 | + document.head.querySelectorAll('style, link[rel="stylesheet"]'), |
| 65 | + ); |
| 66 | + |
| 67 | + documentStyles.forEach((styleElement) => { |
| 68 | + const clonedStyle = styleElement.cloneNode(true) as HTMLElement; |
| 69 | + shadow.appendChild(clonedStyle); |
| 70 | + }); |
| 71 | + |
| 72 | + setStylesAdded(true); |
| 73 | + |
| 74 | + return () => { |
| 75 | + // Cleanup is automatic when host element is removed |
| 76 | + }; |
| 77 | + }, [enabled]); |
| 78 | + |
| 79 | + // If shadow DOM is not enabled, render children normally |
| 80 | + if (!enabled) { |
| 81 | + return <div className={className}>{children}</div>; |
| 82 | + } |
| 83 | + |
| 84 | + // Return the host element and portal to shadow DOM |
| 85 | + return ( |
| 86 | + <div style={{position: 'relative'}}> |
| 87 | + <div |
| 88 | + ref={hostRef} |
| 89 | + className={className} |
| 90 | + style={{ |
| 91 | + position: 'relative', |
| 92 | + }} |
| 93 | + /> |
| 94 | + {shadowRoot && |
| 95 | + stylesAdded && |
| 96 | + createPortal( |
| 97 | + <div |
| 98 | + style={{ |
| 99 | + display: 'contents', // This ensures the children render properly |
| 100 | + }}> |
| 101 | + {children} |
| 102 | + </div>, |
| 103 | + shadowRoot, |
| 104 | + )} |
| 105 | + </div> |
| 106 | + ); |
| 107 | +} |
0 commit comments