mui / mui/material-ui

Make component theme default props variant-aware

Open
#34,812 6 comments 45 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

scope: system type: new feature
Dominant language
JavaScript
Stars
99.1k
Forks
32.5k
Avg merge
2d 17h
Merged PRs (30d)
106

Description

Duplicates
  • I have searched the existing issues
Latest version
  • I have tested the latest version
Summary 💡

When passing component overrides to createTheme(), allow the variants objects for each component to define their own default props as a callback with theme as its argument (same as style on the same object). This would mean that given a minimum set of required props for the component to qualify as a 'variant', we can assign any other default props for that variant.

Examples 🌈
export const MuiAutocompleteOverrides: Components<Theme>['MuiAutocomplete'] = {
  defaultProps: { // THIS IS NOT VARIANT AWARE
    popupIcon: (
      <CaretDownOutlineIcon size={theme.iconScale['x-small']} color={theme.colors['neutral-75']} />
    )
  },
  variants: [
    {
      props: {
        size: 'medium'
      },
      style: (theme) => ({
        padding: 0
      }),
      defaultProps: (theme) => ({ // THIS IS VARIANT AWARE
        popupIcon: (
          <CaretDownOutlineIcon size={theme.iconScale['medium']} color={theme.colors['neutral-75']} />
        )
      })
    }
  ]
};
Motivation 🔦

I want to be able to define what the default props for a component should be given that it matches a specific variant which i have defined. Specifically, i want to define a default icon for the Autocomplete component whose size changes according to the size prop passed to the component. Without this ability, i am forced to use a default icon whose size won't intelligently match the Autocomplete depending on the size. Instead, i have to pass the icon as the appropriate size everywhere that i use the Autocomplete.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at createTheme's component override and variant handling, then compare how variant style callbacks receive the theme. Trace how matching variant props are applied and define the completion criteria around variant defaultProps callbacks receiving the theme and applying their returned props.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
frontend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.