import React, { HTMLAttributes, createContext, useEffect, useMemo, useRef, useState } from 'react'; import { StyleSheetManager } from 'styled-components'; import root from 'react-shadow'; import tailwindStyle from 'tailwindcss/tailwind.css?inline'; import style from './index.css?inline'; import { IUIThemeConfig } from './interfaces'; import { ColorConfigToCSS, zincTheme } from './utils/colors'; import { uiThemeContext } from './store/theme'; export const ShadowDomContext = createContext<{ root: ShadowRoot | null }>({ root: null }); interface IShadowDomProps extends HTMLAttributes { uiTheme?: IUIThemeConfig; onMount?: (shadowRoot: ShadowRoot) => void; onUnmount?: () => void; /** Keep fixed-position overlays in this shadow host. */ containOverlays?: boolean; } export const ShadowDom: React.FC = function ShadowDom({ onMount, onUnmount, children, uiTheme = zincTheme, containOverlays = true, style: hostStyle, ...attrs }) { const [shadowRoot, setShadowRoot] = useState(null); const rootRef = useRef(null); const onMountRef = useRef(onMount); onMountRef.current = onMount; const onUnmountRef = useRef(onUnmount); onUnmountRef.current = onUnmount; const colorStyle = useMemo(() => ColorConfigToCSS(uiTheme), [uiTheme]); useEffect(() => { if (rootRef.current) { const shadowRoot = rootRef.current.shadowRoot!; setShadowRoot(shadowRoot); onMountRef.current?.(shadowRoot); return () => { onUnmountRef.current?.(); }; } }, []); return ( {/* Leaflet CSS file */} {shadowRoot && ( {children} )} ); };