hyperhive/frontend/packages/shared/src/shadow-css.js
iris 1f86354617 review(#2873): use a plain <style> element instead of adoptedStyleSheets
mara: 'i dont like js css attacher. is there a cleaner way?' — yes: each
component instance was already building its own fresh CSSStyleSheet()
per connect, no sharing across instances, so adoptedStyleSheets bought
nothing here over a plain <style> tag. Same raw-text CSS import, just a
simpler attach step.
2026-07-31 21:53:28 +02:00

21 lines
1.1 KiB
JavaScript

// attachShadowCss(host, cssText, shadowInit) — attach an open shadow root
// to `host`, append `cssText` as a plain `<style>` element, and return the
// root. Shared by every shadow-DOM custom element (hive-dialog, hive-toast,
// hive-btn) so the boilerplate lives in one place instead of being
// copy-pasted per component.
//
// A `<style>` element rather than a constructed `CSSStyleSheet` +
// `adoptedStyleSheets` — mara's call on review, and the right one: each
// component instance was already building its own fresh `CSSStyleSheet()`
// per connect (no sharing across instances), so `adoptedStyleSheets` bought
// nothing here over the plain, universally-understood `<style>` tag. Reach
// for `adoptedStyleSheets` again only if a future component actually shares
// one parsed stylesheet object across many instances — that's the case it
// exists for.
export function attachShadowCss(host, cssText, shadowInit = {}) {
const root = host.attachShadow({ mode: 'open', ...shadowInit });
const style = document.createElement('style');
style.textContent = cssText;
root.append(style);
return root;
}