-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Typography] Add ability to specify custom variants #20203
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React from 'react'; | ||
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'; | ||
import Typography from '@material-ui/core/Typography'; | ||
|
||
const theme = createMuiTheme({ | ||
typography: { | ||
customVariants: { | ||
special1: { | ||
fontSize: 15, | ||
fontWeight: 600, | ||
fontStyle: 'italic', | ||
textTransform: 'uppercase', | ||
}, | ||
special2: { | ||
fontSize: 12, | ||
fontWeight: 300, | ||
fontStyle: 'italic', | ||
}, | ||
}, | ||
}, | ||
props: { | ||
MuiTypography: { | ||
variantMapping: { | ||
special1: 'h1', | ||
special2: 'span', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function CustomVariants() { | ||
return ( | ||
<div> | ||
<ThemeProvider theme={theme}> | ||
<Typography variant="special1" color="primary"> | ||
Custom Variant | ||
</Typography> | ||
<Typography variant="special2" color="primary"> | ||
Custom Variant | ||
</Typography> | ||
</ThemeProvider> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React from 'react'; | ||
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles'; | ||
import Typography from '@material-ui/core/Typography'; | ||
|
||
type CustomVariant = 'special1' | 'special2'; | ||
|
||
// You need to specify custom variant names via module augmentation | ||
declare module '@material-ui/core/styles/createTypography' { | ||
interface CustomVariants extends Record<CustomVariant, TypographyStyle> {} | ||
interface CustomVariantsOptions extends Record<CustomVariant, TypographyStyleOptions> {} | ||
} | ||
|
||
const theme = createMuiTheme({ | ||
typography: { | ||
customVariants: { | ||
special1: { | ||
fontSize: 15, | ||
fontWeight: 600, | ||
fontStyle: 'italic', | ||
textTransform: 'uppercase', | ||
}, | ||
special2: { | ||
fontSize: 12, | ||
fontWeight: 300, | ||
fontStyle: 'italic', | ||
}, | ||
}, | ||
}, | ||
props: { | ||
MuiTypography: { | ||
variantMapping: { | ||
special1: 'h1', | ||
special2: 'span', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
export default function CustomVariants() { | ||
return ( | ||
<div> | ||
<ThemeProvider theme={theme}> | ||
<Typography variant="special1" color="primary"> | ||
Custom Variant | ||
</Typography> | ||
<Typography variant="special2" color="primary"> | ||
Custom Variant | ||
</Typography> | ||
</ThemeProvider> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,6 +5,8 @@ import withStyles from '../styles/withStyles'; | |||||
import capitalize from '../utils/capitalize'; | ||||||
|
||||||
export const styles = theme => ({ | ||||||
/* Styles applied to the root element if `variant` is from user defined variants (customVariants). */ | ||||||
...theme.typography.customVariants, | ||||||
/* Styles applied to the root element. */ | ||||||
root: { | ||||||
margin: 0, | ||||||
|
@@ -219,23 +221,7 @@ Typography.propTypes = { | |||||
/** | ||||||
* Applies the theme typography styles. | ||||||
*/ | ||||||
variant: PropTypes.oneOf([ | ||||||
'h1', | ||||||
'h2', | ||||||
'h3', | ||||||
'h4', | ||||||
'h5', | ||||||
'h6', | ||||||
'subtitle1', | ||||||
'subtitle2', | ||||||
'body1', | ||||||
'body2', | ||||||
'caption', | ||||||
'button', | ||||||
'overline', | ||||||
'srOnly', | ||||||
'inherit', | ||||||
]), | ||||||
variant: PropTypes.string, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To keep enough information in the generated API documentation.
Suggested change
|
||||||
/** | ||||||
* The component maps the variant prop to a range of different DOM element types. | ||||||
* For instance, subtitle1 to `<h6>`. | ||||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -15,7 +15,8 @@ export type Variant = | |||
| 'body2' | ||||
| 'caption' | ||||
| 'button' | ||||
| 'overline'; | ||||
| 'overline' | ||||
| keyof CustomVariants; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will prefer module augmentation.
Suggested change
|
||||
|
||||
export interface FontStyle | ||||
extends Required<{ | ||||
|
@@ -38,14 +39,31 @@ export interface FontStyleOptions extends Partial<FontStyle> { | |||
export type TypographyStyle = CSSProperties; | ||||
export interface TypographyStyleOptions extends TypographyStyle {} | ||||
|
||||
export interface CustomVariants {} | ||||
export interface CustomVariantsOptions {} | ||||
|
||||
export interface TypographyCustomVariants { | ||||
customVariants: CustomVariants; | ||||
} | ||||
|
||||
export interface TypographyCustomVariantsOptions { | ||||
customVariants?: CustomVariantsOptions; | ||||
} | ||||
|
||||
export interface TypographyUtils { | ||||
pxToRem: (px: number) => string; | ||||
} | ||||
|
||||
export interface Typography extends Record<Variant, TypographyStyle>, FontStyle, TypographyUtils {} | ||||
export interface Typography | ||||
extends Record<Variant, TypographyStyle>, | ||||
FontStyle, | ||||
TypographyUtils, | ||||
TypographyCustomVariants {} | ||||
|
||||
export interface TypographyOptions | ||||
extends Partial<Record<Variant, TypographyStyleOptions> & FontStyleOptions> {} | ||||
extends Partial< | ||||
Record<Variant, TypographyStyleOptions> & FontStyleOptions & TypographyCustomVariantsOptions | ||||
> {} | ||||
|
||||
export default function createTypography( | ||||
palette: Palette, | ||||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -28,6 +28,7 @@ export default function createTypography(palette, typography) { | |||
// Apply the CSS properties to all the variants. | ||||
allVariants, | ||||
pxToRem: pxToRem2, | ||||
customVariants = {}, | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spreading should already achieve the same.
Suggested change
|
||||
...other | ||||
} = typeof typography === 'function' ? typography(palette) : typography; | ||||
|
||||
|
@@ -85,6 +86,7 @@ export default function createTypography(palette, typography) { | |||
fontWeightRegular, | ||||
fontWeightMedium, | ||||
fontWeightBold, | ||||
customVariants, | ||||
...variants, | ||||
}, | ||||
other, | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.