chore(tools): convert color-system to TypeScript (#44970)

* Change file extension

* Move the files to their own folder

* Fix typecheck
This commit is contained in:
Huyen Nguyen
2022-01-31 21:06:45 +07:00
committed by GitHub
parent 4173c2783a
commit f871bf1dbd
5 changed files with 64 additions and 19 deletions

View File

@ -1,19 +1,26 @@
import PropTypes from 'prop-types';
import React from 'react'; import React from 'react';
import colorList from './colors.css'; import colorList from '../colors.css';
import { Color, ColorList, PaletteProps } from './types';
// ---------------------------------------------------------- // // ---------------------------------------------------------- //
// HELPER FUNCTIONS // // HELPER FUNCTIONS //
// ---------------------------------------------------------- // // ---------------------------------------------------------- //
// Transform colorList from an object to an array of objects /**
const transformedColorList = Object.keys(colorList).map(colorName => ({ * Transform colorList from an object to an array of objects
label: colorName.replace('--', ''), * @example
value: colorList[colorName] * Input: { '--blue10': 'var(--blue10)' }
})); * Output: [{ label: 'blue10', value: 'var(--blue10)' }]
*/
const transformedColorList = Object.keys(colorList as ColorList).map(
colorName => ({
label: colorName.replace('--', ''),
value: (colorList as ColorList)[colorName]
})
);
// Get the background and text color values of each palette item // Get the background and text color values of each palette item
const getPaletteItemStyle = color => { const getPaletteItemStyle = (color: Color) => {
const itemTextColor = color.label.substring(color.label.length - 2); const itemTextColor = color.label.substring(color.label.length - 2);
return { return {
@ -24,13 +31,13 @@ const getPaletteItemStyle = color => {
}; };
}; };
const getPaletteByColorName = name => const getPaletteByColorName = (name: string) =>
transformedColorList.filter(color => color.label.includes(name)); transformedColorList.filter(color => color.label.includes(name));
// ---------------------------------------------------------- // // ---------------------------------------------------------- //
// COMPONENTS // // COMPONENTS //
// ---------------------------------------------------------- // // ---------------------------------------------------------- //
const Palette = ({ colors }) => { const Palette = ({ colors }: PaletteProps) => {
return ( return (
<div className='inline-flex flex-col m-4 w-3/12'> <div className='inline-flex flex-col m-4 w-3/12'>
{colors.map(color => ( {colors.map(color => (
@ -46,15 +53,6 @@ const Palette = ({ colors }) => {
); );
}; };
Palette.propTypes = {
colors: PropTypes.arrayOf(
PropTypes.shape({
label: PropTypes.string,
value: PropTypes.string
})
)
};
export const AllPalettes = () => { export const AllPalettes = () => {
return ( return (
<> <>

View File

@ -0,0 +1,10 @@
export interface Color {
label: string;
value: string;
}
export interface PaletteProps {
colors: Color[];
}
export type ColorList = Record<string, string>;

View File

@ -43,6 +43,42 @@
--red90: #850000; --red90: #850000;
} }
/* Export the variables in order to use them on the Color System Storybook page */
:export {
--gray00: var(--gray00);
--gray05: var(--gray05);
--gray10: var(--gray10);
--gray15: var(--gray15);
--gray45: var(--gray45);
--gray75: var(--gray75);
--gray80: var(--gray80);
--gray85: var(--gray85);
--gray90: var(--gray90);
--purple10: var(--purple10);
--purple50: var(--purple50);
--purple90: var(--purple90);
--yellow10: var(--yellow10);
--yellow20: var(--yellow20);
--yellow50: var(--yellow50);
--yellow90: var(--yellow90);
--blue10: var(--blue10);
--blue20: var(--blue20);
--blue30: var(--blue30);
--blue50: var(--blue50);
--blue90: var(--blue90);
--green10: var(--green10);
--green90: var(--green90);
--red10: var(--red10);
--red20: var(--red20);
--red80: var(--red80);
--red90: var(--red90);
}
html, html,
div.light { div.light {
--default-foreground-primary: var(--gray90); --default-foreground-primary: var(--gray90);

View File

@ -0,0 +1 @@
declare module '*.css';