UNPKG

2.2 kBJavaScriptView Raw
1import _formatMuiErrorMessage from "@mui/utils/formatMuiErrorMessage";
2import { isUnitless, convertLength, responsiveProperty, alignProperty, fontGrid } from "./cssUtils.js";
3export default function responsiveFontSizes(themeInput, options = {}) {
4 const {
5 breakpoints = ['sm', 'md', 'lg'],
6 disableAlign = false,
7 factor = 2,
8 variants = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'subtitle1', 'subtitle2', 'body1', 'body2', 'caption', 'button', 'overline']
9 } = options;
10 const theme = {
11 ...themeInput
12 };
13 theme.typography = {
14 ...theme.typography
15 };
16 const typography = theme.typography;
17
18 // Convert between CSS lengths e.g. em->px or px->rem
19 // Set the baseFontSize for your project. Defaults to 16px (also the browser default).
20 const convert = convertLength(typography.htmlFontSize);
21 const breakpointValues = breakpoints.map(x => theme.breakpoints.values[x]);
22 variants.forEach(variant => {
23 const style = typography[variant];
24 if (!style) {
25 return;
26 }
27 const remFontSize = parseFloat(convert(style.fontSize, 'rem'));
28 if (remFontSize <= 1) {
29 return;
30 }
31 const maxFontSize = remFontSize;
32 const minFontSize = 1 + (maxFontSize - 1) / factor;
33 let {
34 lineHeight
35 } = style;
36 if (!isUnitless(lineHeight) && !disableAlign) {
37 throw new Error(process.env.NODE_ENV !== "production" ? 'MUI: Unsupported non-unitless line height with grid alignment.\n' + 'Use unitless line heights instead.' : _formatMuiErrorMessage(6));
38 }
39 if (!isUnitless(lineHeight)) {
40 // make it unitless
41 lineHeight = parseFloat(convert(lineHeight, 'rem')) / parseFloat(remFontSize);
42 }
43 let transform = null;
44 if (!disableAlign) {
45 transform = value => alignProperty({
46 size: value,
47 grid: fontGrid({
48 pixels: 4,
49 lineHeight,
50 htmlFontSize: typography.htmlFontSize
51 })
52 });
53 }
54 typography[variant] = {
55 ...style,
56 ...responsiveProperty({
57 cssProperty: 'fontSize',
58 min: minFontSize,
59 max: maxFontSize,
60 unit: 'rem',
61 breakpoints: breakpointValues,
62 transform
63 })
64 };
65 });
66 return theme;
67}
\No newline at end of file