@designtools/blocks 0.0.0-stage → 0.2.0
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/CHANGELOG.md +31 -0
- package/LICENSE +21 -0
- package/README.md +121 -2
- package/dist/cli.js +385 -0
- package/package.json +61 -3
- package/reference/README.md +20 -0
- package/reference/app/(design)/design/system/(docs)/brand/imagery/page.tsx +15 -0
- package/reference/app/(design)/design/system/(docs)/brand/logo/page.tsx +15 -0
- package/reference/app/(design)/design/system/(docs)/brand/page.tsx +16 -0
- package/reference/app/(design)/design/system/(docs)/brand/voice/page.tsx +15 -0
- package/reference/app/(design)/design/system/(docs)/colour/page.tsx +26 -0
- package/reference/app/(design)/design/system/(docs)/components/[slug]/page.tsx +86 -0
- package/reference/app/(design)/design/system/(docs)/layout.tsx +35 -0
- package/reference/app/(design)/design/system/(docs)/page.tsx +48 -0
- package/reference/app/(design)/design/system/(docs)/patterns/[id]/page.tsx +23 -0
- package/reference/app/(design)/design/system/(docs)/rules/page.tsx +42 -0
- package/reference/app/(design)/design/system/(docs)/scale/page.tsx +15 -0
- package/reference/app/(design)/design/system/(docs)/standards/page.tsx +21 -0
- package/reference/app/(design)/design/system/(docs)/type/page.tsx +15 -0
- package/reference/app/(design)/design/system/(docs)/vocabulary/page.tsx +15 -0
- package/reference/app/(design)/design/system/_components/copy-page.tsx +32 -0
- package/reference/app/(design)/design/system/_components/prose.tsx +26 -0
- package/reference/app/(design)/design/system/_data.ts +53 -0
- package/reference/app/(design)/design/system/manifest/[file]/route.ts +15 -0
- package/reference/app/(design)/design/system/md/[[...path]]/route.ts +94 -0
- package/reference/app/(design)/design/system/preview/[slug]/[example]/page.tsx +23 -0
- package/reference/app/(design)/layout.tsx +16 -0
- package/reference/app/globals.css +14 -0
- package/reference/app/layout.tsx +16 -0
- package/reference/designtools.json +14 -0
- package/reference/package.json +38 -0
- package/reference/proxy.ts +55 -0
- package/registry/a11y-panel.tsx +99 -0
- package/registry/adherence-summary.tsx +110 -0
- package/registry/agent-view.tsx +58 -0
- package/registry/anatomy.tsx +89 -0
- package/registry/ask-claude.tsx +73 -0
- package/registry/code-view.tsx +77 -0
- package/registry/examples.tsx +97 -0
- package/registry/glossary.tsx +59 -0
- package/registry/imagery.tsx +39 -0
- package/registry/lib/adherence-types.ts +181 -0
- package/registry/lib/boundary.tsx +21 -0
- package/registry/lib/contrast.ts +44 -0
- package/registry/lib/cx.ts +12 -0
- package/registry/lib/jsx.ts +29 -0
- package/registry/lib/lookup.tsx +46 -0
- package/registry/lib/manifest-types.ts +284 -0
- package/registry/lib/manifest.ts +228 -0
- package/registry/lib/markdown.ts +288 -0
- package/registry/lib/search.ts +99 -0
- package/registry/lib/standards.ts +82 -0
- package/registry/lib/status.tsx +32 -0
- package/registry/lib/text.tsx +27 -0
- package/registry/logo-usage.tsx +112 -0
- package/registry/pattern.tsx +69 -0
- package/registry/playground.tsx +181 -0
- package/registry/preview-frame.tsx +106 -0
- package/registry/props-table.tsx +110 -0
- package/registry/registry.generated.tsx +9 -0
- package/registry/rule.tsx +99 -0
- package/registry/scale.tsx +115 -0
- package/registry/search.tsx +134 -0
- package/registry/shell.tsx +247 -0
- package/registry/standards.tsx +41 -0
- package/registry/swatches.tsx +225 -0
- package/registry/tsconfig.json +13 -0
- package/registry/type-ramp.tsx +105 -0
- package/registry/usage.tsx +73 -0
- package/registry/variant-matrix.tsx +105 -0
- package/registry/voice-terms.tsx +45 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { CodeView } from "./code-view";
|
|
4
|
+
import { Boundary } from "./lib/boundary";
|
|
5
|
+
import { cx, target } from "./lib/cx";
|
|
6
|
+
import { examplesFor, Missing, renderExample } from "./lib/lookup";
|
|
7
|
+
import type { ComponentEntry, ExampleRef } from "./lib/manifest-types";
|
|
8
|
+
import { inline } from "./lib/text";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The canonical example first, the one to copy, then each do beside its don't.
|
|
12
|
+
* Every example is the real component; the code under it is the code that drew it.
|
|
13
|
+
*/
|
|
14
|
+
export function Examples({ entry, previewHref }: { entry: ComponentEntry; previewHref?: string }) {
|
|
15
|
+
const ex = entry.examples;
|
|
16
|
+
if (!ex) {
|
|
17
|
+
return (
|
|
18
|
+
<p data-slot="examples-empty" className="text-sm text-muted-foreground">
|
|
19
|
+
No examples yet. Add <code className="font-mono text-xs">{entry.source.replace(/\.tsx$/, ".examples.tsx")}</code> with a{" "}
|
|
20
|
+
<code className="font-mono text-xs">canonical</code> export and do and don't pairs.
|
|
21
|
+
</p>
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
const module = examplesFor(entry);
|
|
25
|
+
if (!module) return <Missing name={entry.name} what="examples" />;
|
|
26
|
+
|
|
27
|
+
const pairs = Array.from({ length: Math.max(ex.do.length, ex.dont.length) }, (_, i) => [ex.do[i], ex.dont[i]] as const);
|
|
28
|
+
const card = (ref: ExampleRef, kind: "canonical" | "do" | "dont" | "other") => (
|
|
29
|
+
<Example key={ref.name} example={ref} kind={kind} value={module[ref.name]} href={previewHref && `${previewHref}/${encodeURIComponent(ref.name)}`} />
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<div data-slot="examples" className="flex flex-col gap-6">
|
|
34
|
+
{ex.canonical && card(ex.canonical, "canonical")}
|
|
35
|
+
{pairs.map(([d, n], i) => (
|
|
36
|
+
<div key={i} data-slot="examples-pair" className="grid grid-cols-1 gap-4 md:grid-cols-2">
|
|
37
|
+
{d ? card(d, "do") : <span />}
|
|
38
|
+
{n ? card(n, "dont") : <span />}
|
|
39
|
+
</div>
|
|
40
|
+
))}
|
|
41
|
+
{ex.other.length > 0 && <div className="grid grid-cols-1 gap-4 md:grid-cols-2">{ex.other.map((o) => card(o, "other"))}</div>}
|
|
42
|
+
</div>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const LABEL = { canonical: "Copy this one", do: "Do", dont: "Don't", other: "" } as const;
|
|
47
|
+
|
|
48
|
+
function Example({ example, kind, value, href }: { example: ExampleRef; kind: keyof typeof LABEL; value: unknown; href?: string }) {
|
|
49
|
+
return (
|
|
50
|
+
<figure
|
|
51
|
+
data-slot="example"
|
|
52
|
+
data-kind={kind}
|
|
53
|
+
className={cx(
|
|
54
|
+
"flex min-w-0 flex-col overflow-hidden rounded-lg border",
|
|
55
|
+
kind === "do" ? "border-success" : kind === "dont" ? "border-destructive" : "border-border",
|
|
56
|
+
)}
|
|
57
|
+
>
|
|
58
|
+
<div data-slot="example-frame" className="flex min-h-32 items-center justify-center overflow-auto bg-surface p-6 text-surface-foreground">
|
|
59
|
+
<Boundary label={example.name}>{renderExample(value, example.name)}</Boundary>
|
|
60
|
+
</div>
|
|
61
|
+
<figcaption className="flex flex-col gap-2 border-t border-border px-4 py-3 text-sm">
|
|
62
|
+
<span className="flex items-baseline justify-between gap-3">
|
|
63
|
+
<span className="flex items-baseline gap-2">
|
|
64
|
+
{LABEL[kind] && (
|
|
65
|
+
<span
|
|
66
|
+
// a measured pairing, not a fill colour used as text (text-success read 3.9:1 on the canvas)
|
|
67
|
+
className={cx(
|
|
68
|
+
"rounded-sm px-1.5 font-medium",
|
|
69
|
+
kind === "do" && "bg-success-subdued text-success-subdued-foreground",
|
|
70
|
+
kind === "dont" && "bg-destructive-subdued text-destructive-subdued-foreground",
|
|
71
|
+
)}
|
|
72
|
+
>
|
|
73
|
+
{kind === "do" ? "✓ " : kind === "dont" ? "✕ " : ""}
|
|
74
|
+
{LABEL[kind]}
|
|
75
|
+
</span>
|
|
76
|
+
)}
|
|
77
|
+
<code className="font-mono text-xs text-muted-foreground">{example.name}</code>
|
|
78
|
+
</span>
|
|
79
|
+
{href && (
|
|
80
|
+
<a href={href} className={cx(target, "inline-flex items-center text-xs underline underline-offset-2")}>
|
|
81
|
+
Open alone
|
|
82
|
+
</a>
|
|
83
|
+
)}
|
|
84
|
+
</span>
|
|
85
|
+
{example.description && <span className="text-muted-foreground">{inline(example.description)}</span>}
|
|
86
|
+
{example.code && (
|
|
87
|
+
<details>
|
|
88
|
+
<summary className={cx(target, "flex w-fit cursor-pointer items-center text-xs text-muted-foreground")}>Code</summary>
|
|
89
|
+
<div className="mt-2">
|
|
90
|
+
<CodeView code={example.code} label={example.name} />
|
|
91
|
+
</div>
|
|
92
|
+
</details>
|
|
93
|
+
)}
|
|
94
|
+
</figcaption>
|
|
95
|
+
</figure>
|
|
96
|
+
);
|
|
97
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { useId, useState } from "react";
|
|
4
|
+
import type { TermEntry } from "./lib/manifest-types";
|
|
5
|
+
import { inline } from "./lib/text";
|
|
6
|
+
import { target } from "./lib/cx";
|
|
7
|
+
|
|
8
|
+
/** Every name in the system, searchable by its wrong forms too, so a wrong name finds the right one. */
|
|
9
|
+
export function Glossary({ terms }: { terms: TermEntry[] }) {
|
|
10
|
+
const id = useId();
|
|
11
|
+
const [query, setQuery] = useState("");
|
|
12
|
+
const q = query.trim().toLowerCase();
|
|
13
|
+
const matches = q ? terms.filter((t) => t.name.toLowerCase().includes(q) || t.wrong?.some((w) => w.toLowerCase().includes(q))) : terms;
|
|
14
|
+
const corrected = q ? terms.find((t) => t.wrong?.some((w) => w.toLowerCase() === q)) : undefined;
|
|
15
|
+
|
|
16
|
+
return (
|
|
17
|
+
<div data-slot="glossary" className="flex flex-col gap-4">
|
|
18
|
+
<div className="flex flex-col gap-1.5">
|
|
19
|
+
<label htmlFor={id} className="text-sm font-medium">
|
|
20
|
+
Find a term
|
|
21
|
+
</label>
|
|
22
|
+
<input
|
|
23
|
+
id={id}
|
|
24
|
+
type="search"
|
|
25
|
+
value={query}
|
|
26
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
27
|
+
placeholder="Try a name you half remember"
|
|
28
|
+
className={`${target} h-(--size-xl) max-w-sm rounded-md border border-input bg-canvas px-3 text-sm focus-visible:outline-2 focus-visible:outline-ring`}
|
|
29
|
+
/>
|
|
30
|
+
</div>
|
|
31
|
+
<p aria-live="polite" className="text-sm text-muted-foreground">
|
|
32
|
+
{corrected ? (
|
|
33
|
+
<>
|
|
34
|
+
The system calls “{query.trim()}” <strong className="text-canvas-foreground">{corrected.name}</strong>.
|
|
35
|
+
</>
|
|
36
|
+
) : q ? (
|
|
37
|
+
`${matches.length} ${matches.length === 1 ? "term" : "terms"}`
|
|
38
|
+
) : (
|
|
39
|
+
`${terms.length} terms`
|
|
40
|
+
)}
|
|
41
|
+
</p>
|
|
42
|
+
<dl data-slot="glossary-list" className="flex flex-col divide-y divide-border border-y border-border">
|
|
43
|
+
{matches.map((t) => (
|
|
44
|
+
<div key={`${t.kind}:${t.name}`} className="grid gap-1 py-3 sm:grid-cols-[12rem_1fr]">
|
|
45
|
+
<dt className="flex items-baseline gap-2">
|
|
46
|
+
<span className="font-medium">{t.name}</span>
|
|
47
|
+
<span className="text-xs text-muted-foreground">{t.kind}</span>
|
|
48
|
+
</dt>
|
|
49
|
+
<dd className="flex flex-col gap-0.5 text-sm text-muted-foreground">
|
|
50
|
+
{t.wrong?.length ? <span>Not {t.wrong.map((w) => `“${w}”`).join(", ")}</span> : null}
|
|
51
|
+
{t.note && <span>{inline(t.note)}</span>}
|
|
52
|
+
{t.ref && <code className="font-mono text-xs">{t.ref}</code>}
|
|
53
|
+
</dd>
|
|
54
|
+
</div>
|
|
55
|
+
))}
|
|
56
|
+
</dl>
|
|
57
|
+
</div>
|
|
58
|
+
);
|
|
59
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { assetUrl } from "./lib/lookup";
|
|
4
|
+
import type { AssetEntry } from "./lib/manifest-types";
|
|
5
|
+
import { inline } from "./lib/text";
|
|
6
|
+
|
|
7
|
+
/** The approved icons and imagery, each with how to use it. */
|
|
8
|
+
export function Imagery({ assets, kinds = ["icon", "image", "other"] }: { assets: AssetEntry[]; kinds?: AssetEntry["kind"][] }) {
|
|
9
|
+
const shown = assets.filter((a) => kinds.includes(a.kind));
|
|
10
|
+
if (shown.length === 0) return <p className="text-sm text-muted-foreground">No icons or imagery in the brand folder yet.</p>;
|
|
11
|
+
return (
|
|
12
|
+
<ul data-slot="imagery" className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
|
|
13
|
+
{shown.map((a) => {
|
|
14
|
+
const src = assetUrl(a);
|
|
15
|
+
return (
|
|
16
|
+
<li key={a.file} className="min-w-0">
|
|
17
|
+
<figure data-slot="asset" className="flex h-full flex-col overflow-hidden rounded-lg border border-border">
|
|
18
|
+
<div className="flex h-36 items-center justify-center bg-surface p-4 text-surface-foreground">
|
|
19
|
+
{src ? (
|
|
20
|
+
<img src={src} alt={a.alt ?? ""} className={a.kind === "icon" ? "size-12" : "max-h-full max-w-full object-contain"} />
|
|
21
|
+
) : (
|
|
22
|
+
<span className="font-mono text-xs text-muted-foreground">{a.file}</span>
|
|
23
|
+
)}
|
|
24
|
+
</div>
|
|
25
|
+
<figcaption className="flex flex-col gap-1 border-t border-border px-4 py-3 text-sm">
|
|
26
|
+
<span className="font-medium">{a.name}</span>
|
|
27
|
+
{a.usage ? <span className="text-muted-foreground">{inline(a.usage)}</span> : <span className="text-muted-foreground">No usage notes yet.</span>}
|
|
28
|
+
<span className="font-mono text-xs text-muted-foreground">
|
|
29
|
+
{a.kind}
|
|
30
|
+
{a.width && a.height ? ` · ${a.width} × ${a.height}` : ""}
|
|
31
|
+
</span>
|
|
32
|
+
</figcaption>
|
|
33
|
+
</figure>
|
|
34
|
+
</li>
|
|
35
|
+
);
|
|
36
|
+
})}
|
|
37
|
+
</ul>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The adherence report: what `designtools-adherence` writes with `--json`, and
|
|
3
|
+
* what the "Adherence summary" docs block renders.
|
|
4
|
+
*
|
|
5
|
+
* This file has no imports on purpose, so @designtools/blocks can carry a copy
|
|
6
|
+
* and read the report through the same types the tool wrote it with.
|
|
7
|
+
*
|
|
8
|
+
* Every path is relative to the project root, with forward slashes. Lines and
|
|
9
|
+
* columns start at 1. Every array is sorted (files and routes by path, findings
|
|
10
|
+
* by line then column, components and elements by name), and there are no
|
|
11
|
+
* timestamps, so the same source gives the same bytes.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
export const ADHERENCE_SCHEMA = "designtools.adherence/1";
|
|
15
|
+
|
|
16
|
+
export interface AdherenceReport {
|
|
17
|
+
/** `@designtools/adherence@<version>`. */
|
|
18
|
+
generator: string;
|
|
19
|
+
schema: string;
|
|
20
|
+
/** The components.json the system was read from. */
|
|
21
|
+
manifest: string;
|
|
22
|
+
/** The system folder, which is never scanned as product. */
|
|
23
|
+
system: string;
|
|
24
|
+
/** The globs that chose the product files, as given or defaulted. */
|
|
25
|
+
include: string[];
|
|
26
|
+
exclude: string[];
|
|
27
|
+
summary: Summary;
|
|
28
|
+
/** One per Next app router page (`app/**\/page.tsx`), sorted by route. */
|
|
29
|
+
routes: RouteEntry[];
|
|
30
|
+
/** Every scanned file with at least one use, raw element or finding, sorted by path. */
|
|
31
|
+
files: FileEntry[];
|
|
32
|
+
/** Every system component, used or not, sorted by name. */
|
|
33
|
+
components: ComponentUsage[];
|
|
34
|
+
/** Every raw element some component `replaces`, sorted by selector. */
|
|
35
|
+
elements: ElementUsage[];
|
|
36
|
+
/** Files a scanner could not parse, sorted by path. Their numbers are missing from the report. */
|
|
37
|
+
skipped: SkippedFile[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The numbers every level of the report shares: the whole product, a route, a file. */
|
|
41
|
+
export interface Counts {
|
|
42
|
+
/** JSX uses of system components, sub-components included. */
|
|
43
|
+
systemUses: number;
|
|
44
|
+
/** Raw elements that some system component `replaces`, such as `<button>` when Button replaces it. */
|
|
45
|
+
rawElements: number;
|
|
46
|
+
/**
|
|
47
|
+
* systemUses ÷ (systemUses + rawElements), from 0 to 1, to four decimal places.
|
|
48
|
+
* Null when both are 0: there was nothing to measure.
|
|
49
|
+
*/
|
|
50
|
+
share: number | null;
|
|
51
|
+
/** Classes that bypass the tokens: palette steps and arbitrary values. */
|
|
52
|
+
offSystemValues: number;
|
|
53
|
+
/** System components given a className that changes colour, spacing or radius. */
|
|
54
|
+
overrides: number;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export interface Summary extends Counts {
|
|
58
|
+
/** Product files scanned. */
|
|
59
|
+
files: number;
|
|
60
|
+
/** Files with at least one raw element, off-system value or override. */
|
|
61
|
+
filesWithFindings: number;
|
|
62
|
+
offSystem: Record<OffSystemKind, number>;
|
|
63
|
+
overrideKinds: Record<OverrideKind, number>;
|
|
64
|
+
/** System components used at least once. */
|
|
65
|
+
componentsUsed: number;
|
|
66
|
+
/** System components never used in the product, by name. */
|
|
67
|
+
componentsUnused: string[];
|
|
68
|
+
/**
|
|
69
|
+
* The raw elements counted, from every component's `replaces`. Empty means no
|
|
70
|
+
* component declares any, so `share` measures nothing yet.
|
|
71
|
+
*/
|
|
72
|
+
replaceable: string[];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface RouteEntry {
|
|
76
|
+
/** The URL path: route groups and parallel-route slots removed, dynamic segments kept. `/companies/[key]`. */
|
|
77
|
+
route: string;
|
|
78
|
+
/** The page file. */
|
|
79
|
+
page: string;
|
|
80
|
+
/**
|
|
81
|
+
* The files counted for the route: the page, files beside it, and files in
|
|
82
|
+
* folders under it that lead to no other page. Layouts shared by several
|
|
83
|
+
* routes are counted per file only.
|
|
84
|
+
*/
|
|
85
|
+
files: string[];
|
|
86
|
+
counts: Counts;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface FileEntry {
|
|
90
|
+
file: string;
|
|
91
|
+
/** The route the file is counted under, when it has one. */
|
|
92
|
+
route?: string;
|
|
93
|
+
counts: Counts;
|
|
94
|
+
uses: ComponentUse[];
|
|
95
|
+
raw: RawElement[];
|
|
96
|
+
offSystem: OffSystemValue[];
|
|
97
|
+
overrides: Override[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** A literal prop value, or the kind of expression it was, e.g. `(Identifier)`. */
|
|
101
|
+
export type PropValue = string | number | boolean | null;
|
|
102
|
+
|
|
103
|
+
export interface ComponentUse {
|
|
104
|
+
/** The component's name in the manifest. */
|
|
105
|
+
component: string;
|
|
106
|
+
/** The element as written, when it differs: `Select.Trigger`, `DS.Button`. */
|
|
107
|
+
as?: string;
|
|
108
|
+
line: number;
|
|
109
|
+
column: number;
|
|
110
|
+
/** Props as written, sorted. A className built from expressions keeps its literal classes only. */
|
|
111
|
+
props: Record<string, PropValue>;
|
|
112
|
+
/** True when it also takes `{...spread}` props, whose names are unknown. */
|
|
113
|
+
spread: boolean;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface RawElement {
|
|
117
|
+
/** The tag as written: `button`. */
|
|
118
|
+
element: string;
|
|
119
|
+
/** The `replaces` selector it matched: `button`, `input[type=checkbox]`. */
|
|
120
|
+
selector: string;
|
|
121
|
+
/** The components that replace it, most specific first. */
|
|
122
|
+
replacedBy: string[];
|
|
123
|
+
line: number;
|
|
124
|
+
column: number;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* `palette`: a primitive ramp step used directly, `bg-blue-500` or `text-primary-500`.
|
|
129
|
+
* `arbitrary`: a value in brackets, `p-[13px]` or `bg-[#fff]`.
|
|
130
|
+
* `status-text`: a status fill used as text, `text-destructive`: use `text-destructive-subdued-foreground`,
|
|
131
|
+
* which reads on every background, or neutral text with an icon for a warning.
|
|
132
|
+
*/
|
|
133
|
+
export type OffSystemKind = "palette" | "arbitrary" | "status-text";
|
|
134
|
+
|
|
135
|
+
export interface OffSystemValue {
|
|
136
|
+
/** The class as written, variants included: `hover:bg-blue-500`. */
|
|
137
|
+
class: string;
|
|
138
|
+
kind: OffSystemKind;
|
|
139
|
+
line: number;
|
|
140
|
+
column: number;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export type OverrideKind = "colour" | "spacing" | "radius";
|
|
144
|
+
|
|
145
|
+
export interface Override {
|
|
146
|
+
component: string;
|
|
147
|
+
/** Each class in the className that overrides the component, with what it changes. */
|
|
148
|
+
classes: { class: string; kind: OverrideKind }[];
|
|
149
|
+
line: number;
|
|
150
|
+
column: number;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export interface ComponentUsage {
|
|
154
|
+
name: string;
|
|
155
|
+
source: string;
|
|
156
|
+
uses: number;
|
|
157
|
+
/** Files it is used in. */
|
|
158
|
+
files: number;
|
|
159
|
+
/** How many uses pass each prop, by prop name. */
|
|
160
|
+
props: Record<string, number>;
|
|
161
|
+
/**
|
|
162
|
+
* Per variant axis, how many uses pick each option. A use that leaves the prop
|
|
163
|
+
* out counts under the axis's default, or `(unset)`; one that computes it
|
|
164
|
+
* counts under `(dynamic)`.
|
|
165
|
+
*/
|
|
166
|
+
variants?: Record<string, Record<string, number>>;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export interface ElementUsage {
|
|
170
|
+
/** The `replaces` selector. */
|
|
171
|
+
selector: string;
|
|
172
|
+
/** Raw uses in the product. */
|
|
173
|
+
uses: number;
|
|
174
|
+
files: number;
|
|
175
|
+
replacedBy: string[];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export interface SkippedFile {
|
|
179
|
+
file: string;
|
|
180
|
+
reason: string;
|
|
181
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { Component, type ReactNode } from "react";
|
|
4
|
+
|
|
5
|
+
/** Keeps one broken example from blanking the whole page: the error shows where the example would. */
|
|
6
|
+
export class Boundary extends Component<{ label: string; children: ReactNode }, { error: Error | null }> {
|
|
7
|
+
state = { error: null as Error | null };
|
|
8
|
+
|
|
9
|
+
static getDerivedStateFromError(error: Error) {
|
|
10
|
+
return { error };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
render() {
|
|
14
|
+
if (!this.state.error) return this.props.children;
|
|
15
|
+
return (
|
|
16
|
+
<p data-slot="block-error" role="alert" className="rounded-md border border-dashed border-destructive p-3 text-xs">
|
|
17
|
+
<span className="font-medium">{this.props.label} did not render:</span> {this.state.error.message}
|
|
18
|
+
</p>
|
|
19
|
+
);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contrast measured on what the browser paints, not on the values written in
|
|
3
|
+
* the stylesheet: a 1px canvas turns any computed CSS colour (oklch, color-mix,
|
|
4
|
+
* a var chain) into sRGB bytes. Browser only.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
let context: CanvasRenderingContext2D | null = null;
|
|
8
|
+
|
|
9
|
+
function rgb(css: string): [number, number, number] | null {
|
|
10
|
+
if (typeof document === "undefined") return null;
|
|
11
|
+
if (!context) {
|
|
12
|
+
const canvas = document.createElement("canvas");
|
|
13
|
+
canvas.width = canvas.height = 1;
|
|
14
|
+
context = canvas.getContext("2d", { willReadFrequently: true });
|
|
15
|
+
}
|
|
16
|
+
if (!context) return null;
|
|
17
|
+
context.clearRect(0, 0, 1, 1);
|
|
18
|
+
context.fillStyle = "#000";
|
|
19
|
+
context.fillStyle = css;
|
|
20
|
+
context.fillRect(0, 0, 1, 1);
|
|
21
|
+
const [r, g, b] = context.getImageData(0, 0, 1, 1).data;
|
|
22
|
+
return [r, g, b];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function luminance([r, g, b]: [number, number, number]): number {
|
|
26
|
+
const channel = (v: number) => {
|
|
27
|
+
const s = v / 255;
|
|
28
|
+
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4;
|
|
29
|
+
};
|
|
30
|
+
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** WCAG 2 contrast ratio between two CSS colours, or null where it cannot be measured. */
|
|
34
|
+
export function contrastRatio(foreground: string, background: string): number | null {
|
|
35
|
+
const a = rgb(foreground);
|
|
36
|
+
const b = rgb(background);
|
|
37
|
+
if (!a || !b) return null;
|
|
38
|
+
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
|
|
39
|
+
return (hi + 0.05) / (lo + 0.05);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function wcagGrade(ratio: number): "AAA" | "AA" | "AA large" | "Fail" {
|
|
43
|
+
return ratio >= 7 ? "AAA" : ratio >= 4.5 ? "AA" : ratio >= 3 ? "AA large" : "Fail";
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The smallest a pressable thing in the docs may be: 44px by default (WCAG 2.5.5).
|
|
3
|
+
* A client with its own rule sets `--docs-target` on the docs layout (`3rem` for
|
|
4
|
+
* 48px), so the site that states the rule also meets it. Links inside a sentence
|
|
5
|
+
* are exempt, as WCAG exempts them.
|
|
6
|
+
*/
|
|
7
|
+
export const target = "min-h-[var(--docs-target,2.75rem)] min-w-[var(--docs-target,2.75rem)]";
|
|
8
|
+
|
|
9
|
+
/** Join class names, skipping the falsy ones. */
|
|
10
|
+
export function cx(...classes: (string | false | null | undefined)[]): string {
|
|
11
|
+
return classes.filter(Boolean).join(" ");
|
|
12
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Props back to JSX, so the code shown beside a preview is the code that drew it.
|
|
3
|
+
* Defaults and undefined values are left out: the snippet is what a person would write.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export type JsxProps = Record<string, unknown>;
|
|
7
|
+
|
|
8
|
+
export function toJSX(name: string, props: JsxProps, children?: string): string {
|
|
9
|
+
const attrs = Object.entries(props)
|
|
10
|
+
.filter(([key, value]) => value !== undefined && key !== "children")
|
|
11
|
+
.map(([key, value]) => attribute(key, value));
|
|
12
|
+
|
|
13
|
+
// text with braces or angle brackets is not valid JSX as written; quote it as an expression
|
|
14
|
+
if (children && /[{}<>]/.test(children)) children = `{${JSON.stringify(children)}}`;
|
|
15
|
+
const open = attrs.length ? `<${name} ${attrs.join(" ")}` : `<${name}`;
|
|
16
|
+
const oneLine = children ? `${open}>${children}</${name}>` : `${open} />`;
|
|
17
|
+
if (oneLine.length <= 80) return oneLine;
|
|
18
|
+
|
|
19
|
+
const lines = [`<${name}`, ...attrs.map((a) => ` ${a}`)];
|
|
20
|
+
return children ? `${lines.join("\n")}\n>\n ${children}\n</${name}>` : `${lines.join("\n")}\n/>`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function attribute(key: string, value: unknown): string {
|
|
24
|
+
if (value === true) return key;
|
|
25
|
+
if (typeof value === "string") return /["\n{}]/.test(value) ? `${key}={${JSON.stringify(value)}}` : `${key}="${value}"`;
|
|
26
|
+
if (typeof value === "number" || typeof value === "boolean") return `${key}={${String(value)}}`;
|
|
27
|
+
if (typeof value === "function") return `${key}={() => {}}`;
|
|
28
|
+
return `${key}={${JSON.stringify(value)}}`;
|
|
29
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createElement, isValidElement, type ComponentType, type ReactNode } from "react";
|
|
2
|
+
import { componentKey } from "./manifest";
|
|
3
|
+
import type { AssetEntry, ComponentEntry, PatternEntry } from "./manifest-types";
|
|
4
|
+
import { assets, components, examples, patterns } from "../registry.generated";
|
|
5
|
+
|
|
6
|
+
/** The real component a manifest entry describes, from the registry `designtools-blocks map` generates. */
|
|
7
|
+
export function componentFor(entry: ComponentEntry): ComponentType<any> | undefined {
|
|
8
|
+
return components[componentKey(entry)];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** The examples module for a component: each named export is an example. */
|
|
12
|
+
export function examplesFor(entry: ComponentEntry): Record<string, unknown> | undefined {
|
|
13
|
+
return entry.examples ? examples[entry.examples.source] : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The pattern module: each named export is a composition. */
|
|
17
|
+
export function compositionsFor(pattern: PatternEntry): Record<string, unknown> | undefined {
|
|
18
|
+
return patterns[pattern.source];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A brand asset's URL, from its static import in the registry. */
|
|
22
|
+
export function assetUrl(asset: AssetEntry): string | undefined {
|
|
23
|
+
const imported = assets[asset.file];
|
|
24
|
+
return typeof imported === "string" ? imported : imported?.src;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Variant values are text in the manifest; boolean variants want booleans. */
|
|
28
|
+
export function coerce(values: Record<string, string>): Record<string, unknown> {
|
|
29
|
+
return Object.fromEntries(Object.entries(values).map(([k, v]) => [k, v === "true" ? true : v === "false" ? false : v]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function Missing({ name, what = "component" }: { name: string; what?: string }) {
|
|
33
|
+
return (
|
|
34
|
+
<p data-slot="block-missing" className="rounded-md border border-dashed border-border p-4 text-sm text-muted-foreground">
|
|
35
|
+
{name}'s {what} is not in the generated registry. Run <code className="font-mono">designtools-blocks map</code>{" "}
|
|
36
|
+
after rebuilding the manifest.
|
|
37
|
+
</p>
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** An example export is a component that takes no props, or an element. */
|
|
42
|
+
export function renderExample(value: unknown, name: string): ReactNode {
|
|
43
|
+
if (isValidElement(value)) return value;
|
|
44
|
+
if (typeof value === "function") return createElement(value as ComponentType);
|
|
45
|
+
return <span className="text-xs text-muted-foreground">{name} is not a component or an element.</span>;
|
|
46
|
+
}
|