Skip to content

Commit a5d40da

Browse files
committed
refactor: generating of css via style dictionary
1 parent 190a7f0 commit a5d40da

40 files changed

+5166
-3743
lines changed

package-lock.json

+1,739-420
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
"preview": "vite preview"
2020
},
2121
"dependencies": {
22+
"@bundled-es-modules/memfs": "^4.9.4",
2223
"@craftjs/core": "^0.2.11",
23-
"@db-ux/react-core-components": "1.0.0",
2424
"@db-ux/db-theme": "1.0.2",
25+
"@db-ux/react-core-components": "1.0.0",
2526
"@redux-devtools/extension": "^3.3.0",
2627
"@tiptap/extension-color": "^2.11.5",
2728
"@tiptap/extension-text-style": "^2.10.3",
@@ -48,6 +49,7 @@
4849
"react-dom": "^18.3.1",
4950
"react-i18next": "15.4.0",
5051
"react-router-dom": "7.1.5",
52+
"style-dictionary": "^4.3.3",
5153
"traverse": "^0.6.10",
5254
"zustand": "^5.0.3"
5355
},
@@ -69,10 +71,10 @@
6971
"ncp": "^2.0.0",
7072
"npm-run-all2": "^7.0.2",
7173
"postcss": "^8.5.1",
74+
"purgecss": "7.0.2",
7275
"sass": "^1.85.0",
7376
"tailwindcss": "^3.4.17",
7477
"typescript": "^5.7.3",
75-
"purgecss": "7.0.2",
7678
"vite": "^6.1.0"
7779
}
7880
}

src/App.tsx

+21-16
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { useThemeBuilderStore } from "./store";
44
import { useEffect } from "react";
55
import { DefaultColorType } from "./utils/data.ts";
66
import {
7-
getNonColorCssProperties,
8-
getPaletteOutput,
9-
getSpeakingNames,
10-
} from "./utils/outputs/web";
7+
getSDColorPalette,
8+
getSDSpeakingColors,
9+
} from "./utils/outputs/style-dictionary/colors.ts";
10+
import { mergeObjectsRecursive } from "./utils";
11+
import { runStyleDictionary } from "./utils/outputs/style-dictionary";
12+
import { appConfig } from "./utils/outputs/style-dictionary/config";
1113

