|
1 | | -import { theme } from '~/theme' |
2 | | - |
3 | 1 | /** |
4 | | - * Helper function to convert theme breakpoint into viewport width in px for |
5 | | - * Chromatic viewpoint snapshots. |
6 | | - * @param breakpoint the theme breakpoint to convert |
7 | | - * @returns the number pixel width of the given breakpoint. |
| 2 | + * This file holds the viewports for Storybook rendering, and also for |
| 3 | + * Choromatic to take snapshots based on the given mode. |
| 4 | + * @see https://www.chromatic.com/docs/modes/ and the `modes.ts` file. |
| 5 | + * |
| 6 | + * Width breakpoints are set as OGPDS defaults (as of writing this ) of |
| 7 | + * |
| 8 | + * sm: '30em', |
| 9 | + * md: '48em', |
| 10 | + * lg: '64em', |
| 11 | + * xl: '90em', |
| 12 | + * |
| 13 | + * @see https://tailwindcss.com/docs/screens |
| 14 | + * |
| 15 | + * If you override default tailwindcss breakpoints, you should update this file |
| 16 | + * so snapshot widths are consistent with the breakpoints in your app. |
8 | 17 | */ |
9 | | -const breakpointToViewportWidth = ( |
10 | | - breakpoint: keyof typeof theme.breakpoints, |
11 | | -) => { |
12 | | - const rem = 16 |
13 | | - return parseInt(theme.breakpoints[breakpoint]) * rem |
| 18 | + |
| 19 | +/** Type that conform to what Storybook expects for the `viewport` parameter */ |
| 20 | +interface StorybookViewportParameter { |
| 21 | + viewports: Record< |
| 22 | + string, |
| 23 | + { |
| 24 | + name: string |
| 25 | + } & ( |
| 26 | + | { |
| 27 | + styles: { |
| 28 | + width: string |
| 29 | + height?: string |
| 30 | + } |
| 31 | + } |
| 32 | + | number |
| 33 | + ) |
| 34 | + > |
14 | 35 | } |
15 | 36 |
|
16 | 37 | /** |
17 | | - * Viewports mapping viewport key to their width in (pixel) number. |
18 | | - * Used for Chromatic viewpoint snapshots which requires the numbers in pixels. |
| 38 | + * The names of the viewports should correspond to the `viewport` parameter you |
| 39 | + * set in `modes.ts`. |
| 40 | + * |
| 41 | + * An additional `xs` viewport is added specifically for Chromatic to have a |
| 42 | + * snapshot for the smallest screen size. |
19 | 43 | */ |
20 | | -export const viewports = { |
21 | | - xs: 320, // '20rem' |
22 | | - sm: breakpointToViewportWidth('sm'), |
23 | | - md: breakpointToViewportWidth('md'), |
24 | | - lg: breakpointToViewportWidth('lg'), |
25 | | - xl: breakpointToViewportWidth('xl'), |
26 | | -} |
27 | | - |
28 | | -export const getMobileViewParameters = () => { |
29 | | - return { |
30 | | - viewport: { |
31 | | - defaultViewport: 'mobile1', |
32 | | - }, |
33 | | - chromatic: { viewports: [viewports.xs] }, |
34 | | - } |
35 | | -} |
36 | | - |
37 | | -export const getTabletViewParameters = () => { |
38 | | - return { |
39 | | - viewport: { |
40 | | - defaultViewport: 'tablet', |
41 | | - }, |
42 | | - chromatic: { viewports: [viewports.md] }, |
43 | | - } |
44 | | -} |
| 44 | +export const viewport = { |
| 45 | + viewports: { |
| 46 | + xs: { name: 'xs', styles: { width: '320px', height: '100%' } }, |
| 47 | + sm: { name: 'sm', styles: { width: '480px', height: '100%' } }, |
| 48 | + md: { name: 'md', styles: { width: '768px', height: '100%' } }, |
| 49 | + lg: { name: 'lg', styles: { width: '1280px', height: '100%' } }, |
| 50 | + xl: { name: 'xl', styles: { width: '1440px', height: '100%' } }, |
| 51 | + '2xl': { name: '2xl', styles: { width: '1536px', height: '100%' } }, |
| 52 | + }, |
| 53 | +} as const satisfies StorybookViewportParameter |
0 commit comments