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. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d2c8b92464
commit
cf98c381ae
1 changed files with 9 additions and 1 deletions
|
|
@ -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.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue