Skip to content

fix(core): a color-mode block follows the nearest media surface; a styled link keeps its hover tint - #11759

Merged
josephfarina merged 1 commit into
mainfrom
core-nearest-surface
Nov 14, 2026
Merged

josephfarina merged 1 commit into
mainfrom
core-nearest-surface

Conversation

@josephfarina

@josephfarina josephfarina commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

A theme's @light / @dark color-mode block applied inside any media surface of its mode. Core itself nests surfaces of the other mode (Toast, Lightbox, the overlay scrim), so a dark-only part color showed on a light surface inside a dark page, and the reverse.

Now the nearest surface decides. The surface rule is :is(SEL):where(T1, …, T4): each T matches an element whose surrounding surfaces form an alternating chain that ends in the block's mode and is not continued by a surface of the other mode. Everything sits inside :where(), so there is no added specificity and no @scope proximity: explicit Text, Heading and onDark/onLight rules keep their weight. It covers up to five alternating nested surfaces; deeper, the block writes nothing (a unit test pins one to six deep). The StyleOverrides JSDoc says the same.

Why ancestor conditions and not @scope: CSS has no selector for an element's nearest ancestor with an attribute. @scope (dark) to (light) (the donut) finds the nearest surface, but its rules are scoped to the surface, and a scoped rule beats one of equal specificity scoped to a farther root by scope proximity. On a surface, a block on an equal-weight style key (a heading level:2 or a text size:lg block) would then override the explicit Heading weight and Text size props that beat it at the Theme root. Chromium: 300 / 30px on a dark surface, 700 / 17px at the root; component:Text FR2, component:Heading FR2 and theme-authoring-contract INV7 require the props to win in both places. So the compiler reads the nearest surface from ancestor chains inside :where() (no specificity, no proximity), one condition per depth: a chain of k surfaces alternating in mode and ending in the block's mode, with no chain one longer ending in the other. That holds exactly when the nearest surface is the block's mode, for up to five alternating surfaces nested one in another; Core's own nesting (a toast or lightbox over a MediaTheme) is well within that. Deeper, the block writes nothing, and a unit test pins that. Browser support is not the reason: Theme already writes its component CSS inside @scope.

A styled Link also keeps Core's hover tint. The color-prop pins keep their resting color and hold Core's hover tint of that color on hover (color-mix with --color-tint-hover, as Link paints it), so a theme that styles link (a color for one mode, or any link color) no longer removes the tint (component:Link FR7). Chromium, light and dark, accent/primary/secondary/disabled, rest and hover: with a link underline, a light-only link color or link.base.color: '#ff0000', hover now tints like a theme with no link setting (on main the styled themes lost the tint). At rest these links show their color prop's color, not the theme's red: the color-prop pins hold each prop's color over a theme's link color, exactly as on main. This change does not alter that resting behavior.

