From 4ba9aa33514423b401c484219d98b441beec20d2 Mon Sep 17 00:00:00 2001 From: Alexey Elizarov Date: Thu, 4 Jun 2026 03:07:56 +0300 Subject: [PATCH] feat: add StyleSheetManager to target another document Adds a provider that routes generated styles (styled / createGlobalStyles) into the given node's document/root instead of the main document.head. Enables rendering into Electron child BrowserWindows, iframes, pop-out windows, and shadow roots. goober already routes by this.target; this threads a target through a context into the existing css.apply({ target }) call. No behavior change when no provider is present. Closes #64 Co-Authored-By: Claude Opus 4.8 --- src/index.d.ts | 11 +++++++ src/index.js | 27 +++++++++++++++- test/styleSheetManager.spec.tsx | 57 +++++++++++++++++++++++++++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 test/styleSheetManager.spec.tsx diff --git a/src/index.d.ts b/src/index.d.ts index 678fd46..1984b95 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -51,6 +51,17 @@ export declare function ThemeProvider< } >(props: T): JSX.Element; export declare function useTheme(): DefaultTheme; +export interface StyleSheetManagerProps { + /** + * DOM node whose root (`document.head` or a shadow root) generated styles are + * injected into. Use to render into another document — an Electron child + * `BrowserWindow`, an iframe, a pop-out `window.open`, or a shadow root. + * Omit to use the main `document.head` (default). + */ + target?: Node; + children?: any; +} +export declare function StyleSheetManager(props: StyleSheetManagerProps): JSX.Element; export interface ThemeProp { theme?: DefaultTheme; } diff --git a/src/index.js b/src/index.js index 346ed9a..027ef15 100644 --- a/src/index.js +++ b/src/index.js @@ -33,11 +33,36 @@ export function useTheme() { return useContext(ThemeContext); } +// Carries the DOM node whose root (`document.head` or a shadow root) goober +// should inject generated rules into. `undefined` = goober's default +// (the main `document.head`). Set via ``. +const StyleTargetContext = createContext(); + +// Mirrors styled-components' ``: every `styled` +// component / `createGlobalStyles` rendered inside this provider injects its +// `