UNPKG

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