pi-pocket 0.26.0 → 0.27.0-beta.2
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/CONTRIBUTING.md +1 -1
- package/docs/design/design-system.md +165 -0
- package/docs/design/ds-notes-b.md +61 -0
- package/docs/design/ds-notes-g.md +170 -0
- package/docs/design/ds-notes-t.md +303 -0
- package/docs/design/ds-notes-u.md +220 -0
- package/docs/design/ds-notes-v.md +186 -0
- package/docs/design/ds-rounds.md +160 -0
- package/docs/design/ds-u-bridge.patch +732 -0
- package/docs/examples/design-system/README.md +15 -0
- package/docs/examples/design-system/pocket/.pi/design/components/badge.html +112 -0
- package/docs/examples/design-system/pocket/.pi/design/components/button.html +203 -0
- package/docs/examples/design-system/pocket/.pi/design/components/card.html +125 -0
- package/docs/examples/design-system/pocket/.pi/design/components/code-block.html +155 -0
- package/docs/examples/design-system/pocket/.pi/design/components/input.html +156 -0
- package/docs/examples/design-system/pocket/.pi/design/components/menu-row.html +167 -0
- package/docs/examples/design-system/pocket/.pi/design/components/notice.html +171 -0
- package/docs/examples/design-system/pocket/.pi/design/components/segmented.html +120 -0
- package/docs/examples/design-system/pocket/.pi/design/components/switch.html +117 -0
- package/docs/examples/design-system/pocket/.pi/design/components/tool-step.html +163 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/components.css +730 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/names.md +495 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tailwind.css +1132 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.css +1126 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.dark.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.high-contrast.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.light.resolved.json +2022 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.resolved.json +371 -0
- package/docs/examples/design-system/pocket/.pi/design/dist/tokens.studio.json +5915 -0
- package/docs/examples/design-system/pocket/.pi/design/guidelines.md +81 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.dark.json +182 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.high-contrast.json +185 -0
- package/docs/examples/design-system/pocket/.pi/design/tokens.json +2034 -0
- package/docs/features.md +6 -2
- package/docs/index.md +1 -0
- package/docs/map.md +34 -25
- package/docs/release-notes.md +1 -1
- package/docs/releases.md +45 -0
- package/package.json +2 -1
- package/src/server/design/check-cli.ts +283 -0
- package/src/server/design/color.ts +654 -0
- package/src/server/design/components.ts +328 -0
- package/src/server/design/docsite-assets.ts +29 -0
- package/src/server/design/docsite-color-section.ts +366 -0
- package/src/server/design/docsite-color.ts +303 -0
- package/src/server/design/docsite-components.ts +89 -0
- package/src/server/design/docsite-core.ts +634 -0
- package/src/server/design/docsite-css-base.ts +397 -0
- package/src/server/design/docsite-css-content.ts +477 -0
- package/src/server/design/docsite-css-phone.ts +251 -0
- package/src/server/design/docsite-css-reference.ts +374 -0
- package/src/server/design/docsite-css-specimens.ts +465 -0
- package/src/server/design/docsite-js-code.ts +96 -0
- package/src/server/design/docsite-js-core.ts +72 -0
- package/src/server/design/docsite-js-demos.ts +24 -0
- package/src/server/design/docsite-js-links.ts +25 -0
- package/src/server/design/docsite-js-nav.ts +73 -0
- package/src/server/design/docsite-js-stages.ts +81 -0
- package/src/server/design/docsite-js-table.ts +81 -0
- package/src/server/design/docsite-markdown.ts +221 -0
- package/src/server/design/docsite-motion.ts +110 -0
- package/src/server/design/docsite-reference.ts +269 -0
- package/src/server/design/docsite-sanitize.ts +193 -0
- package/src/server/design/docsite-shadows.ts +45 -0
- package/src/server/design/docsite-space.ts +289 -0
- package/src/server/design/docsite-type.ts +167 -0
- package/src/server/design/docsite.ts +508 -0
- package/src/server/design/export.ts +86 -9
- package/src/server/design/fix.ts +666 -0
- package/src/server/design/guide.ts +137 -22
- package/src/server/design/index.ts +368 -15
- package/src/server/design/lint-color.ts +293 -0
- package/src/server/design/lint-page-checks.ts +204 -0
- package/src/server/design/lint-page-elements.ts +252 -0
- package/src/server/design/lint-page-focus.ts +165 -0
- package/src/server/design/lint-page-literals.ts +293 -0
- package/src/server/design/lint-page-setup.ts +102 -0
- package/src/server/design/lint-page-states.ts +121 -0
- package/src/server/design/lint-page-values.ts +329 -0
- package/src/server/design/lint-page.ts +101 -0
- package/src/server/design/lint-suggest.ts +340 -0
- package/src/server/design/lint.ts +1136 -0
- package/src/server/design/render.ts +369 -18
- package/src/server/design/scan-braces.ts +115 -0
- package/src/server/design/scan-categorize.ts +187 -0
- package/src/server/design/scan-channels.ts +113 -0
- package/src/server/design/scan-color-base.ts +209 -0
- package/src/server/design/scan-color-names.ts +403 -0
- package/src/server/design/scan-color-palette.ts +91 -0
- package/src/server/design/scan-color-roles.ts +565 -0
- package/src/server/design/scan-colors.ts +22 -0
- package/src/server/design/scan-facts-antd.ts +171 -0
- package/src/server/design/scan-facts-files.ts +300 -0
- package/src/server/design/scan-facts-lists.ts +538 -0
- package/src/server/design/scan-facts-sass.ts +113 -0
- package/src/server/design/scan-facts-theme.ts +400 -0
- package/src/server/design/scan-facts-use.ts +674 -0
- package/src/server/design/scan-facts-wordpress.ts +374 -0
- package/src/server/design/scan-facts.ts +285 -0
- package/src/server/design/scan-files.ts +384 -0
- package/src/server/design/scan-import-convert.ts +277 -0
- package/src/server/design/scan-import-eval.ts +583 -0
- package/src/server/design/scan-import-material.ts +745 -0
- package/src/server/design/scan-import-sd-config.ts +370 -0
- package/src/server/design/scan-import-sd.ts +381 -0
- package/src/server/design/scan-import-studio.ts +242 -0
- package/src/server/design/scan-import.ts +336 -0
- package/src/server/design/scan-infer.ts +313 -0
- package/src/server/design/scan-js-eval.ts +1267 -0
- package/src/server/design/scan-palette.ts +485 -0
- package/src/server/design/scan-parse.ts +1134 -0
- package/src/server/design/scan-prepare.ts +402 -0
- package/src/server/design/scan-render-pages.ts +367 -0
- package/src/server/design/scan-render-probe.ts +229 -0
- package/src/server/design/scan-render.ts +524 -0
- package/src/server/design/scan-review.ts +54 -0
- package/src/server/design/scan-roles.ts +259 -0
- package/src/server/design/scan-sass-colors.ts +275 -0
- package/src/server/design/scan-sass-eval.ts +269 -0
- package/src/server/design/scan-sass-expr.ts +911 -0
- package/src/server/design/scan-sass-modules.ts +353 -0
- package/src/server/design/scan-sass-run.ts +1003 -0
- package/src/server/design/scan-sass-stmt.ts +256 -0
- package/src/server/design/scan-sass-values.ts +886 -0
- package/src/server/design/scan-scales.ts +983 -0
- package/src/server/design/scan-tailwind.ts +34 -0
- package/src/server/design/scan-theme-shapes.ts +579 -0
- package/src/server/design/scan-tree.ts +290 -0
- package/src/server/design/scan-util.ts +44 -0
- package/src/server/design/scan-variables.ts +470 -0
- package/src/server/design/scan.ts +703 -0
- package/src/server/design/system-schema.ts +68 -0
- package/src/server/design/system-types.ts +279 -0
- package/src/server/design/system.ts +510 -82
- package/src/server/design/tokens-base.ts +123 -0
- package/src/server/design/tokens-compile.ts +440 -0
- package/src/server/design/tokens-dtcg.ts +354 -0
- package/src/server/design/tokens-io.ts +417 -0
- package/src/server/design/tokens-model.ts +1163 -0
- package/src/server/design/tokens-names.ts +112 -0
- package/src/server/design/tokens-ramps.ts +112 -0
- package/src/server/design/tokens-summary.ts +317 -0
- package/src/server/design/tokens-tailwind.ts +324 -0
- package/src/server/design/tokens-values.ts +921 -0
- package/src/server/design/tokens.ts +533 -0
- package/src/server/docs.ts +8 -0
- package/src/server/extensions/artifacts.ts +87 -2
- package/src/server/extensions/design/app.js +280 -0
- package/src/server/extensions/design/canvas.js +142 -17
- package/src/server/extensions/design/check.js +656 -0
- package/src/server/extensions/design/color.js +383 -0
- package/src/server/extensions/design/data.js +102 -7
- package/src/server/extensions/design/devices.js +26 -0
- package/src/server/extensions/design/docks.js +117 -3
- package/src/server/extensions/design/index.js +171 -219
- package/src/server/extensions/design/messages.js +49 -0
- package/src/server/extensions/design/parts.js +15 -1
- package/src/server/extensions/design/paths.js +4 -0
- package/src/server/extensions/design/picker.js +123 -0
- package/src/server/extensions/design/specimen.js +55 -0
- package/src/server/extensions/design/state.js +72 -7
- package/src/server/extensions/design/style.css +546 -0
- package/src/server/extensions/design/system-actions.js +214 -0
- package/src/server/extensions/design/system-api.ts +62 -0
- package/src/server/extensions/design/system-data.js +315 -0
- package/src/server/extensions/design/system-preview.js +41 -0
- package/src/server/extensions/design/system-routes.ts +1122 -0
- package/src/server/extensions/design/system-sections.js +1344 -0
- package/src/server/extensions/design/system-ui.js +32 -0
- package/src/server/extensions/design/system-view.js +1196 -0
- package/src/server/extensions/design/system-widgets.js +942 -0
- package/src/server/extensions/design/system.css +2313 -0
- package/src/server/extensions/design/system.js +1479 -0
- package/src/server/extensions/design/unsaved.js +20 -0
- package/src/server/extensions/design/views.js +2 -2
- package/src/server/extensions/design/workspace.js +200 -105
- package/src/server/extensions/design.ts +89 -6
- package/src/server/extensions/updates/index.js +31 -9
- package/src/server/extensions/updates/style.css +11 -0
- package/src/server/extensions/updates.ts +29 -1
- package/src/server/host.ts +4 -1
- package/src/server/http/artifact-inspect.ts +338 -24
- package/src/server/updates.ts +235 -53
- package/src/server/versions.ts +71 -0
- package/web/artifact-bridge.js +34 -1
- package/npm-shrinkwrap.json +0 -3162
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The shared pieces of the tokens engine (stream T): the limits, the names, and the small helpers that the token
|
|
3
|
+
* model, the value converters, and the writer use.
|
|
4
|
+
*/
|
|
5
|
+
import type { SystemProblem, TokenType } from "./system-types.ts";
|
|
6
|
+
|
|
7
|
+
/** The key of Pi Pocket's `$extensions` block. */
|
|
8
|
+
export const EXT = "com.pi-pocket";
|
|
9
|
+
|
|
10
|
+
export const TOKEN_TYPES: readonly TokenType[] = [
|
|
11
|
+
"color",
|
|
12
|
+
"dimension",
|
|
13
|
+
"fontFamily",
|
|
14
|
+
"fontWeight",
|
|
15
|
+
"duration",
|
|
16
|
+
"cubicBezier",
|
|
17
|
+
"number",
|
|
18
|
+
"shadow",
|
|
19
|
+
"typography",
|
|
20
|
+
"border",
|
|
21
|
+
"transition",
|
|
22
|
+
"gradient",
|
|
23
|
+
"strokeStyle",
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
/** What the engine reads and keeps, so that a stray file or a hostile one stays cheap. */
|
|
27
|
+
export const LIMITS = {
|
|
28
|
+
/** One token file. */
|
|
29
|
+
tokenFileBytes: 524_288,
|
|
30
|
+
/** Mode files. */
|
|
31
|
+
modes: 16,
|
|
32
|
+
/** Tokens in the default file. */
|
|
33
|
+
tokens: 5000,
|
|
34
|
+
/** Nesting of groups. */
|
|
35
|
+
depth: 12,
|
|
36
|
+
/** The text of one CSS value. */
|
|
37
|
+
valueChars: 400,
|
|
38
|
+
/** One component file, and how many. */
|
|
39
|
+
componentBytes: 131_072,
|
|
40
|
+
components: 100,
|
|
41
|
+
/** `guidelines.md`. */
|
|
42
|
+
guidelinesBytes: 16_384,
|
|
43
|
+
/** Problems kept in a report. */
|
|
44
|
+
problems: 200,
|
|
45
|
+
} as const;
|
|
46
|
+
|
|
47
|
+
export const MODE_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
|
|
48
|
+
export const NAME = /^[A-Za-z0-9_-]{1,64}$/;
|
|
49
|
+
export const RESERVED = new Set(["__proto__", "constructor", "prototype"]);
|
|
50
|
+
|
|
51
|
+
/** The parsed files of a design system folder, before any check. */
|
|
52
|
+
export interface RawSystem {
|
|
53
|
+
/** The session folder's name: the system's name when `tokens.json` gives none. */
|
|
54
|
+
folder: string;
|
|
55
|
+
/** `tokens.json`, parsed; undefined when there is none. */
|
|
56
|
+
tokens: unknown;
|
|
57
|
+
/** `tokens.<mode>.json`, parsed, by mode name. */
|
|
58
|
+
modes: Map<string, unknown>;
|
|
59
|
+
/** The component files' text, by slug. */
|
|
60
|
+
components: Map<string, string>;
|
|
61
|
+
guidelines: string;
|
|
62
|
+
/** Problems met while reading the files (a file too large, bad JSON). */
|
|
63
|
+
problems: SystemProblem[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type Obj = Record<string, unknown>;
|
|
67
|
+
|
|
68
|
+
export const isObject = (value: unknown): value is Obj =>
|
|
69
|
+
typeof value === "object" && value !== null && !Array.isArray(value);
|
|
70
|
+
export const clip = (text: string, max = 60) =>
|
|
71
|
+
text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
72
|
+
/** A value for a message: a string in quotes, anything else as JSON, cut short. */
|
|
73
|
+
export const show = (value: unknown) => clip(JSON.stringify(value) ?? String(value));
|
|
74
|
+
export const plain = (text: string) =>
|
|
75
|
+
text
|
|
76
|
+
.replace(/[\p{Cc}\p{Cf}\p{Zl}\p{Zp}]+/gu, " ")
|
|
77
|
+
.replace(/\s+/g, " ")
|
|
78
|
+
.trim();
|
|
79
|
+
|
|
80
|
+
/** The `com.pi-pocket` block of a node's `$extensions`, or an empty object. */
|
|
81
|
+
export const extensionOf = (node: Obj): Obj => {
|
|
82
|
+
const extensions = node.$extensions;
|
|
83
|
+
|
|
84
|
+
return isObject(extensions) && isObject(extensions[EXT]) ? extensions[EXT] : {};
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Where `end` leaves a whole UTF-8 sequence after it: it walks back over continuation bytes and then a lead byte whose
|
|
89
|
+
* sequence the kept bytes complete. Text cut this way has no replacement character at its last character.
|
|
90
|
+
*/
|
|
91
|
+
export function utf8Cut(bytes: Buffer, end: number): number {
|
|
92
|
+
let back = 0;
|
|
93
|
+
|
|
94
|
+
if (end <= 0) {
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
while (back < 3 && end - back - 1 >= 0 && (bytes[end - back - 1]! & 0xc0) === 0x80) {
|
|
99
|
+
back++;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const at = end - back - 1;
|
|
103
|
+
|
|
104
|
+
// Only continuation bytes on the way back: the start of the text is one broken sequence, so keep none of it.
|
|
105
|
+
if (at < 0) {
|
|
106
|
+
return 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const lead = bytes[at]!;
|
|
110
|
+
const need = lead >= 0xf0 ? 4 : lead >= 0xe0 ? 3 : lead >= 0xc0 ? 2 : 1;
|
|
111
|
+
|
|
112
|
+
return need <= back + 1 ? end : at;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** The bounded text of bytes: at most `max` bytes, cut without a broken character at the end. */
|
|
116
|
+
export function boundedText(bytes: Buffer, max: number): { text: string; truncated: boolean } {
|
|
117
|
+
const kept = bytes.length > max ? utf8Cut(bytes, max) : bytes.length;
|
|
118
|
+
|
|
119
|
+
return {
|
|
120
|
+
text: bytes.subarray(0, Math.max(0, kept)).toString("utf8"),
|
|
121
|
+
truncated: bytes.length > kept,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compiles a design system into the files of `.pi/design/dist/` (stream T): `tokens.css`, a Tailwind v4 `tailwind.css`,
|
|
3
|
+
* `tokens.resolved.json`, and `components.css`. A pure function of the resolved tokens, so the same system always
|
|
4
|
+
* gives the same bytes.
|
|
5
|
+
*
|
|
6
|
+
* `tokens.css` has the default mode on `:root`, each other mode in `[data-theme="<mode>"]`, and a media query for the
|
|
7
|
+
* operating system's light or dark preference. A mode block repeats every token whose value differs from the default,
|
|
8
|
+
* the tokens that only refer to a changed one included: a custom property that holds `var(--x)` is computed where it
|
|
9
|
+
* is declared, so a nested `[data-theme]` scope must declare it again to see the new `--x`.
|
|
10
|
+
*/
|
|
11
|
+
import type { CompiledFiles, DesignSystem, ResolvedToken, TokenTier } from "./system-types.ts";
|
|
12
|
+
import { compactJson, dtcgJson, studioJson } from "./tokens-dtcg.ts";
|
|
13
|
+
import { namesMarkdown } from "./tokens-names.ts";
|
|
14
|
+
import { tailwindNames } from "./tokens-tailwind.ts";
|
|
15
|
+
|
|
16
|
+
export { scaleOf, tailwindNames, type Scale } from "./tokens-tailwind.ts";
|
|
17
|
+
|
|
18
|
+
export type CompileInput = Pick<
|
|
19
|
+
DesignSystem,
|
|
20
|
+
| "name"
|
|
21
|
+
| "description"
|
|
22
|
+
| "modes"
|
|
23
|
+
| "schemes"
|
|
24
|
+
| "tokens"
|
|
25
|
+
| "components"
|
|
26
|
+
| "tailwindReset"
|
|
27
|
+
| "dtcgFormat"
|
|
28
|
+
>;
|
|
29
|
+
|
|
30
|
+
type Decl = [property: string, value: string];
|
|
31
|
+
|
|
32
|
+
const comment = (text: string, max = 90) => {
|
|
33
|
+
const line = text.replace(/\s+/g, " ").replace(/\*\//g, "* /").trim();
|
|
34
|
+
|
|
35
|
+
return line.length > max ? `${line.slice(0, max - 1)}…` : line;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const TIERS: { tier: TokenTier; title: string }[] = [
|
|
39
|
+
{ tier: "primitive", title: "Primitives" },
|
|
40
|
+
{ tier: "semantic", title: "Semantic roles" },
|
|
41
|
+
{ tier: "component", title: "Component tokens" },
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* A primitive (or a step of a scale, `space.4`) is a raw value, so the stylesheet holds the value (`--space-4: 16px`),
|
|
46
|
+
* also when a person wrote it as a reference to another primitive (`{dim.16}`): DevTools then shows `16px`, not
|
|
47
|
+
* `var(--dim-16)`. A role or a component token that refers to a token keeps its `var()`: the reference is what a
|
|
48
|
+
* person wrote, and what a mode changes.
|
|
49
|
+
*/
|
|
50
|
+
function literalPrimitives(system: CompileInput): CompileInput {
|
|
51
|
+
const byName = new Map(system.tokens.map((token) => [token.name, token]));
|
|
52
|
+
const composite = new Set(["typography", "shadow", "border", "transition", "gradient"]);
|
|
53
|
+
const tokens = system.tokens.map((token) => {
|
|
54
|
+
// A primitive, or a step of a scale (`space.4`, `neutral.500`): both are raw values, whatever the file calls them.
|
|
55
|
+
const raw =
|
|
56
|
+
token.tier === "primitive" ||
|
|
57
|
+
(token.tier !== "component" && /^\d/.test(token.path.at(-1)!));
|
|
58
|
+
|
|
59
|
+
if (!raw || composite.has(token.type)) {
|
|
60
|
+
return token;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
let changed = false;
|
|
64
|
+
const modes: ResolvedToken["modes"] = {};
|
|
65
|
+
|
|
66
|
+
for (const [mode, value] of Object.entries(token.modes)) {
|
|
67
|
+
const target = value.alias === undefined ? undefined : byName.get(value.alias);
|
|
68
|
+
|
|
69
|
+
if (
|
|
70
|
+
target?.tier === "primitive" &&
|
|
71
|
+
value.literal !== undefined &&
|
|
72
|
+
value.literal !== value.css
|
|
73
|
+
) {
|
|
74
|
+
modes[mode] = { ...value, css: value.literal };
|
|
75
|
+
changed = true;
|
|
76
|
+
} else {
|
|
77
|
+
modes[mode] = value;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return changed ? { ...token, modes } : token;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return { ...system, tokens };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The declarations of a token in a mode: its variable, and the variables of a typography token's members. */
|
|
88
|
+
function declarations(token: ResolvedToken, mode: string): Decl[] {
|
|
89
|
+
const value = token.modes[mode]!;
|
|
90
|
+
const decls: Decl[] = [[token.cssVar, value.css]];
|
|
91
|
+
|
|
92
|
+
// Only a type style is used by its members (`font-size: var(--type-body-font-size)`); a shadow's are for editors.
|
|
93
|
+
if (token.type === "typography") {
|
|
94
|
+
for (const [property, css] of Object.entries(value.parts ?? {})) {
|
|
95
|
+
decls.push([`${token.cssVar}-${property}`, css]);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return decls;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** Whether a token's value in `mode` differs from the default's, or its CSS does. */
|
|
103
|
+
function differs(token: ResolvedToken, mode: string, base: string): boolean {
|
|
104
|
+
const a = token.modes[mode]!;
|
|
105
|
+
const b = token.modes[base]!;
|
|
106
|
+
|
|
107
|
+
return (
|
|
108
|
+
a.css !== b.css ||
|
|
109
|
+
JSON.stringify(a.value) !== JSON.stringify(b.value) ||
|
|
110
|
+
JSON.stringify(a.parts) !== JSON.stringify(b.parts)
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** The tokens in tier order (primitives, semantic roles, components), each tier in file order. */
|
|
115
|
+
function inTiers(tokens: readonly ResolvedToken[]): { title: string; tokens: ResolvedToken[] }[] {
|
|
116
|
+
return TIERS.map(({ tier, title }) => ({
|
|
117
|
+
title,
|
|
118
|
+
tokens: tokens.filter((token) => token.tier === tier),
|
|
119
|
+
})).filter((section) => section.tokens.length > 0);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** Writes declarations with a comment where the group changes, and a token's description after it. */
|
|
123
|
+
function body(
|
|
124
|
+
sections: { title: string; tokens: ResolvedToken[] }[],
|
|
125
|
+
decl: (token: ResolvedToken) => Decl[],
|
|
126
|
+
notes: boolean,
|
|
127
|
+
): string[] {
|
|
128
|
+
const lines: string[] = [];
|
|
129
|
+
|
|
130
|
+
for (const section of sections) {
|
|
131
|
+
if (lines.length > 0) {
|
|
132
|
+
lines.push("");
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
lines.push(` /* === ${section.title} === */`);
|
|
136
|
+
|
|
137
|
+
let group: string | undefined;
|
|
138
|
+
|
|
139
|
+
for (const token of section.tokens) {
|
|
140
|
+
const label = token.path.slice(0, -1).join(".") || token.path[0]!;
|
|
141
|
+
|
|
142
|
+
if (label !== group) {
|
|
143
|
+
group = label;
|
|
144
|
+
lines.push("", ` /* ${comment(label, 60)} */`);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const decls = decl(token);
|
|
148
|
+
|
|
149
|
+
decls.forEach(([property, value], at) => {
|
|
150
|
+
let note = "";
|
|
151
|
+
|
|
152
|
+
if (notes && at === 0) {
|
|
153
|
+
if (token.deprecated !== undefined) {
|
|
154
|
+
note = ` /* deprecated${typeof token.deprecated === "string" ? `: ${comment(token.deprecated, 60)}` : ""} */`;
|
|
155
|
+
} else if (token.description !== undefined) {
|
|
156
|
+
note = ` /* ${comment(token.description, 70)} */`;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
lines.push(` ${property}: ${value};${note}`);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return lines;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** The `color-scheme` value of a mode. */
|
|
169
|
+
const schemeOf = (system: CompileInput, mode: string) => system.schemes[mode] ?? "light";
|
|
170
|
+
|
|
171
|
+
/** The mode that the operating system's other preference selects, and the preference's name. */
|
|
172
|
+
function preferred(system: CompileInput): { mode: string; scheme: "light" | "dark" } | undefined {
|
|
173
|
+
const base = system.modes[0]!;
|
|
174
|
+
const other = schemeOf(system, base) === "dark" ? "light" : "dark";
|
|
175
|
+
const mode =
|
|
176
|
+
system.modes.find((each) => each !== base && each === other) ??
|
|
177
|
+
system.modes.find((each) => each !== base && schemeOf(system, each) === other);
|
|
178
|
+
|
|
179
|
+
return mode === undefined ? undefined : { mode, scheme: other };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** The declaration lines of each block of a stylesheet: the default mode, the other modes, and the media query. */
|
|
183
|
+
interface Blocks {
|
|
184
|
+
base: string[];
|
|
185
|
+
others: { mode: string; lines: string[] }[];
|
|
186
|
+
media?: { scheme: "light" | "dark"; lines: string[] };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function blocks(
|
|
190
|
+
system: CompileInput,
|
|
191
|
+
decl: (token: ResolvedToken, mode: string) => Decl[],
|
|
192
|
+
notes: boolean,
|
|
193
|
+
): Blocks {
|
|
194
|
+
const base = system.modes[0]!;
|
|
195
|
+
// The tokens of a mode that differ from the default, in tiers.
|
|
196
|
+
const changed = (mode: string) =>
|
|
197
|
+
inTiers(system.tokens.filter((token) => differs(token, mode, base)));
|
|
198
|
+
const alternate = preferred(system);
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
base: body(inTiers(system.tokens), (token) => decl(token, base), notes),
|
|
202
|
+
others: system.modes.slice(1).map((mode) => ({
|
|
203
|
+
mode,
|
|
204
|
+
lines: body(changed(mode), (token) => decl(token, mode), false),
|
|
205
|
+
})),
|
|
206
|
+
...(alternate === undefined
|
|
207
|
+
? {}
|
|
208
|
+
: {
|
|
209
|
+
media: {
|
|
210
|
+
scheme: alternate.scheme,
|
|
211
|
+
lines: body(
|
|
212
|
+
changed(alternate.mode),
|
|
213
|
+
(token) => decl(token, alternate.mode),
|
|
214
|
+
false,
|
|
215
|
+
),
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function header(system: CompileInput, file: string, lines: string[]): string {
|
|
222
|
+
return [
|
|
223
|
+
"/*",
|
|
224
|
+
` * ${comment(system.name, 60)}: ${file} (generated)`,
|
|
225
|
+
" * Generated by Pi Pocket from .pi/design/. Do not edit by hand: change the tokens, then compile again.",
|
|
226
|
+
...lines.map((line) => ` * ${line}`),
|
|
227
|
+
" */",
|
|
228
|
+
].join("\n");
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function tokensCss(system: CompileInput): string {
|
|
232
|
+
const base = system.modes[0]!;
|
|
233
|
+
const modes = system.modes
|
|
234
|
+
.map((mode, at) => (at === 0 ? `${mode} (default)` : mode))
|
|
235
|
+
.join(", ");
|
|
236
|
+
const parts = blocks(system, (token, mode) => declarations(token, mode), true);
|
|
237
|
+
const out = [
|
|
238
|
+
header(system, "tokens.css", [
|
|
239
|
+
`Modes: ${comment(modes)}.`,
|
|
240
|
+
'Use it: <link rel="stylesheet" href="ds/tokens.css"> then color: var(--color-text-primary);',
|
|
241
|
+
system.modes.length > 1
|
|
242
|
+
? `Switch a mode with <html data-theme="${system.modes[1]}">.${parts.media === undefined ? "" : ` Without data-theme, the system's ${parts.media.scheme} setting applies.`}`
|
|
243
|
+
: "One mode only: add tokens.<mode>.json for a dark or brand mode.",
|
|
244
|
+
]),
|
|
245
|
+
"",
|
|
246
|
+
`:root,\n[data-theme="${base}"] {`,
|
|
247
|
+
` color-scheme: ${schemeOf(system, base)};`,
|
|
248
|
+
"",
|
|
249
|
+
...parts.base,
|
|
250
|
+
"}",
|
|
251
|
+
];
|
|
252
|
+
|
|
253
|
+
for (const { mode, lines } of parts.others) {
|
|
254
|
+
out.push(
|
|
255
|
+
"",
|
|
256
|
+
`/* Mode: ${comment(mode)}. Repeats each token that differs from ${comment(base)}. */`,
|
|
257
|
+
`[data-theme="${mode}"] {`,
|
|
258
|
+
` color-scheme: ${schemeOf(system, mode)};`,
|
|
259
|
+
"",
|
|
260
|
+
...lines,
|
|
261
|
+
"}",
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (parts.media !== undefined) {
|
|
266
|
+
out.push(
|
|
267
|
+
"",
|
|
268
|
+
`/* Follow the operating system's ${parts.media.scheme} setting until a data-theme is set. */`,
|
|
269
|
+
`@media (prefers-color-scheme: ${parts.media.scheme}) {`,
|
|
270
|
+
" :root:not([data-theme]) {",
|
|
271
|
+
` color-scheme: ${parts.media.scheme};`,
|
|
272
|
+
"",
|
|
273
|
+
...parts.media.lines.map((line) => (line === "" ? line : ` ${line}`)),
|
|
274
|
+
" }",
|
|
275
|
+
"}",
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
return `${out.join("\n")}\n`;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// --- Tailwind v4 ----------------------------------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
function tailwindCss(system: CompileInput): string {
|
|
285
|
+
const names = tailwindNames(system);
|
|
286
|
+
const rename = new Map(system.tokens.map((token) => [token.cssVar, names.get(token.name)!]));
|
|
287
|
+
// A reference points at the variable that the token has in this file.
|
|
288
|
+
const swap = (css: string) =>
|
|
289
|
+
css.replace(/var\((--[\w-]+)/g, (all, name: string) =>
|
|
290
|
+
rename.has(name) ? `var(${rename.get(name)}` : all,
|
|
291
|
+
);
|
|
292
|
+
|
|
293
|
+
const decls = (token: ResolvedToken, mode: string): Decl[] => {
|
|
294
|
+
const value = token.modes[mode]!;
|
|
295
|
+
const name = names.get(token.name)!;
|
|
296
|
+
|
|
297
|
+
if (token.type !== "typography") {
|
|
298
|
+
return [[name, swap(value.css)]];
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const parts = value.parts ?? {};
|
|
302
|
+
|
|
303
|
+
// A type style is Tailwind's `--text-*`: the size, with `--line-height`, `--font-weight`, and `--letter-spacing`.
|
|
304
|
+
if (name.startsWith("--text-") && parts["font-size"] !== undefined) {
|
|
305
|
+
const out: Decl[] = [[name, swap(parts["font-size"])]];
|
|
306
|
+
|
|
307
|
+
for (const property of ["line-height", "font-weight", "letter-spacing"] as const) {
|
|
308
|
+
if (parts[property] !== undefined) {
|
|
309
|
+
out.push([`${name}--${property}`, swap(parts[property])]);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return out;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
return [
|
|
317
|
+
[name, swap(value.css)],
|
|
318
|
+
...Object.entries(parts).map(([property, css]): Decl => [
|
|
319
|
+
`${name}-${property}`,
|
|
320
|
+
swap(css),
|
|
321
|
+
]),
|
|
322
|
+
];
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const base = system.modes[0]!;
|
|
326
|
+
const parts = blocks(system, decls, true);
|
|
327
|
+
const dark = system.modes.find((mode) => system.schemes[mode] === "dark" && mode !== base);
|
|
328
|
+
const reset = (system.tailwindReset ?? []).map((namespace) =>
|
|
329
|
+
namespace === "all" ? "--*: initial;" : `--${namespace}-*: initial;`,
|
|
330
|
+
);
|
|
331
|
+
const out = [
|
|
332
|
+
header(system, "tailwind.css", [
|
|
333
|
+
"For Tailwind CSS v4. Import it after Tailwind, in your main stylesheet:",
|
|
334
|
+
' @import "tailwindcss";',
|
|
335
|
+
' @import "./tailwind.css";',
|
|
336
|
+
"Each token has one variable here, in a namespace of Tailwind's theme when one fits: --color-*, --font-* (families),",
|
|
337
|
+
"--text-*, --font-weight-*, --tracking-*, --leading-*, --breakpoint-*, --container-*, --spacing-*, --radius-*,",
|
|
338
|
+
"--shadow-*, --inset-shadow-*, --drop-shadow-*, --blur-*, --perspective-*, --aspect-*, --ease-*. A token with no",
|
|
339
|
+
"namespace keeps its own name (--button-height); one whose name would be read as a namespace of the wrong kind",
|
|
340
|
+
"(--font-leading-tight) gets --ds- in front.",
|
|
341
|
+
"Utilities read well because a role drops the word that the utility repeats: color.bg.surface is --color-surface",
|
|
342
|
+
"(bg-surface), color.text.primary is --color-primary (text-primary), color.border.default is --color-default",
|
|
343
|
+
"(border-default), and a trailing default goes (color.accent.default is --color-accent). When two roles would",
|
|
344
|
+
"have one name, the first keeps it and the other keeps its full name (--color-text-muted).",
|
|
345
|
+
"tokens.css has every token under its own name (--color-bg-surface, --space-4); link it too for var(--space-4)",
|
|
346
|
+
"and the components.",
|
|
347
|
+
'To clear Tailwind\'s default palette (bg-red-500 and the rest), set "tailwindReset": ["color"] in tokens.json\'s',
|
|
348
|
+
'$extensions["com.pi-pocket"].',
|
|
349
|
+
]),
|
|
350
|
+
"",
|
|
351
|
+
`:root,\n[data-theme="${base}"] {`,
|
|
352
|
+
` color-scheme: ${schemeOf(system, base)};`,
|
|
353
|
+
"}",
|
|
354
|
+
"",
|
|
355
|
+
...(dark === undefined
|
|
356
|
+
? []
|
|
357
|
+
: [
|
|
358
|
+
"/* dark: matches [data-theme] when it is set, and the system's dark setting when no data-theme is. */",
|
|
359
|
+
"@custom-variant dark {",
|
|
360
|
+
` &:where([data-theme="${dark}"], [data-theme="${dark}"] *) {`,
|
|
361
|
+
" @slot;",
|
|
362
|
+
" }",
|
|
363
|
+
"",
|
|
364
|
+
" @media (prefers-color-scheme: dark) {",
|
|
365
|
+
" &:where(:not([data-theme]):not([data-theme] *)) {",
|
|
366
|
+
" @slot;",
|
|
367
|
+
" }",
|
|
368
|
+
" }",
|
|
369
|
+
"}",
|
|
370
|
+
"",
|
|
371
|
+
]),
|
|
372
|
+
`/* ${comment(base)} values. Other modes override them below. */`,
|
|
373
|
+
"@theme static {",
|
|
374
|
+
...reset.map((line) => ` ${line}`),
|
|
375
|
+
...(reset.length > 0 ? [""] : []),
|
|
376
|
+
...parts.base,
|
|
377
|
+
"}",
|
|
378
|
+
];
|
|
379
|
+
|
|
380
|
+
for (const { mode, lines } of parts.others) {
|
|
381
|
+
out.push(
|
|
382
|
+
"",
|
|
383
|
+
`/* Mode: ${comment(mode)} */`,
|
|
384
|
+
`[data-theme="${mode}"] {`,
|
|
385
|
+
` color-scheme: ${schemeOf(system, mode)};`,
|
|
386
|
+
"",
|
|
387
|
+
...lines,
|
|
388
|
+
"}",
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (parts.media !== undefined) {
|
|
393
|
+
out.push(
|
|
394
|
+
"",
|
|
395
|
+
`@media (prefers-color-scheme: ${parts.media.scheme}) {`,
|
|
396
|
+
" :root:not([data-theme]) {",
|
|
397
|
+
` color-scheme: ${parts.media.scheme};`,
|
|
398
|
+
"",
|
|
399
|
+
...parts.media.lines.map((line) => (line === "" ? line : ` ${line}`)),
|
|
400
|
+
" }",
|
|
401
|
+
"}",
|
|
402
|
+
);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
return `${out.join("\n")}\n`;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// --- Resolved JSON and component CSS ------------------------------------------------------------------------------------
|
|
409
|
+
|
|
410
|
+
function componentsCss(system: CompileInput): string {
|
|
411
|
+
const head = header(system, "components.css", [
|
|
412
|
+
'Use it: <link rel="stylesheet" href="ds/components.css"> then use the markup of a component specimen.',
|
|
413
|
+
]);
|
|
414
|
+
|
|
415
|
+
if (system.components.length === 0) {
|
|
416
|
+
return `${head}\n\n/* No components yet. */\n`;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
const blocks = system.components.map(
|
|
420
|
+
(component) =>
|
|
421
|
+
`/* ${comment(component.name, 60)} (components/${component.slug}.html) */\n${component.css}`,
|
|
422
|
+
);
|
|
423
|
+
|
|
424
|
+
return `${head}\n\n${blocks.join("\n\n")}\n`;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
/** Compiles a system into the files of `.pi/design/dist/`: four, a DTCG file for each mode, and one for Tokens Studio. */
|
|
428
|
+
export function compileDesignSystem(input: CompileInput): CompiledFiles {
|
|
429
|
+
const system = literalPrimitives(input);
|
|
430
|
+
|
|
431
|
+
return {
|
|
432
|
+
css: tokensCss(system),
|
|
433
|
+
tailwind: tailwindCss(system),
|
|
434
|
+
json: compactJson(system),
|
|
435
|
+
components: componentsCss(system),
|
|
436
|
+
modes: Object.fromEntries(system.modes.map((mode) => [mode, dtcgJson(system, mode)])),
|
|
437
|
+
studio: studioJson(system),
|
|
438
|
+
names: namesMarkdown(system),
|
|
439
|
+
};
|
|
440
|
+
}
|