|
| 1 | +const { getThemeProperties } = require('@skeletonlabs/tw-plugin'); |
| 2 | + |
| 3 | +const themeNames = [ |
| 4 | + 'skeleton', |
| 5 | + 'wintry', |
| 6 | + 'modern', |
| 7 | + 'rocket', |
| 8 | + 'seafoam', |
| 9 | + 'vintage', |
| 10 | + 'sahara', |
| 11 | + 'hamlindigo', |
| 12 | + 'gold-nouveau', |
| 13 | +]; |
| 14 | + |
| 15 | +// Map Skeleton to Svelte UX theme colors |
| 16 | +const skeletonColorMap = { |
| 17 | + // Semantic |
| 18 | + primary: 'primary', |
| 19 | + secondary: 'secondary', |
| 20 | + tertiary: 'accent', |
| 21 | + // '': 'neutral', |
| 22 | + // State |
| 23 | + success: 'success', |
| 24 | + warning: 'warning', |
| 25 | + error: 'danger', |
| 26 | + // Surface |
| 27 | + surface: 'surface', |
| 28 | +}; |
| 29 | + |
| 30 | +function processTheme(themeName, scheme) { |
| 31 | + const properties = getThemeProperties(themeName); |
| 32 | + |
| 33 | + let mappedThemeProperties = Object.entries(properties) |
| 34 | + .map(([key, value]) => { |
| 35 | + if (key.startsWith('--color')) { |
| 36 | + // `--color-primary-500` => `primary-500` |
| 37 | + // `--color-primary-500` => `primary` |
| 38 | + const matches = key.match(/--color-(\w*)-([0-9]{3})/); |
| 39 | + const skeletonColorName = matches?.[1]; |
| 40 | + const skeletonColorShade = matches?.[2]; |
| 41 | + const themeColorName = skeletonColorMap[skeletonColorName]; |
| 42 | + if (themeColorName) { |
| 43 | + return [`${themeColorName}-${skeletonColorShade}`, `rgb(${value})`]; |
| 44 | + } |
| 45 | + } else if (key.startsWith('--on-')) { |
| 46 | + // `--on-primary` => `primary-content` |
| 47 | + const matches = key.match(/--on-(\w*)/); |
| 48 | + const skeletonColorName = matches?.[1]; |
| 49 | + const themeColorName = skeletonColorMap[skeletonColorName]; |
| 50 | + if (themeColorName) { |
| 51 | + return [`${themeColorName}-content`, `rgb(${value})`]; |
| 52 | + } |
| 53 | + } else { |
| 54 | + // consider mapping additional properties |
| 55 | + // '--theme-font-family-base': 'system-ui', |
| 56 | + // '--theme-font-family-heading': 'system-ui', |
| 57 | + // '--theme-font-color-base': '0 0 0', |
| 58 | + // '--theme-font-color-dark': '255 255 255', |
| 59 | + // '--theme-rounded-base': '9999px', |
| 60 | + // '--theme-rounded-container': '8px', |
| 61 | + // '--theme-border-base': '1px', |
| 62 | + } |
| 63 | + }) |
| 64 | + .filter((d) => d); |
| 65 | + |
| 66 | + mappedThemeProperties = |
| 67 | + scheme === 'light' |
| 68 | + ? [ |
| 69 | + ...mappedThemeProperties, |
| 70 | + ['color-scheme', 'light'], |
| 71 | + ['surface-100', `rgb(${properties['--color-surface-50']})`], |
| 72 | + ['surface-200', `rgb(${properties['--color-surface-100']})`], |
| 73 | + ['surface-300', `rgb(${properties['--color-surface-200']})`], |
| 74 | + ['surface-content', `rgb(0 0 0)`], |
| 75 | + ] |
| 76 | + : [ |
| 77 | + ...mappedThemeProperties, |
| 78 | + ['color-scheme', 'dark'], |
| 79 | + ['surface-100', `rgb(${properties['--color-surface-700']})`], |
| 80 | + ['surface-200', `rgb(${properties['--color-surface-800']})`], |
| 81 | + ['surface-300', `rgb(${properties['--color-surface-900']})`], |
| 82 | + ['surface-content', `rgb(255 255 255)`], |
| 83 | + ]; |
| 84 | + |
| 85 | + return [ |
| 86 | + themeName === 'skeleton' ? scheme : scheme === 'dark' ? themeName + '-dark' : themeName, |
| 87 | + Object.fromEntries(mappedThemeProperties), |
| 88 | + ]; |
| 89 | +} |
| 90 | + |
| 91 | +const themes = Object.fromEntries( |
| 92 | + themeNames.flatMap((themeName) => { |
| 93 | + return [processTheme(themeName, 'light'), processTheme(themeName, 'dark')]; |
| 94 | + }) |
| 95 | +); |
| 96 | + |
| 97 | +const lightThemes = Object.keys(themes).filter((themeName) => !themeName.endsWith('dark')); |
| 98 | +const darkThemes = Object.keys(themes).filter((themeName) => themeName.endsWith('dark')); |
| 99 | + |
| 100 | +module.exports = { themes, lightThemes, darkThemes }; |
0 commit comments