Skip to content

[system] Avoid iterating all styleOverrides keys per slot in createStyled #47787

Open

Description

@siriwatknp

Summary

createStyled iterates over all styleOverrides keys for every slot, then overridesResolver picks only the relevant ones. This is unnecessary work that compounds as components grow in slots and override keys. It also caused a class of runtime errors (ownerState undefined) that required per-component workarounds.

Current behavior

In createStyled.js, styleThemeOverrides does this:

for (const slotKey in styleOverrides) {
  resolvedStyleOverrides[slotKey] = processStyle(
    props,
    styleOverrides[slotKey],
    ...
  );
}
return overridesResolver(props, resolvedStyleOverrides);

Every slot's styled component resolves all styleOverrides entries, then overridesResolver cherry-picks the needed ones.

Compounding cost — OutlinedInput example

OutlinedInput has 3 styled slots (Root, NotchedOutline, Input) and up to 15 styleOverrides keys (root, colorSecondary, focused, disabled, adornedStart, adornedEnd, error, sizeSmall, multiline, notchedOutline, input, inputSizeSmall, inputMultiline, inputAdornedStart, inputAdornedEnd).

With the current implementation, each render triggers:

  • Slot Root → iterates 15 keys, uses ~10
  • Slot NotchedOutline → iterates 15 keys, uses 1
  • Slot Input → iterates 15 keys, uses ~7

Total: 45 processStyle calls per render, ~27 of which are discarded. If styleOverrides values are callbacks, each discarded call still executes the function with the wrong slot's props.

For TextField (which composes InputBase + OutlinedInput + InputLabel + FormHelperText), the waste multiplies further.

Runtime errors from the same root cause

Because every slot calls every styleOverrides callback with its own props, slots that don't receive ownerState crash when callbacks depend on it:

These were fixed with per-component workarounds (passing ownerState to internal slots). The systemic fix was deferred because overridesResolver accesses multiple keys from the resolved object — changing it would be breaking.

Proposed behavior

Each slot should only resolve its own styleOverrides entry:

// Instead of iterating all keys:
const resolved = processStyle(props, styleOverrides[slot], ...);
return resolved;

This requires changing overridesResolver to receive the resolved style directly instead of a map of all resolved styles. Components like InputBase that currently read multiple keys would need to be updated.

Context

Search keywords:

