mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-05-23 17:19:37 +02:00
Co-authored-by: Alf Eaton <alf.eaton@overleaf.com> GitOrigin-RevId: 79bb329932b1e6fcc88f648bca9cc4bee215cd41
33 lines
921 B
TypeScript
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>
|
|
)
|
|
}
|