Skip to content

Commit aaa2dbd

Browse files
committed
feat(theme-generator): replace CSS variable parsing with direct token imports for themes
1 parent 839b4ad commit aaa2dbd

File tree

4 files changed

+14
-69
lines changed

4 files changed

+14
-69
lines changed

packages/storybook/src/components/themeGenerator/ThemeGenerator.tsx

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import { ORIENTATION, OrientationSwitch } from '../sandbox/actions/OrientationSw
66
import styles from './themeGenerator.module.css';
77
import { ThemeGeneratorTreeView } from './themeGeneratorTreeView/ThemeGeneratorTreeView';
88
import { ThemeGeneratorPreview } from './themeGeneratorPreview/ThemeGeneratorPreview';
9-
import { parseCssVariables } from './themeVariableUtils';
109
import { ThemeGeneratorSwitchThemeModal } from './themeGeneratorSwitchThemeModal/ThemeGeneratorSwitchThemeModal';
1110
import { ThemeGeneratorJSONModal } from './themeGeneratorJSONModal/ThemeGeneratorJSONModal';
11+
import defaultTokens from '@ovhcloud/ods-themes/default/tokens';
12+
import developerTokens from '@ovhcloud/ods-themes/developer/tokens';
1213

1314
const ThemeGenerator = (): JSX.Element => {
1415
const [isFullscreen, setIsFullscreen] = useState(false);
@@ -21,34 +22,14 @@ const ThemeGenerator = (): JSX.Element => {
2122
const [isJsonOpen, setIsJsonOpen] = useState(false);
2223

2324
useEffect(() => {
24-
const loadTheme = async () => {
25-
if (selectedTheme === 'custom') {
26-
setIsCustomTheme(true);
27-
return;
28-
}
29-
30-
try {
31-
32-
// Fetch the CSS file from the static directory
33-
// Path: /themes/{themeName}/index.css (exposed via staticDirs in main.ts)
34-
const response = await fetch(`/themes/${selectedTheme}/index.css`);
35-
36-
if (!response.ok) {
37-
throw new Error(`Failed to fetch theme: ${response.statusText}`);
38-
}
39-
40-
const cssContent = await response.text();
41-
42-
// Parse CSS variables
43-
const variables = parseCssVariables(cssContent);
44-
setEditedVariables(variables);
45-
setIsCustomTheme(false);
46-
} catch (error) {
47-
console.error('Failed to load theme:', error);
48-
}
49-
};
25+
if (selectedTheme === 'custom') {
26+
setIsCustomTheme(true);
27+
return;
28+
}
5029

51-
loadTheme();
30+
const themeTokens = selectedTheme === 'developer' ? developerTokens : defaultTokens;
31+
setEditedVariables(themeTokens.root);
32+
setIsCustomTheme(false);
5233
}, [selectedTheme]);
5334

5435

@@ -62,7 +43,6 @@ const ThemeGenerator = (): JSX.Element => {
6243
[name]: value,
6344
}));
6445

65-
// Automatically switch to custom theme when user edits a variable
6646
if (!isCustomTheme) {
6747
setSelectedTheme('custom');
6848
setIsCustomTheme(true);
Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,3 @@
1-
const parseCssVariables = (cssContent: string): Record<string, string> => {
2-
const variables: Record<string, string> = {};
3-
4-
const extractVariablesFromRoot = (content: string, startIndex: number = 0): void => {
5-
const rootMatch = /:root\s*\{([^}]+)\}/g;
6-
rootMatch.lastIndex = startIndex;
7-
const match = rootMatch.exec(content);
8-
9-
if (!match) return;
10-
11-
const rootContent = match[1];
12-
extractVariablesFromContent(rootContent);
13-
14-
extractVariablesFromRoot(content, rootMatch.lastIndex);
15-
};
16-
17-
const extractVariablesFromContent = (content: string, startIndex: number = 0): void => {
18-
const varRegex = /(--[^:]+):\s*([^;]+);/g;
19-
varRegex.lastIndex = startIndex;
20-
const match = varRegex.exec(content);
21-
22-
if (!match) return;
23-
24-
const propertyName = match[1].trim();
25-
const propertyValue = match[2].trim();
26-
27-
// Only include --ods-* variables
28-
if (propertyName.startsWith('--ods-')) {
29-
variables[propertyName] = propertyValue;
30-
}
31-
32-
// Recursively find next variable
33-
extractVariablesFromContent(content, varRegex.lastIndex);
34-
};
35-
36-
extractVariablesFromRoot(cssContent);
37-
return variables;
38-
};
39-
401
interface CategorizedVariables {
412
colors: Array<{ name: string; value: string }>;
423
border: Array<{ name: string; value: string }>;
@@ -77,4 +38,4 @@ const categorizeCssVariables = (variables: Record<string, string>): CategorizedV
7738
return categories;
7839
};
7940

80-
export { categorizeCssVariables, type CategorizedVariables, parseCssVariables };
41+
export { categorizeCssVariables, type CategorizedVariables };

packages/themes/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
},
2323
"./default/tokens": {
2424
"default": "./dist/default/tokens.json"
25+
},
26+
"./developer/tokens": {
27+
"default": "./dist/developer/tokens.json"
2528
}
2629
},
2730
"types": "dist/index.d.ts",

packages/themes/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
enum ODS_THEME {
22
default = 'default',
3+
developer = 'developer',
34
}
45

56
const ODS_THEMES = Object.freeze(Object.values(ODS_THEME));

0 commit comments

Comments
 (0)