chore(collection/presentation): render with shadow DOM

This commit is contained in:
Elian Doran
2025-10-15 20:05:39 +03:00
parent f9754cd82d
commit 1a000fdb33
2 changed files with 34 additions and 4 deletions

View File

@@ -0,0 +1,26 @@
import { ComponentChildren, HTMLAttributes, JSX, render } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
interface ShadowDomProps extends HTMLAttributes<HTMLDivElement> {
children: ComponentChildren;
}
export default function ShadowDom({ children, ...containerProps }: ShadowDomProps) {
const containerRef = useRef<HTMLDivElement>(null);
const [ shadowRoot, setShadowRoot ] = useState<ShadowRoot | null>(null);
// Create the shadow root.
useEffect(() => {
if (!containerRef.current || shadowRoot) return;
const shadow = containerRef.current.attachShadow({ mode: "open" });
setShadowRoot(shadow);
}, [ shadowRoot ]);
// Render the child elements.
useEffect(() => {
if (!shadowRoot) return;
render(<>{children}</>, shadowRoot);
}, [ shadowRoot, children ]);
return <div ref={containerRef} {...containerProps} />
}