forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateTypography.ts
154 lines (135 loc) · 4.08 KB
/
createTypography.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Code based on Material UI
// The MIT License (MIT)
// Copyright (c) 2014 Call-Em-All
import { ThemeColors } from './createColors';
/** @beta */
export interface ThemeTypography {
fontFamily: string;
fontFamilyMonospace: string;
fontSize: number;
fontWeightLight: number;
fontWeightRegular: number;
fontWeightMedium: number;
fontWeightBold: number;
// The font-size on the html element.
htmlFontSize?: number;
h1: ThemeTypographyVariant;
h2: ThemeTypographyVariant;
h3: ThemeTypographyVariant;
h4: ThemeTypographyVariant;
h5: ThemeTypographyVariant;
h6: ThemeTypographyVariant;
body: ThemeTypographyVariant;
bodySmall: ThemeTypographyVariant;
/**
* @deprecated
* from legacy old theme
* */
size: {
base: string;
xs: string;
sm: string;
md: string;
lg: string;
};
pxToRem: (px: number) => string;
}
export interface ThemeTypographyVariant {
fontSize: string;
fontWeight: number;
lineHeight: number;
fontFamily: string;
letterSpacing?: string;
}
export interface ThemeTypographyInput {
fontFamily?: string;
fontFamilyMonospace?: string;
fontSize?: number;
fontWeightLight?: number;
fontWeightRegular?: number;
fontWeightMedium?: number;
fontWeightBold?: number;
// hat's the font-size on the html element.
// 16px is the default font-size used by browsers.
htmlFontSize?: number;
}
const defaultFontFamily = '"Source Sans Pro", Nunito, Roboto, Arial'; //'"Roboto", "Helvetica", "Arial", sans-serif';
const defaultFontFamilyMonospace = "'Nunito Mono', 'Roboto Mono', monospace"; // "'Roboto Mono', monospace";
export function createTypography(colors: ThemeColors, typographyInput: ThemeTypographyInput = {}): ThemeTypography {
const {
fontFamily = defaultFontFamily,
fontFamilyMonospace = defaultFontFamilyMonospace,
// The default font size of the Material Specification.
fontSize = 14, // px
fontWeightLight = 300,
fontWeightRegular = 400,
fontWeightMedium = 500,
fontWeightBold = 500,
// Tell Grafana-UI what's the font-size on the html element.
// 16px is the default font-size used by browsers.
htmlFontSize = 14,
} = typographyInput;
if (process.env.NODE_ENV !== 'production') {
if (typeof fontSize !== 'number') {
console.error('Grafana-UI: `fontSize` is required to be a number.');
}
if (typeof htmlFontSize !== 'number') {
console.error('Grafana-UI: `htmlFontSize` is required to be a number.');
}
}
const coef = fontSize / 14;
const pxToRem = (size: number) => `${(size / htmlFontSize) * coef}rem`;
const buildVariant = (
fontWeight: number,
size: number,
lineHeight: number,
letterSpacing: number,
casing?: object
): ThemeTypographyVariant => {
if (lineHeight % 2 !== 0 || size % 2 !== 0) {
throw new Error('Font size and line height should be integer multiples of 2 to prevent issues with alignment');
}
return {
fontFamily,
fontWeight,
fontSize: pxToRem(size),
lineHeight: lineHeight / size,
...(fontFamily === defaultFontFamily ? { letterSpacing: `${round(letterSpacing / size)}em` } : {}),
...casing,
};
};
// All our fonts/line heights should be integer multiples of 2 to prevent issues with alignment
const variants = {
h1: buildVariant(fontWeightRegular, 28, 32, -0.25),
h2: buildVariant(fontWeightRegular, 24, 28, 0),
h3: buildVariant(fontWeightRegular, 22, 24, 0),
h4: buildVariant(fontWeightRegular, 18, 22, 0.25),
h5: buildVariant(fontWeightRegular, 16, 22, 0),
h6: buildVariant(fontWeightMedium, 14, 22, 0.15),
body: buildVariant(fontWeightRegular, fontSize, 22, 0.15),
bodySmall: buildVariant(fontWeightRegular, 12, 18, 0.15),
};
const size = {
base: '14px',
xs: '10px',
sm: '12px',
md: '14px',
lg: '18px',
};
return {
htmlFontSize,
pxToRem,
fontFamily,
fontFamilyMonospace,
fontSize,
fontWeightLight,
fontWeightRegular,
fontWeightMedium,
fontWeightBold,
size,
...variants,
};
}
function round(value: number) {
return Math.round(value * 1e5) / 1e5;
}