Skip to content

Commit

Permalink
setup theming via asset sets
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanLovely committed Oct 18, 2023
1 parent f1d6b3a commit 2d107ab
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 44 deletions.
75 changes: 48 additions & 27 deletions knapsack/data/knapsack.asset-sets.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,59 @@
{
"globalAssetSetIds": [
"mode--light-density--low",
"mode--dark-density--low",
"mode--light-density--high",
"mode--dark-density--high"
"salt-light",
"salt-dark",
"check",
"coin",
"dollar"
],
"allAssetSets": {
"mode--light-density--low": {
"id": "mode--light-density--low",
"title": "mode--light-density--low",
"inlineCss": "",
"inlineJs": "document.body.setAttribute('mode', 'light'); document.body.setAttribute('density', 'low');",
"salt-light": {
"id": "salt-light",
"title": "Salt (light)",
"inlineJs": "document.body.setAttribute('data-mode', 'light');",
"assets": []
},
"mode--dark-density--low": {
"id": "mode--dark-density--low",
"title": "mode--dark-density--low",
"inlineCss": "",
"inlineJs": "document.body.setAttribute('mode', 'dark'); document.body.setAttribute('density', 'low');",
"salt-dark": {
"id": "salt-dark",
"title": "Salt (dark)",
"inlineJs": "document.body.setAttribute('data-mode', 'dark');",
"assets": []
},
"mode--light-density--high": {
"id": "mode--light-density--high",
"title": "mode--light-density--high",
"inlineCss": "",
"inlineJs": "document.body.setAttribute('mode', 'light'); document.body.setAttribute('density', 'high');",
"assets": []
"check": {
"id": "check",
"title": "check",
"assets": [
{
"src": "../dist/tokens/design-tokens.css"
},
{
"src": "../../ks-theme-check.css"
}
]
},
"mode--dark-density--high": {
"id": "mode--dark-density--high",
"title": "mode--dark-density--high",
"inlineCss": "",
"inlineJs": "document.body.setAttribute('mode', 'dark'); document.body.setAttribute('density', 'high');",
"assets": []
"coin": {
"id": "coin",
"title": "coin",
"assets": [
{
"src": "../dist/tokens/design-tokens.css"
},
{
"src": "../../ks-theme-coin.css"
}
]
},
"dollar": {
"id": "dollar",
"title": "dollar",
"assets": [
{
"src": "../dist/tokens/design-tokens.css"
},
{
"src": "../../ks-theme-dollar.css"
}
]
}
}
}
}
65 changes: 65 additions & 0 deletions knapsack/data/knapsack.design-tokens.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,70 @@
"description": ""
},
"description": ""
},
"theme": {
"description": "",
"type": "string",
"coin": {
"description": "",
"type": "string",
"actionable": {
"description": "",
"type": "string",
"primary": {
"description": "",
"type": "string",
"background": {
"$value": "#510433",
"$description": "",
"$type": "color"
},
"background hover": {
"$value": "#d1e909",
"$description": "",
"$type": "color"
}
}
}
},
"dollar": {
"description": "",
"type": "string",
"actionable": {
"description": "",
"type": "string",
"primary": {
"description": "",
"type": "string",
"background": {
"$value": "#15abd6",
"$description": "",
"$type": "color"
},
"background hover": {
"$value": "#09dee8",
"$description": "",
"$type": "color"
}
}
}
},
"check": {
"description": "",
"type": "string",
"actionable": {
"description": "",
"type": "string",
"primary": {
"description": "",
"type": "string",
"background": {
"$value": "#ee0000",
"$description": "",
"$type": "color"
}
}
}
}
}
}
26 changes: 13 additions & 13 deletions knapsack/demo-wrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { SaltProvider, Mode, Density } from "@salt-ds/core";

import "@salt-ds/theme/index.css";

const mode = document.body.getAttribute("mode");
if (mode !== "light" && mode !== "dark") {
throw new Error(`Invalid mode: ${mode}`);
}
const density = document.body.getAttribute("density");
if (
density !== "medium" &&
density !== "high" &&
density !== "low" &&
density !== "touch"
) {
throw new Error(`Invalid density: ${density}`);
}
const mode = document.body.getAttribute("data-mode") || "light";
// if (mode !== "light" && mode !== "dark") {
// throw new Error(`Invalid mode: ${mode}`);
// }
const density = document.body.getAttribute("data-density") || "medium";
// if (
// density !== "medium" &&
// density !== "high" &&
// density !== "low" &&
// density !== "touch"
// ) {
// throw new Error(`Invalid density: ${density}`);
// }