Activity

  1. linear commented on Feb 16, 2026

    @linear
  2. added
    status: waiting for maintainer
    on Feb 16, 2026
  3. added
    breaking change
    customization: theme
    package: material-ui
    scope: system
    and removed
    status: waiting for maintainer
    on Feb 16, 2026
  4. siriwatknp commented on Feb 16, 2026

    @siriwatknp
    MemberAuthor

    Breaking change impact assessment

    What changes

    Current overridesResolver signature:

    overridesResolver(props, resolvedStyleOverrides)
    // resolvedStyleOverrides = { root: ..., formControl: ..., adornedStart: ..., ... }

    Proposed: Each slot resolves only its own styleOverrides[slot] entry. overridesResolver either receives a single resolved style or is removed entirely.

    Casegorization

    Case 1: Single-key resolvers (no migration needed) — ~52 components

    These only access styles[slotName] and work as-is with the new approach.

    // Example: CardHeader, Avatar, Alert, Accordion, Badge (root), etc.
    overridesResolver: (props, styles) => styles.root,

    The defaultOverridesResolver(slot) in createStyled.js already produces this pattern. These are unaffected.


    Case 2: Multi-key resolvers on ROOT slots — ~40 components

    These access styles.root plus conditional state/variant keys. This is the main migration surface.

    Pattern A — Conditional state keys (most common)

    // Button (9 keys)
    overridesResolver: (props, styles) => {
      const { ownerState } = props;
      return [
        styles.root,
        styles[ownerState.variant],
        styles[`${ownerState.variant}${capitalize(ownerState.color)}`],
        styles[`size${capitalize(ownerState.size)}`],
        styles[`${ownerState.variant}Size${capitalize(ownerState.size)}`],
        ownerState.color === 'inherit' && styles.colorInherit,
        ownerState.disableElevation && styles.disableElevation,
        ownerState.fullWidth && styles.fullWidth,
        ownerState.loading && styles.loading,
      ];
    }

    Affected: Button, IconButton, Checkbox, Radio, Switch, Slider, TableCell, ListItem, ListItemButton, Fab, Badge, CircularProgress, LinearProgress, Typography, FormControlLabel, InputLabel, Tooltip, Tab, ToggleButton, Pagination, etc.

    Pattern B — Cross-slot keys via nested selectors

    These root resolvers apply child slot styles using class selectors:

    // Chip (18+ keys — worst case)
    overridesResolver: (props, styles) => {
      const { ownerState } = props;
      return [
        { [`& .${chipClasses.avatar}`]: styles.avatar },
        { [`& .${chipClasses.avatar}`]: styles[`avatarColor${capitalize(color)}`] },
        { [`& .${chipClasses.icon}`]: styles.icon },
        { [`& .${chipClasses.deleteIcon}`]: styles.deleteIcon },
        // ... 14 more entries
        styles.root,
        styles[`size${capitalize(size)}`],
        styles[variant],
      ];
    }

    Affected: Chip, Autocomplete, Rating, Switch (switchBase reads input), ButtonGroup.

    Pattern C — Exported/shared resolvers

    // InputBase — exported and reused by OutlinedInput, FilledInput, Input
    export const rootOverridesResolver = (props, styles) => {
      const { ownerState } = props;
      return [
        styles.root,
        ownerState.formControl && styles.formControl,
        ownerState.startAdornment && styles.adornedStart,
        ownerState.endAdornment && styles.adornedEnd,
        ownerState.error && styles.error,
        ownerState.size === 'small' && styles.sizeSmall,
        ownerState.multiline && styles.multiline,
        ownerState.color && styles[`color${capitalize(ownerState.color)}`],
        ownerState.fullWidth && styles.fullWidth,
        ownerState.hiddenLabel && styles.hiddenLabel,
      ];
    };
    
    export const inputOverridesResolver = (props, styles) => {
      const { ownerState } = props;
      return [
        styles.input,
        ownerState.size === 'small' && styles.inputSizeSmall,
        ownerState.multiline && styles.inputMultiline,
        ownerState.type === 'search' && styles.inputTypeSearch,
        ownerState.startAdornment && styles.inputAdornedStart,
        ownerState.endAdornment && styles.inputAdornedEnd,
        ownerState.hiddenLabel && styles.inputHiddenLabel,
      ];
    };

    Affected: InputBase (reused by OutlinedInput, FilledInput, Input).


    Case 3: Multi-key resolvers on NON-ROOT slots — ~15 slots

    // Button's StartIcon slot (3 keys)
    overridesResolver: (props, styles) => {
      const { ownerState } = props;
      return [
        styles.startIcon,
        ownerState.loading && styles.startIconLoadingStart,
        styles[`iconSize${capitalize(ownerState.size)}`],
      ];
    }
    
    // CircularProgress's circle slot (3 keys)
    overridesResolver: (props, styles) => {
      const { ownerState } = props;
      return [
        styles.circle,
        styles[`circle${capitalize(ownerState.variant)}`],
        ownerState.disableShrink && styles.circleDisableShrink,
      ];
    }

    Affected: Button (startIcon, endIcon), Chip (label), CircularProgress (circle, svg), LinearProgress (bar1, bar2), Rating (icon, label, decimal), Slider (thumb, track, rail, mark, markLabel, valueLabel), Autocomplete (popupIndicator, popper).


    Migration approach

    For Case 2 & 3, the multi-key access is used to apply deprecated styleOverrides keys that map states/variants to separate keys (e.g., styleOverrides.colorPrimary, styleOverrides.sizeSmall). In v7, these can be replaced by the variants API in theme.components:

    // Before (v6): users define separate keys
    MuiButton: {
      styleOverrides: {
        root: { ... },
        colorInherit: { color: 'red' },         // separate key
        sizeSmall: { padding: '4px 8px' },       // separate key
      }
    }
    
    // After (v7): users use variants instead
    MuiButton: {
      styleOverrides: {
        root: { ... },    // each slot has exactly one key
      },
      variants: [
        { props: { color: 'inherit' }, style: { color: 'red' } },
        { props: { size: 'small' }, style: { padding: '4px 8px' } },
      ]
    }

    This would:

    1. Make overridesResolver always single-key → eliminate the iteration
    2. Deprecate non-slot keys in styleOverrides (e.g., colorPrimary, sizeSmall)
    3. Fix the ownerState-undefined bug class systemically

    External impact

    This is a breaking change for users who define non-slot keys in theme.components.Mui*.styleOverrides. A codemod could automate migration from state-based keys to variants.

  5. removed this from the Material UI v9 (draft) milestone on Feb 16, 2026
  6. siriwatknp commented on Feb 16, 2026

    @siriwatknp
    MemberAuthor

    Prerequisite: deprecate non-slot styleOverrides keys

    Checked upgrade-to-v6.md, upgrade-to-v7.md, and migrating-from-deprecated-apis.md — no deprecation exists for non-slot styleOverrides keys. The existing v6 deprecation is about composed CSS class names (e.g., .MuiButton-textPrimary → .MuiButton-text.MuiButton-colorPrimary), not about the styleOverrides object keys themselves.

    What's deprecated (v6) vs what's not

    // CSS class name deprecation (v6) — DONE
    styleOverrides: {
      root: {
        [`&.${buttonClasses.textPrimary}`]: { ... }     // ← deprecated class name
        [`&.${buttonClasses.text}.${buttonClasses.colorPrimary}`]: { ... } // ← replacement
      }
    }
    
    // styleOverrides KEY deprecation — NOT STARTED
    styleOverrides: {
      colorPrimary: { color: 'red' },  // ← top-level key, not deprecated
      sizeSmall: { padding: '4px' },   // ← top-level key, not deprecated
    }

    Why this blocks the iteration fix

    The multi-key overridesResolver pattern exists to support these top-level keys. For example, Button's root resolver reads 9 keys (root, variant, variantColor, size, variantSize, colorInherit, disableElevation, fullWidth, loading). Removing the iteration without first deprecating these keys would silently break user themes.

    Suggested sequence

    1. Deprecate non-slot styleOverrides keys with runtime warnings + migration guide pointing users to variants API
    2. Provide codemod to transform styleOverrides.colorPrimary: { ... } → styleOverrides.root.variants: [{ props: { color: 'primary' }, style: { ... } }]
    3. Remove the iteration and simplify overridesResolver to single-key (next major)
  7. Yash-Tamboli commented on Dec 3, 2026

    @Yash-Tamboli

    Hi maintainers! I'd like to investigate the styleOverrides iteration overhead in createStyled and benchmark the proposed direct slot lookup approach. Is this available for contribution, and are there compatibility requirements I should follow?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    breaking changecustomization: themepackage: material-uiperformancescope: system

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions