Theme engine: a rule is read as it takes effect (a Tailwind opacity utility's nested @supports value, not its solid fallback); unsupported @supports groups are left out; reach() is exported for site adapters
This commit is contained in:
parent
01763ccdba
commit
b3042f9325
3 changed files with 106 additions and 7 deletions
|
|
@ -21,18 +21,62 @@ export function resolvedFonts(font: FontSpec, siteStack: string): ResolvedFonts
|
|||
})) as ResolvedFonts;
|
||||
}
|
||||
|
||||
/** Every style rule of `doc`'s stylesheets (inside media/supports groups too), except `own`'s. */
|
||||
type RuleNode = { selectorText?: string; style?: RuleLike['style']; cssRules?: ArrayLike<RuleNode>; conditionText?: string; type?: number; constructor?: { name?: string } };
|
||||
const isSupports = (rule: RuleNode) => rule.type === 12 || rule.constructor?.name === 'CSSSupportsRule';
|
||||
|
||||
/**
|
||||
* A style rule as it takes effect. Tailwind 4 writes an opacity utility as a fallback plus the real
|
||||
* value in a nested `@supports`:
|
||||
* `.bg-white\/15 { background-color: var(--white); @supports (color: color-mix(in lab, red, red)) { background-color: color-mix(in oklab, var(--white) 15%, transparent) } }`.
|
||||
* Read alone, the outer declaration is a solid white fill; where the browser passes the condition the
|
||||
* nested one is what paints, so it answers for the rule.
|
||||
*/
|
||||
function effectiveRule(rule: RuleNode, supported: (condition: string) => boolean): RuleLike {
|
||||
const overrides: Array<RuleLike['style']> = [];
|
||||
for (const child of Array.from(rule.cssRules ?? [])) {
|
||||
if (!isSupports(child) || !supported(child.conditionText ?? '')) continue;
|
||||
for (const inner of Array.from(child.cssRules ?? [])) {
|
||||
// Bare declarations inside the group: a CSSNestedDeclarations, or `& { }` in older engines.
|
||||
if (inner.style && (inner.selectorText === undefined || inner.selectorText === '&')) overrides.push(inner.style);
|
||||
}
|
||||
}
|
||||
if (!overrides.length) return rule as RuleLike;
|
||||
const outer = rule.style!;
|
||||
return {
|
||||
selectorText: rule.selectorText!,
|
||||
style: {
|
||||
getPropertyValue(name) {
|
||||
for (let i = overrides.length - 1; i >= 0; i--) { const value = overrides[i]!.getPropertyValue(name); if (value) return value; }
|
||||
return outer.getPropertyValue(name);
|
||||
},
|
||||
length: outer.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Every style rule of `doc`'s stylesheets (inside media/supports groups too), except `own`'s, each as
|
||||
* it takes effect (`effectiveRule`). A `@supports` group the browser does not pass is left out.
|
||||
*/
|
||||
export function collectRules(doc: Document, own: HTMLStyleElement | null): RuleLike[] {
|
||||
const rules: RuleLike[] = [];
|
||||
const walk = (list: CSSRuleList) => {
|
||||
const css = (doc.defaultView as (Window & typeof globalThis) | null)?.CSS;
|
||||
const verdicts = new Map<string, boolean>();
|
||||
const supported = (condition: string) => {
|
||||
if (!condition || !css?.supports) return true;
|
||||
let verdict = verdicts.get(condition);
|
||||
if (verdict === undefined) { try { verdict = css.supports(condition); } catch { verdict = true; } verdicts.set(condition, verdict); }
|
||||
return verdict;
|
||||
};
|
||||
const walk = (list: ArrayLike<RuleNode>) => {
|
||||
for (const rule of Array.from(list)) {
|
||||
if ('selectorText' in rule && 'style' in rule) rules.push(rule as unknown as RuleLike);
|
||||
else if ('cssRules' in rule && !('selectorText' in rule)) walk((rule as CSSGroupingRule).cssRules);
|
||||
if (rule.selectorText !== undefined && rule.style) rules.push(effectiveRule(rule, supported));
|
||||
else if (rule.cssRules && rule.selectorText === undefined && (!isSupports(rule) || supported(rule.conditionText ?? ''))) walk(rule.cssRules);
|
||||
}
|
||||
};
|
||||
for (const sheet of Array.from(doc.styleSheets)) {
|
||||
if (own && sheet.ownerNode === own) continue;
|
||||
try { walk(sheet.cssRules); } catch { /* Cross-origin sheet. */ }
|
||||
try { walk(sheet.cssRules as unknown as ArrayLike<RuleNode>); } catch { /* Cross-origin sheet. */ }
|
||||
}
|
||||
return rules;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@ export const isHex = (value: unknown): value is string => typeof value === 'stri
|
|||
export const rgba = (hex: string, alpha: number) => `rgba(${hexToRgb(hex).join(', ')}, ${+alpha.toFixed(3)})`;
|
||||
|
||||
/** Moves `color` toward black or white until it reaches `ratio` against every surface. */
|
||||
function reach(color: string, surfaces: string[], ratio: number): string {
|
||||
export function reach(color: string, surfaces: string[], ratio: number): string {
|
||||
const worst = (c: string) => Math.min(...surfaces.map(s => contrastRatio(c, s)));
|
||||
if (worst(color) >= ratio) return color;
|
||||
const lightSurfaces = surfaces.reduce((sum, s) => sum + relativeLuminance(s), 0) / surfaces.length > 0.18;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue