Theme in shadow roots: read the site's primitives inside the root

Live on X's chat embed: --x-white is declared only inside the embed's root, so
the document's values left .text-white unresolved and the sent bubble's label
stayed white on a light accent (2:1 under Nord). Each root's theme now reads
the site's primitive variables from the root's own content (our sheet off
while reading), falling back to the document's.
This commit is contained in:
CRTkafa 2026-10-01 05:34:23 +07:00
parent 127dd41926
commit b3f160e4c1

View file

@ -286,8 +286,16 @@ export function installThemeRuntime<Mode extends string>(win: Window, site: Them
const key = `${snapshotKey}|${rootRuleCount(root, item.sheet)}|${staticText.length}`; const key = `${snapshotKey}|${rootRuleCount(root, item.sheet)}|${staticText.length}`;
if (force || item.key !== key) { if (force || item.key !== key) {
item.key = key; item.key = key;
// The site's primitives as the root sees them (its own sheets may declare them; the document may not), read with our sheet off.
const vars = { ...siteVars };
if (site.siteVars?.length) {
const probe = root.firstElementChild ?? root.host;
item.sheet.disabled = true;
try { const style = win.getComputedStyle(probe); for (const name of site.siteVars) { const value = style.getPropertyValue(name).trim(); if (value) vars[name] = value; } } catch { /* Keep the document's. */ }
item.sheet.disabled = false;
}
let css = ''; let css = '';
try { css = site.css(collectRootRules(root, item.sheet, (win as Window & typeof globalThis).CSS), choice, mode, siteVars); } catch (error) { console.error(`[${name}] theme (shadow root) failed`, error); } try { css = site.css(collectRootRules(root, item.sheet, (win as Window & typeof globalThis).CSS), choice, mode, vars); } catch (error) { console.error(`[${name}] theme (shadow root) failed`, error); }
try { item.sheet.replaceSync(staticText ? `${css}\n${staticText}` : css); } catch { /* Keeps the last good rules. */ } try { item.sheet.replaceSync(staticText ? `${css}\n${staticText}` : css); } catch { /* Keeps the last good rules. */ }
} }
// Last, so equal-specificity `!important` rules win over the site's; the site may re-set its list. // Last, so equal-specificity `!important` rules win over the site's; the site may re-set its list.