@hanzo/design 0.5.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +10 -24
- package/README.md +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/preference.d.ts +65 -0
- package/dist/preference.d.ts.map +1 -0
- package/dist/preference.js +102 -0
- package/dist/tokens.gen.d.ts +74 -70
- package/dist/tokens.gen.d.ts.map +1 -1
- package/dist/tokens.gen.js +74 -70
- package/package.json +8 -6
- package/scripts/check-dist.mjs +81 -0
- package/scripts/check-preference.mjs +168 -0
- package/scripts/check-tokens.mjs +34 -0
- package/skills/design-system/SKILL.md +1 -1
- package/src/index.ts +6 -0
- package/src/preference.ts +122 -0
- package/src/tokens.gen.ts +74 -70
- package/styles.css +82 -50
- package/tailwind.css +82 -50
- package/tokens/base.css +33 -13
- package/tokens/colors.css +9 -9
- package/tokens/spacing.css +20 -14
- package/tokens/typography.css +20 -14
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hanzo/design",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.1",
|
|
4
4
|
"packageManager": "pnpm@11.17.0",
|
|
5
5
|
"description": "Hanzo Design System \u2014 monochrome, dark-default tokens + components + brand assets, the single source of truth for every Hanzo surface. CSS + typed programmatic tokens.",
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "MIT OR Apache-2.0",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "https://github.com/hanzoai/design.git"
|
|
@@ -19,8 +19,10 @@
|
|
|
19
19
|
"exports": {
|
|
20
20
|
".": {
|
|
21
21
|
"types": "./dist/index.d.ts",
|
|
22
|
-
"import": "./dist/index.js"
|
|
22
|
+
"import": "./dist/index.js",
|
|
23
|
+
"default": "./dist/index.js"
|
|
23
24
|
},
|
|
25
|
+
"./package.json": "./package.json",
|
|
24
26
|
"./styles.css": "./styles.css",
|
|
25
27
|
"./tokens/*": "./tokens/*",
|
|
26
28
|
"./components/*": "./components/*",
|
|
@@ -32,8 +34,8 @@
|
|
|
32
34
|
},
|
|
33
35
|
"scripts": {
|
|
34
36
|
"gen": "node scripts/gen-tokens.mjs",
|
|
35
|
-
"test": "node scripts/check-tokens.mjs && node scripts/lint.mjs .",
|
|
36
|
-
"build": "pnpm gen && pnpm test && tsc -p tsconfig.json",
|
|
37
|
+
"test": "node scripts/check-tokens.mjs && node scripts/check-preference.mjs && node scripts/lint.mjs .",
|
|
38
|
+
"build": "pnpm gen && pnpm test && tsc -p tsconfig.json && node scripts/check-dist.mjs",
|
|
37
39
|
"prepublishOnly": "pnpm build",
|
|
38
40
|
"lint": "node scripts/lint.mjs"
|
|
39
41
|
},
|
|
@@ -70,4 +72,4 @@
|
|
|
70
72
|
"bin": {
|
|
71
73
|
"hanzo-design-lint": "./scripts/lint.mjs"
|
|
72
74
|
}
|
|
73
|
-
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import the BUILT package the way a consumer does.
|
|
3
|
+
*
|
|
4
|
+
* Everything else in this repo checks the sources: the token files, the
|
|
5
|
+
* generator's output, the preference contract. All of it passed while
|
|
6
|
+
* `dist/index.js` was unimportable — 0.4.10 and 0.4.11 both shipped a barrel
|
|
7
|
+
* that re-exported `'./preference'` with no extension, which TypeScript emits
|
|
8
|
+
* verbatim and Node ESM refuses to resolve. `pnpm build` was green, `pnpm test`
|
|
9
|
+
* was green, and `import '@hanzo/design'` threw ERR_MODULE_NOT_FOUND for every
|
|
10
|
+
* consumer.
|
|
11
|
+
*
|
|
12
|
+
* A test that reads source cannot see that. This one loads the artifact.
|
|
13
|
+
*/
|
|
14
|
+
import { readFileSync, existsSync } from 'node:fs'
|
|
15
|
+
import { fileURLToPath, pathToFileURL } from 'node:url'
|
|
16
|
+
import { dirname, join } from 'node:path'
|
|
17
|
+
|
|
18
|
+
const here = dirname(fileURLToPath(import.meta.url))
|
|
19
|
+
const root = join(here, '..')
|
|
20
|
+
|
|
21
|
+
let failed = 0
|
|
22
|
+
const check = async (name, fn) => {
|
|
23
|
+
try {
|
|
24
|
+
await fn()
|
|
25
|
+
console.log(` ok ${name}`)
|
|
26
|
+
} catch (e) {
|
|
27
|
+
failed++
|
|
28
|
+
console.error(` FAIL ${name}\n ${e.message}`)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
console.log('dist:')
|
|
33
|
+
|
|
34
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'))
|
|
35
|
+
|
|
36
|
+
await check('every export the package advertises actually exists on disk', () => {
|
|
37
|
+
const missing = []
|
|
38
|
+
for (const [name, entry] of Object.entries(pkg.exports ?? {})) {
|
|
39
|
+
for (const target of typeof entry === 'string' ? [entry] : Object.values(entry)) {
|
|
40
|
+
if (typeof target !== 'string' || target.includes('*')) continue
|
|
41
|
+
if (!existsSync(join(root, target))) missing.push(`${name} -> ${target}`)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (missing.length) throw new Error(`exports pointing at nothing:\n ${missing.join('\n ')}`)
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
await check('the barrel IMPORTS — the check that 0.4.10 and 0.4.11 needed', async () => {
|
|
48
|
+
const entry = join(root, 'dist/index.js')
|
|
49
|
+
if (!existsSync(entry)) throw new Error('dist/index.js is missing — run the build first')
|
|
50
|
+
const mod = await import(pathToFileURL(entry).href)
|
|
51
|
+
// The preference API is the part that was unreachable; naming it here means a
|
|
52
|
+
// future re-export losing its extension fails by name rather than by silence.
|
|
53
|
+
for (const name of ['vars', 'css', 'isColor', 'TYPE_MIN', 'TYPE_MAX', 'cssVar', 'tokenValue']) {
|
|
54
|
+
if (mod[name] === undefined) throw new Error(`dist/index.js does not export ${name}`)
|
|
55
|
+
}
|
|
56
|
+
if (typeof mod.vars !== 'function') throw new Error('vars is not callable from the built package')
|
|
57
|
+
const v = mod.vars({ type: 1.2, density: 'compact' })
|
|
58
|
+
if (v['--type-scale'] !== '1.2') throw new Error(`built vars() returned ${JSON.stringify(v)}`)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
await check('no emitted module uses an extensionless relative specifier', () => {
|
|
62
|
+
// The class of bug, not just the one instance: Node ESM resolves relative
|
|
63
|
+
// specifiers literally, so a missing `.js` is always a runtime failure.
|
|
64
|
+
const bad = []
|
|
65
|
+
for (const [, entry] of Object.entries(pkg.exports ?? {})) {
|
|
66
|
+
const target = typeof entry === 'string' ? entry : entry.import
|
|
67
|
+
if (typeof target !== 'string' || target.includes('*') || !target.endsWith('.js')) continue
|
|
68
|
+
const file = join(root, target)
|
|
69
|
+
if (!existsSync(file)) continue
|
|
70
|
+
for (const m of readFileSync(file, 'utf8').matchAll(/from\s+['"](\.[^'"]*)['"]/g)) {
|
|
71
|
+
if (!/\.(js|mjs|cjs|json|css)$/.test(m[1])) bad.push(`${target}: ${m[1]}`)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
if (bad.length) throw new Error(`extensionless relative imports:\n ${bad.join('\n ')}`)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
if (failed) {
|
|
78
|
+
console.error(`\n${failed} dist check(s) failed`)
|
|
79
|
+
process.exit(1)
|
|
80
|
+
}
|
|
81
|
+
console.log('dist: all checks passed')
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Preference contract, checked the way this package already checks tokens:
|
|
3
|
+
* a plain node script, no framework, run by `npm test`.
|
|
4
|
+
*
|
|
5
|
+
* The load-bearing check is the last one. The first version of preference.ts
|
|
6
|
+
* kept its own copy of the type ramp and the copy was WRONG (lg/xl were 16/18
|
|
7
|
+
* against the tokens' 15/17), so a preference of 1 — "leave it alone" — would
|
|
8
|
+
* have resized two rungs of the published design. Nothing caught it, because
|
|
9
|
+
* nothing compared the copy to the source. Now there is no copy: the knobs are
|
|
10
|
+
* multipliers and the ramps live only in tokens/*.css, and check 5 fails if a
|
|
11
|
+
* rung ever stops carrying its multiplier.
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
import { fileURLToPath } from "node:url";
|
|
15
|
+
import { dirname, join } from "node:path";
|
|
16
|
+
|
|
17
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
18
|
+
const root = join(here, "..");
|
|
19
|
+
|
|
20
|
+
// Load the source directly — this package ships tokens, and the preference
|
|
21
|
+
// module is small enough to evaluate without a build step in the check.
|
|
22
|
+
const src = readFileSync(join(root, "src/preference.ts"), "utf8");
|
|
23
|
+
const js = src
|
|
24
|
+
.replace(/^import[^\n]*\n/gm, "")
|
|
25
|
+
.replace(/export (type|interface) [\s\S]*?\n}\n/g, "")
|
|
26
|
+
.replace(/export type [^\n]*\n/g, "")
|
|
27
|
+
.replace(/: Record<[^>]*>/g, "")
|
|
28
|
+
.replace(/: Preference/g, "")
|
|
29
|
+
.replace(/: Density/g, "")
|
|
30
|
+
.replace(/: string/g, "")
|
|
31
|
+
.replace(/: number/g, "")
|
|
32
|
+
.replace(/: boolean/g, "")
|
|
33
|
+
.replace(/export /g, "");
|
|
34
|
+
|
|
35
|
+
const mod = new Function(`${js}; return { vars, css, isColor, TYPE_MIN, TYPE_MAX };`)();
|
|
36
|
+
const { vars, css, isColor, TYPE_MIN, TYPE_MAX } = mod;
|
|
37
|
+
|
|
38
|
+
let failed = 0;
|
|
39
|
+
const check = (name, fn) => {
|
|
40
|
+
try {
|
|
41
|
+
fn();
|
|
42
|
+
console.log(` ok ${name}`);
|
|
43
|
+
} catch (e) {
|
|
44
|
+
failed++;
|
|
45
|
+
console.error(` FAIL ${name}\n ${e.message}`);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
const eq = (a, b, m) => {
|
|
49
|
+
const A = JSON.stringify(a), B = JSON.stringify(b);
|
|
50
|
+
if (A !== B) throw new Error(`${m || ""} got ${A} want ${B}`);
|
|
51
|
+
};
|
|
52
|
+
const ok = (v, m) => { if (!v) throw new Error(m || "expected truthy"); };
|
|
53
|
+
|
|
54
|
+
console.log("preference:");
|
|
55
|
+
|
|
56
|
+
check("an unset preference changes nothing", () => {
|
|
57
|
+
eq(vars({}), {});
|
|
58
|
+
eq(css({}), "");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
check("type is ONE knob, not a restated ramp", () => {
|
|
62
|
+
const v = vars({ type: 1.2 });
|
|
63
|
+
eq(Object.keys(v), ["--type-scale"], "type must emit exactly one name:");
|
|
64
|
+
eq(v["--type-scale"], "1.2");
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
check("type is CLAMPED — a preference cannot make the UI illegible", () => {
|
|
68
|
+
eq(vars({ type: 0.1 })["--type-scale"], String(TYPE_MIN), "min");
|
|
69
|
+
eq(vars({ type: 99 })["--type-scale"], String(TYPE_MAX), "max");
|
|
70
|
+
// the smallest rung must stay readable at the floor: 11px * TYPE_MIN
|
|
71
|
+
ok(11 * TYPE_MIN >= 9, "xs falls below 9px at the floor");
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
check("density moves spacing only, and never touches type", () => {
|
|
75
|
+
const v = vars({ density: "compact" });
|
|
76
|
+
eq(Object.keys(v), ["--density"]);
|
|
77
|
+
ok(Number(v["--density"]) < 1, "compact should tighten");
|
|
78
|
+
ok(Number(vars({ density: "comfortable" })["--density"]) > 1, "comfortable should loosen");
|
|
79
|
+
ok(!("--type-scale" in v), "density must not touch type");
|
|
80
|
+
// Spacing compounds through nesting, so the range stays tight enough that a
|
|
81
|
+
// 44px coarse-pointer target does not fall under the floor.
|
|
82
|
+
ok(Number(vars({ density: "compact" })["--density"]) >= 0.8, "compact is too tight to stay tappable");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
check("EVERY ramp rung carries its multiplier — no rung can opt out", () => {
|
|
86
|
+
// This is the check that the drifting copy would have failed.
|
|
87
|
+
const type = readFileSync(join(root, "tokens/typography.css"), "utf8");
|
|
88
|
+
const space = readFileSync(join(root, "tokens/spacing.css"), "utf8");
|
|
89
|
+
|
|
90
|
+
const bare = [];
|
|
91
|
+
for (const [file, text, knob, re] of [
|
|
92
|
+
["typography.css", type, "--type-scale", /--text-[a-z0-9]+:\s*([^;]+);/g],
|
|
93
|
+
["spacing.css", space, "--density", /--space-\d+:\s*([^;]+);/g],
|
|
94
|
+
]) {
|
|
95
|
+
for (const m of text.matchAll(re)) {
|
|
96
|
+
const value = m[1].trim();
|
|
97
|
+
if (value === "0") continue; // zero times anything is zero
|
|
98
|
+
if (value.startsWith("var(")) continue; // an alias inherits its target's calc
|
|
99
|
+
if (!value.includes(`var(${knob}`)) bare.push(`${file}: ${m[0].trim()}`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
eq(bare, [], "rungs that do NOT scale with their knob:");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
check("a unitless leading stays a RATIO — scaling it would double-apply", () => {
|
|
106
|
+
const type = readFileSync(join(root, "tokens/typography.css"), "utf8");
|
|
107
|
+
const bad = [];
|
|
108
|
+
for (const m of type.matchAll(/--leading-[a-z0-9]+:\s*([^;]+);/g)) {
|
|
109
|
+
const v = m[1].trim();
|
|
110
|
+
// A ratio (`1.05`) already scales with the font size it multiplies.
|
|
111
|
+
if (/^[0-9.]+$/.test(v) === false) continue;
|
|
112
|
+
if (v.includes("var(")) bad.push(m[0].trim());
|
|
113
|
+
}
|
|
114
|
+
eq(bad, [], "unitless leadings must not carry a multiplier:");
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
check("a colour lands on both --primary and --accent", () => {
|
|
118
|
+
const v = vars({ accent: "#808000" });
|
|
119
|
+
eq(v["--primary"], "#808000");
|
|
120
|
+
eq(v["--accent"], "#808000");
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
check("a NON-colour is dropped, never sanitised into something plausible", () => {
|
|
124
|
+
for (const bad of [
|
|
125
|
+
"red; background-image:url(//evil/x)",
|
|
126
|
+
"}html{display:none",
|
|
127
|
+
"url(//evil/x)",
|
|
128
|
+
"expression(alert(1))",
|
|
129
|
+
"",
|
|
130
|
+
" ",
|
|
131
|
+
]) {
|
|
132
|
+
const v = vars({ accent: bad });
|
|
133
|
+
ok(!("--primary" in v), `accepted ${JSON.stringify(bad)}`);
|
|
134
|
+
ok(!isColor(bad), `isColor said yes to ${JSON.stringify(bad)}`);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
check("real colour notations are accepted", () => {
|
|
139
|
+
for (const good of ["#fff", "#ffffff", "#ffffffff", "rgb(1 2 3)", "oklch(.7 .1 120)", "rebeccapurple"]) {
|
|
140
|
+
ok(isColor(good), `isColor said no to ${good}`);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
check("css() emits ONE block for the selector it is given", () => {
|
|
145
|
+
const out = css({ type: 1.1, density: "compact", accent: "#fff" }, ":root");
|
|
146
|
+
ok(out.startsWith(":root{") && out.endsWith("}"), out.slice(0, 40));
|
|
147
|
+
ok(!out.includes("}" + "{"), "emitted more than one block");
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
check("every emitted name is one the token files actually read", () => {
|
|
151
|
+
// A variable nothing reads is a write into another document — the exact
|
|
152
|
+
// mistake this package exists to prevent. The knobs are READ by the ramps
|
|
153
|
+
// (as var(--knob, 1)); the colours are DECLARED by colors.css.
|
|
154
|
+
const files = ["tokens/typography.css", "tokens/spacing.css", "tokens/grid.css", "tokens/colors.css"];
|
|
155
|
+
const text = files.map((f) => readFileSync(join(root, f), "utf8")).join("\n");
|
|
156
|
+
const declared = new Set([...text.matchAll(/^\s*(--[a-z0-9-]+)\s*:/gm)].map((m) => m[1]));
|
|
157
|
+
const read = new Set([...text.matchAll(/var\((--[a-z0-9-]+)/g)].map((m) => m[1]));
|
|
158
|
+
|
|
159
|
+
const emitted = Object.keys(vars({ type: 1.1, density: "compact", accent: "#fff" }));
|
|
160
|
+
const orphans = emitted.filter((k) => !declared.has(k) && !read.has(k));
|
|
161
|
+
eq(orphans, [], "emitted names no token file declares or reads:");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
if (failed) {
|
|
165
|
+
console.error(`\n${failed} preference check(s) failed`);
|
|
166
|
+
process.exit(1);
|
|
167
|
+
}
|
|
168
|
+
console.log("preference: all checks passed");
|
package/scripts/check-tokens.mjs
CHANGED
|
@@ -140,6 +140,40 @@ const pass = (msg) => console.log(` ok ${msg}`)
|
|
|
140
140
|
: fail('base.css is UNLAYERED — its element rules outrank every utility an app writes')
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
// ── 1d. exactly ONE rule decides focus ───────────────────────────────────
|
|
144
|
+
// Until 0.4.9 there were two: a field rule that suppressed the outline and drew
|
|
145
|
+
// a brightened edge + halo, and the generic ring. Both computed to (0,1,0) —
|
|
146
|
+
// :where() zeroes its contents and each side keeps one pseudo-class — so the
|
|
147
|
+
// cascade fell through to SOURCE ORDER inside @layer base, the generic rule was
|
|
148
|
+
// written later, and it overrode the `outline:none` the field rule stated
|
|
149
|
+
// expressly to prevent it. Every focused input on every consumer drew BOTH.
|
|
150
|
+
// Each rule read as correct alone; the defect existed only in their order,
|
|
151
|
+
// which is why it survived review in both files.
|
|
152
|
+
//
|
|
153
|
+
// Order is not testable as intent, so this tests the property that replaced it:
|
|
154
|
+
// one rule paints the indicator, and nothing else touches focus. A second rule
|
|
155
|
+
// is how they disagree, `outline:none` is how an indicator disappears, and
|
|
156
|
+
// box-shadow is how a second one appears — none of the three can return quietly.
|
|
157
|
+
{
|
|
158
|
+
const base = strip(read(join(tokensDir, 'base.css')))
|
|
159
|
+
// `outline-offset` is a different property and never matches: the colon must
|
|
160
|
+
// follow `outline` itself.
|
|
161
|
+
const OUTLINE = /(?:^|[;{\s])outline\s*:\s*([^;}]+)/
|
|
162
|
+
const rules = [...base.matchAll(/([^{}]+)\{([^{}]*)\}/g)]
|
|
163
|
+
.map(([, sel, body]) => ({ sel: sel.trim().replace(/\s+/g, ' '), body }))
|
|
164
|
+
.filter(({ sel }) => sel.includes(':focus-visible'))
|
|
165
|
+
|
|
166
|
+
const paints = rules.filter(({ body }) => { const m = body.match(OUTLINE); return m && m[1].trim() !== 'none' })
|
|
167
|
+
const mutes = rules.filter(({ body }) => { const m = body.match(OUTLINE); return m && m[1].trim() === 'none' })
|
|
168
|
+
const halos = rules.filter(({ body }) => /(?:^|[;{\s])box-shadow\s*:/.test(body))
|
|
169
|
+
|
|
170
|
+
paints.length === 1
|
|
171
|
+
? pass(`one focus indicator, one rule: \`${paints[0].sel}\``)
|
|
172
|
+
: fail(`${paints.length} rules paint a focus outline (${paints.map((r) => r.sel).join(' / ')}) — equal specificity inside @layer base, so source order decides which one a user actually sees`)
|
|
173
|
+
mutes.forEach(({ sel }) => fail(`\`${sel}\` sets outline:none — it removes the focus indicator instead of replacing it`))
|
|
174
|
+
halos.forEach(({ sel }) => fail(`\`${sel}\` adds a box-shadow on focus — a second indicator beside the ring`))
|
|
175
|
+
}
|
|
176
|
+
|
|
143
177
|
// ── 2. every var() used inside the token layer must resolve ──────────────
|
|
144
178
|
{
|
|
145
179
|
const declared = new Set()
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: design-system
|
|
3
3
|
description: "Use whenever you generate, edit, or review a Hanzo user interface — a page, component, screen, email, or any code change touching colour, type, spacing, elevation, motion, or stacking order. Use it when PLANNING UI work too, so the plan names tokens rather than values. Teaches the Hanzo token layer (@hanzo/design, derived from @hanzo/brand plus @hanzo/logo) and runs the linter that proves generated code actually reaches it. Triggers — build a page, add a component, style, theme, dark mode, colour, hex, palette, font size, spacing, padding, z-index, modal, dropdown, toast, button, card, dialog, icon, make it look Hanzo, brand, design review."
|
|
4
|
-
license:
|
|
4
|
+
license: MIT OR Apache-2.0
|
|
5
5
|
---
|
|
6
6
|
|
|
7
7
|
# The Hanzo design system
|
package/src/index.ts
CHANGED
|
@@ -63,3 +63,9 @@ export function injectDesignCss(href: string): void {
|
|
|
63
63
|
l.setAttribute('data-hanzo-design', '')
|
|
64
64
|
document.head.appendChild(l)
|
|
65
65
|
}
|
|
66
|
+
|
|
67
|
+
// A person's own reading of the system — type size, density, accent — as CSS
|
|
68
|
+
// custom properties. Pure: it maps a preference to variables and returns them,
|
|
69
|
+
// so an app, an embedded preview and a server render all apply it the same way.
|
|
70
|
+
export { vars, css, isColor, TYPE_MIN, TYPE_MAX } from './preference.js';
|
|
71
|
+
export type { Preference, Density } from './preference.js';
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A person's own reading of the system: type size, density, accent.
|
|
3
|
+
*
|
|
4
|
+
* Three knobs, and each is ONE multiplier on a whole axis — never a restated
|
|
5
|
+
* ramp. The ramps live in `tokens/*.css`, authored once, and each rung carries
|
|
6
|
+
* its own `calc(<base> * var(--type-scale, 1))`. So a preference sets three
|
|
7
|
+
* numbers and every rung follows, including rungs added later and rungs this
|
|
8
|
+
* file has never heard of.
|
|
9
|
+
*
|
|
10
|
+
* That is not a style choice; it is the fix for a real bug. The first version of
|
|
11
|
+
* this module kept its own copy of the type ramp so it could recompute each
|
|
12
|
+
* rung, and the copy was WRONG — it had `lg: 1rem` and `xl: 1.125rem` (16px and
|
|
13
|
+
* 18px) while `tokens/typography.css` says `0.9375rem` and `1.0625rem` (15px and
|
|
14
|
+
* 17px). Setting a preference of 1 — "leave it alone" — would have silently
|
|
15
|
+
* resized two rungs of the published design. A second copy of a value is a
|
|
16
|
+
* second source of truth, and it drifted before anyone used it.
|
|
17
|
+
*
|
|
18
|
+
* Because the knobs are plain multipliers, any OTHER ramp can opt in the same
|
|
19
|
+
* way. @hanzo/gui compiles its own `--f-size-*` scale for the 1600-odd
|
|
20
|
+
* `fontSize="$n"` call sites in the apps; an app that redeclares those as
|
|
21
|
+
* `calc(<its px> * var(--type-scale, 1))` gets the same control with no change
|
|
22
|
+
* at scale 1.
|
|
23
|
+
*
|
|
24
|
+
* It is a pure function on purpose: it maps a preference to custom properties
|
|
25
|
+
* and returns them, touching no document. That is what lets an app, an embedded
|
|
26
|
+
* preview and a server render apply it identically.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
export type Density = "compact" | "default" | "comfortable";
|
|
30
|
+
|
|
31
|
+
export interface Preference {
|
|
32
|
+
/** Multiplier on the type ramp. 1 is the published scale. */
|
|
33
|
+
type?: number;
|
|
34
|
+
density?: Density;
|
|
35
|
+
/** A CSS colour for --primary / --accent. Rejected unless it is one. */
|
|
36
|
+
accent?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* The type multiplier is CLAMPED, and the bounds are not arbitrary.
|
|
41
|
+
*
|
|
42
|
+
* Below 0.85 the smallest rung (--text-xs, 11px) drops under 9.4px, which stops
|
|
43
|
+
* being small and starts being unreadable — and a preference that lets someone
|
|
44
|
+
* render their own tools illegible is a trap, not a choice. Above 1.4 the
|
|
45
|
+
* chrome stops fitting its own containers: this app's builder header already
|
|
46
|
+
* overlaps its actions below 1440px at scale 1.
|
|
47
|
+
*/
|
|
48
|
+
export const TYPE_MIN = 0.85;
|
|
49
|
+
export const TYPE_MAX = 1.4;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Density moves SPACING only, and its range is much tighter than type's.
|
|
53
|
+
*
|
|
54
|
+
* Spacing compounds: a page nests padding inside gap inside margin, so a 0.75
|
|
55
|
+
* multiplier is already three-quarters of every one of those in sequence. Below
|
|
56
|
+
* that, touch targets fall under the 44px floor `base.css` sets for coarse
|
|
57
|
+
* pointers, and the control that promised comfort takes it away.
|
|
58
|
+
*/
|
|
59
|
+
const DENSITY: Record<Density, number> = {
|
|
60
|
+
compact: 0.85,
|
|
61
|
+
default: 1,
|
|
62
|
+
comfortable: 1.15,
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const clamp = (n: number, lo: number, hi: number) => Math.min(hi, Math.max(lo, n));
|
|
66
|
+
|
|
67
|
+
/** Trim to 4dp so a multiplier cannot emit a 17-digit float into a stylesheet. */
|
|
68
|
+
const round = (n: number) => String(Math.round(n * 10000) / 10000);
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Is this a colour, or is it something being smuggled into a style attribute?
|
|
72
|
+
*
|
|
73
|
+
* A preference is user input and its destination is CSS. `#fff`, `rgb(...)`,
|
|
74
|
+
* `oklch(...)` and the bare keywords are colours; anything carrying a `;`, a
|
|
75
|
+
* `}`, or a `url(` is trying to be a second declaration, and the answer is to
|
|
76
|
+
* drop the axis rather than to sanitise a string into something plausible.
|
|
77
|
+
*/
|
|
78
|
+
export function isColor(v: string): boolean {
|
|
79
|
+
const s = v.trim();
|
|
80
|
+
if (!s || s.length > 64) return false;
|
|
81
|
+
if (/[;{}()]/.test(s) && !/^(rgb|rgba|hsl|hsla|oklch|oklab|lab|lch|color)\([^;{}]*\)$/i.test(s)) return false;
|
|
82
|
+
return (
|
|
83
|
+
/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(s) ||
|
|
84
|
+
/^(rgb|rgba|hsl|hsla|oklch|oklab|lab|lch|color)\([^;{}]*\)$/i.test(s) ||
|
|
85
|
+
/^[a-z]{3,20}$/i.test(s)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The custom properties a preference produces.
|
|
91
|
+
*
|
|
92
|
+
* Only the axes actually set appear, so an app can spread the result over
|
|
93
|
+
* whatever it already has without a default silently overriding a brand.
|
|
94
|
+
*/
|
|
95
|
+
export function vars(p: Preference): Record<string, string> {
|
|
96
|
+
const out: Record<string, string> = {};
|
|
97
|
+
|
|
98
|
+
if (typeof p.type === "number" && Number.isFinite(p.type)) {
|
|
99
|
+
out["--type-scale"] = round(clamp(p.type, TYPE_MIN, TYPE_MAX));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (p.density && p.density in DENSITY) {
|
|
103
|
+
out["--density"] = round(DENSITY[p.density]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (p.accent && isColor(p.accent)) {
|
|
107
|
+
// Both names, because the ramp uses --primary for action surfaces and
|
|
108
|
+
// --accent for selection. One hue, stated once, landing on both.
|
|
109
|
+
out["--primary"] = p.accent.trim();
|
|
110
|
+
out["--accent"] = p.accent.trim();
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return out;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** `vars()` as a declaration block, for a <style> tag or an SSR inline. */
|
|
117
|
+
export function css(p: Preference, selector = "html:root"): string {
|
|
118
|
+
const v = vars(p);
|
|
119
|
+
const keys = Object.keys(v);
|
|
120
|
+
if (!keys.length) return "";
|
|
121
|
+
return `${selector}{${keys.map((k) => `${k}:${v[k]}`).join(";")}}`;
|
|
122
|
+
}
|
package/src/tokens.gen.ts
CHANGED
|
@@ -96,31 +96,32 @@ export const colors = {
|
|
|
96
96
|
|
|
97
97
|
/** typography tokens (from tokens/typography.css). Values are raw CSS. */
|
|
98
98
|
export const typography = {
|
|
99
|
-
'
|
|
100
|
-
'
|
|
101
|
-
'
|
|
102
|
-
'
|
|
103
|
-
'
|
|
104
|
-
'
|
|
105
|
-
'
|
|
106
|
-
'
|
|
107
|
-
'
|
|
108
|
-
'
|
|
109
|
-
'
|
|
110
|
-
'
|
|
111
|
-
'
|
|
112
|
-
'
|
|
113
|
-
'
|
|
114
|
-
'
|
|
115
|
-
'
|
|
99
|
+
'type-scale': '1',
|
|
100
|
+
'text-xs': 'calc(0.6875rem * var(--type-scale, 1))',
|
|
101
|
+
'leading-xs': 'calc(1rem * var(--type-scale, 1))',
|
|
102
|
+
'text-sm': 'calc(0.8125rem * var(--type-scale, 1))',
|
|
103
|
+
'leading-sm': 'calc(1.15rem * var(--type-scale, 1))',
|
|
104
|
+
'text-base': 'calc(0.875rem * var(--type-scale, 1))',
|
|
105
|
+
'leading-base': 'calc(1.35rem * var(--type-scale, 1))',
|
|
106
|
+
'text-lg': 'calc(0.9375rem * var(--type-scale, 1))',
|
|
107
|
+
'leading-lg': 'calc(1.4rem * var(--type-scale, 1))',
|
|
108
|
+
'text-xl': 'calc(1.0625rem * var(--type-scale, 1))',
|
|
109
|
+
'leading-xl': 'calc(1.55rem * var(--type-scale, 1))',
|
|
110
|
+
'text-2xl': 'calc(1.3125rem * var(--type-scale, 1))',
|
|
111
|
+
'leading-2xl': 'calc(1.7rem * var(--type-scale, 1))',
|
|
112
|
+
'text-3xl': 'calc(1.625rem * var(--type-scale, 1))',
|
|
113
|
+
'leading-3xl': 'calc(1.95rem * var(--type-scale, 1))',
|
|
114
|
+
'text-4xl': 'calc(2rem * var(--type-scale, 1))',
|
|
115
|
+
'leading-4xl': 'calc(2.25rem * var(--type-scale, 1))',
|
|
116
|
+
'text-5xl': 'calc(2.5rem * var(--type-scale, 1))',
|
|
116
117
|
'leading-5xl': '1.05',
|
|
117
|
-
'text-6xl': '3.25rem',
|
|
118
|
+
'text-6xl': 'calc(3.25rem * var(--type-scale, 1))',
|
|
118
119
|
'leading-6xl': '1',
|
|
119
|
-
'text-7xl': '4rem',
|
|
120
|
+
'text-7xl': 'calc(4rem * var(--type-scale, 1))',
|
|
120
121
|
'leading-7xl': '1',
|
|
121
|
-
'text-8xl': '5.25rem',
|
|
122
|
+
'text-8xl': 'calc(5.25rem * var(--type-scale, 1))',
|
|
122
123
|
'leading-8xl': '1',
|
|
123
|
-
'text-9xl': '7rem',
|
|
124
|
+
'text-9xl': 'calc(7rem * var(--type-scale, 1))',
|
|
124
125
|
'leading-9xl': '1',
|
|
125
126
|
'font-size-xs': 'var(--text-xs)',
|
|
126
127
|
'font-size-sm': 'var(--text-sm)',
|
|
@@ -162,21 +163,22 @@ export const typography = {
|
|
|
162
163
|
|
|
163
164
|
/** spacing tokens (from tokens/spacing.css). Values are raw CSS. */
|
|
164
165
|
export const spacing = {
|
|
166
|
+
'density': '1',
|
|
165
167
|
'space-0': '0',
|
|
166
|
-
'space-1': '0.25rem',
|
|
167
|
-
'space-2': '0.5rem',
|
|
168
|
-
'space-3': '0.75rem',
|
|
169
|
-
'space-4': '1rem',
|
|
170
|
-
'space-5': '1.25rem',
|
|
171
|
-
'space-6': '1.5rem',
|
|
172
|
-
'space-8': '2rem',
|
|
173
|
-
'space-10': '2.5rem',
|
|
174
|
-
'space-12': '3rem',
|
|
175
|
-
'space-14': '3.5rem',
|
|
176
|
-
'space-16': '4rem',
|
|
177
|
-
'space-20': '5rem',
|
|
178
|
-
'space-24': '6rem',
|
|
179
|
-
'space-32': '8rem',
|
|
168
|
+
'space-1': 'calc(0.25rem * var(--density, 1))',
|
|
169
|
+
'space-2': 'calc(0.5rem * var(--density, 1))',
|
|
170
|
+
'space-3': 'calc(0.75rem * var(--density, 1))',
|
|
171
|
+
'space-4': 'calc(1rem * var(--density, 1))',
|
|
172
|
+
'space-5': 'calc(1.25rem * var(--density, 1))',
|
|
173
|
+
'space-6': 'calc(1.5rem * var(--density, 1))',
|
|
174
|
+
'space-8': 'calc(2rem * var(--density, 1))',
|
|
175
|
+
'space-10': 'calc(2.5rem * var(--density, 1))',
|
|
176
|
+
'space-12': 'calc(3rem * var(--density, 1))',
|
|
177
|
+
'space-14': 'calc(3.5rem * var(--density, 1))',
|
|
178
|
+
'space-16': 'calc(4rem * var(--density, 1))',
|
|
179
|
+
'space-20': 'calc(5rem * var(--density, 1))',
|
|
180
|
+
'space-24': 'calc(6rem * var(--density, 1))',
|
|
181
|
+
'space-32': 'calc(8rem * var(--density, 1))',
|
|
180
182
|
'golden-1': '0.25rem',
|
|
181
183
|
'golden-2': '0.405rem',
|
|
182
184
|
'golden-3': '0.654rem',
|
|
@@ -390,31 +392,32 @@ export const cssVars = {
|
|
|
390
392
|
'--chrome-dot-red': 'rgb(239 68 68 / .6)',
|
|
391
393
|
'--chrome-dot-yellow': 'rgb(234 179 8 / .6)',
|
|
392
394
|
'--chrome-dot-green': 'rgb(34 197 94 / .6)',
|
|
393
|
-
'--
|
|
394
|
-
'--
|
|
395
|
-
'--
|
|
396
|
-
'--
|
|
397
|
-
'--
|
|
398
|
-
'--
|
|
399
|
-
'--
|
|
400
|
-
'--
|
|
401
|
-
'--
|
|
402
|
-
'--
|
|
403
|
-
'--
|
|
404
|
-
'--
|
|
405
|
-
'--
|
|
406
|
-
'--
|
|
407
|
-
'--
|
|
408
|
-
'--
|
|
409
|
-
'--
|
|
395
|
+
'--type-scale': '1',
|
|
396
|
+
'--text-xs': 'calc(0.6875rem * var(--type-scale, 1))',
|
|
397
|
+
'--leading-xs': 'calc(1rem * var(--type-scale, 1))',
|
|
398
|
+
'--text-sm': 'calc(0.8125rem * var(--type-scale, 1))',
|
|
399
|
+
'--leading-sm': 'calc(1.15rem * var(--type-scale, 1))',
|
|
400
|
+
'--text-base': 'calc(0.875rem * var(--type-scale, 1))',
|
|
401
|
+
'--leading-base': 'calc(1.35rem * var(--type-scale, 1))',
|
|
402
|
+
'--text-lg': 'calc(0.9375rem * var(--type-scale, 1))',
|
|
403
|
+
'--leading-lg': 'calc(1.4rem * var(--type-scale, 1))',
|
|
404
|
+
'--text-xl': 'calc(1.0625rem * var(--type-scale, 1))',
|
|
405
|
+
'--leading-xl': 'calc(1.55rem * var(--type-scale, 1))',
|
|
406
|
+
'--text-2xl': 'calc(1.3125rem * var(--type-scale, 1))',
|
|
407
|
+
'--leading-2xl': 'calc(1.7rem * var(--type-scale, 1))',
|
|
408
|
+
'--text-3xl': 'calc(1.625rem * var(--type-scale, 1))',
|
|
409
|
+
'--leading-3xl': 'calc(1.95rem * var(--type-scale, 1))',
|
|
410
|
+
'--text-4xl': 'calc(2rem * var(--type-scale, 1))',
|
|
411
|
+
'--leading-4xl': 'calc(2.25rem * var(--type-scale, 1))',
|
|
412
|
+
'--text-5xl': 'calc(2.5rem * var(--type-scale, 1))',
|
|
410
413
|
'--leading-5xl': '1.05',
|
|
411
|
-
'--text-6xl': '3.25rem',
|
|
414
|
+
'--text-6xl': 'calc(3.25rem * var(--type-scale, 1))',
|
|
412
415
|
'--leading-6xl': '1',
|
|
413
|
-
'--text-7xl': '4rem',
|
|
416
|
+
'--text-7xl': 'calc(4rem * var(--type-scale, 1))',
|
|
414
417
|
'--leading-7xl': '1',
|
|
415
|
-
'--text-8xl': '5.25rem',
|
|
418
|
+
'--text-8xl': 'calc(5.25rem * var(--type-scale, 1))',
|
|
416
419
|
'--leading-8xl': '1',
|
|
417
|
-
'--text-9xl': '7rem',
|
|
420
|
+
'--text-9xl': 'calc(7rem * var(--type-scale, 1))',
|
|
418
421
|
'--leading-9xl': '1',
|
|
419
422
|
'--font-size-xs': 'var(--text-xs)',
|
|
420
423
|
'--font-size-sm': 'var(--text-sm)',
|
|
@@ -452,21 +455,22 @@ export const cssVars = {
|
|
|
452
455
|
'--type-caption': '400 var(--text-xs)/var(--leading-xs) var(--font-sans)',
|
|
453
456
|
'--type-code': '400 var(--text-sm)/var(--leading-relaxed) var(--font-mono)',
|
|
454
457
|
'--type-eyebrow': '600 0.625rem/1 var(--font-sans)',
|
|
458
|
+
'--density': '1',
|
|
455
459
|
'--space-0': '0',
|
|
456
|
-
'--space-1': '0.25rem',
|
|
457
|
-
'--space-2': '0.5rem',
|
|
458
|
-
'--space-3': '0.75rem',
|
|
459
|
-
'--space-4': '1rem',
|
|
460
|
-
'--space-5': '1.25rem',
|
|
461
|
-
'--space-6': '1.5rem',
|
|
462
|
-
'--space-8': '2rem',
|
|
463
|
-
'--space-10': '2.5rem',
|
|
464
|
-
'--space-12': '3rem',
|
|
465
|
-
'--space-14': '3.5rem',
|
|
466
|
-
'--space-16': '4rem',
|
|
467
|
-
'--space-20': '5rem',
|
|
468
|
-
'--space-24': '6rem',
|
|
469
|
-
'--space-32': '8rem',
|
|
460
|
+
'--space-1': 'calc(0.25rem * var(--density, 1))',
|
|
461
|
+
'--space-2': 'calc(0.5rem * var(--density, 1))',
|
|
462
|
+
'--space-3': 'calc(0.75rem * var(--density, 1))',
|
|
463
|
+
'--space-4': 'calc(1rem * var(--density, 1))',
|
|
464
|
+
'--space-5': 'calc(1.25rem * var(--density, 1))',
|
|
465
|
+
'--space-6': 'calc(1.5rem * var(--density, 1))',
|
|
466
|
+
'--space-8': 'calc(2rem * var(--density, 1))',
|
|
467
|
+
'--space-10': 'calc(2.5rem * var(--density, 1))',
|
|
468
|
+
'--space-12': 'calc(3rem * var(--density, 1))',
|
|
469
|
+
'--space-14': 'calc(3.5rem * var(--density, 1))',
|
|
470
|
+
'--space-16': 'calc(4rem * var(--density, 1))',
|
|
471
|
+
'--space-20': 'calc(5rem * var(--density, 1))',
|
|
472
|
+
'--space-24': 'calc(6rem * var(--density, 1))',
|
|
473
|
+
'--space-32': 'calc(8rem * var(--density, 1))',
|
|
470
474
|
'--golden-1': '0.25rem',
|
|
471
475
|
'--golden-2': '0.405rem',
|
|
472
476
|
'--golden-3': '0.654rem',
|