1214
const App = () => {
1315
const { speakingNames, luminanceSteps, theme } = useThemeBuilderStore(
@@ -21,20 +23,23 @@ const App = () => {
2123
...theme.customColors,
2224
};
2325

24-
const cssProps: any = {
25-
...getPaletteOutput(allColors, luminanceSteps),
26-
...getSpeakingNames(speakingNames, allColors),
27-
...getNonColorCssProperties(theme),
26+
const sdColorPalette = getSDColorPalette(allColors, luminanceSteps);
27+
const sdSpeakingColors = getSDSpeakingColors(speakingNames, allColors);
28+
29+
const finalTheme = {
30+
...theme,
31+
...mergeObjectsRecursive(sdColorPalette, sdSpeakingColors),
2832
};
2933

30-
const pages = document.getElementsByTagName("html");
31-
Array.from(pages).forEach((page: Element) => {
32-
page.setAttribute(
33-
"style",
34-
Object.entries(cssProps)
35-
.map((value) => `${value[0]}:${value[1]};`)
36-
.join(" "),
37-
);
34+
runStyleDictionary({
35+
tokens: finalTheme,
36+
...appConfig,
37+
}).then((result) => {
38+
const page = document.querySelector("html");
39+
const overwrites = result["/overwrites"];
40+
if (page && overwrites) {
41+
page.setAttribute("style", overwrites);
42+
}
3843
});
3944
}, [speakingNames, theme, luminanceSteps]);
4045

src/components/Customization/Settings/Scaling/data.ts

+4-28
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import DefaultTheme from "../../../../data/default-theme.json";
21
import traverse from "traverse";
2+
import { defaultTheme } from "../../../../store/themes.ts";
33

44
export type ShirtSelectionType = {
55
label: string;
66
params: string[];
77
};
88

9-
const theme = traverse(DefaultTheme);
9+
const theme = traverse(defaultTheme);
1010

1111
export const getShirtValue = (
1212
scaleString: string,
1313
path: string[],
1414
): string | number | undefined => {
15-
if (path.at(-1) === "_scale") {
15+
if (path.at(-2) === "_scale") {
1616
return scaleString;
1717
}
1818

19-
let scale = 1;
19+
let scale: number;
2020

2121
if (scaleString === "none") {
2222
scale = 0;
@@ -30,30 +30,6 @@ export const getShirtValue = (
3030
return undefined;
3131
}
3232

33-
if (path[0] === "elevation") {
34-
if (path.at(-1) === "sm") {
35-
return (
36-
`0 0 ${scale}px -${scale}px rgba(0, 0, 0, 0.2),` +
37-
`0 0 ${4 * scale}px ${scale}px rgba(0, 0, 0, 0.12),` +
38-
`0 0 ${2 * scale}px 0 rgba(0, 0, 0, 0.14)`
39-
);
40-
}
41-
if (path.at(-1) === "md") {
42-
return (
43-
`0 0 ${2 * scale}px -${scale}px rgba(0, 0, 0, 0.2),` +
44-
`0 0 ${8 * scale}px ${scale}px rgba(0, 0, 0, 0.12),` +
45-
`0 0 ${4 * scale}px 0 rgba(0, 0, 0, 0.14)`
46-
);
47-
}
48-
if (path.at(-1) === "lg") {
49-
return (
50-
`0 0 ${4 * scale}px -${3 * scale}px rgba(0, 0, 0, 0.2),` +
51-
`0 0 ${16 * scale}px ${3 * scale}px rgba(0, 0, 0, 0.12),` +
52-
`0 0 ${8 * scale}px ${scale}px rgba(0, 0, 0, 0.14)`
53-
);
54-
}
55-
}
56-
5733
if (path.length < 2) {
5834
return undefined;
5935
}

src/components/Customization/Settings/Scaling/scaling.tsx

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import traverse from "traverse";
77
const getFromJsonByArray = (params: string[], json: any): any => {
88
try {
99
let currentObject = json;
10-
params.forEach((param) => {
10+
for (const param of params) {
1111
currentObject = currentObject[param];
12-
});
12+
}
1313

1414
return currentObject;
1515
} catch (error) {
@@ -29,7 +29,8 @@ const Scaling = ({ label, params }: ShirtSelectionType) => {
2929
if (
3030
this.isLeaf &&
3131
this.path.length > 0 &&
32-
path.every((value, index) => value === this.path[index])
32+
path.every((value, index) => value === this.path[index]) &&
33+
this.path.at(-1) === "value"
3334
) {
3435
this.update(getShirtValue(scale, this.path) || value);
3536
}
@@ -46,7 +47,7 @@ const Scaling = ({ label, params }: ShirtSelectionType) => {
4647
<DBSelect
4748
label={`${t(label)} ${t("scale")}`}
4849
variant="floating"
49-
value={getFromJsonByArray([...params, "_scale"], theme)}
50+
value={getFromJsonByArray([...params, "_scale", "value"], theme)}
5051
onChange={(event) => {
5152
setDetfaultTheme(event.target.value);
5253
}}
@@ -75,7 +76,7 @@ const Scaling = ({ label, params }: ShirtSelectionType) => {
7576
<option>120%</option>
7677
</>
7778
)}
78-
{(params.includes("elevation") || params.includes("radius")) && (
79+
{params.includes("radius") && (
7980
<>
8081
<option>none</option>
8182
<option>50%</option>

src/components/Landing/ThemeSelect/theme-select.tsx

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { DBCard, DBIcon, DBSection, DBTooltip } from "@db-ux/react-core-components";
1+
import {
2+
DBCard,
3+
DBIcon,
4+
DBSection,
5+
DBTooltip,
6+
} from "@db-ux/react-core-components";
27
import { useThemeBuilderStore } from "../../../store";
38
import { Link } from "react-router-dom";
49
import Demo from "../../../pages/Demo";
@@ -10,14 +15,8 @@ import {
1015
ThemeType,
1116
speakingNamesDefaultMapping,
1217
} from "../../../utils/data.ts";
13-
import DefaultTheme from "../../../data/default-theme.json";
14-
import DBTheme from "../../../data/db-theme.json";
15-
import SBahnTheme from "../../../data/sbahn-theme.json";
1618
import { getThemeImage } from "../../../utils";
17-
18-
const defaultTheme = DefaultTheme as unknown as ThemeType;
19-
const sBahnTheme = SBahnTheme as unknown as ThemeType;
20-
const dbTheme = DBTheme as unknown as ThemeType;
19+
import { dbTheme, defaultTheme, sBahnTheme } from "../../../store/themes.ts";
2120

2221
const themes: Record<string, ThemeType> = {
2322
neutralTheme: defaultTheme,

0 commit comments

Comments
 (0)