Files
overleaf-cep/libraries/ai/components/shadow-root-portal.tsx
Mathias Jakobsen 8024fe2c58 [web] Add AI workbench to alpha users (#29417)
Co-authored-by: Alf Eaton <alf.eaton@overleaf.com>
GitOrigin-RevId: 79bb329932b1e6fcc88f648bca9cc4bee215cd41
2025-11-11 09:06:08 +00:00

33 lines
921 B
TypeScript

import { FC, PropsWithChildren, useEffect, useRef, useState } from 'react'
import { createPortal } from 'react-dom'
// @ts-expect-error this is using exportType: 'css-style-sheet'
import tailwindCSS from '../tailwind.css'
export const ShadowRootPortal: FC<PropsWithChildren> = ({ children }) => {
const [shadowRoot, setShadowRoot] = useState<ShadowRoot | null>(null)
const hostRef = useRef<HTMLDivElement | null>(null)
useEffect(() => {
if (!shadowRoot && hostRef.current) {
const shadowRoot = hostRef.current.attachShadow({ mode: 'open' })
shadowRoot.adoptedStyleSheets = [tailwindCSS]
setShadowRoot(shadowRoot)
}
}, [shadowRoot])
return (
<div
ref={hostRef}
style={{
flex: 1,
display: 'flex',
flexDirection: 'column',
overflow: 'auto',
}}
>
{shadowRoot ? createPortal(children, shadowRoot) : null}
</div>
)
}