mui / material-ui

Material UI: Ready-to-use foundational React components, free forever. It includes Material UI, which implements Google's Material Design.
https://mui.com/material-ui/
MIT License
92.44k stars 31.84k forks source link

[material] Add `variant` prop type in all components #33510

Open emlai opened 1 year ago

emlai commented 1 year ago

Duplicates

Latest version

Current behavior 😯

Creating a List component variant causes TypeScript errors when using the variant prop in the variants declaration or at the component usage site.

Suppressing the TypeScript errors allows the variant to be declared and used as expected.

Screen Shot 2022-07-14 at 14 52 39

Expected behavior 🤔

Creating a List component variant is supported in TypeScript.

ListProps should have a declaration for variant, like we have in ButtonProps:

    /**
     * The variant to use.
     * @default 'text'
     */
    variant?: OverridableStringUnion<
      'text' | 'outlined' | 'contained',
      ButtonPropsVariantOverrides
    >;

Steps to reproduce 🕹

Steps:

  1. Create a component variant for List using TypeScript.

CodeSandbox

Context 🔦

We're maintaining a UI library built on top of MUI, and we want to provide a "bordered" List variant for our users. This is is blocking us from doing that.

Your environment 🌎

No response

ZeeshanTamboli commented 1 year ago

The List component does not have a variant prop in it's API.

jussirantala commented 1 year ago

The List component does not have a variant prop in it's API.

Isn't that because the API docs are generated from typings? So there would be an issue in API also because an existing prop is missing from the docs. Though it wouldn't be nice to have it in the API docs since there seems to be no ready-made variants for it by default 🤔 But now the typings are blocking everyone from using the prop...

jussirantala commented 1 year ago

I started to work on a PR for this.

ZeeshanTamboli commented 1 year ago

@jussirantala Can you provide a CodeSandbox showing a List component using a variant prop, and that the type is missing for it? The issue template maybe a good starting point. Thanks.

jussirantala commented 1 year ago

@jussirantala Can you provide a CodeSandbox showing a List component using a variant prop, and that the type is missing for it? The issue template maybe a good starting point. Thanks.

Here you go: https://codesandbox.io/s/list-variant-bug-5fqdfp?file=/src/App.tsx

ZeeshanTamboli commented 1 year ago

Thanks for the reproduction. Looks like a bug indeed.

Not sure how does the style (background: red) gets applied if there is no support of variant prop in the List component implementation.

Looks like this would be the case for many other components in general.

jussirantala commented 1 year ago

Seems it also complains about missing component prop if variant prop is used. I don't know the reason for that. Component prop should be optional.

DinhBaoTran commented 1 year ago

Hi guys, I have similar issue with FormHelperText -> https://github.com/mui/material-ui/issues/33564

mnajdova commented 1 year ago

Adding here some of the conversations we had on the open pull request. These are some open questions that would arise if we decide to support this in all components:

CoennW commented 1 month ago

Hi @mnajdova,

Wondering if there is any progress on this? We assumed variants to be supported in all component as mentioned in the docs. Our designer put time and effort in creating variants, now this does not seem to be supported. I would at least expect something like a notice in the docs, then we could have been aware of this months ago.

matthew-h-wang commented 2 days ago

Hi @mnajdova,

Wondering if there is any progress on this? We assumed variants to be supported in all component as mentioned in the docs. Our designer put time and effort in creating variants, now this does not seem to be supported. I would at least expect something like a notice in the docs, then we could have been aware of this months ago.

Similar situation here, without the variant prop in List/ListItem/ListButtonItem, I am not able to implement my designer's variants the way we we implement new variants to other components like Button or Typography.

matthew-h-wang commented 1 day ago

Hi @mnajdova, Wondering if there is any progress on this? We assumed variants to be supported in all component as mentioned in the docs. Our designer put time and effort in creating variants, now this does not seem to be supported. I would at least expect something like a notice in the docs, then we could have been aware of this months ago.

Similar situation here, without the variant prop in List/ListItem/ListButtonItem, I am not able to implement my designer's variants the way we we implement new variants to other components like Button or Typography.

Actually, I learned that you can add your own variant prop (or any other custom prop) via the OwnProps interfaces. For example:

/* eslint-disable @typescript-eslint/no-empty-interface */

export type CustomListItemButtonOwnProps = {
  variant?: "standard" | "condensed" | "comfort";
  otherCustomProp?: boolean;
};

declare module "@mui/material/ListItemButton" {
  interface ListItemButtonOwnProps extends CustomListItemButtonOwnProps {}
}

or just

declare module "@mui/material/ListItemButton" {
  interface ListItemButtonOwnProps {
    variant?: "standard" | "condensed" | "comfort";
    otherCustomProp?: boolean;
  }
}
CoennW commented 1 day ago

Hi @mnajdova, Wondering if there is any progress on this? We assumed variants to be supported in all component as mentioned in the docs. Our designer put time and effort in creating variants, now this does not seem to be supported. I would at least expect something like a notice in the docs, then we could have been aware of this months ago.

Similar situation here, without the variant prop in List/ListItem/ListButtonItem, I am not able to implement my designer's variants the way we we implement new variants to other components like Button or Typography.

Actually, I learned that you can add your own variant prop (or any other custom prop) via the OwnProps interfaces. For example:

/* eslint-disable @typescript-eslint/no-empty-interface */

export type CustomListItemButtonOwnProps = {
  variant?: "standard" | "condensed" | "comfort";
  otherCustomProp?: boolean;
};

declare module "@mui/material/ListItemButton" {
  interface ListItemButtonOwnProps extends CustomListItemButtonOwnProps {}
}

or just

declare module "@mui/material/ListItemButton" {
  interface ListItemButtonOwnProps {
    variant?: "standard" | "condensed" | "comfort";
    otherCustomProp?: boolean;
  }
}

Yes you can extend the props or find other ways to create variants. But the variant functionality, so using variant in the theme, that is described in the docs of MUI, will not work. This means we have different ways of adding variants, which is not really desirable.