Skip to content

Instantly share code, notes, and snippets.

@dpeek
Created July 13, 2021 04:41
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 dpeek/079b8ac4268e7b818316d192da4f8794 to your computer and use it in GitHub Desktop.
Save dpeek/079b8ac4268e7b818316d192da4f8794 to your computer and use it in GitHub Desktop.
Radix Color Scale Mapper
import { createCss } from '@stitches/react';
import { mint } from '@radix-ui/colors';
type ScaleNum = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type ScaleKey<Name extends string> = `${Name}${ScaleNum}`;
type ColorScale<Name extends string> = { [K in ScaleKey<Name>]: string };
function mapColorScale<FromName extends string, ToName extends string>(
fromScale: ColorScale<FromName>,
fromName: FromName,
toName: ToName
): ColorScale<ToName> {
const scale: Record<string, string> = {};
for (let i = 1; i <= 12; i++) {
scale[`${toName}${i}`] = fromScale[`${fromName}${i}` as ScaleKey<FromName>];
}
return scale as ColorScale<ToName>;
}
export const { styled, css, global, keyframes, getCssString, theme } =
createCss({
theme: {
colors: {
...mapColorScale(mint, 'mint', 'brand')
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment