Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow globalFontFace to accept array of font face rules #1379

Merged
merged 5 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eighty-shirts-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': minor
---

Add support for passing multiple font face rules to `globalFontFace`
askoufis marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 12 additions & 5 deletions packages/css/src/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,18 @@ export function fontFace(
return fontFamily;
}

export function globalFontFace(fontFamily: string, rule: FontFaceRule) {
appendCss(
{ type: 'fontFace', rule: { ...rule, fontFamily } },
getFileScope(),
);
export function globalFontFace(
fontFamily: string,
rule: FontFaceRule | FontFaceRule[],
) {
const rules = Array.isArray(rule) ? rule : [rule];

for (const singleRule of rules) {
appendCss(
{ type: 'fontFace', rule: { ...singleRule, fontFamily } },
getFileScope(),
);
}
}

export function keyframes(rule: CSSKeyframes, debugId?: string) {
Expand Down
30 changes: 30 additions & 0 deletions site/docs/global-api/global-font-face.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,33 @@ export const font = style({
fontFamily: comicSans
});
```

## Multiple Global Fonts with Single Family

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.

```ts compiled
// text.css.ts

import {
globalFontFace,
style
} from '@vanilla-extract/css';

const gentium = 'GlobalGentium';

globalFontFace(gentium, [
{
src: 'local("Gentium")',
fontWeight: 'normal'
},
{
src: 'local("Gentium Bold")',
fontWeight: 'bold'
}
]);

export const font = style({
fontFamily: gentium
});
```