Created
August 22, 2022 16:30
-
-
Save konstantinmuenster/74fe49f6ca07fcefe3f7faac89c8cf5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Stitches from '@stitches/react'; | |
import { theme } from '@config/stitches.config'; | |
const COLOR_PATTERN = '$––color––'; | |
const REGEX = /\"([^\"]*?\$––color––[^\"]*?)\"/; | |
export const generateColorPaletteVariants = (css: Stitches.CSS) => { | |
const cssString = JSON.stringify(css); | |
return Object.values(theme.colors).reduce((prev, color) => { | |
const substringToReplace = REGEX.exec(cssString); | |
if (!substringToReplace?.length) throw Error('No Color Token provided.'); | |
const replacedSubstring = substringToReplace[0].replace( | |
COLOR_PATTERN, | |
`$${color.token}` | |
); | |
const replacedString = cssString.replace( | |
substringToReplace[0], | |
replacedSubstring | |
); | |
return { ...prev, [color.token]: JSON.parse(replacedString) }; | |
}, {}) as Record<keyof typeof theme.colors, Stitches.CSS>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment