import * as React from 'react'; import Color from 'color'; import * as variables from './variables'; import mdNotes from './colors.md'; type TSwatch = { scssVariableName: string; colorHex: string; colorKey: string; contrastRatio: number }; const bdlColors: { [k: string]: Array } = {}; const WCAG_AA = 4.5; // minimum contrast ratio for text const isPaletteColor = (hex: string, key: string) => { return key.startsWith('bdl') && !key.includes('Neutral') && key !== 'bdlSecondaryBlue' && hex.startsWith('#'); }; Object.keys(variables).forEach(colorKey => { const colorHex = (variables as { [k: string]: string | Array })[colorKey]; if (Array.isArray(colorHex)) return; if (isPaletteColor(colorHex, colorKey)) { const paletteGroup = (colorKey.match(/[A-Z][a-z]+/g) as Array).join(' '); if (!bdlColors[paletteGroup]) { bdlColors[paletteGroup] = []; } const color = Color(colorHex); const scssVariableName = (colorKey.match(/(bdl)|([A-Z][a-z]+)|(\d+)/g) as Array).join('-'); const contrastRatio = color.contrast(Color('#fff')); bdlColors[paletteGroup].push({ scssVariableName, colorHex, colorKey, contrastRatio }); } }); const wrapper: React.CSSProperties = { display: 'flex', flexDirection: 'row', flexWrap: 'wrap', }; const palette: React.CSSProperties = {}; const swatchContainer: React.CSSProperties = { display: 'flex', alignItems: 'center', }; const swatch: React.CSSProperties = { borderRadius: '4px', height: '40px', width: '200px', display: 'inline-block', }; const label: React.CSSProperties = { margin: '0 8px 0 16px', }; const LabelCell = (props: { children: React.ReactNode }) => {props.children}; const Swatch = ({ color }: { color: TSwatch }) => (
SCSS: ${color.scssVariableName.toLowerCase()} WCAG:{' '} {color.contrastRatio.toFixed(2)}{' '} {color.contrastRatio > WCAG_AA ? '(AA ✔︎)' : (AA)} JS: {color.colorKey} Hex: {color.colorHex}
); const allColors = () => (

Base

{Object.entries(bdlColors) .sort((A, B) => { // Sort the palette by grayness (hue/saturation = 0) and then by color const a = Color(A[1][0].colorHex); const b = Color(B[1][0].colorHex); if (a.hsl().object().h === 0) return -1; if (b.hsl().object().h === 0) return 1; return a.rgbNumber() - b.rgbNumber(); }) .map(([name, colors]) => (

{name}

{colors.map(color => ( ))}
))}
); export { allColors }; export default { title: 'Theming/Colors', parameters: { notes: mdNotes, }, };