✨ Add tokens package (@navikt/ds-tokens)
Color, spacing, radius and typography tokens defined in src/tokens.json, compiled to CSS custom properties on the --a-* namespace by build.mjs.
This commit is contained in:
1 parent
eead0535a2
commit
7382e691ef
4 files changed
+125
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
||||
import { dirname, join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const root = dirname(fileURLToPath(import.meta.url));
|
||||
const tokens = JSON.parse(readFileSync(join(root, "src", "tokens.json"), "utf8"));
|
||||
|
||||
function flatten(node, prefix = []) {
|
||||
return Object.entries(node).flatMap(([key, value]) =>
|
||||
typeof value === "object" && "value" in value
|
||||
? [[prefix.concat(key).join("-"), value.value]]
|
||||
: flatten(value, prefix.concat(key)),
|
||||
);
|
||||
}
|
||||
|
||||
const lines = flatten(tokens)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([name, value]) => ` --a-${name}: ${value};`);
|
||||
|
||||
const css = `/**
|
||||
* Design tokens — generated by build.mjs from src/tokens.json. Do not edit manually.
|
||||
*/
|
||||
:root {
|
||||
${lines.join("\n")}
|
||||
}
|
||||
`;
|
||||
|
||||
mkdirSync(join(root, "dist"), { recursive: true });
|
||||
writeFileSync(join(root, "dist", "tokens.css"), css);
|
||||
|
||||
console.log(`Emitted ${lines.length} tokens to dist/tokens.css`);
|
||||
Reference in new issue
Block a user