Skip to content

Commit

Permalink
test: wait for fonts to load before rendering story in Chromatic (#2851)
Browse files Browse the repository at this point in the history
  • Loading branch information
unekinn authored Nov 29, 2024
1 parent 90818f6 commit 373c63f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/storybook/.storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import './style.css';
import '../../../packages/css/src/index.css';
import '@digdir/designsystemet-theme/digdir.css';
import type { Preview } from '@storybook/react';
import isChromatic from 'chromatic/isChromatic';

import type { LinkProps } from '@digdir/designsystemet-react';
import { Link, List, Paragraph, Table } from '@digdir/designsystemet-react';

import { customStylesDecorator } from '../story-utils/customStylesDecorator';
import { fontsLoader } from '../story-utils/fontsLoader';
import { allModes, viewportWidths } from '../story-utils/modes';
import customTheme from './customTheme';

Expand Down Expand Up @@ -166,6 +168,7 @@ const preview: Preview = {
},
},
decorators: [customStylesDecorator],
loaders: isChromatic() && document.fonts ? [fontsLoader] : [],
};

/* Add this back when https://github.com/storybookjs/storybook/issues/29189 is fixed */
Expand Down
6 changes: 5 additions & 1 deletion apps/storybook/chromatic.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@
"storybookBaseDir": "apps/storybook",
"storybookBuildDir": "dist",
"zip": true,
"externals": ["packages/css/**/*.css", "packages/theme/**/*.css"]
"externals": [
"packages/css/**/*.css",
"packages/theme/**/*.css",
"apps/storybook/{.storybook,story-utils}/**/*"
]
}
5 changes: 5 additions & 0 deletions apps/storybook/story-utils/fontsLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Loader } from '@storybook/react';

export const fontsLoader: Loader = async () => ({
fonts: await Promise.all([document.fonts.load('400 1em Inter')]),
});

0 comments on commit 373c63f

Please sign in to comment.