Skip to content

Commit 11b21f5

Browse files
committed
fix: support top-level weight/style in localFont with string src
- Add top-level weight and style to @font-face when src is a string - Exclude variable font weight ranges from className (only static weights) - Align with Next.js localFont behavior
1 parent 54cbcd2 commit 11b21f5

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/plugins/next-font/local/get-font-face-declarations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export async function getFontFaceDeclarations(options: LoaderOptions) {
7373
return dedent`@font-face {
7474
font-family: ${id};
7575
src: url(${localFontSrc.fontReferenceId ? getPlaceholderFontUrl(localFontSrc.fontReferenceId) : `/@fs/${localFontSrc.fontPath}`});
76+
${weight ? `font-weight: ${weight};` : ""}
77+
${style ? `font-style: ${style};` : ""}
7678
${fontDeclarations}
7779
}`;
7880
}

src/plugins/next-font/utils/get-css-meta.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export function getCSSMeta(options: Options) {
1515
.${className} {
1616
font-family: ${options.fontFamily};
1717
${isNextCSSPropertyValid(options.styles) ? `font-style: ${options.styles[0]};` : ""}
18-
${isNextCSSPropertyValid(options.weights) ? `font-weight: ${options.weights[0]};` : ""}
18+
${
19+
isNextCSSPropertyValid(options.weights) &&
20+
!options.weights[0]?.includes(" ")
21+
? `font-weight: ${options.weights[0]};`
22+
: ""
23+
}
1924
}
2025
2126
${
@@ -39,7 +44,9 @@ export function getCSSMeta(options: Options) {
3944
function getClassName({ styles, weights, fontFamily }: Options) {
4045
const font = fontFamily.replaceAll(" ", "-").toLowerCase();
4146
const style = isNextCSSPropertyValid(styles) ? styles[0] : null;
42-
const weight = isNextCSSPropertyValid(weights) ? weights[0] : null;
47+
const weight = isNextCSSPropertyValid(weights)
48+
? weights[0]?.replaceAll(" ", "-")
49+
: null;
4350

4451
return `${font}${style ? `-${style}` : ""}${weight ? `-${weight}` : ""}`;
4552
}

0 commit comments

Comments
 (0)