|
| 1 | +import { Meta, Story } from '@storybook/react'; |
| 2 | +import getCssVariables from '../../06-utility/storybook/getCssVariables'; |
| 3 | + |
| 4 | +const settings: Meta = { |
| 5 | + title: 'Global/Spacing', |
| 6 | +}; |
| 7 | + |
| 8 | +const allVars = getCssVariables(); |
| 9 | + |
| 10 | +const baseFontVar = allVars.find( |
| 11 | + ([key]) => key.indexOf('--base-font-size') === 0, |
| 12 | +); |
| 13 | +const baseFontSize = baseFontVar ? parseInt(baseFontVar[1]) : 16; |
| 14 | + |
| 15 | +interface SpacingOptions { |
| 16 | + [space: string]: string; |
| 17 | +} |
| 18 | + |
| 19 | +const spacing = allVars.reduce((allSpacing, [key, value]) => { |
| 20 | + if (key.indexOf('--spacing') === 0) { |
| 21 | + let space = key.substring(10); |
| 22 | + if (space.includes('-')) { |
| 23 | + space = space.replace('-', '.'); |
| 24 | + } |
| 25 | + allSpacing[space] = value; |
| 26 | + } |
| 27 | + return allSpacing; |
| 28 | +}, {} as SpacingOptions); |
| 29 | + |
| 30 | +const Spacing: Story = args => { |
| 31 | + return ( |
| 32 | + <table> |
| 33 | + <thead> |
| 34 | + <tr> |
| 35 | + <th>Token</th> |
| 36 | + <th>REM</th> |
| 37 | + <th>PX</th> |
| 38 | + <th>Example</th> |
| 39 | + </tr> |
| 40 | + </thead> |
| 41 | + <tbody> |
| 42 | + {Object.entries(args.spacing as SpacingOptions) |
| 43 | + .sort(([keyA], [keyB]) => Number(keyA) - Number(keyB)) |
| 44 | + .map(([name, unit]) => ( |
| 45 | + <tr key={`spacing-${name}`}> |
| 46 | + <td>{name}</td> |
| 47 | + <td> |
| 48 | + {parseInt(unit) / baseFontSize} |
| 49 | + rem |
| 50 | + </td> |
| 51 | + <td>{unit}</td> |
| 52 | + <td> |
| 53 | + <div |
| 54 | + style={{ |
| 55 | + height: unit, |
| 56 | + width: unit, |
| 57 | + backgroundColor: '#ccc', |
| 58 | + }} |
| 59 | + ></div> |
| 60 | + </td> |
| 61 | + </tr> |
| 62 | + ))} |
| 63 | + </tbody> |
| 64 | + </table> |
| 65 | + ); |
| 66 | +}; |
| 67 | +Spacing.args = { |
| 68 | + spacing: spacing, |
| 69 | +}; |
| 70 | + |
| 71 | +export default settings; |
| 72 | +export { Spacing }; |
0 commit comments