UNPKG

3.4 kBJavaScriptView Raw
1import deepmerge from '@mui/utils/deepmerge';
2function round(value) {
3 return Math.round(value * 1e5) / 1e5;
4}
5const caseAllCaps = {
6 textTransform: 'uppercase'
7};
8const defaultFontFamily = '"Roboto", "Helvetica", "Arial", sans-serif';
9
10/**
11 * @see @link{https://m2.material.io/design/typography/the-type-system.html}
12 * @see @link{https://m2.material.io/design/typography/understanding-typography.html}
13 */
14export default function createTypography(palette, typography) {
15 const {
16 fontFamily = defaultFontFamily,
17 // The default font size of the Material Specification.
18 fontSize = 14,
19 // px
20 fontWeightLight = 300,
21 fontWeightRegular = 400,
22 fontWeightMedium = 500,
23 fontWeightBold = 700,
24 // Tell MUI what's the font-size on the html element.
25 // 16px is the default font-size used by browsers.
26 htmlFontSize = 16,
27 // Apply the CSS properties to all the variants.
28 allVariants,
29 pxToRem: pxToRem2,
30 ...other
31 } = typeof typography === 'function' ? typography(palette) : typography;
32 if (process.env.NODE_ENV !== 'production') {
33 if (typeof fontSize !== 'number') {
34 console.error('MUI: `fontSize` is required to be a number.');
35 }
36 if (typeof htmlFontSize !== 'number') {
37 console.error('MUI: `htmlFontSize` is required to be a number.');
38 }
39 }
40 const coef = fontSize / 14;
41 const pxToRem = pxToRem2 || (size => `${size / htmlFontSize * coef}rem`);
42 const buildVariant = (fontWeight, size, lineHeight, letterSpacing, casing) => ({
43 fontFamily,
44 fontWeight,
45 fontSize: pxToRem(size),
46 // Unitless following https://meyerweb.com/eric/thoughts/2006/02/08/unitless-line-heights/
47 lineHeight,
48 // The letter spacing was designed for the Roboto font-family. Using the same letter-spacing
49 // across font-families can cause issues with the kerning.
50 ...(fontFamily === defaultFontFamily ? {
51 letterSpacing: `${round(letterSpacing / size)}em`
52 } : {}),
53 ...casing,
54 ...allVariants
55 });
56 const variants = {
57 h1: buildVariant(fontWeightLight, 96, 1.167, -1.5),
58 h2: buildVariant(fontWeightLight, 60, 1.2, -0.5),
59 h3: buildVariant(fontWeightRegular, 48, 1.167, 0),
60 h4: buildVariant(fontWeightRegular, 34, 1.235, 0.25),
61 h5: buildVariant(fontWeightRegular, 24, 1.334, 0),
62 h6: buildVariant(fontWeightMedium, 20, 1.6, 0.15),
63 subtitle1: buildVariant(fontWeightRegular, 16, 1.75, 0.15),
64 subtitle2: buildVariant(fontWeightMedium, 14, 1.57, 0.1),
65 body1: buildVariant(fontWeightRegular, 16, 1.5, 0.15),
66 body2: buildVariant(fontWeightRegular, 14, 1.43, 0.15),
67 button: buildVariant(fontWeightMedium, 14, 1.75, 0.4, caseAllCaps),
68 caption: buildVariant(fontWeightRegular, 12, 1.66, 0.4),
69 overline: buildVariant(fontWeightRegular, 12, 2.66, 1, caseAllCaps),
70 // TODO v6: Remove handling of 'inherit' variant from the theme as it is already handled in Material UI's Typography component. Also, remember to remove the associated types.
71 inherit: {
72 fontFamily: 'inherit',
73 fontWeight: 'inherit',
74 fontSize: 'inherit',
75 lineHeight: 'inherit',
76 letterSpacing: 'inherit'
77 }
78 };
79 return deepmerge({
80 htmlFontSize,
81 pxToRem,
82 fontFamily,
83 fontSize,
84 fontWeightLight,
85 fontWeightRegular,
86 fontWeightMedium,
87 fontWeightBold,
88 ...variants
89 }, other, {
90 clone: false // No need to clone deep
91 });
92}
\No newline at end of file