@styleframe/theme 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.tsbuildinfo +1 -0
- package/CHANGELOG.md +12 -0
- package/package.json +48 -0
- package/src/constants/border.ts +0 -0
- package/src/constants/breakpoint.ts +0 -0
- package/src/constants/color.ts +0 -0
- package/src/constants/index.ts +4 -0
- package/src/constants/scale.ts +3 -0
- package/src/constants/typography.ts +0 -0
- package/src/index.ts +4 -0
- package/src/shims.d.ts +8 -0
- package/src/types.ts +32 -0
- package/src/utils/createUseVariable.test.ts +928 -0
- package/src/utils/createUseVariable.ts +107 -0
- package/src/utils/index.ts +1 -0
- package/src/variables/index.ts +22 -0
- package/src/variables/useBorderColor.ts +27 -0
- package/src/variables/useBorderRadius.test.ts +335 -0
- package/src/variables/useBorderRadius.ts +26 -0
- package/src/variables/useBorderStyle.test.ts +569 -0
- package/src/variables/useBorderStyle.ts +49 -0
- package/src/variables/useBorderWidth.test.ts +535 -0
- package/src/variables/useBorderWidth.ts +38 -0
- package/src/variables/useBoxShadow.test.ts +336 -0
- package/src/variables/useBoxShadow.ts +54 -0
- package/src/variables/useBreakpoint.test.ts +447 -0
- package/src/variables/useBreakpoint.ts +38 -0
- package/src/variables/useColor.test.ts +360 -0
- package/src/variables/useColor.ts +35 -0
- package/src/variables/useColorLightness.test.ts +168 -0
- package/src/variables/useColorLightness.ts +59 -0
- package/src/variables/useColorShade.test.ts +166 -0
- package/src/variables/useColorShade.ts +52 -0
- package/src/variables/useColorTint.test.ts +164 -0
- package/src/variables/useColorTint.ts +52 -0
- package/src/variables/useEasing.ts +0 -0
- package/src/variables/useFontFamily.test.ts +228 -0
- package/src/variables/useFontFamily.ts +33 -0
- package/src/variables/useFontSize.test.ts +299 -0
- package/src/variables/useFontSize.ts +32 -0
- package/src/variables/useFontStyle.test.ts +555 -0
- package/src/variables/useFontStyle.ts +37 -0
- package/src/variables/useFontWeight.test.ts +650 -0
- package/src/variables/useFontWeight.ts +55 -0
- package/src/variables/useLetterSpacing.test.ts +455 -0
- package/src/variables/useLetterSpacing.ts +41 -0
- package/src/variables/useLineHeight.test.ts +410 -0
- package/src/variables/useLineHeight.ts +41 -0
- package/src/variables/useMultiplier.test.ts +722 -0
- package/src/variables/useMultiplier.ts +44 -0
- package/src/variables/useScale.test.ts +393 -0
- package/src/variables/useScale.ts +52 -0
- package/src/variables/useScalePowers.test.ts +412 -0
- package/src/variables/useScalePowers.ts +35 -0
- package/src/variables/useSpacing.test.ts +309 -0
- package/src/variables/useSpacing.ts +26 -0
- package/src/vite-env.d.ts +1 -0
- package/test-scale.js +22 -0
- package/tsconfig.json +7 -0
- package/vite.config.ts +5 -0
|
@@ -0,0 +1,650 @@
|
|
|
1
|
+
import type { Variable } from "@styleframe/core";
|
|
2
|
+
import { styleframe } from "@styleframe/core";
|
|
3
|
+
import { consume } from "@styleframe/transpiler";
|
|
4
|
+
import { defaultFontWeightValues, useFontWeight } from "./useFontWeight";
|
|
5
|
+
|
|
6
|
+
describe("useFontWeight", () => {
|
|
7
|
+
it("should create all font weight variables with correct names and values", () => {
|
|
8
|
+
const s = styleframe();
|
|
9
|
+
const {
|
|
10
|
+
fontWeightExtralight,
|
|
11
|
+
fontWeightLight,
|
|
12
|
+
fontWeightNormal,
|
|
13
|
+
fontWeightMedium,
|
|
14
|
+
fontWeightSemibold,
|
|
15
|
+
fontWeightBold,
|
|
16
|
+
fontWeightBlack,
|
|
17
|
+
fontWeightLighter,
|
|
18
|
+
fontWeightBolder,
|
|
19
|
+
fontWeight,
|
|
20
|
+
} = useFontWeight(s);
|
|
21
|
+
|
|
22
|
+
expect(fontWeightExtralight).toEqual({
|
|
23
|
+
type: "variable",
|
|
24
|
+
name: "font-weight--extralight",
|
|
25
|
+
value: 200,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(fontWeightLight).toEqual({
|
|
29
|
+
type: "variable",
|
|
30
|
+
name: "font-weight--light",
|
|
31
|
+
value: 300,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
expect(fontWeightNormal).toEqual({
|
|
35
|
+
type: "variable",
|
|
36
|
+
name: "font-weight--normal",
|
|
37
|
+
value: "normal",
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
expect(fontWeightMedium).toEqual({
|
|
41
|
+
type: "variable",
|
|
42
|
+
name: "font-weight--medium",
|
|
43
|
+
value: 500,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(fontWeightSemibold).toEqual({
|
|
47
|
+
type: "variable",
|
|
48
|
+
name: "font-weight--semibold",
|
|
49
|
+
value: 600,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(fontWeightBold).toEqual({
|
|
53
|
+
type: "variable",
|
|
54
|
+
name: "font-weight--bold",
|
|
55
|
+
value: "bold",
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
expect(fontWeightBlack).toEqual({
|
|
59
|
+
type: "variable",
|
|
60
|
+
name: "font-weight--black",
|
|
61
|
+
value: 900,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
expect(fontWeightLighter).toEqual({
|
|
65
|
+
type: "variable",
|
|
66
|
+
name: "font-weight--lighter",
|
|
67
|
+
value: "lighter",
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
expect(fontWeightBolder).toEqual({
|
|
71
|
+
type: "variable",
|
|
72
|
+
name: "font-weight--bolder",
|
|
73
|
+
value: "bolder",
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
expect(fontWeight).toEqual({
|
|
77
|
+
type: "variable",
|
|
78
|
+
name: "font-weight",
|
|
79
|
+
value: {
|
|
80
|
+
type: "reference",
|
|
81
|
+
name: "font-weight--normal",
|
|
82
|
+
fallback: undefined,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("should add all font weight variables to root", () => {
|
|
88
|
+
const s = styleframe();
|
|
89
|
+
useFontWeight(s);
|
|
90
|
+
|
|
91
|
+
expect(s.root.variables).toHaveLength(11);
|
|
92
|
+
expect(s.root.variables[0]?.name).toBe("font-weight--extralight");
|
|
93
|
+
expect(s.root.variables[1]?.name).toBe("font-weight--light");
|
|
94
|
+
expect(s.root.variables[2]?.name).toBe("font-weight--normal");
|
|
95
|
+
expect(s.root.variables[3]?.name).toBe("font-weight--medium");
|
|
96
|
+
expect(s.root.variables[4]?.name).toBe("font-weight--semibold");
|
|
97
|
+
expect(s.root.variables[5]?.name).toBe("font-weight--bold");
|
|
98
|
+
expect(s.root.variables[6]?.name).toBe("font-weight--black");
|
|
99
|
+
expect(s.root.variables[7]?.name).toBe("font-weight--lighter");
|
|
100
|
+
expect(s.root.variables[8]?.name).toBe("font-weight--bolder");
|
|
101
|
+
expect(s.root.variables[9]?.name).toBe("font-weight--inherit");
|
|
102
|
+
expect(s.root.variables[10]?.name).toBe("font-weight");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("should return all font weight variables in an object", () => {
|
|
106
|
+
const s = styleframe();
|
|
107
|
+
const fontWeights = useFontWeight(s);
|
|
108
|
+
|
|
109
|
+
expect(Object.keys(fontWeights)).toEqual([
|
|
110
|
+
"fontWeightExtralight",
|
|
111
|
+
"fontWeightLight",
|
|
112
|
+
"fontWeightNormal",
|
|
113
|
+
"fontWeightMedium",
|
|
114
|
+
"fontWeightSemibold",
|
|
115
|
+
"fontWeightBold",
|
|
116
|
+
"fontWeightBlack",
|
|
117
|
+
"fontWeightLighter",
|
|
118
|
+
"fontWeightBolder",
|
|
119
|
+
"fontWeightInherit",
|
|
120
|
+
"fontWeight",
|
|
121
|
+
]);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("should compile to correct CSS output using consume", () => {
|
|
125
|
+
const s = styleframe();
|
|
126
|
+
useFontWeight(s);
|
|
127
|
+
|
|
128
|
+
const css = consume(s.root, s.options);
|
|
129
|
+
|
|
130
|
+
expect(css).toBe(`:root {
|
|
131
|
+
--font-weight--extralight: 200;
|
|
132
|
+
--font-weight--light: 300;
|
|
133
|
+
--font-weight--normal: normal;
|
|
134
|
+
--font-weight--medium: 500;
|
|
135
|
+
--font-weight--semibold: 600;
|
|
136
|
+
--font-weight--bold: bold;
|
|
137
|
+
--font-weight--black: 900;
|
|
138
|
+
--font-weight--lighter: lighter;
|
|
139
|
+
--font-weight--bolder: bolder;
|
|
140
|
+
--font-weight--inherit: inherit;
|
|
141
|
+
--font-weight: var(--font-weight--normal);
|
|
142
|
+
}`);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("should compile individual font weight variable to correct CSS", () => {
|
|
146
|
+
const s = styleframe();
|
|
147
|
+
const { fontWeightSemibold } = useFontWeight(s);
|
|
148
|
+
|
|
149
|
+
const css = consume(fontWeightSemibold, s.options);
|
|
150
|
+
|
|
151
|
+
expect(css).toBe("--font-weight--semibold: 600;");
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it("should not create duplicate variables when called multiple times", () => {
|
|
155
|
+
const s = styleframe();
|
|
156
|
+
const fontWeights1 = useFontWeight(s);
|
|
157
|
+
const fontWeights2 = useFontWeight(s);
|
|
158
|
+
|
|
159
|
+
// Should return the same variables (default: true behavior)
|
|
160
|
+
expect(fontWeights1.fontWeightExtralight).toBe(
|
|
161
|
+
fontWeights2.fontWeightExtralight,
|
|
162
|
+
);
|
|
163
|
+
expect(fontWeights1.fontWeight).toBe(fontWeights2.fontWeight);
|
|
164
|
+
expect(s.root.variables).toHaveLength(11);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should allow font weight variables to be used as references", () => {
|
|
168
|
+
const s = styleframe();
|
|
169
|
+
const { fontWeightBold } = useFontWeight(s);
|
|
170
|
+
|
|
171
|
+
const customFontWeight = s.variable(
|
|
172
|
+
"custom-font-weight",
|
|
173
|
+
s.ref(fontWeightBold),
|
|
174
|
+
);
|
|
175
|
+
|
|
176
|
+
expect(customFontWeight.value).toEqual({
|
|
177
|
+
type: "reference",
|
|
178
|
+
name: "font-weight--bold",
|
|
179
|
+
fallback: undefined,
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
const css = consume(s.root, s.options);
|
|
183
|
+
expect(css).toEqual(`:root {
|
|
184
|
+
--font-weight--extralight: 200;
|
|
185
|
+
--font-weight--light: 300;
|
|
186
|
+
--font-weight--normal: normal;
|
|
187
|
+
--font-weight--medium: 500;
|
|
188
|
+
--font-weight--semibold: 600;
|
|
189
|
+
--font-weight--bold: bold;
|
|
190
|
+
--font-weight--black: 900;
|
|
191
|
+
--font-weight--lighter: lighter;
|
|
192
|
+
--font-weight--bolder: bolder;
|
|
193
|
+
--font-weight--inherit: inherit;
|
|
194
|
+
--font-weight: var(--font-weight--normal);
|
|
195
|
+
--custom-font-weight: var(--font-weight--bold);
|
|
196
|
+
}`);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("should work with selector overrides", () => {
|
|
200
|
+
const s = styleframe();
|
|
201
|
+
const { fontWeightNormal } = useFontWeight(s);
|
|
202
|
+
|
|
203
|
+
s.selector(".custom-font-weight", ({ variable }) => {
|
|
204
|
+
variable(fontWeightNormal, 450);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
const css = consume(s.root, s.options);
|
|
208
|
+
|
|
209
|
+
expect(css).toEqual(`:root {
|
|
210
|
+
--font-weight--extralight: 200;
|
|
211
|
+
--font-weight--light: 300;
|
|
212
|
+
--font-weight--normal: normal;
|
|
213
|
+
--font-weight--medium: 500;
|
|
214
|
+
--font-weight--semibold: 600;
|
|
215
|
+
--font-weight--bold: bold;
|
|
216
|
+
--font-weight--black: 900;
|
|
217
|
+
--font-weight--lighter: lighter;
|
|
218
|
+
--font-weight--bolder: bolder;
|
|
219
|
+
--font-weight--inherit: inherit;
|
|
220
|
+
--font-weight: var(--font-weight--normal);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
.custom-font-weight {
|
|
224
|
+
--font-weight--normal: 450;
|
|
225
|
+
}`);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
describe("type safety", () => {
|
|
229
|
+
it("should preserve exact font weight variable names in return type", () => {
|
|
230
|
+
const s = styleframe();
|
|
231
|
+
const fontWeights = useFontWeight(s);
|
|
232
|
+
|
|
233
|
+
// Type assertions to verify the generic types are preserved
|
|
234
|
+
const extralight: Variable<"font-weight--extralight"> =
|
|
235
|
+
fontWeights.fontWeightExtralight;
|
|
236
|
+
const light: Variable<"font-weight--light"> = fontWeights.fontWeightLight;
|
|
237
|
+
const normal: Variable<"font-weight--normal"> =
|
|
238
|
+
fontWeights.fontWeightNormal;
|
|
239
|
+
const medium: Variable<"font-weight--medium"> =
|
|
240
|
+
fontWeights.fontWeightMedium;
|
|
241
|
+
const semibold: Variable<"font-weight--semibold"> =
|
|
242
|
+
fontWeights.fontWeightSemibold;
|
|
243
|
+
const bold: Variable<"font-weight--bold"> = fontWeights.fontWeightBold;
|
|
244
|
+
const black: Variable<"font-weight--black"> = fontWeights.fontWeightBlack;
|
|
245
|
+
const lighter: Variable<"font-weight--lighter"> =
|
|
246
|
+
fontWeights.fontWeightLighter;
|
|
247
|
+
const bolder: Variable<"font-weight--bolder"> =
|
|
248
|
+
fontWeights.fontWeightBolder;
|
|
249
|
+
const fontWeight: Variable<"font-weight"> = fontWeights.fontWeight;
|
|
250
|
+
|
|
251
|
+
expect(extralight.name).toBe("font-weight--extralight");
|
|
252
|
+
expect(light.name).toBe("font-weight--light");
|
|
253
|
+
expect(normal.name).toBe("font-weight--normal");
|
|
254
|
+
expect(medium.name).toBe("font-weight--medium");
|
|
255
|
+
expect(semibold.name).toBe("font-weight--semibold");
|
|
256
|
+
expect(bold.name).toBe("font-weight--bold");
|
|
257
|
+
expect(black.name).toBe("font-weight--black");
|
|
258
|
+
expect(lighter.name).toBe("font-weight--lighter");
|
|
259
|
+
expect(bolder.name).toBe("font-weight--bolder");
|
|
260
|
+
expect(fontWeight.name).toBe("font-weight");
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it("should have correct value types", () => {
|
|
264
|
+
const s = styleframe();
|
|
265
|
+
const fontWeights = useFontWeight(s);
|
|
266
|
+
|
|
267
|
+
expect(typeof fontWeights.fontWeightExtralight.value).toBe("number");
|
|
268
|
+
expect(typeof fontWeights.fontWeightLight.value).toBe("number");
|
|
269
|
+
expect(typeof fontWeights.fontWeightNormal.value).toBe("string");
|
|
270
|
+
expect(typeof fontWeights.fontWeightMedium.value).toBe("number");
|
|
271
|
+
expect(typeof fontWeights.fontWeightSemibold.value).toBe("number");
|
|
272
|
+
expect(typeof fontWeights.fontWeightBold.value).toBe("string");
|
|
273
|
+
expect(typeof fontWeights.fontWeightBlack.value).toBe("number");
|
|
274
|
+
expect(typeof fontWeights.fontWeightLighter.value).toBe("string");
|
|
275
|
+
expect(typeof fontWeights.fontWeightBolder.value).toBe("string");
|
|
276
|
+
expect(typeof fontWeights.fontWeight.value).toBe("object");
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
describe("default font weight", () => {
|
|
281
|
+
it("should create a default font weight variable referencing normal by default", () => {
|
|
282
|
+
const s = styleframe();
|
|
283
|
+
const { fontWeight } = useFontWeight(s);
|
|
284
|
+
|
|
285
|
+
expect(fontWeight).toEqual({
|
|
286
|
+
type: "variable",
|
|
287
|
+
name: "font-weight",
|
|
288
|
+
value: {
|
|
289
|
+
type: "reference",
|
|
290
|
+
name: "font-weight--normal",
|
|
291
|
+
fallback: undefined,
|
|
292
|
+
},
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it("should allow customizing the default font weight", () => {
|
|
297
|
+
const s = styleframe();
|
|
298
|
+
const { fontWeight } = useFontWeight(s, {
|
|
299
|
+
...defaultFontWeightValues,
|
|
300
|
+
default: "@bold",
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
expect(fontWeight.value).toEqual({
|
|
304
|
+
type: "reference",
|
|
305
|
+
name: "font-weight--bold",
|
|
306
|
+
fallback: undefined,
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it("should compile default font weight to CSS correctly", () => {
|
|
311
|
+
const s = styleframe();
|
|
312
|
+
useFontWeight(s, {
|
|
313
|
+
...defaultFontWeightValues,
|
|
314
|
+
default: "@semibold",
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
const css = consume(s.root, s.options);
|
|
318
|
+
|
|
319
|
+
expect(css).toEqual(`:root {
|
|
320
|
+
--font-weight--extralight: 200;
|
|
321
|
+
--font-weight--light: 300;
|
|
322
|
+
--font-weight--normal: normal;
|
|
323
|
+
--font-weight--medium: 500;
|
|
324
|
+
--font-weight--semibold: 600;
|
|
325
|
+
--font-weight--bold: bold;
|
|
326
|
+
--font-weight--black: 900;
|
|
327
|
+
--font-weight--lighter: lighter;
|
|
328
|
+
--font-weight--bolder: bolder;
|
|
329
|
+
--font-weight--inherit: inherit;
|
|
330
|
+
--font-weight: var(--font-weight--semibold);
|
|
331
|
+
}`);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("should work with different default font weights", () => {
|
|
335
|
+
const fontWeights = [
|
|
336
|
+
"extralight",
|
|
337
|
+
"light",
|
|
338
|
+
"normal",
|
|
339
|
+
"medium",
|
|
340
|
+
"semibold",
|
|
341
|
+
"bold",
|
|
342
|
+
"black",
|
|
343
|
+
"lighter",
|
|
344
|
+
"bolder",
|
|
345
|
+
];
|
|
346
|
+
|
|
347
|
+
for (const fontWeightName of fontWeights) {
|
|
348
|
+
const s = styleframe();
|
|
349
|
+
const { fontWeight } = useFontWeight(s, {
|
|
350
|
+
...defaultFontWeightValues,
|
|
351
|
+
default: `@${fontWeightName}`,
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
expect(fontWeight.value).toEqual({
|
|
355
|
+
type: "reference",
|
|
356
|
+
name: `font-weight--${fontWeightName}`,
|
|
357
|
+
fallback: undefined,
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
});
|
|
362
|
+
|
|
363
|
+
describe("font weight value relationships", () => {
|
|
364
|
+
it("should have numeric weights in ascending order", () => {
|
|
365
|
+
const s = styleframe();
|
|
366
|
+
const {
|
|
367
|
+
fontWeightExtralight,
|
|
368
|
+
fontWeightLight,
|
|
369
|
+
fontWeightMedium,
|
|
370
|
+
fontWeightSemibold,
|
|
371
|
+
fontWeightBlack,
|
|
372
|
+
} = useFontWeight(s);
|
|
373
|
+
|
|
374
|
+
expect(fontWeightExtralight.value).toBe(200);
|
|
375
|
+
expect(fontWeightLight.value).toBe(300);
|
|
376
|
+
expect(fontWeightMedium.value).toBe(500);
|
|
377
|
+
expect(fontWeightSemibold.value).toBe(600);
|
|
378
|
+
expect(fontWeightBlack.value).toBe(900);
|
|
379
|
+
|
|
380
|
+
expect(fontWeightExtralight.value).toBeLessThan(
|
|
381
|
+
fontWeightLight.value as number,
|
|
382
|
+
);
|
|
383
|
+
expect(fontWeightLight.value).toBeLessThan(
|
|
384
|
+
fontWeightMedium.value as number,
|
|
385
|
+
);
|
|
386
|
+
expect(fontWeightMedium.value).toBeLessThan(
|
|
387
|
+
fontWeightSemibold.value as number,
|
|
388
|
+
);
|
|
389
|
+
expect(fontWeightSemibold.value).toBeLessThan(
|
|
390
|
+
fontWeightBlack.value as number,
|
|
391
|
+
);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it("should have black as the heaviest numeric weight", () => {
|
|
395
|
+
const s = styleframe();
|
|
396
|
+
const fontWeights = useFontWeight(s);
|
|
397
|
+
const numericValues = Object.values(fontWeights)
|
|
398
|
+
.filter((v) => typeof v.value === "number")
|
|
399
|
+
.map((v) => v.value as number);
|
|
400
|
+
const maxValue = Math.max(...numericValues);
|
|
401
|
+
|
|
402
|
+
expect(fontWeights.fontWeightBlack.value).toBe(maxValue);
|
|
403
|
+
expect(fontWeights.fontWeightBlack.value).toBe(900);
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("should have extralight as the lightest numeric weight", () => {
|
|
407
|
+
const s = styleframe();
|
|
408
|
+
const fontWeights = useFontWeight(s);
|
|
409
|
+
const numericValues = Object.values(fontWeights)
|
|
410
|
+
.filter((v) => typeof v.value === "number")
|
|
411
|
+
.map((v) => v.value as number);
|
|
412
|
+
const minValue = Math.min(...numericValues);
|
|
413
|
+
|
|
414
|
+
expect(fontWeights.fontWeightExtralight.value).toBe(minValue);
|
|
415
|
+
expect(fontWeights.fontWeightExtralight.value).toBe(200);
|
|
416
|
+
});
|
|
417
|
+
|
|
418
|
+
it("should have CSS keyword values for normal, bold, lighter, and bolder", () => {
|
|
419
|
+
const s = styleframe();
|
|
420
|
+
const {
|
|
421
|
+
fontWeightNormal,
|
|
422
|
+
fontWeightBold,
|
|
423
|
+
fontWeightLighter,
|
|
424
|
+
fontWeightBolder,
|
|
425
|
+
} = useFontWeight(s);
|
|
426
|
+
|
|
427
|
+
expect(fontWeightNormal.value).toBe("normal");
|
|
428
|
+
expect(fontWeightBold.value).toBe("bold");
|
|
429
|
+
expect(fontWeightLighter.value).toBe("lighter");
|
|
430
|
+
expect(fontWeightBolder.value).toBe("bolder");
|
|
431
|
+
});
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
describe("practical usage", () => {
|
|
435
|
+
it("should work for creating typography with different font weights", () => {
|
|
436
|
+
const s = styleframe();
|
|
437
|
+
const { fontWeightNormal, fontWeightBold } = useFontWeight(s);
|
|
438
|
+
|
|
439
|
+
s.selector("p", ({ variable }) => {
|
|
440
|
+
variable("font-weight", s.ref(fontWeightNormal));
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
s.selector("strong, b", ({ variable }) => {
|
|
444
|
+
variable("font-weight", s.ref(fontWeightBold));
|
|
445
|
+
});
|
|
446
|
+
|
|
447
|
+
const css = consume(s.root, s.options);
|
|
448
|
+
expect(css).toEqual(`:root {
|
|
449
|
+
--font-weight--extralight: 200;
|
|
450
|
+
--font-weight--light: 300;
|
|
451
|
+
--font-weight--normal: normal;
|
|
452
|
+
--font-weight--medium: 500;
|
|
453
|
+
--font-weight--semibold: 600;
|
|
454
|
+
--font-weight--bold: bold;
|
|
455
|
+
--font-weight--black: 900;
|
|
456
|
+
--font-weight--lighter: lighter;
|
|
457
|
+
--font-weight--bolder: bolder;
|
|
458
|
+
--font-weight--inherit: inherit;
|
|
459
|
+
--font-weight: var(--font-weight--normal);
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
p {
|
|
463
|
+
--font-weight: var(--font-weight--normal);
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
strong, b {
|
|
467
|
+
--font-weight: var(--font-weight--bold);
|
|
468
|
+
}`);
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it("should work for heading hierarchy with different weights", () => {
|
|
472
|
+
const s = styleframe();
|
|
473
|
+
const { fontWeightBlack, fontWeightBold, fontWeightSemibold } =
|
|
474
|
+
useFontWeight(s);
|
|
475
|
+
|
|
476
|
+
s.selector("h1", ({ variable }) => {
|
|
477
|
+
variable("font-weight", s.ref(fontWeightBlack));
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
s.selector("h2", ({ variable }) => {
|
|
481
|
+
variable("font-weight", s.ref(fontWeightBold));
|
|
482
|
+
});
|
|
483
|
+
|
|
484
|
+
s.selector("h3, h4, h5, h6", ({ variable }) => {
|
|
485
|
+
variable("font-weight", s.ref(fontWeightSemibold));
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
const css = consume(s.root, s.options);
|
|
489
|
+
expect(css).toEqual(`:root {
|
|
490
|
+
--font-weight--extralight: 200;
|
|
491
|
+
--font-weight--light: 300;
|
|
492
|
+
--font-weight--normal: normal;
|
|
493
|
+
--font-weight--medium: 500;
|
|
494
|
+
--font-weight--semibold: 600;
|
|
495
|
+
--font-weight--bold: bold;
|
|
496
|
+
--font-weight--black: 900;
|
|
497
|
+
--font-weight--lighter: lighter;
|
|
498
|
+
--font-weight--bolder: bolder;
|
|
499
|
+
--font-weight--inherit: inherit;
|
|
500
|
+
--font-weight: var(--font-weight--normal);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
h1 {
|
|
504
|
+
--font-weight: var(--font-weight--black);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
h2 {
|
|
508
|
+
--font-weight: var(--font-weight--bold);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
h3, h4, h5, h6 {
|
|
512
|
+
--font-weight: var(--font-weight--semibold);
|
|
513
|
+
}`);
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
it("should work for theme-specific font weight overrides", () => {
|
|
517
|
+
const s = styleframe();
|
|
518
|
+
const { fontWeightNormal, fontWeightMedium } = useFontWeight(s);
|
|
519
|
+
|
|
520
|
+
s.selector(".theme-strong", ({ variable }) => {
|
|
521
|
+
variable(fontWeightNormal, s.ref(fontWeightMedium));
|
|
522
|
+
});
|
|
523
|
+
|
|
524
|
+
const css = consume(s.root, s.options);
|
|
525
|
+
expect(css).toEqual(`:root {
|
|
526
|
+
--font-weight--extralight: 200;
|
|
527
|
+
--font-weight--light: 300;
|
|
528
|
+
--font-weight--normal: normal;
|
|
529
|
+
--font-weight--medium: 500;
|
|
530
|
+
--font-weight--semibold: 600;
|
|
531
|
+
--font-weight--bold: bold;
|
|
532
|
+
--font-weight--black: 900;
|
|
533
|
+
--font-weight--lighter: lighter;
|
|
534
|
+
--font-weight--bolder: bolder;
|
|
535
|
+
--font-weight--inherit: inherit;
|
|
536
|
+
--font-weight: var(--font-weight--normal);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
.theme-strong {
|
|
540
|
+
--font-weight--normal: var(--font-weight--medium);
|
|
541
|
+
}`);
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it("should work with lighter and bolder relative weights", () => {
|
|
545
|
+
const s = styleframe();
|
|
546
|
+
const { fontWeightLighter, fontWeightBolder } = useFontWeight(s);
|
|
547
|
+
|
|
548
|
+
s.selector(".text-lighter", ({ variable }) => {
|
|
549
|
+
variable("font-weight", s.ref(fontWeightLighter));
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
s.selector(".text-bolder", ({ variable }) => {
|
|
553
|
+
variable("font-weight", s.ref(fontWeightBolder));
|
|
554
|
+
});
|
|
555
|
+
|
|
556
|
+
const css = consume(s.root, s.options);
|
|
557
|
+
expect(css).toEqual(`:root {
|
|
558
|
+
--font-weight--extralight: 200;
|
|
559
|
+
--font-weight--light: 300;
|
|
560
|
+
--font-weight--normal: normal;
|
|
561
|
+
--font-weight--medium: 500;
|
|
562
|
+
--font-weight--semibold: 600;
|
|
563
|
+
--font-weight--bold: bold;
|
|
564
|
+
--font-weight--black: 900;
|
|
565
|
+
--font-weight--lighter: lighter;
|
|
566
|
+
--font-weight--bolder: bolder;
|
|
567
|
+
--font-weight--inherit: inherit;
|
|
568
|
+
--font-weight: var(--font-weight--normal);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
.text-lighter {
|
|
572
|
+
--font-weight: var(--font-weight--lighter);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
.text-bolder {
|
|
576
|
+
--font-weight: var(--font-weight--bolder);
|
|
577
|
+
}`);
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
it("should work for responsive font weight adjustments", () => {
|
|
581
|
+
const s = styleframe();
|
|
582
|
+
const { fontWeightNormal, fontWeightMedium } = useFontWeight(s);
|
|
583
|
+
|
|
584
|
+
const bodyFontWeight = s.variable(
|
|
585
|
+
"body-font-weight",
|
|
586
|
+
s.ref(fontWeightNormal),
|
|
587
|
+
);
|
|
588
|
+
|
|
589
|
+
s.selector("@media (min-width: 768px)", ({ variable }) => {
|
|
590
|
+
variable(bodyFontWeight, s.ref(fontWeightMedium));
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
const css = consume(s.root, s.options);
|
|
594
|
+
expect(css).toEqual(`:root {
|
|
595
|
+
--font-weight--extralight: 200;
|
|
596
|
+
--font-weight--light: 300;
|
|
597
|
+
--font-weight--normal: normal;
|
|
598
|
+
--font-weight--medium: 500;
|
|
599
|
+
--font-weight--semibold: 600;
|
|
600
|
+
--font-weight--bold: bold;
|
|
601
|
+
--font-weight--black: 900;
|
|
602
|
+
--font-weight--lighter: lighter;
|
|
603
|
+
--font-weight--bolder: bolder;
|
|
604
|
+
--font-weight--inherit: inherit;
|
|
605
|
+
--font-weight: var(--font-weight--normal);
|
|
606
|
+
--body-font-weight: var(--font-weight--normal);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
@media (min-width: 768px) {
|
|
610
|
+
--body-font-weight: var(--font-weight--medium);
|
|
611
|
+
}`);
|
|
612
|
+
});
|
|
613
|
+
|
|
614
|
+
it("should work for light text on dark backgrounds", () => {
|
|
615
|
+
const s = styleframe();
|
|
616
|
+
const { fontWeightLight, fontWeightExtralight } = useFontWeight(s);
|
|
617
|
+
|
|
618
|
+
s.selector(".dark-theme", ({ variable }) => {
|
|
619
|
+
variable("font-weight", s.ref(fontWeightLight));
|
|
620
|
+
});
|
|
621
|
+
|
|
622
|
+
s.selector(".dark-theme .text-subtle", ({ variable }) => {
|
|
623
|
+
variable("font-weight", s.ref(fontWeightExtralight));
|
|
624
|
+
});
|
|
625
|
+
|
|
626
|
+
const css = consume(s.root, s.options);
|
|
627
|
+
expect(css).toEqual(`:root {
|
|
628
|
+
--font-weight--extralight: 200;
|
|
629
|
+
--font-weight--light: 300;
|
|
630
|
+
--font-weight--normal: normal;
|
|
631
|
+
--font-weight--medium: 500;
|
|
632
|
+
--font-weight--semibold: 600;
|
|
633
|
+
--font-weight--bold: bold;
|
|
634
|
+
--font-weight--black: 900;
|
|
635
|
+
--font-weight--lighter: lighter;
|
|
636
|
+
--font-weight--bolder: bolder;
|
|
637
|
+
--font-weight--inherit: inherit;
|
|
638
|
+
--font-weight: var(--font-weight--normal);
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
.dark-theme {
|
|
642
|
+
--font-weight: var(--font-weight--light);
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
.dark-theme .text-subtle {
|
|
646
|
+
--font-weight: var(--font-weight--extralight);
|
|
647
|
+
}`);
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createUseVariable } from "../utils";
|
|
2
|
+
|
|
3
|
+
export const defaultFontWeightValues = {
|
|
4
|
+
default: "@normal",
|
|
5
|
+
extralight: 200,
|
|
6
|
+
light: 300,
|
|
7
|
+
normal: "normal",
|
|
8
|
+
medium: 500,
|
|
9
|
+
semibold: 600,
|
|
10
|
+
bold: "bold",
|
|
11
|
+
black: 900,
|
|
12
|
+
lighter: "lighter",
|
|
13
|
+
bolder: "bolder",
|
|
14
|
+
inherit: "inherit",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Create a set of font-weight variables for use in a Styleframe instance.
|
|
19
|
+
*
|
|
20
|
+
* @usage
|
|
21
|
+
* ```typescript
|
|
22
|
+
* import { styleframe } from "styleframe";
|
|
23
|
+
* import { useFontWeight } from "styleframe/theme";
|
|
24
|
+
*
|
|
25
|
+
* const s = styleframe();
|
|
26
|
+
*
|
|
27
|
+
* const {
|
|
28
|
+
* fontWeightExtralight,
|
|
29
|
+
* fontWeightLight,
|
|
30
|
+
* fontWeightNormal,
|
|
31
|
+
* fontWeightMedium,
|
|
32
|
+
* fontWeightSemibold,
|
|
33
|
+
* fontWeightBold,
|
|
34
|
+
* fontWeightBlack,
|
|
35
|
+
* fontWeightLighter,
|
|
36
|
+
* fontWeightBolder,
|
|
37
|
+
* fontWeight,
|
|
38
|
+
* } = useFontWeight(s, {
|
|
39
|
+
* default: "normal",
|
|
40
|
+
* extralight: 200,
|
|
41
|
+
* light: 300,
|
|
42
|
+
* normal: "normal",
|
|
43
|
+
* medium: 500,
|
|
44
|
+
* semibold: 600,
|
|
45
|
+
* bold: 700,
|
|
46
|
+
* black: 900,
|
|
47
|
+
* lighter: "lighter",
|
|
48
|
+
* bolder: "bolder",
|
|
49
|
+
* inherit: "inherit",
|
|
50
|
+
* });
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
export const useFontWeight = createUseVariable("font-weight", {
|
|
54
|
+
defaults: defaultFontWeightValues,
|
|
55
|
+
});
|