@protolabsai/ui 0.15.0 → 0.17.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/package.json +4 -1
- package/src/ThemePanel.stories.tsx +56 -0
- package/src/styles/theming.css +150 -0
- package/src/styles.css +1 -0
- package/src/theming.tsx +383 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protolabsai/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"./data": "./src/data.tsx",
|
|
22
22
|
"./menu": "./src/menu.tsx",
|
|
23
23
|
"./app-shell": "./src/app-shell.tsx",
|
|
24
|
+
"./theming": "./src/theming.tsx",
|
|
24
25
|
"./styles.css": "./src/styles.css"
|
|
25
26
|
},
|
|
26
27
|
"files": [
|
|
@@ -34,6 +35,7 @@
|
|
|
34
35
|
"@radix-ui/react-popover": "^1.1.16",
|
|
35
36
|
"@types/react": "^19.0.0",
|
|
36
37
|
"@types/react-dom": "^19.0.0",
|
|
38
|
+
"culori": "^4.0.2",
|
|
37
39
|
"@protolabsai/design": "0.5.0"
|
|
38
40
|
},
|
|
39
41
|
"peerDependencies": {
|
|
@@ -43,6 +45,7 @@
|
|
|
43
45
|
"devDependencies": {
|
|
44
46
|
"@storybook/react": "^10.4.1",
|
|
45
47
|
"@storybook/react-vite": "^10.4.1",
|
|
48
|
+
"@types/culori": "^4.0.1",
|
|
46
49
|
"@vitejs/plugin-react": "^4.3.4",
|
|
47
50
|
"react": "^19.0.0",
|
|
48
51
|
"react-dom": "^19.0.0",
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react";
|
|
2
|
+
import { useState } from "react";
|
|
3
|
+
import { ThemePanel } from "./theming";
|
|
4
|
+
import { Badge, Button, Callout, Tag, TextLink } from "./primitives";
|
|
5
|
+
import { Alert, Progress, StatusDot } from "./data";
|
|
6
|
+
import { Tabs } from "./navigation";
|
|
7
|
+
|
|
8
|
+
const meta: Meta = { title: "Components/Theming/ThemePanel", parameters: { layout: "fullscreen" } };
|
|
9
|
+
export default meta;
|
|
10
|
+
type Story = StoryObj;
|
|
11
|
+
|
|
12
|
+
/** A spread of real components — watch them all repaint as you drag a picker. */
|
|
13
|
+
function Sample() {
|
|
14
|
+
const [tab, setTab] = useState("overview");
|
|
15
|
+
return (
|
|
16
|
+
<div style={{ display: "grid", gap: 16, maxWidth: 520 }}>
|
|
17
|
+
<div style={{ display: "flex", gap: 8, flexWrap: "wrap", alignItems: "center" }}>
|
|
18
|
+
<Button>Primary</Button>
|
|
19
|
+
<Button variant="ghost">Ghost</Button>
|
|
20
|
+
<Button variant="danger">Danger</Button>
|
|
21
|
+
<Badge status="info">v0.15.0</Badge>
|
|
22
|
+
<Tag>filter</Tag>
|
|
23
|
+
<StatusDot status="success" pulse label="connected" />
|
|
24
|
+
</div>
|
|
25
|
+
<Tabs
|
|
26
|
+
ariaLabel="Section"
|
|
27
|
+
items={[
|
|
28
|
+
{ id: "overview", label: "Overview" },
|
|
29
|
+
{ id: "activity", label: "Activity", badge: 12 },
|
|
30
|
+
{ id: "settings", label: "Settings" },
|
|
31
|
+
]}
|
|
32
|
+
active={tab}
|
|
33
|
+
onSelect={setTab}
|
|
34
|
+
/>
|
|
35
|
+
<Progress value={64} label="Indexing corpus" showValue />
|
|
36
|
+
<Alert status="info" title="Live theming">
|
|
37
|
+
Drag any swatch — every token resolves through <TextLink href="#">--pl-*</TextLink>, so the whole surface
|
|
38
|
+
repaints in real time.
|
|
39
|
+
</Alert>
|
|
40
|
+
<Callout tone="success" title="on brand">
|
|
41
|
+
Keep the lavender + indigo, or dial in a white-label accent and save it as a preset.
|
|
42
|
+
</Callout>
|
|
43
|
+
</div>
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const LiveEditor: Story = {
|
|
48
|
+
render: () => (
|
|
49
|
+
<div style={{ display: "flex", gap: 24, alignItems: "flex-start", padding: 24, flexWrap: "wrap" }}>
|
|
50
|
+
<ThemePanel />
|
|
51
|
+
<div style={{ flex: "1 1 360px" }}>
|
|
52
|
+
<Sample />
|
|
53
|
+
</div>
|
|
54
|
+
</div>
|
|
55
|
+
),
|
|
56
|
+
};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/* @protolabsai/ui — ThemePanel (live token editor). All --pl-* driven, so the
|
|
2
|
+
panel itself re-skins live as you edit. */
|
|
3
|
+
|
|
4
|
+
.pl-theme-panel {
|
|
5
|
+
display: flex;
|
|
6
|
+
flex-direction: column;
|
|
7
|
+
width: 100%;
|
|
8
|
+
max-width: 440px;
|
|
9
|
+
color: var(--pl-color-fg);
|
|
10
|
+
font-size: 13px;
|
|
11
|
+
background: var(--pl-color-bg-raised);
|
|
12
|
+
border: var(--pl-border-width) solid var(--pl-color-border);
|
|
13
|
+
border-radius: var(--pl-radius);
|
|
14
|
+
}
|
|
15
|
+
.pl-theme-panel__bar {
|
|
16
|
+
display: flex;
|
|
17
|
+
align-items: center;
|
|
18
|
+
gap: var(--pl-space-3);
|
|
19
|
+
padding: var(--pl-space-3);
|
|
20
|
+
border-bottom: var(--pl-border-width) solid var(--pl-color-border);
|
|
21
|
+
}
|
|
22
|
+
.pl-theme-panel__title {
|
|
23
|
+
font-weight: var(--pl-font-weight-medium);
|
|
24
|
+
}
|
|
25
|
+
.pl-theme-panel__spacer {
|
|
26
|
+
flex: 1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/* segmented light/dark control */
|
|
30
|
+
.pl-segmented {
|
|
31
|
+
display: inline-flex;
|
|
32
|
+
padding: 2px;
|
|
33
|
+
background: var(--pl-color-bg-inset);
|
|
34
|
+
border: var(--pl-border-width) solid var(--pl-color-border);
|
|
35
|
+
border-radius: var(--pl-radius);
|
|
36
|
+
}
|
|
37
|
+
.pl-segmented__btn {
|
|
38
|
+
padding: 3px 12px;
|
|
39
|
+
font: inherit;
|
|
40
|
+
font-size: 12px;
|
|
41
|
+
text-transform: capitalize;
|
|
42
|
+
color: var(--pl-color-fg-muted);
|
|
43
|
+
background: none;
|
|
44
|
+
border: none;
|
|
45
|
+
border-radius: calc(var(--pl-radius) - 1px);
|
|
46
|
+
cursor: pointer;
|
|
47
|
+
transition:
|
|
48
|
+
background var(--pl-motion-fast) var(--pl-motion-ease),
|
|
49
|
+
color var(--pl-motion-fast) var(--pl-motion-ease);
|
|
50
|
+
}
|
|
51
|
+
.pl-segmented__btn--active {
|
|
52
|
+
color: var(--pl-color-fg);
|
|
53
|
+
background: var(--pl-color-bg-raised);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
.pl-theme-panel__presets {
|
|
57
|
+
display: flex;
|
|
58
|
+
flex-wrap: wrap;
|
|
59
|
+
align-items: center;
|
|
60
|
+
gap: var(--pl-space-2);
|
|
61
|
+
padding: var(--pl-space-3);
|
|
62
|
+
border-bottom: var(--pl-border-width) solid var(--pl-color-border);
|
|
63
|
+
}
|
|
64
|
+
.pl-theme-panel__preset-select {
|
|
65
|
+
flex: 1 1 140px;
|
|
66
|
+
}
|
|
67
|
+
.pl-theme-panel__preset-name {
|
|
68
|
+
flex: 1 1 120px;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.pl-theme-panel__groups {
|
|
72
|
+
display: flex;
|
|
73
|
+
flex-direction: column;
|
|
74
|
+
gap: var(--pl-space-4);
|
|
75
|
+
max-height: 460px;
|
|
76
|
+
padding: var(--pl-space-3);
|
|
77
|
+
overflow-y: auto;
|
|
78
|
+
}
|
|
79
|
+
.pl-theme-group {
|
|
80
|
+
margin: 0;
|
|
81
|
+
padding: 0;
|
|
82
|
+
border: none;
|
|
83
|
+
}
|
|
84
|
+
.pl-theme-group__legend {
|
|
85
|
+
padding: 0 0 var(--pl-space-2);
|
|
86
|
+
font-size: 10px;
|
|
87
|
+
font-weight: var(--pl-font-weight-medium);
|
|
88
|
+
text-transform: uppercase;
|
|
89
|
+
letter-spacing: 0.06em;
|
|
90
|
+
color: var(--pl-color-fg-subtle);
|
|
91
|
+
}
|
|
92
|
+
.pl-theme-row {
|
|
93
|
+
display: flex;
|
|
94
|
+
align-items: center;
|
|
95
|
+
gap: var(--pl-space-2);
|
|
96
|
+
padding: 3px 0;
|
|
97
|
+
}
|
|
98
|
+
.pl-theme-row__swatch {
|
|
99
|
+
flex-shrink: 0;
|
|
100
|
+
width: 22px;
|
|
101
|
+
height: 22px;
|
|
102
|
+
padding: 0;
|
|
103
|
+
background: none;
|
|
104
|
+
border: var(--pl-border-width) solid var(--pl-color-border-strong);
|
|
105
|
+
border-radius: var(--pl-radius);
|
|
106
|
+
cursor: pointer;
|
|
107
|
+
}
|
|
108
|
+
.pl-theme-row__swatch::-webkit-color-swatch-wrapper {
|
|
109
|
+
padding: 0;
|
|
110
|
+
}
|
|
111
|
+
.pl-theme-row__swatch::-webkit-color-swatch {
|
|
112
|
+
border: none;
|
|
113
|
+
border-radius: calc(var(--pl-radius) - 1px);
|
|
114
|
+
}
|
|
115
|
+
.pl-theme-row__swatch::-moz-color-swatch {
|
|
116
|
+
border: none;
|
|
117
|
+
border-radius: calc(var(--pl-radius) - 1px);
|
|
118
|
+
}
|
|
119
|
+
.pl-theme-row__swatch--none {
|
|
120
|
+
border-color: transparent;
|
|
121
|
+
cursor: default;
|
|
122
|
+
}
|
|
123
|
+
.pl-theme-row__label {
|
|
124
|
+
flex: 1;
|
|
125
|
+
overflow: hidden;
|
|
126
|
+
color: var(--pl-color-fg-muted);
|
|
127
|
+
text-overflow: ellipsis;
|
|
128
|
+
white-space: nowrap;
|
|
129
|
+
}
|
|
130
|
+
.pl-theme-row__value {
|
|
131
|
+
flex-shrink: 0;
|
|
132
|
+
width: 116px;
|
|
133
|
+
padding: 3px 6px;
|
|
134
|
+
font-family: var(--pl-font-mono);
|
|
135
|
+
font-size: 11px;
|
|
136
|
+
color: var(--pl-color-fg);
|
|
137
|
+
background: var(--pl-color-bg-inset);
|
|
138
|
+
border: var(--pl-border-width) solid var(--pl-color-border);
|
|
139
|
+
border-radius: var(--pl-radius);
|
|
140
|
+
}
|
|
141
|
+
.pl-theme-row__value:focus-visible {
|
|
142
|
+
outline: 2px solid var(--pl-color-focus);
|
|
143
|
+
outline-offset: -1px;
|
|
144
|
+
}
|
|
145
|
+
.pl-theme-row--changed .pl-theme-row__label {
|
|
146
|
+
color: var(--pl-color-fg);
|
|
147
|
+
}
|
|
148
|
+
.pl-theme-row--changed .pl-theme-row__value {
|
|
149
|
+
border-color: var(--pl-color-accent);
|
|
150
|
+
}
|
package/src/styles.css
CHANGED
package/src/theming.tsx
ADDED
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from "react";
|
|
2
|
+
import { converter, formatHex, formatRgb, parse } from "culori";
|
|
3
|
+
import tokens from "@protolabsai/design/tokens.json";
|
|
4
|
+
import { Button } from "./primitives";
|
|
5
|
+
import { cx } from "./internal";
|
|
6
|
+
|
|
7
|
+
// ── Token registry, derived from @protolabsai/design so it stays in sync ────────
|
|
8
|
+
// Mirrors the design package's build flatten (kebab + `--pl` prefix) exactly, so
|
|
9
|
+
// the var names we write match the ones the tokens.css emits.
|
|
10
|
+
const kebab = (s: string) => s.replace(/([a-z0-9])([A-Z])/g, "$1-$2").toLowerCase();
|
|
11
|
+
function flatten(obj: Record<string, unknown>, prefix: string, acc: Record<string, string>) {
|
|
12
|
+
for (const [k, v] of Object.entries(obj)) {
|
|
13
|
+
const name = `${prefix}-${kebab(k)}`;
|
|
14
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
15
|
+
flatten(v as Record<string, unknown>, name, acc);
|
|
16
|
+
} else {
|
|
17
|
+
acc[name] = String(v);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return acc;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const T = tokens as {
|
|
24
|
+
color: Record<string, unknown>;
|
|
25
|
+
radius: unknown;
|
|
26
|
+
borderWidth: unknown;
|
|
27
|
+
light?: { color?: Record<string, unknown> };
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Default (dark) values for every editable token, keyed by CSS var. */
|
|
31
|
+
const DARK = flatten({ color: T.color, radius: T.radius, borderWidth: T.borderWidth }, "--pl", {});
|
|
32
|
+
/** Light values = dark base with the design package's light overrides layered on. */
|
|
33
|
+
const LIGHT = { ...DARK, ...flatten({ color: T.light?.color ?? {} }, "--pl", {}) };
|
|
34
|
+
|
|
35
|
+
export type ThemeMode = "light" | "dark";
|
|
36
|
+
|
|
37
|
+
type TokenGroup = { label: string; match: (v: string) => boolean };
|
|
38
|
+
// Colors + shape — the meaningful white-label knobs. Order = panel order.
|
|
39
|
+
const GROUPS: TokenGroup[] = [
|
|
40
|
+
{ label: "Brand", match: (v) => v.startsWith("--pl-color-brand-") },
|
|
41
|
+
{ label: "Accent", match: (v) => v.startsWith("--pl-color-accent") || v === "--pl-color-focus" },
|
|
42
|
+
{ label: "Surfaces", match: (v) => v.startsWith("--pl-color-bg") || v === "--pl-color-overlay" },
|
|
43
|
+
{ label: "Text", match: (v) => v.startsWith("--pl-color-fg") },
|
|
44
|
+
{ label: "Status", match: (v) => v.startsWith("--pl-color-status-") },
|
|
45
|
+
{ label: "Borders", match: (v) => v.startsWith("--pl-color-border") },
|
|
46
|
+
{ label: "Shape", match: (v) => v === "--pl-radius" || v === "--pl-border-width" },
|
|
47
|
+
];
|
|
48
|
+
const ALL_VARS = Object.keys(DARK);
|
|
49
|
+
|
|
50
|
+
const prettyLabel = (v: string) =>
|
|
51
|
+
v
|
|
52
|
+
.replace(/^--pl-(color-)?/, "")
|
|
53
|
+
.replace(/^(brand|status|accent|bg|fg|border)-?/, "")
|
|
54
|
+
.replace(/-/g, " ")
|
|
55
|
+
.trim()
|
|
56
|
+
.replace(/\b\w/g, (c) => c.toUpperCase()) || v.replace(/^--pl-/, "");
|
|
57
|
+
|
|
58
|
+
// ── Color format handling — every color token is pickable; a pick is written
|
|
59
|
+
// back in the token's OWN format (hex / rgb(a) / hsl / oklch / oklab), alpha kept.
|
|
60
|
+
const toRgb = converter("rgb");
|
|
61
|
+
const toHsl = converter("hsl");
|
|
62
|
+
const toOklch = converter("oklch");
|
|
63
|
+
const toOklab = converter("oklab");
|
|
64
|
+
const round = (n: number, d: number) => Math.round(n * 10 ** d) / 10 ** d;
|
|
65
|
+
|
|
66
|
+
/** A token value culori can read as a color (vs a length like radius/border-width). */
|
|
67
|
+
const isColor = (val: string) => !!parse(val);
|
|
68
|
+
|
|
69
|
+
/** Hex for the native `<input type="color">` (drops alpha, sRGB-clamped — display only). */
|
|
70
|
+
function toSwatchHex(val: string): string {
|
|
71
|
+
const c = parse(val);
|
|
72
|
+
return c ? formatHex(c) : "#000000";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function formatOf(val: string): "hex" | "rgb" | "hsl" | "oklch" | "oklab" | "other" {
|
|
76
|
+
const v = val.trim().toLowerCase();
|
|
77
|
+
if (v.startsWith("#")) return "hex";
|
|
78
|
+
if (v.startsWith("rgb")) return "rgb";
|
|
79
|
+
if (v.startsWith("hsl")) return "hsl";
|
|
80
|
+
if (v.startsWith("oklch")) return "oklch";
|
|
81
|
+
if (v.startsWith("oklab")) return "oklab";
|
|
82
|
+
return "other";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Reproject a freshly-picked hex into `original`'s color format, preserving its alpha. */
|
|
86
|
+
function reformat(pickedHex: string, original: string): string {
|
|
87
|
+
const picked = parse(pickedHex);
|
|
88
|
+
if (!picked) return pickedHex;
|
|
89
|
+
const alpha = parse(original)?.alpha;
|
|
90
|
+
const hasA = alpha != null && alpha < 1;
|
|
91
|
+
const aTail = hasA ? ` / ${alpha}` : "";
|
|
92
|
+
switch (formatOf(original)) {
|
|
93
|
+
case "rgb":
|
|
94
|
+
return formatRgb({ ...toRgb(picked), alpha: hasA ? alpha : undefined });
|
|
95
|
+
case "hsl": {
|
|
96
|
+
const h = toHsl(picked);
|
|
97
|
+
return `hsl(${round(h.h ?? 0, 1)} ${round((h.s ?? 0) * 100, 1)}% ${round((h.l ?? 0) * 100, 1)}%${aTail})`;
|
|
98
|
+
}
|
|
99
|
+
case "oklch": {
|
|
100
|
+
const o = toOklch(picked);
|
|
101
|
+
return `oklch(${round(o.l ?? 0, 4)} ${round(o.c ?? 0, 4)} ${round(o.h ?? 0, 2)}${aTail})`;
|
|
102
|
+
}
|
|
103
|
+
case "oklab": {
|
|
104
|
+
const o = toOklab(picked);
|
|
105
|
+
return `oklab(${round(o.l ?? 0, 4)} ${round(o.a ?? 0, 4)} ${round(o.b ?? 0, 4)}${aTail})`;
|
|
106
|
+
}
|
|
107
|
+
default:
|
|
108
|
+
return formatHex(picked);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// ── Presets ─────────────────────────────────────────────────────────────────────
|
|
113
|
+
export type ThemePreset = {
|
|
114
|
+
id: string;
|
|
115
|
+
name: string;
|
|
116
|
+
mode: ThemeMode;
|
|
117
|
+
/** CSS-var → value overrides layered on the base theme. */
|
|
118
|
+
overrides: Record<string, string>;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const BUILTIN_PRESETS: ThemePreset[] = [
|
|
122
|
+
{ id: "pl-dark", name: "protoLabs Dark", mode: "dark", overrides: {} },
|
|
123
|
+
{ id: "pl-light", name: "protoLabs Light", mode: "light", overrides: {} },
|
|
124
|
+
{
|
|
125
|
+
id: "ex-amber",
|
|
126
|
+
name: "Amber (example)",
|
|
127
|
+
mode: "dark",
|
|
128
|
+
overrides: {
|
|
129
|
+
"--pl-color-accent": "#f59e0b",
|
|
130
|
+
"--pl-color-accent-hover": "#d97706",
|
|
131
|
+
"--pl-color-accent-fg": "#fbbf24",
|
|
132
|
+
"--pl-color-focus": "#f59e0b",
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
id: "ex-teal",
|
|
137
|
+
name: "Teal (example)",
|
|
138
|
+
mode: "dark",
|
|
139
|
+
overrides: {
|
|
140
|
+
"--pl-color-accent": "#14b8a6",
|
|
141
|
+
"--pl-color-accent-hover": "#0d9488",
|
|
142
|
+
"--pl-color-accent-fg": "#2dd4bf",
|
|
143
|
+
"--pl-color-focus": "#14b8a6",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
const LS_THEME = "pl-theme";
|
|
149
|
+
const LS_PRESETS = "pl-theme-presets";
|
|
150
|
+
|
|
151
|
+
const root = () => document.documentElement;
|
|
152
|
+
const applyMode = (mode: ThemeMode) => root().setAttribute("data-theme", mode);
|
|
153
|
+
const applyOverrides = (overrides: Record<string, string>) => {
|
|
154
|
+
for (const [k, v] of Object.entries(overrides)) root().style.setProperty(k, v);
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
function readStored(): { mode: ThemeMode; overrides: Record<string, string> } | null {
|
|
158
|
+
try {
|
|
159
|
+
const raw = localStorage.getItem(LS_THEME);
|
|
160
|
+
return raw ? (JSON.parse(raw) as { mode: ThemeMode; overrides: Record<string, string> }) : null;
|
|
161
|
+
} catch {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Apply the persisted theme (mode + overrides). Call once on app boot — ideally
|
|
167
|
+
* before first paint — so a user's saved theme survives reloads without a flash. */
|
|
168
|
+
export function applyStoredTheme() {
|
|
169
|
+
const t = readStored();
|
|
170
|
+
if (!t) return;
|
|
171
|
+
applyMode(t.mode);
|
|
172
|
+
applyOverrides(t.overrides);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** Live theme editor — maps every color + shape token to a control, repaints the
|
|
176
|
+
* document in real time as you drag a picker, toggles light/dark, and saves named
|
|
177
|
+
* presets (localStorage) you can switch back to. White-label control surface. */
|
|
178
|
+
export function ThemePanel({
|
|
179
|
+
className,
|
|
180
|
+
presets: extraPresets = [],
|
|
181
|
+
}: {
|
|
182
|
+
className?: string;
|
|
183
|
+
/** Extra read-only presets to offer alongside the built-ins. */
|
|
184
|
+
presets?: ThemePreset[];
|
|
185
|
+
}) {
|
|
186
|
+
const [mode, setMode] = useState<ThemeMode>("dark");
|
|
187
|
+
const [overrides, setOverrides] = useState<Record<string, string>>({});
|
|
188
|
+
const [userPresets, setUserPresets] = useState<ThemePreset[]>([]);
|
|
189
|
+
const [presetName, setPresetName] = useState("");
|
|
190
|
+
const [copied, setCopied] = useState("");
|
|
191
|
+
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
const t = readStored();
|
|
194
|
+
if (t) {
|
|
195
|
+
setMode(t.mode);
|
|
196
|
+
setOverrides(t.overrides);
|
|
197
|
+
applyMode(t.mode);
|
|
198
|
+
applyOverrides(t.overrides);
|
|
199
|
+
} else {
|
|
200
|
+
const cur = root().getAttribute("data-theme");
|
|
201
|
+
setMode(cur === "light" ? "light" : "dark");
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
const p = localStorage.getItem(LS_PRESETS);
|
|
205
|
+
if (p) setUserPresets(JSON.parse(p) as ThemePreset[]);
|
|
206
|
+
} catch {
|
|
207
|
+
/* ignore */
|
|
208
|
+
}
|
|
209
|
+
}, []);
|
|
210
|
+
|
|
211
|
+
const persist = useCallback((m: ThemeMode, o: Record<string, string>) => {
|
|
212
|
+
try {
|
|
213
|
+
localStorage.setItem(LS_THEME, JSON.stringify({ mode: m, overrides: o }));
|
|
214
|
+
} catch {
|
|
215
|
+
/* ignore */
|
|
216
|
+
}
|
|
217
|
+
}, []);
|
|
218
|
+
|
|
219
|
+
const baseFor = (cssVar: string) => (mode === "light" ? LIGHT : DARK)[cssVar];
|
|
220
|
+
const valueOf = (cssVar: string) => overrides[cssVar] ?? baseFor(cssVar);
|
|
221
|
+
|
|
222
|
+
const setVar = (cssVar: string, value: string) => {
|
|
223
|
+
root().style.setProperty(cssVar, value);
|
|
224
|
+
setOverrides((prev) => {
|
|
225
|
+
const next = { ...prev, [cssVar]: value };
|
|
226
|
+
persist(mode, next);
|
|
227
|
+
return next;
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const changeMode = (m: ThemeMode) => {
|
|
232
|
+
setMode(m);
|
|
233
|
+
applyMode(m);
|
|
234
|
+
persist(m, overrides);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const reset = () => {
|
|
238
|
+
for (const k of Object.keys(overrides)) root().style.removeProperty(k);
|
|
239
|
+
setOverrides({});
|
|
240
|
+
persist(mode, {});
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
const applyPreset = (id: string) => {
|
|
244
|
+
if (!id) return;
|
|
245
|
+
const p = [...BUILTIN_PRESETS, ...extraPresets, ...userPresets].find((x) => x.id === id);
|
|
246
|
+
if (!p) return;
|
|
247
|
+
for (const k of Object.keys(overrides)) root().style.removeProperty(k);
|
|
248
|
+
setMode(p.mode);
|
|
249
|
+
applyMode(p.mode);
|
|
250
|
+
applyOverrides(p.overrides);
|
|
251
|
+
setOverrides(p.overrides);
|
|
252
|
+
persist(p.mode, p.overrides);
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const savePreset = () => {
|
|
256
|
+
const name = presetName.trim();
|
|
257
|
+
if (!name) return;
|
|
258
|
+
const p: ThemePreset = { id: `user-${name.toLowerCase().replace(/\s+/g, "-")}`, name, mode, overrides };
|
|
259
|
+
const next = [...userPresets.filter((x) => x.id !== p.id), p];
|
|
260
|
+
setUserPresets(next);
|
|
261
|
+
setPresetName("");
|
|
262
|
+
try {
|
|
263
|
+
localStorage.setItem(LS_PRESETS, JSON.stringify(next));
|
|
264
|
+
} catch {
|
|
265
|
+
/* ignore */
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const copy = (text: string, what: string) => {
|
|
270
|
+
navigator.clipboard?.writeText(text);
|
|
271
|
+
setCopied(what);
|
|
272
|
+
setTimeout(() => setCopied(""), 1400);
|
|
273
|
+
};
|
|
274
|
+
const exportCss = () => {
|
|
275
|
+
const body = Object.entries(overrides)
|
|
276
|
+
.map(([k, v]) => ` ${k}: ${v};`)
|
|
277
|
+
.join("\n");
|
|
278
|
+
copy(`:root {\n${body}\n}`, "css");
|
|
279
|
+
};
|
|
280
|
+
const exportJson = () => copy(JSON.stringify({ mode, overrides }, null, 2), "json");
|
|
281
|
+
|
|
282
|
+
const dirty = Object.keys(overrides).length;
|
|
283
|
+
const allPresets = [...BUILTIN_PRESETS, ...extraPresets, ...userPresets];
|
|
284
|
+
|
|
285
|
+
return (
|
|
286
|
+
<div className={cx("pl-theme-panel", className)}>
|
|
287
|
+
<div className="pl-theme-panel__bar">
|
|
288
|
+
<span className="pl-theme-panel__title">Theme</span>
|
|
289
|
+
<div className="pl-segmented" role="group" aria-label="Color scheme">
|
|
290
|
+
{(["dark", "light"] as const).map((m) => (
|
|
291
|
+
<button
|
|
292
|
+
key={m}
|
|
293
|
+
type="button"
|
|
294
|
+
className={cx("pl-segmented__btn", mode === m && "pl-segmented__btn--active")}
|
|
295
|
+
aria-pressed={mode === m}
|
|
296
|
+
onClick={() => changeMode(m)}
|
|
297
|
+
>
|
|
298
|
+
{m}
|
|
299
|
+
</button>
|
|
300
|
+
))}
|
|
301
|
+
</div>
|
|
302
|
+
<span className="pl-theme-panel__spacer" />
|
|
303
|
+
<Button size="sm" variant="ghost" onClick={reset} disabled={!dirty}>
|
|
304
|
+
Reset{dirty ? ` (${dirty})` : ""}
|
|
305
|
+
</Button>
|
|
306
|
+
</div>
|
|
307
|
+
|
|
308
|
+
<div className="pl-theme-panel__presets">
|
|
309
|
+
<select
|
|
310
|
+
className="pl-input pl-select pl-theme-panel__preset-select"
|
|
311
|
+
aria-label="Apply preset"
|
|
312
|
+
value=""
|
|
313
|
+
onChange={(e) => applyPreset(e.target.value)}
|
|
314
|
+
>
|
|
315
|
+
<option value="">Apply preset…</option>
|
|
316
|
+
{allPresets.map((p) => (
|
|
317
|
+
<option key={p.id} value={p.id}>
|
|
318
|
+
{p.name}
|
|
319
|
+
</option>
|
|
320
|
+
))}
|
|
321
|
+
</select>
|
|
322
|
+
<input
|
|
323
|
+
className="pl-input pl-theme-panel__preset-name"
|
|
324
|
+
placeholder="Save current as…"
|
|
325
|
+
value={presetName}
|
|
326
|
+
onChange={(e) => setPresetName(e.target.value)}
|
|
327
|
+
onKeyDown={(e) => e.key === "Enter" && savePreset()}
|
|
328
|
+
/>
|
|
329
|
+
<Button size="sm" onClick={savePreset} disabled={!presetName.trim()}>
|
|
330
|
+
Save
|
|
331
|
+
</Button>
|
|
332
|
+
<span className="pl-theme-panel__spacer" />
|
|
333
|
+
<Button size="sm" variant="ghost" onClick={exportCss} disabled={!dirty}>
|
|
334
|
+
{copied === "css" ? "Copied" : "Copy CSS"}
|
|
335
|
+
</Button>
|
|
336
|
+
<Button size="sm" variant="ghost" onClick={exportJson} disabled={!dirty}>
|
|
337
|
+
{copied === "json" ? "Copied" : "Copy JSON"}
|
|
338
|
+
</Button>
|
|
339
|
+
</div>
|
|
340
|
+
|
|
341
|
+
<div className="pl-theme-panel__groups">
|
|
342
|
+
{GROUPS.map((g) => {
|
|
343
|
+
const vars = ALL_VARS.filter(g.match);
|
|
344
|
+
if (!vars.length) return null;
|
|
345
|
+
return (
|
|
346
|
+
<fieldset key={g.label} className="pl-theme-group">
|
|
347
|
+
<legend className="pl-theme-group__legend">{g.label}</legend>
|
|
348
|
+
{vars.map((v) => {
|
|
349
|
+
const val = valueOf(v);
|
|
350
|
+
const color = isColor(val);
|
|
351
|
+
const changed = v in overrides;
|
|
352
|
+
return (
|
|
353
|
+
<label key={v} className={cx("pl-theme-row", changed && "pl-theme-row--changed")}>
|
|
354
|
+
{color ? (
|
|
355
|
+
<input
|
|
356
|
+
type="color"
|
|
357
|
+
className="pl-theme-row__swatch"
|
|
358
|
+
value={toSwatchHex(val)}
|
|
359
|
+
onInput={(e) => setVar(v, reformat((e.target as HTMLInputElement).value, val))}
|
|
360
|
+
aria-label={prettyLabel(v)}
|
|
361
|
+
/>
|
|
362
|
+
) : (
|
|
363
|
+
<span className="pl-theme-row__swatch pl-theme-row__swatch--none" aria-hidden />
|
|
364
|
+
)}
|
|
365
|
+
<span className="pl-theme-row__label" title={v}>
|
|
366
|
+
{prettyLabel(v)}
|
|
367
|
+
</span>
|
|
368
|
+
<input
|
|
369
|
+
className="pl-theme-row__value"
|
|
370
|
+
value={val}
|
|
371
|
+
spellCheck={false}
|
|
372
|
+
onChange={(e) => setVar(v, e.target.value)}
|
|
373
|
+
/>
|
|
374
|
+
</label>
|
|
375
|
+
);
|
|
376
|
+
})}
|
|
377
|
+
</fieldset>
|
|
378
|
+
);
|
|
379
|
+
})}
|
|
380
|
+
</div>
|
|
381
|
+
</div>
|
|
382
|
+
);
|
|
383
|
+
}
|