@open-document/core 0.9.1 → 0.11.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.
Files changed (86) hide show
  1. package/dist/{build-B0GJXy8I.js → build-BHFGY8eF.js} +1 -1
  2. package/dist/{check-CuDIWsPT.js → check-UEBmDfT_.js} +3 -3
  3. package/dist/cli/bin.js +10 -6
  4. package/dist/{config-CAZ7Z_va.js → config-B_pdNbYg.js} +1166 -591
  5. package/dist/{context-PWAh9qbf.js → context-BM9_-u9P.js} +1 -1
  6. package/dist/{dev-CYqppXDI.js → dev-DX3QMIRi.js} +1 -1
  7. package/dist/{diagram-CCaUUnRe.js → diagram-BK4jTZr4.js} +146 -146
  8. package/dist/diff-DayvbXfX.js +44 -0
  9. package/dist/{export-1JtO2OMC.js → export-BPsXj1i6.js} +3 -3
  10. package/dist/{import-CsrdBOZ8.js → import-71wlAsrd.js} +2 -2
  11. package/dist/index.d.ts +53 -2
  12. package/dist/index.js +457 -18
  13. package/dist/ops/index.d.ts +72 -17
  14. package/dist/ops/index.js +3 -3
  15. package/dist/{ops-B2y9BMpk.js → ops-BUo67DaP.js} +642 -124
  16. package/dist/{preview-DW3PM-JV.js → preview-D3m1-gUL.js} +1 -1
  17. package/dist/{sdk-BqU6mwD_.d.ts → sdk-CpDEGRIo.d.ts} +2 -0
  18. package/dist/vite/index.js +1 -1
  19. package/package.json +2 -2
  20. package/skills/doc-authoring/SKILL.md +9 -4
  21. package/skills/doc-authoring/references/pagination.md +1 -1
  22. package/skills/doc-authoring/references/tables-and-charts.md +18 -1
  23. package/src/app/components/browser/browser-ui.tsx +105 -0
  24. package/src/app/components/chart.tsx +239 -0
  25. package/src/app/components/command-palette.tsx +284 -0
  26. package/src/app/components/design-panel/design-panel.tsx +22 -20
  27. package/src/app/components/design-panel/design-provider.tsx +4 -2
  28. package/src/app/components/diagram.tsx +11 -1
  29. package/src/app/components/doc-assets.tsx +6 -5
  30. package/src/app/components/doc-search.tsx +12 -10
  31. package/src/app/components/doc-sidebar.tsx +24 -18
  32. package/src/app/components/flow-page.tsx +2 -0
  33. package/src/app/components/inspector/format-section.tsx +434 -0
  34. package/src/app/components/inspector/inspector.tsx +310 -89
  35. package/src/app/components/inspector/use-style-edits.ts +210 -0
  36. package/src/app/components/language-toggle.tsx +38 -0
  37. package/src/app/components/palette-items.ts +128 -0
  38. package/src/app/components/panel/edit-save-card.tsx +4 -2
  39. package/src/app/components/panel/fields.tsx +100 -9
  40. package/src/app/components/panel/save-card.tsx +18 -10
  41. package/src/app/components/sidebar/folder-item.tsx +10 -8
  42. package/src/app/components/sidebar/icon-picker.tsx +3 -1
  43. package/src/app/components/sidebar/sidebar.tsx +44 -13
  44. package/src/app/components/theme-toggle.tsx +5 -3
  45. package/src/app/components/ui/menu.tsx +5 -0
  46. package/src/app/lib/agent-bridge.ts +8 -2
  47. package/src/app/lib/chart.test.ts +108 -0
  48. package/src/app/lib/chart.ts +343 -0
  49. package/src/app/lib/doc-preview.tsx +0 -10
  50. package/src/app/lib/docs.ts +3 -0
  51. package/src/app/lib/docx/from-dom.ts +11 -11
  52. package/src/app/lib/docx/ooxml.test.ts +25 -9
  53. package/src/app/lib/docx/ooxml.ts +58 -37
  54. package/src/app/lib/export-docx.ts +13 -8
  55. package/src/app/lib/flow.ts +1 -1
  56. package/src/app/lib/i18n-ja/browser.ts +88 -0
  57. package/src/app/lib/i18n-ja/doc.ts +65 -0
  58. package/src/app/lib/i18n-ja/index.ts +5 -0
  59. package/src/app/lib/i18n-ja/panels.ts +151 -0
  60. package/src/app/lib/i18n-ko/browser.ts +87 -0
  61. package/src/app/lib/i18n-ko/doc.ts +65 -0
  62. package/src/app/lib/i18n-ko/index.ts +5 -0
  63. package/src/app/lib/i18n-ko/panels.ts +148 -0
  64. package/src/app/lib/i18n-zh-cn/browser.ts +84 -0
  65. package/src/app/lib/i18n-zh-cn/doc.ts +64 -0
  66. package/src/app/lib/i18n-zh-cn/index.ts +5 -0
  67. package/src/app/lib/i18n-zh-cn/panels.ts +143 -0
  68. package/src/app/lib/i18n-zh-tw/browser.ts +84 -0
  69. package/src/app/lib/i18n-zh-tw/doc.ts +64 -0
  70. package/src/app/lib/i18n-zh-tw/index.ts +5 -0
  71. package/src/app/lib/i18n-zh-tw/panels.ts +144 -0
  72. package/src/app/lib/i18n.test.ts +24 -0
  73. package/src/app/lib/i18n.tsx +109 -0
  74. package/src/app/lib/inspector/format.ts +156 -0
  75. package/src/app/lib/outline.ts +23 -0
  76. package/src/app/lib/page-range.test.ts +10 -1
  77. package/src/app/lib/page-range.ts +26 -0
  78. package/src/app/lib/sdk.ts +2 -0
  79. package/src/app/main.tsx +4 -1
  80. package/src/app/routes/assets.tsx +193 -132
  81. package/src/app/routes/doc.tsx +668 -310
  82. package/src/app/routes/home-shell.tsx +115 -88
  83. package/src/app/routes/home.tsx +200 -67
  84. package/src/app/routes/themes.tsx +42 -35
  85. package/src/app/styles.css +84 -5
  86. package/src/app/virtual.d.ts +1 -0
@@ -0,0 +1,210 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'react';
2
+ import {
3
+ cssName,
4
+ cssValue,
5
+ type StyleChanges,
6
+ type StyleOrigin,
7
+ type StyleProp,
8
+ type StyleValue,
9
+ } from '../../lib/inspector/format';
10
+ import { PROP_ATTR } from '../../lib/source-loc';
11
+ import { useHistory } from '../history-provider';
12
+
13
+ const LOC_ATTR = 'data-od-loc';
14
+
15
+ export type StyleTarget = { line: number; column: number; tag: string };
16
+
17
+ /** A style edit waiting for Save, plus what the panel was shown when it began. */
18
+ export type PendingStyle = StyleTarget & {
19
+ changes: StyleChanges;
20
+ expected: Partial<Record<StyleProp, StyleOrigin | null>>;
21
+ };
22
+
23
+ type Original = { value: string; priority: string };
24
+
25
+ export const styleKey = (target: StyleTarget) => `${target.line}:${target.column}`;
26
+
27
+ /**
28
+ * Every element printed by one source location — a `.map()` or a flow block
29
+ * split across pages prints the same JSX more than once, and a source edit
30
+ * changes them all, so the preview has to as well.
31
+ */
32
+ export function elementsAt(root: ParentNode, target: StyleTarget): HTMLElement[] {
33
+ return [
34
+ ...root.querySelectorAll<HTMLElement>(
35
+ `[${LOC_ATTR}="${styleKey(target)}"]:not([${PROP_ATTR}])`,
36
+ ),
37
+ ].filter((el) => el.tagName.toLowerCase() === target.tag);
38
+ }
39
+
40
+ /**
41
+ * Style changes from the element panel, shown on the page before they are
42
+ * written. A change is set on the elements' inline style, over whatever React
43
+ * rendered there, and every value it covered is kept so Discard can put it
44
+ * back. The edits themselves live in a ref, keyed by source location, so a
45
+ * hot reload that replaces the elements can lay them on the new ones.
46
+ */
47
+ export function useStyleEdits(container: HTMLElement | null) {
48
+ const history = useHistory();
49
+ const editsRef = useRef(new Map<string, PendingStyle>());
50
+ const originalsRef = useRef(new Map<HTMLElement, Map<string, Original>>());
51
+ const [version, setVersion] = useState(0);
52
+ const bump = useCallback(() => setVersion((v) => v + 1), []);
53
+
54
+ const restore = useCallback((el: HTMLElement, names?: Set<string>) => {
55
+ const saved = originalsRef.current.get(el);
56
+ if (!saved) return;
57
+ for (const [name, original] of saved) {
58
+ if (names && !names.has(name)) continue;
59
+ if (original.value === '') el.style.removeProperty(name);
60
+ else el.style.setProperty(name, original.value, original.priority);
61
+ saved.delete(name);
62
+ }
63
+ if (saved.size === 0) originalsRef.current.delete(el);
64
+ }, []);
65
+
66
+ const paint = useCallback(
67
+ (edit: PendingStyle) => {
68
+ if (!container) return;
69
+ for (const el of elementsAt(container, edit)) {
70
+ let saved = originalsRef.current.get(el);
71
+ if (!saved) {
72
+ saved = new Map();
73
+ originalsRef.current.set(el, saved);
74
+ }
75
+ const wanted = new Set<string>();
76
+ for (const [prop, value] of Object.entries(edit.changes) as Array<
77
+ [StyleProp, StyleValue]
78
+ >) {
79
+ const name = cssName(prop);
80
+ wanted.add(name);
81
+ if (!saved.has(name)) {
82
+ saved.set(name, {
83
+ value: el.style.getPropertyValue(name),
84
+ priority: el.style.getPropertyPriority(name),
85
+ });
86
+ }
87
+ if (value === null) el.style.removeProperty(name);
88
+ else el.style.setProperty(name, cssValue(prop, value));
89
+ }
90
+ const stale = new Set([...saved.keys()].filter((name) => !wanted.has(name)));
91
+ if (stale.size > 0) restore(el, stale);
92
+ }
93
+ },
94
+ [container, restore],
95
+ );
96
+
97
+ /** Replaces a location's pending changes wholesale — the one way in, for edits and undo alike. */
98
+ const put = useCallback(
99
+ (key: string, next: PendingStyle | null) => {
100
+ const prev = editsRef.current.get(key);
101
+ if (next && Object.keys(next.changes).length > 0) {
102
+ editsRef.current.set(key, next);
103
+ paint(next);
104
+ } else {
105
+ editsRef.current.delete(key);
106
+ if (prev && container) for (const el of elementsAt(container, prev)) restore(el);
107
+ }
108
+ bump();
109
+ },
110
+ [paint, restore, container, bump],
111
+ );
112
+
113
+ const change = useCallback(
114
+ /** `undefined` takes back an unsaved change; `null` takes the key off in source. */
115
+ (
116
+ target: StyleTarget,
117
+ prop: StyleProp,
118
+ value: StyleValue | undefined,
119
+ shown: StyleOrigin | null,
120
+ ) => {
121
+ const key = styleKey(target);
122
+ const prev = editsRef.current.get(key) ?? null;
123
+ const base: PendingStyle = prev ?? { ...target, changes: {}, expected: {} };
124
+ const changes = { ...base.changes };
125
+ const expected = { ...base.expected };
126
+ if (value === undefined) {
127
+ delete changes[prop];
128
+ delete expected[prop];
129
+ } else {
130
+ changes[prop] = value;
131
+ if (!(prop in expected)) expected[prop] = shown;
132
+ }
133
+ const next: PendingStyle = { ...base, changes, expected };
134
+ put(key, next);
135
+ history.record({
136
+ coalesceKey: `style:${key}:${prop}`,
137
+ undo: () => put(key, prev),
138
+ redo: () => put(key, next),
139
+ });
140
+ },
141
+ [put, history],
142
+ );
143
+
144
+ const pendingFor = useCallback(
145
+ (target: StyleTarget | null): StyleChanges =>
146
+ (target && editsRef.current.get(styleKey(target))?.changes) || {},
147
+ [],
148
+ );
149
+
150
+ const discard = useCallback(() => {
151
+ for (const key of [...editsRef.current.keys()]) put(key, null);
152
+ }, [put]);
153
+
154
+ /** Lets go of saved locations; the reload that follows renders them from source. */
155
+ const forget = useCallback(
156
+ (keys: string[]) => {
157
+ for (const key of keys) {
158
+ editsRef.current.delete(key);
159
+ }
160
+ for (const [el] of originalsRef.current) {
161
+ if (keys.includes(el.getAttribute(LOC_ATTR) ?? '')) originalsRef.current.delete(el);
162
+ }
163
+ bump();
164
+ },
165
+ [bump],
166
+ );
167
+
168
+ /** After a reload replaced elements, the unsaved changes go onto the new ones. */
169
+ const repaint = useCallback(() => {
170
+ for (const el of [...originalsRef.current.keys()]) {
171
+ if (!el.isConnected) originalsRef.current.delete(el);
172
+ }
173
+ for (const edit of editsRef.current.values()) paint(edit);
174
+ }, [paint]);
175
+
176
+ useEffect(() => {
177
+ const originals = originalsRef.current;
178
+ return () => {
179
+ for (const el of [...originals.keys()]) {
180
+ const saved = originals.get(el);
181
+ if (!saved) continue;
182
+ for (const [name, original] of saved) {
183
+ if (original.value === '') el.style.removeProperty(name);
184
+ else el.style.setProperty(name, original.value, original.priority);
185
+ }
186
+ }
187
+ originals.clear();
188
+ };
189
+ }, []);
190
+
191
+ const edits = useCallback(() => [...editsRef.current.values()], []);
192
+ const revert = useCallback(
193
+ (keys: string[]) => {
194
+ for (const key of keys) put(key, null);
195
+ },
196
+ [put],
197
+ );
198
+
199
+ return {
200
+ version,
201
+ count: editsRef.current.size,
202
+ edits,
203
+ change,
204
+ pendingFor,
205
+ discard,
206
+ forget,
207
+ revert,
208
+ repaint,
209
+ };
210
+ }
@@ -0,0 +1,38 @@
1
+ import { Languages } from 'lucide-react';
2
+ import { LOCALES, useLocale } from '../lib/i18n';
3
+ import { Menu, MenuItem } from './ui/menu';
4
+
5
+ export function LanguageToggle() {
6
+ const { locale, setLocale, t } = useLocale();
7
+
8
+ return (
9
+ <Menu
10
+ trigger={(props) => (
11
+ <button
12
+ type="button"
13
+ aria-label={t('Language')}
14
+ title={t('Language')}
15
+ className="relative flex size-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-accent hover:text-foreground aria-expanded:bg-accent"
16
+ {...props}
17
+ >
18
+ <Languages className="size-3.5" />
19
+ </button>
20
+ )}
21
+ >
22
+ {(close) =>
23
+ LOCALES.map(({ value, label }) => (
24
+ <MenuItem
25
+ key={value}
26
+ active={locale === value}
27
+ onClick={() => {
28
+ setLocale(value);
29
+ close();
30
+ }}
31
+ >
32
+ {label}
33
+ </MenuItem>
34
+ ))
35
+ }
36
+ </Menu>
37
+ );
38
+ }
@@ -0,0 +1,128 @@
1
+ import { FileText, Folder, Image, Languages, Monitor, Moon, Palette, Sun } from 'lucide-react';
2
+ import { docIds, docTitles } from '../lib/docs';
3
+ import { LOCALES, type Locale, type Translate } from '../lib/i18n';
4
+ import type { FoldersManifest } from '../lib/sdk';
5
+ import { themes } from '../lib/themes';
6
+ import type { PaletteItem } from './command-palette';
7
+
8
+ /** What the palette offers everywhere: every document, folder, theme and page, and the appearance. */
9
+ export function browserItems({
10
+ navigate,
11
+ manifest,
12
+ setTheme,
13
+ t,
14
+ setLocale,
15
+ exclude,
16
+ }: {
17
+ navigate: (to: string) => void;
18
+ manifest: FoldersManifest | null;
19
+ setTheme: (theme: string) => void;
20
+ t: Translate;
21
+ /** Offers the languages too; left out where switching would not be expected. */
22
+ setLocale?: (locale: Locale) => void;
23
+ /** A document not to offer — the one already open. */
24
+ exclude?: string;
25
+ }): PaletteItem[] {
26
+ const documents = docIds
27
+ .filter((id) => id !== exclude)
28
+ .map(
29
+ (id): PaletteItem => ({
30
+ id: `doc:${id}`,
31
+ group: t('Documents'),
32
+ label: docTitles[id] ?? id,
33
+ keywords: id,
34
+ icon: FileText,
35
+ run: () => navigate(`/d/${id}`),
36
+ }),
37
+ )
38
+ .sort((a, b) => a.label.localeCompare(b.label));
39
+
40
+ const folders = (manifest?.folders ?? []).map(
41
+ (folder): PaletteItem => ({
42
+ id: `folder:${folder.id}`,
43
+ group: t('Folders'),
44
+ label: folder.name,
45
+ icon: Folder,
46
+ run: () => navigate(`/?f=${encodeURIComponent(folder.id)}`),
47
+ }),
48
+ );
49
+
50
+ const themeItems = themes.map(
51
+ (theme): PaletteItem => ({
52
+ id: `theme:${theme.id}`,
53
+ group: t('Themes'),
54
+ label: theme.name,
55
+ keywords: `${theme.id} theme`,
56
+ icon: Palette,
57
+ run: () => navigate(`/themes/${theme.id}`),
58
+ }),
59
+ );
60
+
61
+ const pages: PaletteItem[] = [
62
+ {
63
+ id: 'go:docs',
64
+ group: t('Go to'),
65
+ label: t('All documents'),
66
+ icon: FileText,
67
+ run: () => navigate('/'),
68
+ },
69
+ {
70
+ id: 'go:themes',
71
+ group: t('Go to'),
72
+ label: t('Themes'),
73
+ icon: Palette,
74
+ run: () => navigate('/themes'),
75
+ },
76
+ ...(import.meta.env.DEV
77
+ ? [
78
+ {
79
+ id: 'go:assets',
80
+ group: t('Go to'),
81
+ label: t('Assets'),
82
+ icon: Image,
83
+ run: () => navigate('/assets'),
84
+ },
85
+ ]
86
+ : []),
87
+ ];
88
+
89
+ const appearance: PaletteItem[] = [
90
+ {
91
+ id: 'theme:light',
92
+ group: t('Appearance'),
93
+ label: t('Light theme'),
94
+ keywords: 'appearance mode',
95
+ icon: Sun,
96
+ run: () => setTheme('light'),
97
+ },
98
+ {
99
+ id: 'theme:dark',
100
+ group: t('Appearance'),
101
+ label: t('Dark theme'),
102
+ keywords: 'appearance mode',
103
+ icon: Moon,
104
+ run: () => setTheme('dark'),
105
+ },
106
+ {
107
+ id: 'theme:system',
108
+ group: t('Appearance'),
109
+ label: t('System theme'),
110
+ keywords: 'appearance mode auto',
111
+ icon: Monitor,
112
+ run: () => setTheme('system'),
113
+ },
114
+ ];
115
+
116
+ const languages: PaletteItem[] = setLocale
117
+ ? LOCALES.map(({ value, label }) => ({
118
+ id: `locale:${value}`,
119
+ group: t('Language'),
120
+ label,
121
+ keywords: 'language 語言',
122
+ icon: Languages,
123
+ run: () => setLocale(value),
124
+ }))
125
+ : [];
126
+
127
+ return [...documents, ...folders, ...pages, ...themeItems, ...appearance, ...languages];
128
+ }
@@ -1,4 +1,5 @@
1
1
  import { type MutableRefObject, useCallback, useEffect, useState } from 'react';
2
+ import { useT } from '../../lib/i18n';
2
3
  import { useDesignPanelState } from '../design-panel/design-provider';
3
4
  import { useHistory } from '../history-provider';
4
5
  import type { InspectorControls } from '../inspector/inspector';
@@ -32,6 +33,7 @@ export function EditSaveCard({
32
33
  }) {
33
34
  const design = useDesignPanelState();
34
35
  const history = useHistory();
36
+ const t = useT();
35
37
  const [committing, setCommitting] = useState(false);
36
38
  const [error, setError] = useState<string | null>(null);
37
39
  const count = textCount + (design.dirty ? 1 : 0);
@@ -46,7 +48,7 @@ export function EditSaveCard({
46
48
  const tokens = design.dirty ? await design.commit() : { ok: true };
47
49
  const failure = text.ok ? tokens : text;
48
50
  if (!failure.ok) {
49
- setError(failure.error ?? 'Not everything was saved');
51
+ setError(failure.error ?? t('Not everything was saved'));
50
52
  return false;
51
53
  }
52
54
  history.clear();
@@ -54,7 +56,7 @@ export function EditSaveCard({
54
56
  } finally {
55
57
  setCommitting(false);
56
58
  }
57
- }, [committing, textCount, controlsRef, design, history]);
59
+ }, [committing, textCount, controlsRef, design, history, t]);
58
60
 
