Skip to content

Commit 001be83

Browse files
Allow fontFace to accept array of font face rules (#1114)
1 parent 8a35dff commit 001be83

File tree

4 files changed

+102
-13
lines changed

4 files changed

+102
-13
lines changed

.changeset/fontFace-array.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
'@vanilla-extract/css': minor
3+
---
4+
5+
Supports passing multiple font face rules to `fontFace`
6+
7+
**Example usage**
8+
9+
```ts
10+
import { fontFace, style } from '@vanilla-extract/css';
11+
12+
const gentium = fontFace([
13+
{
14+
src: 'local("Gentium")',
15+
fontWeight: 'normal',
16+
},
17+
{
18+
src: 'local("Gentium Bold")',
19+
fontWeight: 'bold',
20+
},
21+
]);
22+
23+
export const font = style({
24+
fontFamily: gentium,
25+
});
26+
```

packages/css/src/style.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,26 +93,31 @@ export function globalStyle(selector: string, rule: GlobalStyleRule) {
9393
appendCss({ type: 'global', selector, rule }, getFileScope());
9494
}
9595

96-
export function fontFace(rule: FontFaceRule, debugId?: string) {
96+
export function fontFace(
97+
rule: FontFaceRule | FontFaceRule[],
98+
debugId?: string,
99+
) {
97100
const fontFamily = `"${cssesc(generateIdentifier(debugId), {
98101
quotes: 'double',
99102
})}"`;
100103

101-
if ('fontFamily' in rule) {
102-
throw new Error(
103-
outdent`
104-
This function creates and returns a hashed font-family name, so the "fontFamily" property should not be provided.
105-
106-
If you'd like to define a globally scoped custom font, you can use the "globalFontFace" function instead.
107-
`,
104+
const rules = Array.isArray(rule) ? rule : [rule];
105+
106+
for (const singleRule of rules) {
107+
if ('fontFamily' in singleRule) {
108+
throw new Error(outdent`
109+
This function creates and returns a hashed font-family name, so the "fontFamily" property should not be provided.
110+
111+
If you'd like to define a globally scoped custom font, you can use the "globalFontFace" function instead.
112+
`);
113+
}
114+
115+
appendCss(
116+
{ type: 'fontFace', rule: { ...singleRule, fontFamily } },
117+
getFileScope(),
108118
);
109119
}
110120

111-
appendCss(
112-
{ type: 'fontFace', rule: { ...rule, fontFamily } },
113-
getFileScope(),
114-
);
115-
116121
return fontFamily;
117122
}
118123

packages/css/src/transformCss.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,6 +1825,40 @@ describe('transformCss', () => {
18251825
`);
18261826
});
18271827

1828+
it('should handle multiple font faces of the same family', () => {
1829+
expect(
1830+
transformCss({
1831+
composedClassLists: [],
1832+
localClassNames: [],
1833+
cssObjs: [
1834+
{
1835+
type: 'fontFace',
1836+
rule: {
1837+
fontFamily: 'MyFont',
1838+
src: 'local("Helvetica")',
1839+
},
1840+
},
1841+
{
1842+
type: 'fontFace',
1843+
rule: {
1844+
fontFamily: 'MyFont',
1845+
src: 'local("Helvetica-Bold")',
1846+
},
1847+
},
1848+
],
1849+
}).join('\n'),
1850+
).toMatchInlineSnapshot(`
1851+
@font-face {
1852+
font-family: MyFont;
1853+
src: local("Helvetica");
1854+
}
1855+
@font-face {
1856+
font-family: MyFont;
1857+
src: local("Helvetica-Bold");
1858+
}
1859+
`);
1860+
});
1861+
18281862
it('should handle multiple font faces', () => {
18291863
expect(
18301864
transformCss({

site/docs/api/font-face.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ export const font = style({
2020
fontFamily: comicSans
2121
});
2222
```
23+
24+
## Multiple Fonts with Single Family
25+
26+
The `fontFace` function allows you to pass an array of font-face rules that may contain different rules but treat them as if they are from one font family.
27+
28+
```ts compiled
29+
// text.css.ts
30+
import { fontFace, style } from '@vanilla-extract/css';
31+
32+
const gentium = fontFace([
33+
{
34+
src: 'local("Gentium")',
35+
fontWeight: 'normal'
36+
},
37+
{
38+
src: 'local("Gentium Bold")',
39+
fontWeight: 'bold'
40+
}
41+
]);
42+
43+
export const font = style({
44+
fontFamily: gentium
45+
});
46+
```

0 commit comments

Comments
 (0)