[material-ui] Derive interaction state colors from the palette (enhanceColorStates) - #49048
Draft
siriwatknp wants to merge 2 commits into
Draft
[material-ui] Derive interaction state colors from the palette (enhanceColorStates)#49048siriwatknp wants to merge 2 commits into
siriwatknp wants to merge 2 commits into
Conversation
…lors Today the library produces interaction colors four different ways (palette[color].dark shift, alpha(hoverOpacity) tint, flat action.hover, or nothing at all), offers no theme-level lever to redirect them, and has no pressed state. The compound rule `selectedOpacity + hoverOpacity` is hand-copied 14 times across 7 components because there is nowhere to store it. enhanceColorStates(theme, config) runs a generator once and attaches theme.states: ready-to-use style objects keyed by state name, derived per palette color with color-mix toward palette.common.onBackground so one expression is correct in every scheme. Levers are a pole, two magnitudes, per-state levels and a disabled model. Components gate only; sharedStateComponents applies the values at each family's own selectors. Button and MenuItem converted. createTheme is untouched — a theme that never calls the enhancer emits identical CSS.
Tokens-only theme (zero styleOverrides) plus enhanceColorStates, light and dark. Swatches read rest/hover/active straight out of theme.states; a toggle compares against the baseline theme.
Deploy previewBundle size
Check out the code infra dashboard for more information about this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Material UI produces interaction colors one fixed way and offers no level at which a design system can change it.
The production rules are hard-coded per component and mutually inconsistent. Four mechanisms coexist today: a
palette[color].darkshift (Button contained, Chip, Fab, PaginationItem, ButtonGroup); analpha(…, action.hoverOpacity)tint (13 components referencehoverOpacity); a flataction.hovercolor (7 components use it as a state background); and no state color at all in Tab, AccordionSummary, BottomNavigationAction, StepButton, OutlinedInput and FilledInput. Several components mix two or three. There is no pressed state anywhere.The global surface is too small to redirect any of it. All a theme can say globally is
palette.action.*andpalette[color].light/.dark. That surface is flat (one hover color for the whole app, so a primary row and an error row hover identically), absolute (an rgba constant and a fixed "darker" direction, neither of which can move away from the page background), and partial (Button reads none ofaction.hover, so the one global lever misses the most-used component).So every correction lands inside a component, per variant, per scheme, through
styleOverrides.The clearest evidence the mechanism is missing is that the library itself has nowhere to put one. This string is hand-written, character for character, 14 times across 7 components (Chip ×3; Autocomplete, ToggleButton, MenuItem, PaginationItem, ListItemButton ×2 each; TableRow ×1):
`${(theme.vars || theme).palette.action.selectedOpacity} + ${(theme.vars || theme).palette.action.hoverOpacity}`Composing "selected" with "hover" is a rule. With no place to store a rule, it gets copied.
Proposal
One function. The palette is authored exactly as it is today — nothing is marked or wrapped.
It runs a generator once and attaches
theme.states, a sibling oftheme.paletteholding ready-to-use style objects keyed by state name:Every palette entry carrying a
mainderives, including custom colors;text,background,action,grey,commonanddividerare skipped without a list to maintain.Why derived, and why toward a pole
palette[color].darkencodes an absolute direction. "Darker on hover" is right on a light ground and wrong on a dark one, which is why dark mode needs state colors hand-authored a second time. Measured against a production design system's token export (3 color modes, ~100default → hover → pressedtriads), every state is the rest color moved a fixed number of steps away from the page background — one step for hover, two for pressed.Material UI already ships the pole that expresses this:
palette.common.onBackground(#000light /#fffdark), already consumed by the input and ButtonGroup borders, and settable — which matters, because a third mode in that same export uses a pale blue rather than white.in oklab, notoklch: with a neutral pole the two are identical, but a tinted pole makesoklchrotate hue — a red button drifts purple — whileoklabdesaturates and keeps color identity.Configuration
The entire public surface is a pole, two magnitudes, per-state levels, and a disabled model:
Per-scheme magnitudes need no second API: every field is a plain CSS string, so a theme passes a
var()reference and lets the existing color-scheme emitter resolve it per scheme (palette.statescarries the values).Component contract
Components gate; they never consume.
It gates the colors, not the block, so behavior like a touch reset or
text-decoration: nonekeeps applying. A private shared layer (sharedStateComponents.ts, sibling ofsharedDensityComponents.ts) then applies the values at each family's own selectors, as function overrides.Selectors belong to the component, never to the data. Which selector a state maps to — and whether hover is media-gated — already differs per family, so encoding it in the theme would force every family into one family's assumptions. Keying by name makes both access patterns first-class:
Hover is emitted inside
@media (hover: hover)by the layer, so nothing downstream needs a@media (hover: none)reset;:activestays ungated so a tap still gives press feedback.Backward compatibility
enhanceColorStates→ notheme.states→ every gate takes its legacy branch → identical output.createThemeis not modified (verified by an empty diff). The enhancer is the seam, exactly as it is for density.light/darkremain authored tokens and keep being read by everything that reads them today.palettegains no color keys, so code iterating palette colors is unaffected.Results
step: 4.6%; dark Δ0 / Δ1 / Δ3 at4.4%.activeexists — contained Button reads three distinct rest/hover/active values.Scope here, and what follows
Converted: Button (unpacks into its private
--variant-*properties) and MenuItem (applies straight at its own selectors) — deliberately the two different shapes.Next, ordered by the mechanism each component uses rather than popularity: Autocomplete, TableRow, ListItemButton, ListItem, PaginationItem (direct painters — the layer only spreads, and they remove 7 of the 14 rule copies), then Chip, ToggleButton, ButtonGroup, IconButton, Fab (border ramp), then the inputs, then the SwitchBase controls.
Open questions
hoverstay media-gated by default, or become a lever?enhanceColorStates/theme.states.--variant-*properties, a rename would fail silently — this needs a CI test asserting those names still appear in the component's output. It is intended to land with the VRT pass once the conversion set is final. Reworking Button's indirection is not proposed; it correctly decomposes 3 variants × N colors into 3 + N rules.Try it
docs/pages/experiments/color-gap.tsx→/experiments/color-gap/— a tokens-only theme (zerostyleOverrides) with a baseline toggle and swatches read straight fromtheme.states.