|
| 1 | +import { |
| 2 | + fontSize100, |
| 3 | + fontSize200, |
| 4 | + fontSize300, |
| 5 | + fontSize400, |
| 6 | + fontSize500, |
| 7 | + fontSize600, |
| 8 | + fontSize700, |
| 9 | + fontSize800, |
| 10 | + fontSize900, |
| 11 | + fontSize1000, |
| 12 | + fontWeightSemibold, |
| 13 | + fontWeightRegular, |
| 14 | + fontWeightMedium, |
| 15 | + fontWeightBold, |
| 16 | +} from '../tokens/global.generated'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Represents the available font sizes for this platform. Available via key lookup |
| 20 | + */ |
| 21 | +export type FontSize = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 1000; |
| 22 | + |
| 23 | +/** |
| 24 | + * Represents the available font weights for this platform. Available via key lookup |
| 25 | + */ |
| 26 | +export type FontWeight = 'bold' | 'medium' | 'regular' | 'semibold'; |
| 27 | + |
| 28 | +/** |
| 29 | + * Get the size value associated with a named font size for this platform |
| 30 | + * @param size The font size key to retrieve from the FONT_SIZES mapping. |
| 31 | + * @param defaultSize The default font size key to use if the specified size is not found. |
| 32 | + * @returns The corresponding font size value from the FONT_SIZES mapping. |
| 33 | + */ |
| 34 | +export const fontSize = (() => { |
| 35 | + let fontSizes: Record<FontSize, number>; |
| 36 | + return (size: FontSize, defaultSize: FontSize = 300) => { |
| 37 | + // initialize fontSizes one time when used |
| 38 | + fontSizes ??= { |
| 39 | + 100: fontSize100, |
| 40 | + 200: fontSize200, |
| 41 | + 300: fontSize300, |
| 42 | + 400: fontSize400, |
| 43 | + 500: fontSize500, |
| 44 | + 600: fontSize600, |
| 45 | + 700: fontSize700, |
| 46 | + 800: fontSize800, |
| 47 | + 900: fontSize900, |
| 48 | + 1000: fontSize1000, |
| 49 | + }; |
| 50 | + return fontSizes[size] ?? fontSizes[defaultSize]; |
| 51 | + }; |
| 52 | +})(); |
| 53 | + |
| 54 | +/** |
| 55 | + * Get the weight value associated with a named font weight for this platform |
| 56 | + * @param weight The font weight key to retrieve from the FONT_WEIGHTS mapping. |
| 57 | + * @param defaultWeight The default font weight key to use if the specified weight is not found. |
| 58 | + * @returns The corresponding font weight value from the FONT_WEIGHTS mapping. |
| 59 | + */ |
| 60 | +export const fontWeight = (() => { |
| 61 | + let fontWeights: Record<FontWeight, string>; |
| 62 | + return (weight: FontWeight, defaultWeight: FontWeight = 'regular') => { |
| 63 | + // initialize fontWeights one time when used |
| 64 | + fontWeights ??= { |
| 65 | + bold: fontWeightBold, |
| 66 | + medium: fontWeightMedium, |
| 67 | + regular: fontWeightRegular, |
| 68 | + semibold: fontWeightSemibold, |
| 69 | + }; |
| 70 | + return fontWeights[weight] ?? fontWeights[defaultWeight]; |
| 71 | + }; |
| 72 | +})(); |
0 commit comments