const DemoWrapper = ({ children }: { children: React.ReactElement }) => {
return (
Expand Down
3 changes: 3 additions & 0 deletions ks-theme-check.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.salt-theme.salt-theme {
--salt-actionable-primary-background: var(--theme-check-actionable-primary-background);
}
4 changes: 4 additions & 0 deletions ks-theme-coin.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.salt-theme.salt-theme {
--salt-actionable-primary-background: var(--theme-coin-actionable-primary-background);
--salt-actionable-primary-background-hover: var(--theme-coin-actionable-primary-background-hover);
}
85 changes: 85 additions & 0 deletions ks-theme-create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { join, relative } from "path";
import fs from "fs";
import {
KnapsackAssetSetsConfig,
KnapsackAssetSetConfig,
} from "@knapsack/types";
import tokens from "./knapsack/dist/tokens/design-tokens.nested.json";

const cssTokens = fs.readFileSync(
join(__dirname, "./knapsack/dist/tokens/design-tokens.css"),
"utf8"
);
const assetSetsPath = join(
__dirname,
"./knapsack/data/knapsack.asset-sets.json"
);
console.log("Creating theme...");
const themeNames = Object.keys(tokens.theme);

const themes = themeNames.map((theme) => {
console.log(`Making theme ${theme}`);
const themeTokenNames = cssTokens.split("\n").flatMap((line) => {
if (!line.trim().startsWith(`--theme-${theme}`)) return [];
const tokenName = line.trim().split(":")[0];
return [
`${tokenName.replace(`theme-${theme}`, `salt`)}: var(${tokenName});`,
];
});
const contents = [".salt-theme.salt-theme {", ...themeTokenNames, "}"].join(
"\n"
);
const cssPath = join(__dirname, `ks-theme-${theme}.css`);
fs.writeFileSync(cssPath, contents, {
encoding: "utf8",
});
return {
cssPath,
themeName: theme,
};
});

const assetSets: KnapsackAssetSetsConfig = {
globalAssetSetIds: ["salt-light", "salt-dark", ...themeNames],
allAssetSets: Object.fromEntries([
[
"salt-light",
((): KnapsackAssetSetConfig => ({
id: "salt-light",
title: "Salt (light)",
inlineJs: "document.body.setAttribute('data-mode', 'light');",
assets: [],
}))(),
],
[
"salt-dark",
((): KnapsackAssetSetConfig => ({
id: "salt-dark",
title: "Salt (dark)",
inlineJs: "document.body.setAttribute('data-mode', 'dark');",
assets: [],
}))(),
],
...themes.map(({ cssPath, themeName }) => {
const assetSet: KnapsackAssetSetConfig = {
id: themeName,
title: themeName,
assets: [
{
src: "../dist/tokens/design-tokens.css",
},
{
src: relative(join(__dirname, "./knapsack/data"), cssPath),
},
],
};
return [themeName, assetSet];
}),
]),
};

fs.writeFileSync(
join(__dirname, "./knapsack/data/knapsack.asset-sets.json"),
JSON.stringify(assetSets, null, 2)
);
console.log("Done");
4 changes: 4 additions & 0 deletions ks-theme-dollar.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.salt-theme.salt-theme {
--salt-actionable-primary-background: var(--theme-dollar-actionable-primary-background);
--salt-actionable-primary-background-hover: var(--theme-dollar-actionable-primary-background-hover);
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@
"build-storybook": "yarn build:ag-grid-theme && storybook build",
"typecheck": "tsc --noEmit",
"chromatic": "chromatic",
"ks:build": "knapsack --config ./knapsack/knapsack.config.js build",
"ks:start": "knapsack --config ./knapsack/knapsack.config.js start",
"ks:build": "knapsack --config ./knapsack/knapsack.config.js build && npx ts-node ./ks-theme-create.ts'",
"ks:start-ks": "knapsack --config ./knapsack/knapsack.config.js start",
"ks:start": "run-p ks:start-ks ks:watch-tokens",
"ks:serve": "NODE_ENV=production knapsack --config ./knapsack/knapsack.config.js serve",
"ks:test": "knapsack --config ./knapsack/knapsack.config.js test",
"ks:import": "knapsack --config ./knapsack/knapsack.config.js react-patterns --pkg-path @salt-ds/core --importPrefix null",
"ks:watch-tokens": "npx nodemon --watch knapsack/dist/tokens/design-tokens.css --exec 'touch ./knapsack/demo-wrapper.tsx && npx ts-node ./ks-theme-create.ts'",
"heroku-postbuild": "yarn ks:build"
},
"dependencies": {
Expand Down Expand Up @@ -103,6 +105,7 @@
"modular-scripts": "patch:modular-scripts@npm:3.6.0#.yarn/patches/modular-scripts-npm-3.6.0-d967962075.patch",
"msw": "^1.2.1",
"msw-storybook-addon": "^1.8.0",
"npm-run-all": "^4.1.5",
"nyse-holidays": "^1.2.0",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
Expand Down
Loading

0 comments on commit 2d107ab

Please sign in to comment.