-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[theme] Custom Variant #15573
Comments
I'm not sure what you're asking for. It sounds like you can already do this via import MuiButton from '@material-ui/core/Button';
function Button({ variant, ...muiButtonProps }) {
if (variant === 'fancy') {
return <MyButtonForVariantFancy />
}
return <MuiButton {...muiButtonProps} />
}
<Button variant="raised">Raised Material-UI</Button>
<Button variant="fancy">Fancy</Button/> |
@philip-lf Could it be a duplicate of #13875 or #15454? |
Thanks for your responses. @oliviertassinari , I don't believe they are the same. Given two buttons, one with an
The feature I would hope for is that a variant of any name can be applied and targeted through the theme. It would look something like this:
The variant would act as an identifier that could be accessed through the theme. So instead of applying unique styles to every component I would centralize my styles in one place. |
@philip-lf Thank you for detailing the use case! It's definitely a problem we are interested in solving. It looks like a duplicate. In theory, if we solve the problem for the color prop, we can solve it too for the variant prop with little effort. |
I am interested in creating custom variant in Typography. It seems to be working, however there are errors in console. Theme: const theme = createMuiTheme({
MuiTypography: {
question: {
fontSize: '14px',
fontWeight: 'bold',
}
}
} Code: <Typography variant="question" > text </Typography> Just works! But with error in console: 🤕
So the only change required is to not to threat this as an error? |
@romanr Thanks for experimenting around with it. People using TypeScript will also have an issue with the accepted values. I think that we can soften the static prop types to accept more values. The challenge is to keep the default values documented, while ideally have a dynamic check to make sure the variant you are trying to use is defined. |
any update regarding the variant error in console? |
Typescript — yes it would be lovely to able to use this without compiler errors. |
This would be extremely helpful for creating Typography variants, our team wants to create more semantically named variants like |
To add to that -- it's also just a bit nicer not to need to rely on variants involving older HTML element entities such as |
MaterialUI has been a pleasure to work with but I'm surprised about the lack of support for custom colors/variants. I see this is bookmarked for v5 but the Oct. 2020 due date is some time away. Are there any updates / plans to support this in v4? |
This comment has been minimized.
This comment has been minimized.
Hi @oliviertassinari when do you think we can have this feature ? I really appreciate your hard work ! |
I'd love to see this sooner as well. Any chance to release this in some sort of beta? |
This will surely help to extend components with lot of flexibility. |
With this feature and style System expands to all components (and not just Box), this will be for me the best UI Toolkit ever. |
@roma @oliviertassinari |
@mehuljariwala you shouldn't expect this to work yet, I don't think. That's what this issue is open to ask for, and the most recent PR to attempt to fix it - #20203 - was closed without being merged. |
Any updates about this "feature"? (this looks so simple to achieve if you think about the other possibilities that the theme provides, yet... I know about the static thing...) |
My approach can be unsafe, but the code below works at least to add custom Variant type. If you see a hidden danger in this approach, please let me know--I just started using TS... declare module '@material-ui/core/styles/createTypography' {
export interface Theme {
noDecoLink?: NoDecoLink;
}
export interface ThemeOptions {
noDecoLink?: NoDecoLink;
}
export type CustomVariant =
| 'h1' // From to this point, defined in the vanilla Variant
| 'h2' // let me know if I do not need this vanilla portion...
| 'h3'
| 'h4'
| 'h5'
| 'h6'
| 'subtitle1'
| 'subtitle2'
| 'body1'
| 'body2'
| 'caption'
| 'button'
| 'overline' // up to this point, defined as vanilla Variant
| 'root' // newly added variant
| 'tab'; // end of newly added variant
// below is simple copy and paste from the createTypography.d.ts
export interface Typography
extends Record<CustomVariant, TypographyStyle>,
FontStyle,
TypographyUtils {}
export interface TypographyOptions
extends Partial<
Record<CustomVariant, TypographyStyleOptions> & FontStyleOptions
> {}
} |
@WongyuChoi |
Having strict prop types by default is one of the awesome features of MUI. It enables auto completion in the editor and provides a really nice development experience. |
Would be great if custom button variants worked like typography... function makeTypography (baseTheme: Theme): MyTypography {
return {
...baseTheme.typography,
myRedText: {
color: red,
}
}
}
function makeMyTheme(theme: Theme) {
return {
...theme,
typography: makeTypography(theme),
}
} |
This comment has been minimized.
This comment has been minimized.
At least in v5 I've been able to get around this issue by using variants in my components theme: import { createMuiTheme } from '@material-ui/core/styles';
const theme = createMuiTheme({
components: {
MuiButton: {
variants: [
{
props: { variant: 'twitter' },
style: {
backgroundColor: '#00acee',
color: '#FFFFFF',
"&:hover": {
backgroundColor: "#007cad",
},
},
},
{
props: { variant: 'facebook' },
style: {
backgroundColor: '#3b5998',
color: '#FFFFFF',
"&:hover": {
backgroundColor: "#314c85",
},
},
},
],
},
},
});
export default theme Then use the button like so: <Grid item>
<Button variant="facebook" startIcon={<FacebookIcon />}>Facebook</Button>
</Grid>
<Grid item>
<Button variant="twitter" startIcon={<TwitterIcon />}>Twitter</Button>
</Grid> I was hoping to be able to do this with the color prop as well in v5, but have not been able to get it to work. 13875 was the closest issue I found to being able to customize the color prop. I would ideally like to be able to do: const theme = createMuiTheme({
palette: {
twitter: {
main: '#00acee'
},
facebook: {
main: '#3b5998'
}
}
}) in my theme then: <Grid item>
<Button color="facebook" startIcon={<FacebookIcon />}>Facebook</Button>
</Grid>
<Grid item>
<Button color="twitter" startIcon={<TwitterIcon />}>Twitter</Button>
</Grid> As of v5 I havent been able to get something that prevents the prop error saying |
@kcarra Thanks for trying it out! We plan to continue pushing in this direction for v5:
|
@oliviertassinari I'm wondering if this issue can be closed then. As far as creating custom variants this seems to be working as expected in V5 using |
If someone wants to avoid typescript errors in V4:
|
I want to add custom variant in version 4. I have created my application with typescript. For the following example variant is successfully added and working fine but i receive the typescript prediction errors const FancyButton = withStyles ({ <FancyButton How i solve typescript prediction |
@Ravindersingh1526 you need to add these new variants with TypeScript module augmentation, please see https://next.material-ui.com/customization/theme-components/#adding-new-component-variants for an example. |
As this API is already available, I am closing this issue. You can take a look on how it can be done on this guide. |
Expected Behavior 🤔
I would love the ability to use a variant with any string and with that variant I could style the component using theme overrides. For example, let's say I have 4 types of buttons throughout my app and each button has a custom variant, inside theme I would simply use
MuiButton
with my custom variant names and style them as I please. The variant would act as an identifier and would give the theme more flexibility in styling the entire app and it would not tie me down to a preset list of variants.Current Behavior 😯
Currently variants are limited and come with additional styling.
Context 🔦
Limits my ability to use theme to a greater extent and pushes me to style more locally than globally.
The text was updated successfully, but these errors were encountered: