diff --git a/.storybook/main.js b/.storybook/main.js index 82d374bc..1b25d3b6 100644 --- a/.storybook/main.js +++ b/.storybook/main.js @@ -49,6 +49,16 @@ module.exports = { parse: YAML.parse, }, }); + config.module.rules.find( + rule => rule.test && rule.test.toString().includes('css'), + ).resourceQuery = { + not: /raw/, + }; + config.module.rules.push({ + test: /\.css$/, + resourceQuery: /raw/, + type: 'asset/source', + }); return config; }, }; diff --git a/source/01-global/01-typography/typographic-scale.stories.tsx b/source/01-global/01-typography/typographic-scale.stories.tsx new file mode 100644 index 00000000..be093c90 --- /dev/null +++ b/source/01-global/01-typography/typographic-scale.stories.tsx @@ -0,0 +1,127 @@ +import { Meta, StoryObj } from '@storybook/react'; +import { Property } from 'csstype'; +import { useEffect, useState } from 'react'; +import typographyVarsAsString from '../../00-config/vars/typography.css?raw'; +import getCssVariables from '../../06-utility/storybook/getCssVariables'; +import styles from './typographic-scale.module.css'; + +const typographyVars = typographyVarsAsString + .replace(':root {', '') + .replace('}', '') + .split(';') + .map((e: string) => { + return e.trim(); + }) + .filter((e: string) => e.includes('--responsive-font-size')) + .map((e: string) => { + return e.replace(')', '').split('(').pop(); + }); + +const typographyVarsMax = typographyVars.map((e: string) => { + return e.split(' ').pop(); +}); +const typographyVarsMin = typographyVars.map((e: string) => { + return e.split(' ', 1).pop(); +}); + +const settings: Meta = { + title: 'Global/Typography/Typographic Scale', +}; + +interface FontOptions { + [name: string]: Property.FontFamily; +} + +interface ResponsiveFontSizeOptions { + [number: number]: string; +} + +const TypographicScale: StoryObj = { + render: function Render() { + const [fonts, setFonts] = useState(undefined); + const [responsiveFontSizes, setResponsiveFontSizes] = useState< + ResponsiveFontSizeOptions | undefined + >(undefined); + + useEffect(() => { + const allVars = getCssVariables(); + + const fonts = allVars.reduce((allFonts, [key, value]) => { + if (key.indexOf('--font-family') === 0) { + const name = + key.substring(14).charAt(0).toUpperCase() + + key.substring(14).slice(1); + allFonts[name] = value; + } + return allFonts; + }, {} as FontOptions); + setFonts(fonts); + + const fontSizes = allVars.reduce( + (allResponsiveFontSizes, [key, value]) => { + if (key.indexOf('--responsive-font-size') === 0) { + const number = parseInt(key.substring(23)); + allResponsiveFontSizes[number] = value; + } + return allResponsiveFontSizes; + }, + {} as ResponsiveFontSizeOptions, + ); + setResponsiveFontSizes(fontSizes); + }, []); + return ( + <> + {fonts && + Object.entries(fonts) + .sort((fontA, fontB) => { + // Sort fonts so that Primary and Secondary (if used) appear at the + // top of the list. + if (fontA[0].toLowerCase().includes('primary')) { + return -1; + } + if (fontB[0].toLowerCase().includes('primary')) { + return 1; + } + if (fontA[0].toLowerCase().includes('secondary')) { + return -1; + } + if (fontB[0].toLowerCase().includes('secondary')) { + return 1; + } + return 0; + }) + .map(([name, fontFamily]) => ( +
+

{name}

+
+ {responsiveFontSizes && + Object.entries(responsiveFontSizes).map( + ([number, responsiveFontSize]) => ( + <> +
+
{number}
+
+ This text goes from{' '} + {typographyVarsMin[parseInt(number) - 1]} to{' '} + {typographyVarsMax[parseInt(number) - 1]}. +
+
+ + ), + )} +
+
+ ))} + + ); + }, + args: {}, +}; + +export default settings; +export { TypographicScale }; diff --git a/source/ambient.d.ts b/source/ambient.d.ts index 033f3a77..9b918cb2 100644 --- a/source/ambient.d.ts +++ b/source/ambient.d.ts @@ -1 +1,3 @@ declare module '*.yml'; + +declare module '*.css?raw';