Size: Theme's eager JS grows 186 B gzip (the nearest-surface chain in the rule compiler); the component CSS is unchanged. A theme's own hover color for a link color key still wins over the tint (the pin's hover sits in :where()).

Evidence

Chromium, server-rendered Theme/MediaTheme with real Core CSS and a blocks theme (dark ghost #ff00ff, light input border #00ff00, dark Text 30px, dark Heading 300):

  • Light root > dark > light > dark > light: before, the ghost was magenta at every inner level and the green border leaked into the dark levels. After, each level shows only its own mode. Dark root > light > dark: the same.
  • Explicit Text size/secondary and Heading weight hold at the root and on a dark surface, while plain Text takes the block's size. A light surface inside a dark one: plain Text was 30px before (leak) and is 14px now.
  • A light-only link setting: accent hover was the flat color before (tint lost) and is the tinted color after, the same as a theme with no link setting.

Tests

colorModeBlocks.test, generateThemeRules.test (new link-pin case), the CLI build-theme.color-mode-blocks test, and the Storybook Chromium spec (nearest surface: light inner has green and no magenta; dark again has magenta and no green). Core theme unit tests 1787/1787; check:knowledge clean.

Writing rubric results

One record edit: architecture:theme-compilation INV18 (which surface decides a mode block).

  • R1 narration: none; present tense.
  • R2 research: none.
  • R3 evidence: none in the record.
  • R4 rejected alternatives: none recorded (the @scope reasoning lives in this description and a code comment, not the record).
  • R5 change references: none.
  • R6 open questions: none.
  • R7 project status: none; the five-surface limit and its reason are stated as behavior.

@josephfarina

@vercel

vercel Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 14, 2026 5:27pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 14, 2026
@astracat-bot

astracat-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 14, 2026, 5:45 PM UTC

@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 14, 2026
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 1ca4367

@github-actions github-actions Bot removed the needs:spec-owner-review Current knowledge records await owner approval label Nov 14, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested. The nearest-surface rule for @light/@dark blocks works as described: the nested-surface leaks are gone, and the new tests fail before the change and pass after it. Four things need fixing:

  • A theme's link color now shows only on hover: generateThemeRules.ts:1705-1712 drops the color-prop pins on hover, so with link: {base: {color: '#ff0000'}} links rest at Core's colors and turn rgb(255, 0, 0) under the pointer, against component:Link FR7's hover tint; as written this desktop behavior change needs an owner decision, while keeping the pin on hover as Core's tint of the prop's color (as Link.tsx paints it) restores FR7 and needs none.
  • Stale public doc: the JSDoc on StyleOverrides (defineTheme.ts:194-197) still says that inside surfaces of both modes both blocks apply and the light one wins, the opposite of the new behavior.
  • Changesets claim more than the code does: the link note says a one-mode color no longer removes the tint "in either mode", which is false in that mode and for any theme link color, and the color-mode note calls a lightbox a light surface (Lightbox is always dark) and leaves out the five-surface limit.
  • Record vs. rubric: INV18 now explains the rejected @scope approach over four lines inside an invariant, while the rubric results report no rejected alternative; keep the limit as behavior and drop the rationale or collapse it to one Rejected: line in a decision.

A new commit voids the current /approve-spec, so the record edit needs a new one at the new head.

Claims and files checked
  • Claim, color-mode changeset: the nearest surface decides, so a light surface inside a dark one takes only the light block. Verified (Chromium story probe; colorModeBlocks.test.ts:100-155).
  • Claim, color-mode changeset: a lightbox over a dark surface is a light surface. False (Lightbox.tsx:1791 is always dark); the five-surface limit is also missing.
  • Claim, link changeset: a theme that styles link keeps the hover tint, including a one-mode color in either mode. Verified for a setting that writes no color; false for a theme link color, and in the mode a one-mode color targets.
  • Claim: Core nests surfaces of the other mode (Toast, Lightbox, overlay scrim). Verified (Toast.tsx:449, Lightbox.tsx:1791, OverlayScrim.tsx:267).
  • Claim: the surface rule is one :where() list with no added specificity and no scope proximity. Verified (generated CSS; unit test).
  • Claim: it covers up to five alternating surfaces, deeper writes nothing, and a unit test pins one to six. Verified (colorModeBlocks.test.ts:100-155).
  • Claim: an @scope version measured 300 / 30px on a surface against 700 / 17px at the root. Not reproduced (it describes code that is not in this change).
  • Claim: before, inner surfaces leaked the other mode's block; after, each shows only its own. Verified in Chromium on the ColorModeBlocks story.
  • Claim: explicit Text and Heading props hold at the root and on a surface. Covered by the story's Chromium spec; not separately measured.
  • Claim: plain Text on a light surface inside a dark one went from 30px to 14px. Not separately measured; consistent with the measured ghost and border values.
  • Claim: a light-only link setting regains the tinted hover. Verified in the mode without the setting; false in light mode, where the hover turns the theme's color.
  • Claim: Theme eager CSS grows by 96 B gzip. Not measured.
  • Claim: Core theme unit tests pass and check:knowledge is clean. Verified (1,788 of 1,788 at this head; check:knowledge passes).
  • Claim: writing rubric R1-R7 results. R4 false (INV18 holds a rejected alternative); the others verified.
  • File packages/core/src/theme/generateThemeRules.ts: nearest-surface chains clear; Link pin, finding 1.
  • File packages/core/src/theme/generateThemeRules.test.ts: covers only a non-color link setting (finding 1).
  • File packages/core/src/theme/colorModeBlocks.test.ts: clear.
  • File packages/core/src/theme/__tests__/colorModeBlocks.a11y.chromium.spec.ts: clear.
  • File packages/cli/clients/cli/commands/build-theme.color-mode-blocks.test.mjs: clear.
  • File docs/architecture/theme-compilation.md: finding 4.
  • File .changeset/theme-color-mode-blocks.md: finding 3.
  • File .changeset/theme-link-hover-kept.md: finding 3.
  • Unchanged file packages/core/src/theme/defineTheme.ts: finding 2.

Automated review (maintainer link)

Comment thread .changeset/theme-link-hover-kept.md Resolved
Comment thread .changeset/theme-color-mode-blocks.md Resolved
Comment thread docs/architecture/theme-compilation.md Resolved
Comment on lines +1497 to +1506
* How many media surfaces of alternating modes, one inside another, a
* color-mode block tells apart. CSS has no selector for an element's nearest
* ancestor with an attribute: `@scope (A) to (B)` finds it, but a scoped rule
* beats an unscoped or farther-scoped one of equal specificity by scope
* proximity, so on a surface the block would override the theme root's
* equal-weight rules (an explicit Heading weight) that win over it at the
* root. The nearest surface is read from ancestor chains instead, one
* condition per depth. Core nests a surface in a surface (a toast or a
* lightbox over a MediaTheme) well within this; deeper, the block writes
* nothing.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (INV29 style): one 50-word sentence holds three topics here. Shorter: "CSS cannot select an element's nearest ancestor with an attribute. @scope can, but scope proximity then lets a block beat the theme root's equal-weight rules on a surface. The compiler reads the nearest surface from ancestor chains, one per depth. Deeper nesting writes nothing."

@github-actions github-actions Bot added the needs:spec-owner-review Current knowledge records await owner approval label Nov 14, 2026
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 7547c25

@github-actions github-actions Bot removed the needs:spec-owner-review Current knowledge records await owner approval label Nov 14, 2026
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec 7068978

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes requested. The nearest-surface rule for @light/@dark blocks works as described, and the four points from the last review are fixed; one problem remains in the Link hover pin.

  • The new hover rule for Link's color pins (generateThemeRules.ts:1720-1724) weighs the same as a theme's own link: {'color:accent': {':hover': {…}}} rule and comes after it, so a hover color a theme sets on a Link color key now shows Core's tint instead (rgb(0, 0, 255) on main, the tint here, in light and dark); writing the pin as ${pin}:where(:hover)${HOVER_DISABLED_GUARD} keeps the restored tint, lets that hover color win again, and needs a test and a changeset note.

A fixing commit needs a new /approve-spec at the new head for the INV18 edit. Optional: the size line in the description does not match the repository's size tool (Theme's eager JavaScript grows 186 B gzip; its CSS is unchanged).

Claims and files checked
  • Claim, description: a mode block now follows the nearest media surface, not every enclosing surface of its mode: verified (unit tests, Chromium story).
  • Claim, description: the surface conditions sit inside :where(), adding no specificity and no scope proximity, so explicit Text, Heading and surface-override rules keep their weight: verified (generateThemeRules.ts:1476-1479, story's explicit-prop cases).
  • Claim, description: up to five alternating nested surfaces are read and deeper the block writes nothing, pinned one to six deep: verified (colorModeBlocks.test.ts:100-155 and the chain logic at generateThemeRules.ts:1522-1535).
  • Claim, description: the StyleOverrides docs say the same: verified (defineTheme.ts:191-198).
  • Claim, description: Core's own nesting (toast, lightbox, overlay scrim) stays within the limit, and Theme already writes its component CSS in a scope: verified (each nests one surface; the generator writes inside the theme scope).
  • Claim, description: the scope-based alternative would let a block beat explicit Text size and Heading weight on a surface: describes an alternative the head does not ship; the cited Text and Heading requirements do say the explicit prop wins.
  • Claim, description and link changeset: a styled Link keeps Core's hover tint for each tinted color prop, whatever the link setting: verified for settings without their own hover color; false for a theme that sets a hover color on a Link color key (finding above).
  • Claim, description: at rest styled links keep their color prop's color, as before: verified (Chromium, identical resting colors).
  • Claim, description: Theme eager size grows 96 B gzip: not reproduced (the repository's size tool gives +186 B of eager JavaScript and unchanged CSS).
  • Claim, description: the listed tests pass, the theme unit suite passes and the knowledge check is clean: verified (1788 theme tests pass; the knowledge check exits clean).
  • Claim, description: one record edit, with no rejected alternative recorded in it: verified.
  • Claim, mode-block changeset: the nearest surface decides for up to five alternating surfaces, so a light surface inside a dark one takes only the light block: verified.
  • File packages/core/src/theme/generateThemeRules.ts: finding (hover pin weight); the nearest-surface code is clear.
  • File packages/core/src/theme/generateThemeRules.test.ts: covers only an underline setting; needs the color-key hover case.
  • File packages/core/src/theme/colorModeBlocks.test.ts: clear.
  • File packages/core/src/theme/__tests__/colorModeBlocks.a11y.chromium.spec.ts: clear.
  • File packages/cli/clients/cli/commands/build-theme.color-mode-blocks.test.mjs: clear.
  • File packages/core/src/theme/defineTheme.ts: clear.
  • File docs/architecture/theme-compilation.md: clear (approved by an engineering owner at this head).
  • File .changeset/theme-color-mode-blocks.md: clear.
  • File .changeset/theme-link-hover-kept.md: needs the wording follow-up with the finding.

Automated review (maintainer link)

…yled link keeps Core's hover tint

Inside media surfaces, a component style key's @light or @dark block now applies where the closest surface around the element is of its mode, so a light MediaTheme inside a dark one (a toast over a dark surface) takes only the light block. The condition stays an ancestor condition inside :where(), so the block adds no specificity and no scope proximity and weighs the same on a surface as at the root: explicit Text and Heading props still win. It reads up to five surfaces of alternating modes nested one in another; deeper, the block writes nothing. The theme-compilation record's INV18 and the StyleOverrides docs say so.

The rules that hold each Link color prop's color when a theme styles link now hold, on hover, Core's tint of that color (component:Link FR7), so a link setting of any kind no longer removes the hover tint.
@josephfarina

Copy link
Copy Markdown
Contributor Author

/approve-spec f176f42

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved pending CI. CI has not finished on this head yet.

The nearest-surface rule for @light/@dark blocks works as described, and the Link hover pin now lets a theme's own color-key hover color win, which fixes the last review's finding. Optional: shorten two source comments to the repository's short-sentence comment style (see the inline notes), and in the description cite the Text and Heading requirements alone for explicit props winning and update the theme test count (1789 at this head).

Claims and files checked
  • Claim, description: a mode block now follows the nearest media surface instead of every enclosing surface of its mode: verified (unit tests; the Chromium spec passes on this head's Storybook build; before and after captures).
  • Claim, description: the surface conditions sit inside :where(), adding no specificity and no scope proximity, so explicit Text, Heading and surface-override rules keep their weight: verified (generateThemeRules.ts:1476-1479; the Chromium spec's explicit-prop cases).
  • Claim, description: up to five alternating nested surfaces are read and deeper the block writes nothing, pinned one to six deep: verified (colorModeBlocks.test.ts:100-155, generateThemeRules.ts:1522-1535).
  • Claim, description: the StyleOverrides docs say the same: verified (defineTheme.ts:191-198).
  • Claim, description: Core nests surfaces of the other mode (toast, lightbox, overlay scrim) within that limit: verified (Toast.tsx:449, Lightbox.tsx:1791, OverlayScrim.tsx:267).
  • Claim, description: Theme already writes its component CSS inside a scope: verified (generateThemeRules.ts:57-68).
  • Claim, description: a scope-based alternative would let a block beat explicit Text size and Heading weight on a surface, against requirements that those props win: the Text and Heading requirements do say so; the third record cited covers surface-override inheritance, not prop precedence (non-blocking).
  • Claim, description and link changeset: a theme that styles link keeps Core's hover tint, and a theme's own hover color on a link color key still wins: verified (generateThemeRules.ts:1712-1725, generateThemeRules.test.ts:425-461, Chromium).
  • Claim, description: at rest, styled links keep their color prop's color, as before: verified (Chromium, identical resting colors for all twelve links).
  • Claim, description: Theme's eager JavaScript grows 186 B gzip and its CSS is unchanged: verified within a few bytes (+190 B against the current base; CSS unchanged).
  • Claim, description: the evidence cases (alternating surfaces, explicit props, a light-only link color): verified in Chromium for three nested levels and the link cases; deeper chains by the unit test.
  • Claim, description: the listed tests and the theme unit suite pass, and the knowledge check is clean: verified (1789 theme tests pass at this head; the description says 1787).
  • Claim, description: one record edit, with no rejected alternative recorded in it: verified.
  • Claim, mode-block changeset: the nearest surface decides for up to five alternating surfaces, so a light surface inside a dark one takes only the light block: verified.
  • File packages/core/src/theme/generateThemeRules.ts: clear (two optional comment-style notes).
  • File packages/core/src/theme/generateThemeRules.test.ts: clear.
  • File packages/core/src/theme/colorModeBlocks.test.ts: clear.
  • File packages/core/src/theme/__tests__/colorModeBlocks.a11y.chromium.spec.ts: clear.
  • File packages/cli/clients/cli/commands/build-theme.color-mode-blocks.test.mjs: clear.
  • File packages/core/src/theme/defineTheme.ts: clear.
  • File docs/architecture/theme-compilation.md: clear (approved by an engineering owner at this head).
  • File .changeset/theme-color-mode-blocks.md: clear.
  • File .changeset/theme-link-hover-kept.md: clear.

Automated review (maintainer link)

Comment on lines +1718 to +1720
// On hover Core mixes the hover tint into these colors, so the pin
// holds that tint of the prop's color there too, at no added weight:
// a theme's own hover color for the color key wins over it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (INV29 style): this is one 38-word sentence with two topics. Shorter: "Core tints these colors on hover. The hover pin holds that tint at the resting pin's weight, so a theme's own hover color for the color key still wins."

Comment on lines +1514 to +1518
* the last run's. Condition k is a chain of k surfaces alternating in mode
* and ending in `mode` around the element, with no such chain one longer
* ending in the other mode. When the last run is `mode`, the longest chain
* ending in `mode` is R long and the longest ending in the other mode R - 1,
* so condition R - 1 or R holds; when it is not, no condition holds. With

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking (INV29 style): two sentences here run 29 and 39 words. Shorter: "When the last run is mode, the longest chain that ends in mode has R surfaces. The longest that ends in the other mode has R - 1. So condition R - 1 or R holds. Otherwise no condition holds."

@github-actions

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · f176f42

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC Visual · A11y · RTL
lab/Comments 367.6 kB (+4 B) · 2.5 kB CSS (0) — — · — · —
lab/CommentThread 360.3 kB (+4 B) · 2.4 kB CSS (0) — — · — · —
Markdown 201.2 kB (+3 B) · 21.7 kB CSS (0) — — · — · —
6 more components
Component Size LoC Visual · A11y · RTL
charts/Chart 237.2 kB (+1 B) · 516 B CSS (0) — — · — · —
Lightbox 145.2 kB (0) · 11.9 kB CSS (0) — — · — · —
Overlay 23.0 kB (0) · 3.9 kB CSS (0) — — · — · —
Toast 103.3 kB (0) · 11.1 kB CSS (0) — — · — · —
lab/DialogHeroHeader 88.4 kB (0) · 372 B CSS (0) — — · — · —
lab/ListInput 93.4 kB (0) · 922 B CSS (0) — — · — · —

Package size: @astryxdesign/core 7.58 MB packed (+1.9 kB); no new dependencies.

Wide change (shared styles: packages/core/src/theme/defineTheme.ts, packages/core/src/theme/generateThemeRules.ts): add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

@josephfarina
josephfarina merged commit decc980 into main Nov 14, 2026
91 checks passed
@github-actions
github-actions Bot deleted the core-nearest-surface branch November 15, 2026 06:03

This branch was successfully deployed

1 active deployment
Preview — f176f422 Deployed Nov 14, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant