Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit a6a6567

Browse files
refactor: refactoring get css variable function to work for both brand and theme tokens
1 parent 5abc7de commit a6a6567

File tree

3 files changed

+15
-51
lines changed

3 files changed

+15
-51
lines changed

docs/BrandColors.stories.tsx

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
22
import type { Meta, StoryObj } from '@storybook/react';
33

44
import tokens from '../src/figma/tokens.json';
5-
import { Color } from './utils/getThemeColorsFromStylesheet';
5+
import getCSSVariablesFromStylesheet from './utils/getCSSVariablesFromStylesheet';
66

77
import { ColorSwatchGroup, ColorSwatch } from './components';
88

@@ -28,41 +28,7 @@ export const Figma: Story = {
2828

2929
export const CSS: Story = {
3030
render: () => {
31-
const [brandColors, setBrandColors] = useState<Color>({});
32-
33-
useEffect(() => {
34-
// Getting all CSS variables that start with '--brand-colors' from all stylesheets
35-
const cssVars = Array.from(document.styleSheets)
36-
.flatMap((styleSheet) => {
37-
try {
38-
return Array.from(styleSheet.cssRules);
39-
} catch (err) {
40-
return [];
41-
}
42-
})
43-
.filter((cssRule) => cssRule.type === CSSRule.STYLE_RULE)
44-
.flatMap((cssRule) => Array.from((cssRule as CSSStyleRule).style))
45-
.filter((varName) => varName.startsWith('--brand-colors'));
46-
47-
const brandColors: Color = {};
48-
49-
// Looping through each CSS variable and getting its value
50-
cssVars.forEach((varName) => {
51-
const name = varName.split('-').slice(2).join('-');
52-
const color = getComputedStyle(document.documentElement)
53-
.getPropertyValue(varName)
54-
.trim();
55-
brandColors[name] = {
56-
color: color,
57-
name: `var(${varName})`,
58-
};
59-
});
60-
61-
// Updating the state with the new brandColors
62-
setBrandColors(brandColors);
63-
}, []);
64-
65-
// Rendering the color swatches
31+
const cssBrandColors = getCSSVariablesFromStylesheet('--brand-colors');
6632
return (
6733
<div
6834
style={{
@@ -72,7 +38,7 @@ export const CSS: Story = {
7238
}}
7339
>
7440
{/* Mapping through each brand color and rendering a ColorSwatch component for it */}
75-
{Object.values(brandColors).map(({ color, name }) => (
41+
{Object.values(cssBrandColors).map(({ color, name }) => (
7642
<ColorSwatch key={name} color={color} name={name} />
7743
))}
7844
</div>

docs/ThemeColors.stories.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import React from 'react';
33
import tokens from '../src/figma/tokens.json';
44
import { lightTheme, darkTheme } from '../src';
55

6-
import getThemeColorsFromStylesheet from './utils/getThemeColorsFromStylesheet';
6+
import getCSSVariablesFromStylesheet from './utils/getCSSVariablesFromStylesheet';
77
import { ColorSwatchGroup, ColorSwatch } from './components';
88
import README from './ThemeColors.mdx';
99

@@ -57,8 +57,7 @@ export const FigmaDarkTheme = {
5757

5858
export const CSSLightTheme = {
5959
render: () => {
60-
const lightThemeColors = getThemeColorsFromStylesheet();
61-
60+
const lightThemeColors = getCSSVariablesFromStylesheet('--color-');
6261
return (
6362
<div
6463
style={{
@@ -79,7 +78,7 @@ export const CSSLightTheme = {
7978

8079
export const CSSDarkTheme = {
8180
render: () => {
82-
const darkThemeColors = getThemeColorsFromStylesheet();
81+
const darkThemeColors = getCSSVariablesFromStylesheet('--color-');
8382
return (
8483
<div
8584
style={{
@@ -116,12 +115,10 @@ export const CSSDarkTheme = {
116115
values: [{ name: 'dark', value: 'var(--color-background-default)' }],
117116
},
118117
decorators: [
119-
// eslint-disable-next-line no-unused-vars
120118
(StoryFn) => {
121119
// Check if document object is available
122120
if (typeof document !== 'undefined') {
123121
// Add the data-theme attribute to the root element
124-
// eslint-disable-next-line no-undef
125122
document.documentElement.setAttribute('data-theme', 'dark');
126123
}
127124
// Render the story

docs/utils/getThemeColorsFromStylesheet.ts renamed to docs/utils/getCSSVariablesFromStylesheet.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ export interface Color {
77
}
88

99
/**
10-
* Retrieves CSS color variables from the stylesheet.
10+
* Retrieves CSS variables from the stylesheet.
1111
*
12-
* @returns An object containing the retrieved theme color CSS variables.
12+
* @param varPrefix - The prefix of the CSS variables to retrieve.
13+
* @returns An object containing the retrieved CSS variables.
1314
*/
14-
function getThemeColorsFromStylesheet(): Color {
15-
const themeColors: Color = {};
15+
function getCSSVariablesFromStylesheet(varPrefix: string): Color {
16+
const cssVariables: Color = {};
1617

1718
Array.from(document.styleSheets)
1819
.flatMap((styleSheet) => {
@@ -30,19 +31,19 @@ function getThemeColorsFromStylesheet(): Color {
3031
(cssRule: CSSRule) =>
3132
Array.from((cssRule as CSSStyleRule).style) as string[],
3233
)
33-
.filter((varName) => varName.startsWith('--color-'))
34+
.filter((varName) => varName.startsWith(varPrefix))
3435
.forEach((varName) => {
3536
const name = varName.split('-').slice(2).join('-');
3637
const color = getComputedStyle(document.documentElement)
3738
.getPropertyValue(varName)
3839
.trim();
39-
themeColors[name] = {
40+
cssVariables[name] = {
4041
color,
4142
name: `var(${varName})`,
4243
};
4344
});
4445

45-
return themeColors;
46+
return cssVariables;
4647
}
4748

48-
export default getThemeColorsFromStylesheet;
49+
export default getCSSVariablesFromStylesheet;

0 commit comments

Comments
 (0)