Skip to content

Commit df9fe3e

Browse files
ronciaskoufis
andauthored
Allow globalFontFace to accept array of font face rules (#1379)
Co-authored-by: Adam Skoufis <[email protected]>
1 parent fae3c9c commit df9fe3e

File tree

3 files changed

+63
-5
lines changed

3 files changed

+63
-5
lines changed

.changeset/eighty-shirts-lie.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
'@vanilla-extract/css': minor
3+
---
4+
5+
Add support for passing multiple font face rules to `globalFontFace`
6+
7+
**EXAMPLE USAGE:**
8+
9+
```ts
10+
const gentium = 'GlobalGentium';
11+
12+
globalFontFace(gentium, [
13+
{
14+
src: 'local("Gentium")',
15+
fontWeight: 'normal'
16+
},
17+
{
18+
src: 'local("Gentium Bold")',
19+
fontWeight: 'bold'
20+
}
21+
]);

packages/css/src/style.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,18 @@ export function fontFace(
121121
return fontFamily;
122122
}
123123

124-
export function globalFontFace(fontFamily: string, rule: FontFaceRule) {
125-
appendCss(
126-
{ type: 'fontFace', rule: { ...rule, fontFamily } },
127-
getFileScope(),
128-
);
124+
export function globalFontFace(
125+
fontFamily: string,
126+
rule: FontFaceRule | FontFaceRule[],
127+
) {
128+
const rules = Array.isArray(rule) ? rule : [rule];
129+
130+
for (const singleRule of rules) {
131+
appendCss(
132+
{ type: 'fontFace', rule: { ...singleRule, fontFamily } },
133+
getFileScope(),
134+
);
135+
}
129136
}
130137

131138
export function keyframes(rule: CSSKeyframes, debugId?: string) {

site/docs/global-api/global-font-face.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,33 @@ export const font = style({
2525
fontFamily: comicSans
2626
});
2727
```
28+
29+
## Multiple Global Fonts with Single Family
30+
31+
The `globalFontFace` 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.
32+
33+
```ts compiled
34+
// text.css.ts
35+
36+
import {
37+
globalFontFace,
38+
style
39+
} from '@vanilla-extract/css';
40+
41+
const gentium = 'GlobalGentium';
42+
43+
globalFontFace(gentium, [
44+
{
45+
src: 'local("Gentium")',
46+
fontWeight: 'normal'
47+
},
48+
{
49+
src: 'local("Gentium Bold")',
50+
fontWeight: 'bold'
51+
}
52+
]);
53+
54+
export const font = style({
55+
fontFamily: gentium
56+
});
57+
```

0 commit comments

Comments
 (0)