import { createElement } from "../create_element.js"; import { FunctionalComponent } from "../component.js"; import { Text } from "./Text.js"; import { MaybeSignal, useEffect } from "../scope.js"; import { Fragment } from "./Fragment.js"; import { useRenderer } from "../renderer.js"; import { Portal } from "./Portal.js"; type StyleSheetRegistry = Map< string, { _sheet: CSSStyleSheet; _refs: number; } >; const styleSheetRegistrySym = Symbol("styleSheetRegistry"); const globalStyleSheetRegistry: StyleSheetRegistry = new Map(); const getLocalStyleSheetRegistry = ( obj: T & { [styleSheetRegistrySym]?: StyleSheetRegistry }, ): StyleSheetRegistry => (obj[styleSheetRegistrySym] ??= new Map()); const useStyleSheet = ( registry: StyleSheetRegistry, css: string, cleanup: () => void, ): CSSStyleSheet => { if (!globalStyleSheetRegistry.has(css)) { const sheet = new CSSStyleSheet(); sheet.replaceSync(css); globalStyleSheetRegistry.set(css, { _sheet: sheet, _refs: 0, }); } const globalEntry = globalStyleSheetRegistry.get(css)!; globalEntry._refs++; if (!registry.has(css)) { registry.set(css, { _sheet: globalEntry._sheet, _refs: 0, }); } const entry = registry.get(css)!; entry._refs++; useEffect(() => () => { if (!--entry._refs) { registry.delete(css); cleanup(); } if (!--globalEntry._refs) { globalStyleSheetRegistry.delete(css); } }); return entry._sheet; }; export const Style: FunctionalComponent<{ light?: boolean; children?: MaybeSignal; }> = (props) => { const css = props.children; if (typeof css == "function") { // Dynamic CSS will be inserted into the DOM as a