59
61
  const discard = useCallback(() => {
60
62
  controlsRef.current?.discard();
@@ -1,14 +1,74 @@
1
+ import { ChevronRight } from 'lucide-react';
1
2
  import { type ReactNode, useEffect, useId, useState } from 'react';
3
+ import { useT } from '../../lib/i18n';
2
4
 
3
- export function Section({ title, children }: { title: string; children: ReactNode }) {
5
+ export function Section({
6
+ title,
7
+ action,
8
+ children,
9
+ }: {
10
+ title: string;
11
+ /** A small control on the title's row, acting on the section as a whole. */
12
+ action?: ReactNode;
13
+ children: ReactNode;
14
+ }) {
4
15
  return (
5
16
  <section className="border-border border-b px-3 py-3 last:border-b-0">
6
- <h3 className="mb-2 text-[10px] text-muted-foreground uppercase tracking-wider">{title}</h3>
17
+ <div className="mb-2 flex min-h-5 items-center justify-between gap-2">
18
+ <h3 className="text-[10px] text-muted-foreground uppercase tracking-wider">{title}</h3>
19
+ {action}
20
+ </div>
7
21
  <div className="flex flex-col gap-2">{children}</div>
8
22
  </section>
9
23
  );
10
24
  }
11
25
 
26
+ /** A section that starts closed: what is there for a second look, below what is used every time. */
27
+ export function CollapsibleSection({
28
+ title,
29
+ summary,
30
+ defaultOpen = false,
31
+ children,
32
+ }: {
33
+ title: string;
34
+ /** Shown beside the title while closed, so the closed row still says something. */
35
+ summary?: ReactNode;
36
+ defaultOpen?: boolean;
37
+ children: ReactNode;
38
+ }) {
39
+ const [open, setOpen] = useState(defaultOpen);
40
+ const id = useId();
41
+ return (
42
+ <section className="border-border border-b last:border-b-0">
43
+ <button
44
+ type="button"
45
+ aria-expanded={open}
46
+ aria-controls={id}
47
+ onClick={() => setOpen((value) => !value)}
48
+ className="flex h-10 w-full items-center gap-2 px-3 text-left transition-colors hover:bg-accent/60 focus-visible:outline-2 focus-visible:outline-foreground/60 focus-visible:-outline-offset-2"
49
+ >
50
+ <h3 className="flex-none text-[10px] text-muted-foreground uppercase tracking-wider">
51
+ {title}
52
+ </h3>
53
+ <span className="min-w-0 flex-1 truncate text-right font-mono text-[10px] text-muted-foreground/80">
54
+ {!open && summary}
55
+ </span>
56
+ <ChevronRight
57
+ aria-hidden
58
+ className={`size-3.5 flex-none text-muted-foreground transition-transform duration-150 ease-out motion-reduce:transition-none ${
59
+ open ? 'rotate-90' : ''
60
+ }`}
61
+ />
62
+ </button>
63
+ {open && (
64
+ <div id={id} className="flex flex-col gap-2 px-3 pb-3">
65
+ {children}
66
+ </div>
67
+ )}
68
+ </section>
69
+ );
70
+ }
71
+
12
72
  export function Field({
13
73
  label,
14
74
  htmlFor,
@@ -42,24 +102,34 @@ const HEX6 = /^#[0-9a-fA-F]{6}$/;
42
102
  export function ColorField({
43
103
  label,
44
104
  value,
105
+ placeholder,
106
+ trailing,
45
107
  onChange,
46
108
  }: {
47
109
  label: string;
48
110
  value: string;
111
+ /** Sits after the hex, inside the row — the token picker in the element panel. */
112
+ trailing?: ReactNode;
113
+ /** Shown for an empty value, which then reads as unset rather than invalid. */
114
+ placeholder?: string;
49
115
  onChange: (value: string) => void;
50
116
  }) {
117
+ const t = useT();
51
118
  const id = useId();
52
119
  const [draft, setDraft] = useState(value);
53
120
  useEffect(() => setDraft(value), [value]);
54
- const invalid = !HEX6.test(draft);
121
+ const invalid = !HEX6.test(draft) && !(placeholder !== undefined && draft === value);
55
122
 
56
123
  return (
57
124
  <Field label={label} htmlFor={id}>
58
125
  <span className="relative inline-flex size-8 flex-none items-center justify-center rounded border border-border has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-foreground/60">
59
- <span className="size-5 rounded-sm border border-border" style={{ background: value }} />
126
+ <span
127
+ className="size-5 rounded-sm border border-border"
128
+ style={{ background: value || 'transparent' }}
129
+ />
60
130
  <input
61
131
  type="color"
62
- aria-label={`${label} swatch`}
132
+ aria-label={t('{label} swatch', { label })}
63
133
  value={HEX6.test(value) ? value : '#000000'}
64
134
  onChange={(e) => onChange(e.target.value)}
65
135
  className="absolute inset-0 cursor-pointer opacity-0"
@@ -70,6 +140,7 @@ export function ColorField({
70
140
  type="text"
71
141
  spellCheck={false}
72
142
  value={draft}
143
+ placeholder={placeholder}
73
144
  aria-invalid={invalid}
74
145
  onChange={(e) => {
75
146
  const next = e.target.value.trim();
@@ -79,8 +150,9 @@ export function ColorField({
79
150
  onBlur={() => {
80
151
  if (!HEX6.test(draft)) setDraft(value);
81
152
  }}
82
- className={`${INPUT_CLASS} min-w-0 flex-1 font-mono uppercase tabular-nums`}
153
+ className={`${INPUT_CLASS} min-w-0 flex-1 font-mono uppercase tabular-nums placeholder:normal-case`}
83
154
  />
155
+ {trailing}
84
156
  </Field>
85
157
  );
86
158
  }
@@ -103,6 +175,8 @@ export function NumberField({
103
175
  max,
104
176
  step = 1,
105
177
  suffix,
178
+ prefix,
179
+ bare = false,
106
180
  onChange,
107
181
  }: {
108
182
  label: string;
@@ -111,6 +185,10 @@ export function NumberField({
111
185
  max: number;
112
186
  step?: number;
113
187
  suffix?: string;
188
+ /** An icon in front of the number, standing in for a visible label. */
189
+ prefix?: ReactNode;
190
+ /** No frame of its own: the field sits inside a composite control that draws one. */
191
+ bare?: boolean;
114
192
  onChange: (value: number) => void;
115
193
  }) {
116
194
  const [draft, setDraft] = useState(String(value));
@@ -132,7 +210,15 @@ export function NumberField({
132
210
  };
133
211
 
134
212
  return (
135
- <span className="relative flex-none">
213
+ <span className={bare ? 'relative min-w-0 flex-1' : 'relative flex-none'}>
214
+ {prefix && (
215
+ <span
216
+ aria-hidden
217
+ className="pointer-events-none absolute top-1/2 left-2 flex -translate-y-1/2 text-muted-foreground"
218
+ >
219
+ {prefix}
220
+ </span>
221
+ )}
136
222
  <input
137
223
  type="text"
138
224
  inputMode="decimal"
@@ -152,7 +238,11 @@ export function NumberField({
152
238
  if (next !== value) onChange(next);
153
239
  }
154
240
  }}
155
- className={`${INPUT_CLASS} w-16 pr-6 text-right font-mono tabular-nums`}
241
+ className={
242
+ bare
243
+ ? `h-8 w-full bg-transparent text-xs outline-none ${prefix ? 'pl-7' : 'pl-2'} ${suffix ? 'pr-7' : 'pr-1'} font-mono tabular-nums`
244
+ : `${INPUT_CLASS} w-16 ${prefix ? 'pl-7' : ''} pr-6 text-right font-mono tabular-nums`
245
+ }
156
246
  />
157
247
  {suffix && (
158
248
  <span
@@ -223,6 +313,7 @@ export function FontField({
223
313
  presets: FontPreset[];
224
314
  onChange: (value: string) => void;
225
315
  }) {
316
+ const t = useT();
226
317
  const id = useId();
227
318
  const matched = presets.find((preset) => preset.value === value);
228
319
  return (
@@ -240,7 +331,7 @@ export function FontField({
240
331
  {preset.label}
241
332
  </option>
242
333
  ))}
243
- {!matched && <option value="__custom__">Custom (from source)</option>}
334
+ {!matched && <option value="__custom__">{t('Custom (from source)')}</option>}
244
335
  </select>
245
336
  </Field>
246
337
  );