Skip to content

Instantly share code, notes, and snippets.

@dejurin
Last active April 13, 2025 08:48
Show Gist options
  • Save dejurin/dbbec6eb6b659c545e264e95ce250de8 to your computer and use it in GitHub Desktop.
Save dejurin/dbbec6eb6b659c545e264e95ce250de8 to your computer and use it in GitHub Desktop.
Headless UI shadow DOM support for Preact
const containerRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
if (!containerRef.current) return;
const observer = new MutationObserver((records) => {
const record = records.filter((record) => {
const el = record.target as HTMLDivElement;
return el.id === 'headlessui-portal-root';
}).pop();
if (!record) return;
containerRef.current?.append(record.target);
if (containerRef.current) {
console.log(containerRef.current, 'containerRef.current')
containerRef.current.removeAttribute('inert');
containerRef.current.removeAttribute('aria-hidden');
containerRef.current.inert = false;
}
});
observer.observe(document, { childList: true, subtree: true });
return () => {
console.log('unmount')
observer.disconnect();
};
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment