Skip to content

Instantly share code, notes, and snippets.

@konstantinmuenster
Created August 22, 2022 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konstantinmuenster/74fe49f6ca07fcefe3f7faac89c8cf5f to your computer and use it in GitHub Desktop.
Save konstantinmuenster/74fe49f6ca07fcefe3f7faac89c8cf5f to your computer and use it in GitHub Desktop.
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