1 | import deepmerge from '@mui/utils/deepmerge';
|
2 | function round(value) {
|
3 | return Math.round(value * 1e5) / 1e5;
|
4 | }
|
5 | const caseAllCaps = {
|
6 | textTransform: 'uppercase'
|
7 | };
|
8 | const defaultFontFamily = '"Roboto", "Helvetica", "Arial", sans-serif';
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export default function createTypography(palette, typography) {
|
15 | const {
|
16 | fontFamily = defaultFontFamily,
|
17 |
|
18 | fontSize = 14,
|
19 |
|
20 | fontWeightLight = 300,
|
21 | fontWeightRegular = 400,
|
22 | fontWeightMedium = 500,
|
23 | fontWeightBold = 700,
|
24 |
|
25 |
|
26 | htmlFontSize = 16,
|
27 |
|
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 |
|
47 | lineHeight,
|
48 |
|
49 |
|
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 |
|
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
|
91 | });
|
92 | } |
\ | No newline at end of file |