Skip to content

Commit

Permalink
Merge pull request #24983 from MauricioRobayo/next-font-local-declara…
Browse files Browse the repository at this point in the history
…tions-support

Next.js: Add next/font/local declarations support

(cherry picked from commit 0327b57)
  • Loading branch information
valentinpalkovic authored and storybook-bot committed Dec 6, 2023
1 parent b8defb6 commit 827062a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
1 change: 0 additions & 1 deletion code/frameworks/nextjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,6 @@ The following features are not supported (yet). Support for these features might
- [Support font loaders configuration in next.config.js](https://nextjs.org/docs/basic-features/font-optimization#specifying-a-subset)
- [fallback](https://nextjs.org/docs/api-reference/next/font#fallback) option
- [adjustFontFallback](https://nextjs.org/docs/api-reference/next/font#adjustfontfallback) option
- [declarations](https://nextjs.org/docs/api-reference/next/font#declarations) option
- [preload](https://nextjs.org/docs/api-reference/next/font#preload) option gets ignored. Storybook handles Font loading its own way.
- [display](https://nextjs.org/docs/api-reference/next/font#display) option gets ignored. All fonts are loaded with display set to "block" to make Storybook load the font properly.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function getFontFaceDeclarations(
: path.dirname(options.filename).replace(rootContext, '');

const { validateData } = require('../utils/local-font-utils');
const { weight, style, variable } = validateData('', options.props);
const { weight, style, variable, declarations = [] } = validateData('', options.props);

const id = `font-${loaderUtils.getHashDigest(
Buffer.from(JSON.stringify(localFontSrc)),
Expand All @@ -30,6 +30,10 @@ export async function getFontFaceDeclarations(
6
)}`;

const fontDeclarations = declarations
.map(({ prop, value }: { prop: string; value: string }) => `${prop}: ${value};`)
.join('\n');

const arePathsWin32Format = /^[a-z]:\\/iu.test(options.filename);
const cleanWin32Path = (pathString: string): string =>
arePathsWin32Format ? pathString.replace(/\\/gu, '/') : pathString;
Expand All @@ -41,6 +45,7 @@ export async function getFontFaceDeclarations(
return `@font-face {
font-family: ${id};
src: url(.${localFontPath});
${fontDeclarations}
}`;
}
return localFontSrc
Expand All @@ -52,6 +57,7 @@ export async function getFontFaceDeclarations(
src: url(.${localFontPath});
${font.weight ? `font-weight: ${font.weight};` : ''}
${font.style ? `font-style: ${font.style};` : ''}
${fontDeclarations}
}`;
})
.join('');
Expand Down

0 comments on commit 827062a

Please sign in to comment.