@open-artifact/ui 0.0.0-stage → 0.1.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 (48) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +48 -4
  5. package/src/button.d.ts +30 -0
  6. package/src/button.js +28 -0
  7. package/src/cn.d.ts +3 -0
  8. package/src/cn.js +5 -0
  9. package/src/color-picker.d.ts +98 -0
  10. package/src/color-picker.js +370 -0
  11. package/src/color.d.ts +144 -0
  12. package/src/color.js +528 -0
  13. package/src/data/google-fonts.json +1775 -0
  14. package/src/effect-editors.d.ts +48 -0
  15. package/src/effect-editors.js +230 -0
  16. package/src/effects.d.ts +118 -0
  17. package/src/effects.js +425 -0
  18. package/src/fields.d.ts +105 -0
  19. package/src/fields.js +77 -0
  20. package/src/focusables.d.ts +26 -0
  21. package/src/focusables.js +42 -0
  22. package/src/font-picker.d.ts +62 -0
  23. package/src/font-picker.js +238 -0
  24. package/src/fonts.d.ts +121 -0
  25. package/src/fonts.js +273 -0
  26. package/src/history-diff.d.ts +64 -0
  27. package/src/history-diff.js +151 -0
  28. package/src/history.d.ts +95 -0
  29. package/src/history.js +160 -0
  30. package/src/index.d.ts +25 -0
  31. package/src/index.js +25 -0
  32. package/src/keys.d.ts +60 -0
  33. package/src/keys.js +78 -0
  34. package/src/layout.d.ts +55 -0
  35. package/src/layout.js +64 -0
  36. package/src/menu.d.ts +50 -0
  37. package/src/menu.js +36 -0
  38. package/src/numbers.d.ts +24 -0
  39. package/src/numbers.js +114 -0
  40. package/src/overlays.d.ts +38 -0
  41. package/src/overlays.js +26 -0
  42. package/src/scroll.d.ts +24 -0
  43. package/src/scroll.js +111 -0
  44. package/src/scrub-number-field.d.ts +56 -0
  45. package/src/scrub-number-field.js +190 -0
  46. package/src/styles.css +122 -0
  47. package/src/tooltip.d.ts +18 -0
  48. package/src/tooltip.js +10 -0
@@ -0,0 +1,48 @@
1
+ import { type ColorSwatch } from './color-picker.tsx';
2
+ import { type FilterKind } from './effects.ts';
3
+ /** Props shared by the effect editors. */
4
+ export interface EffectEditorProps {
5
+ /** The CSS value (`linear-gradient(…)`, a `box-shadow`, a `filter`). */
6
+ value: string;
7
+ /** Live updates while dragging or scrubbing. */
8
+ onChange: (css: string) => void;
9
+ /** Final values (a release, a typed value, an add or remove). */
10
+ onCommit?: (css: string) => void;
11
+ /** Swatches offered by the color pickers (design tokens). */
12
+ swatches?: readonly ColorSwatch[];
13
+ className?: string;
14
+ }
15
+ /** Props of {@link GradientEditor}. */
16
+ export interface GradientEditorProps extends EffectEditorProps {
17
+ 'aria-label'?: string;
18
+ }
19
+ /**
20
+ * A gradient editor: linear or radial, the angle (linear) or shape
21
+ * (radial), and the stops on a bar. Click the bar to add a stop; drag a
22
+ * handle (or focus it and use the arrow keys, Shift for 10%) to move it;
23
+ * Delete removes the focused stop (two always stay).
24
+ */
25
+ export declare function GradientEditor({ value, onChange, onCommit, swatches, className, ...aria }: GradientEditorProps): import("react/jsx-runtime").JSX.Element;
26
+ /** Props of {@link ShadowEditor}. */
27
+ export interface ShadowEditorProps extends EffectEditorProps {
28
+ 'aria-label'?: string;
29
+ }
30
+ /**
31
+ * A `box-shadow` editor: a list of layers (front first), each with color,
32
+ * x, y, blur, spread and inset; layers can be added, removed and moved
33
+ * up or down.
34
+ */
35
+ export declare function ShadowEditor({ value, onChange, onCommit, swatches, className, ...aria }: ShadowEditorProps): import("react/jsx-runtime").JSX.Element;
36
+ /** Props of {@link FilterEditor}. */
37
+ export interface FilterEditorProps extends EffectEditorProps {
38
+ 'aria-label'?: string;
39
+ /** The functions offered by "Add filter" (default: all, drop-shadow last). */
40
+ kinds?: readonly FilterKind[];
41
+ }
42
+ /**
43
+ * A `filter` editor: the chain in order, one row per function (blur,
44
+ * brightness, contrast, grayscale, saturate, hue-rotate, invert, sepia,
45
+ * opacity, drop-shadow), each with its amount; functions can be added and
46
+ * removed.
47
+ */
48
+ export declare function FilterEditor({ value, onChange, onCommit, swatches, className, kinds, ...aria }: FilterEditorProps): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,230 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /**
3
+ * Visual editors for CSS effect values: {@link GradientEditor},
4
+ * {@link ShadowEditor} (`box-shadow`) and {@link FilterEditor} (`filter`).
5
+ * Each takes and produces CSS text; a value it cannot represent is shown
6
+ * as text to edit by hand instead of being rewritten.
7
+ */
8
+ import { ArrowDown, ArrowUp, Minus, Plus } from 'lucide-react';
9
+ import { useEffect, useMemo, useRef, useState } from 'react';
10
+ import { IconButton } from "./button.js";
11
+ import { cn } from "./cn.js";
12
+ import { CHECKERBOARD, ColorPicker } from "./color-picker.js";
13
+ import { DEFAULT_SHADOW, FILTER_SPECS, colorAtPosition, defaultGradient, formatBoxShadow, formatFilter, formatGradient, newFilter, parseBoxShadow, parseFilter, parseGradient, sortStops, } from "./effects.js";
14
+ import { CommitInput, Segmented } from "./fields.js";
15
+ import { Menu, MenuItem } from "./menu.js";
16
+ import { ScrubNumberField } from "./scrub-number-field.js";
17
+ let nextId = 1;
18
+ const withIds = (items) => items.map((item) => ({ ...item, id: nextId++ }));
19
+ const stripIds = (items) => items.map(({ id: _id, ...rest }) => rest);
20
+ /**
21
+ * Keeps a parsed model of `value`, re-reading it when it changes from
22
+ * outside (not when it is the echo of this editor's own output).
23
+ */
24
+ function useParsedValue(value, parse) {
25
+ const [model, setModel] = useState(() => parse(value));
26
+ const emitted = useRef(null);
27
+ useEffect(() => {
28
+ if (value === emitted.current)
29
+ return;
30
+ setModel(parse(value));
31
+ // eslint-disable-next-line react-hooks/exhaustive-deps
32
+ }, [value]);
33
+ return [
34
+ model,
35
+ (next, css) => {
36
+ emitted.current = css;
37
+ setModel(next);
38
+ },
39
+ ];
40
+ }
41
+ /** The text fallback for a value an editor cannot read. */
42
+ function RawValue({ label, value, onCommit, onReset, resetLabel }) {
43
+ return (_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx(CommitInput, { "aria-label": label, value: value, onCommit: onCommit, spellCheck: false, className: "font-mono text-[12px]" }), _jsxs("div", { className: "flex items-center gap-2 text-xs text-fg-subtle", children: [_jsx("span", { className: "min-w-0 flex-1", children: "This value can only be edited as text." }), _jsx("button", { type: "button", onClick: onReset, className: "shrink-0 text-accent outline-none hover:underline focus-visible:outline-2 focus-visible:outline-accent", children: resetLabel })] })] }));
44
+ }
45
+ /** A small caption above a field group. */
46
+ function Caption({ children }) {
47
+ return _jsx("div", { className: "text-[11px] font-medium text-fg-subtle", children: children });
48
+ }
49
+ const toGradientModel = (gradient) => (gradient ? { ...gradient, stops: withIds(gradient.stops) } : null);
50
+ const fromGradientModel = (model) => ({ ...model, stops: stripIds(model.stops) });
51
+ /**
52
+ * A gradient editor: linear or radial, the angle (linear) or shape
53
+ * (radial), and the stops on a bar. Click the bar to add a stop; drag a
54
+ * handle (or focus it and use the arrow keys, Shift for 10%) to move it;
55
+ * Delete removes the focused stop (two always stay).
56
+ */
57
+ export function GradientEditor({ value, onChange, onCommit, swatches, className, ...aria }) {
58
+ const [model, setModel] = useParsedValue(value, (css) => toGradientModel(parseGradient(css)));
59
+ const [selected, setSelected] = useState(null);
60
+ const label = aria['aria-label'] ?? 'Gradient';
61
+ const emit = (next, final) => {
62
+ const css = formatGradient(fromGradientModel(next));
63
+ setModel(next, css);
64
+ onChange(css);
65
+ if (final)
66
+ onCommit?.(css);
67
+ };
68
+ if (!model) {
69
+ return (_jsx("div", { className: className, children: _jsx(RawValue, { label: label, value: value, resetLabel: "Use a gradient", onCommit: (css) => {
70
+ onChange(css);
71
+ onCommit?.(css);
72
+ }, onReset: () => emit(toGradientModel(defaultGradient()), true) }) }));
73
+ }
74
+ const sorted = sortStops(model.stops);
75
+ const active = sorted.find((stop) => stop.id === selected) ?? sorted[0];
76
+ const update = (patch, final = true) => emit({ ...model, ...patch }, final);
77
+ const updateStop = (id, patch, final = true) => update({ stops: model.stops.map((stop) => (stop.id === id ? { ...stop, ...patch } : stop)) }, final);
78
+ const removeStop = (id) => {
79
+ if (model.stops.length <= 2)
80
+ return;
81
+ update({ stops: model.stops.filter((stop) => stop.id !== id) });
82
+ setSelected(null);
83
+ };
84
+ const addStop = (position) => {
85
+ const stop = { id: nextId++, color: colorAtPosition(model.stops, position), position: Math.round(position * 10) / 10 };
86
+ update({ stops: [...model.stops, stop] });
87
+ setSelected(stop.id);
88
+ };
89
+ return (_jsxs("div", { className: cn('flex flex-col gap-2.5', className), role: "group", "aria-label": label, children: [_jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx(Segmented, { "aria-label": "Gradient type", value: model.type, options: [
90
+ { value: 'linear', label: 'Linear' },
91
+ { value: 'radial', label: 'Radial' },
92
+ ], onChange: (type) => update({ type }), className: "flex-1" }), model.type === 'linear' ? (_jsx(ScrubNumberField, { "aria-label": "Angle", label: "\u2220", unit: "\u00B0", value: model.angle, min: -360, max: 720, step: 1, largeStep: 15, onChange: (angle) => update({ angle: ((angle % 360) + 360) % 360 }, false), onCommit: (angle) => update({ angle: ((angle % 360) + 360) % 360 }), className: "w-20 shrink-0" })) : (_jsx(Segmented, { "aria-label": "Shape", value: model.shape, options: [
93
+ { value: 'ellipse', label: 'Ellipse' },
94
+ { value: 'circle', label: 'Circle' },
95
+ ], onChange: (shape) => update({ shape }), className: "w-32 shrink-0" }))] }), _jsx(StopBar, { stops: sorted, selected: active.id, onSelect: setSelected, onMove: (id, position, final) => updateStop(id, { position }, final), onAdd: addStop, onRemove: removeStop }), _jsxs("div", { className: "flex flex-col gap-1", children: [_jsxs("div", { className: "flex items-center justify-between", children: [_jsx(Caption, { children: "Stops" }), _jsx(IconButton, { size: "sm", label: "Add stop", icon: _jsx(Plus, { className: "size-3.5" }), onClick: () => addStop(nextGap(sorted)) })] }), sorted.map((stop) => (_jsxs("div", { className: cn('flex items-center gap-1.5 rounded-control px-1 py-0.5', stop.id === active.id && 'bg-hover'), onFocusCapture: () => setSelected(stop.id), children: [_jsx(ColorPicker, { "aria-label": `Stop at ${Math.round(stop.position)}%`, trigger: "swatch", value: stop.color, swatches: swatches, onChange: (color) => updateStop(stop.id, { color }, false), onCommit: (color) => updateStop(stop.id, { color }) }), _jsx("span", { className: "min-w-0 flex-1 truncate font-mono text-[11px] text-fg-muted", title: stop.color, children: stop.color }), _jsx(ScrubNumberField, { "aria-label": "Stop position", value: stop.position, unit: "%", min: 0, max: 100, step: 1, precision: 1, onChange: (position) => updateStop(stop.id, { position }, false), onCommit: (position) => updateStop(stop.id, { position }), className: "w-16 shrink-0" }), _jsx(IconButton, { size: "sm", label: "Remove stop", icon: _jsx(Minus, { className: "size-3.5" }), disabled: model.stops.length <= 2, onClick: () => removeStop(stop.id) })] }, stop.id)))] })] }));
96
+ }
97
+ /** The middle of the widest gap between stops: where "Add stop" puts one. */
98
+ function nextGap(sorted) {
99
+ let best = 50;
100
+ let width = -1;
101
+ const points = [0, ...sorted.map((stop) => stop.position), 100];
102
+ for (let i = 1; i < points.length; i++) {
103
+ const gap = points[i] - points[i - 1];
104
+ if (gap > width) {
105
+ width = gap;
106
+ best = (points[i] + points[i - 1]) / 2;
107
+ }
108
+ }
109
+ return best;
110
+ }
111
+ /** The stop bar: a left-to-right preview with a draggable handle per stop. */
112
+ function StopBar({ stops, selected, onSelect, onMove, onAdd, onRemove, }) {
113
+ const bar = useRef(null);
114
+ const dragging = useRef(null);
115
+ const preview = useMemo(() => `linear-gradient(90deg, ${stops.map((stop) => `${stop.color} ${stop.position}%`).join(', ')}), ${CHECKERBOARD}`, [stops]);
116
+ const positionAt = (clientX) => {
117
+ const box = bar.current.getBoundingClientRect();
118
+ return Math.round(Math.max(0, Math.min(1, (clientX - box.left) / Math.max(1, box.width))) * 1000) / 10;
119
+ };
120
+ const onHandleKey = (event, stop) => {
121
+ const step = event.shiftKey ? 10 : 1;
122
+ const moves = { ArrowLeft: stop.position - step, ArrowDown: stop.position - step, ArrowRight: stop.position + step, ArrowUp: stop.position + step, Home: 0, End: 100 };
123
+ if (event.key === 'Delete' || event.key === 'Backspace') {
124
+ event.preventDefault();
125
+ onRemove(stop.id);
126
+ return;
127
+ }
128
+ const next = moves[event.key];
129
+ if (next === undefined)
130
+ return;
131
+ event.preventDefault();
132
+ onMove(stop.id, Math.max(0, Math.min(100, next)), true);
133
+ };
134
+ return (_jsx("div", { className: "px-2", children: _jsx("div", { ref: bar, className: "relative h-6 cursor-copy rounded-[4px] shadow-[inset_0_0_0_1px_rgb(0_0_0/0.12)]", style: { background: preview }, title: "Click to add a stop", onPointerDown: (event) => {
135
+ if (event.target !== event.currentTarget || event.button !== 0)
136
+ return;
137
+ onAdd(positionAt(event.clientX));
138
+ }, children: stops.map((stop) => (_jsx("button", { type: "button", role: "slider", "aria-label": "Gradient stop", "aria-valuemin": 0, "aria-valuemax": 100, "aria-valuenow": Math.round(stop.position), "aria-valuetext": `${stop.color} at ${Math.round(stop.position)}%`, className: cn('absolute top-1/2 h-7 w-3 -translate-x-1/2 -translate-y-1/2 cursor-ew-resize touch-none rounded-[3px] border-2 border-white shadow-[0_0_0_1px_rgb(0_0_0/0.35),0_1px_3px_rgb(0_0_0/0.3)] outline-none', 'focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-accent', stop.id === selected && 'shadow-[0_0_0_2px_var(--color-accent),0_1px_3px_rgb(0_0_0/0.3)]'), style: { left: `${stop.position}%`, background: `linear-gradient(${stop.color}, ${stop.color}), ${CHECKERBOARD}` }, onFocus: () => onSelect(stop.id), onKeyDown: (event) => onHandleKey(event, stop), onPointerDown: (event) => {
139
+ if (event.button !== 0)
140
+ return;
141
+ event.stopPropagation();
142
+ event.currentTarget.setPointerCapture(event.pointerId);
143
+ dragging.current = { id: stop.id, pointerId: event.pointerId };
144
+ onSelect(stop.id);
145
+ }, onPointerMove: (event) => {
146
+ if (dragging.current?.pointerId !== event.pointerId)
147
+ return;
148
+ onMove(stop.id, positionAt(event.clientX), false);
149
+ }, onPointerUp: (event) => {
150
+ if (dragging.current?.pointerId !== event.pointerId)
151
+ return;
152
+ dragging.current = null;
153
+ event.currentTarget.releasePointerCapture(event.pointerId);
154
+ onMove(stop.id, positionAt(event.clientX), true);
155
+ } }, stop.id))) }) }));
156
+ }
157
+ /**
158
+ * A `box-shadow` editor: a list of layers (front first), each with color,
159
+ * x, y, blur, spread and inset; layers can be added, removed and moved
160
+ * up or down.
161
+ */
162
+ export function ShadowEditor({ value, onChange, onCommit, swatches, className, ...aria }) {
163
+ const [layers, setLayers] = useParsedValue(value, (css) => {
164
+ const parsed = parseBoxShadow(css);
165
+ return parsed ? withIds(parsed) : null;
166
+ });
167
+ const label = aria['aria-label'] ?? 'Shadows';
168
+ const emit = (next, final = true) => {
169
+ const css = formatBoxShadow(stripIds(next));
170
+ setLayers(next, css);
171
+ onChange(css);
172
+ if (final)
173
+ onCommit?.(css);
174
+ };
175
+ if (!layers) {
176
+ return (_jsx("div", { className: className, children: _jsx(RawValue, { label: label, value: value, resetLabel: "Start over", onCommit: (css) => {
177
+ onChange(css);
178
+ onCommit?.(css);
179
+ }, onReset: () => emit(withIds([DEFAULT_SHADOW])) }) }));
180
+ }
181
+ const patch = (id, change, final = true) => emit(layers.map((layer) => (layer.id === id ? { ...layer, ...change } : layer)), final);
182
+ const move = (index, by) => {
183
+ const target = index + by;
184
+ if (target < 0 || target >= layers.length)
185
+ return;
186
+ const next = [...layers];
187
+ [next[index], next[target]] = [next[target], next[index]];
188
+ emit(next);
189
+ };
190
+ return (_jsxs("div", { className: cn('flex flex-col gap-2', className), role: "group", "aria-label": label, children: [layers.map((layer, index) => (_jsxs("div", { role: "group", "aria-label": `Shadow ${index + 1}`, className: "flex flex-col gap-1.5 rounded-control border border-line p-1.5", onKeyDown: (event) => {
191
+ if (!event.altKey || (event.key !== 'ArrowUp' && event.key !== 'ArrowDown') || event.target.getAttribute('role') === 'spinbutton')
192
+ return;
193
+ event.preventDefault();
194
+ move(index, event.key === 'ArrowUp' ? -1 : 1);
195
+ }, children: [_jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx(ColorPicker, { "aria-label": `Shadow ${index + 1} color`, trigger: "swatch", value: layer.color === 'currentcolor' ? null : layer.color, placeholder: "Text color", fallback: "#000000", swatches: swatches, onChange: (color) => patch(layer.id, { color }, false), onCommit: (color) => patch(layer.id, { color }), onClear: () => patch(layer.id, { color: 'currentcolor' }) }), _jsx(Segmented, { "aria-label": "Shadow kind", value: layer.inset ? 'inset' : 'drop', options: [
196
+ { value: 'drop', label: 'Drop' },
197
+ { value: 'inset', label: 'Inner' },
198
+ ], onChange: (kind) => patch(layer.id, { inset: kind === 'inset' }), className: "flex-1 text-xs" }), _jsx(IconButton, { size: "sm", label: "Move up", icon: _jsx(ArrowUp, { className: "size-3.5" }), disabled: index === 0, onClick: () => move(index, -1) }), _jsx(IconButton, { size: "sm", label: "Move down", icon: _jsx(ArrowDown, { className: "size-3.5" }), disabled: index === layers.length - 1, onClick: () => move(index, 1) }), _jsx(IconButton, { size: "sm", label: "Remove shadow", icon: _jsx(Minus, { className: "size-3.5" }), onClick: () => emit(layers.filter((item) => item.id !== layer.id)) })] }), _jsx("div", { className: "grid grid-cols-4 gap-1", children: ['x', 'y', 'blur', 'spread'].map((key) => (_jsx(ScrubNumberField, { "aria-label": `Shadow ${index + 1} ${key}`, label: key === 'x' ? 'X' : key === 'y' ? 'Y' : key === 'blur' ? 'B' : 'S', title: key === 'blur' ? 'Blur' : key === 'spread' ? 'Spread' : undefined, value: layer[key], min: key === 'blur' ? 0 : undefined, onChange: (n) => patch(layer.id, { [key]: n }, false), onCommit: (n) => patch(layer.id, { [key]: n }) }, key))) })] }, layer.id))), _jsxs("button", { type: "button", onClick: () => emit([...layers, ...withIds([DEFAULT_SHADOW])]), className: "flex h-7 items-center justify-center gap-1.5 rounded-control border border-dashed border-line-strong text-xs text-fg-muted outline-none hover:bg-hover hover:text-fg focus-visible:outline-2 focus-visible:outline-accent", children: [_jsx(Plus, { className: "size-3.5" }), "Add shadow"] })] }));
199
+ }
200
+ const ALL_FILTER_KINDS = [...Object.keys(FILTER_SPECS), 'drop-shadow'];
201
+ /**
202
+ * A `filter` editor: the chain in order, one row per function (blur,
203
+ * brightness, contrast, grayscale, saturate, hue-rotate, invert, sepia,
204
+ * opacity, drop-shadow), each with its amount; functions can be added and
205
+ * removed.
206
+ */
207
+ export function FilterEditor({ value, onChange, onCommit, swatches, className, kinds = ALL_FILTER_KINDS, ...aria }) {
208
+ const [chain, setChain] = useParsedValue(value, (css) => {
209
+ const parsed = parseFilter(css);
210
+ return parsed ? withIds(parsed) : null;
211
+ });
212
+ const label = aria['aria-label'] ?? 'Filters';
213
+ const emit = (next, final = true) => {
214
+ const css = formatFilter(stripIds(next));
215
+ setChain(next, css);
216
+ onChange(css);
217
+ if (final)
218
+ onCommit?.(css);
219
+ };
220
+ if (!chain) {
221
+ return (_jsx("div", { className: className, children: _jsx(RawValue, { label: label, value: value, resetLabel: "Clear filters", onCommit: (css) => {
222
+ onChange(css);
223
+ onCommit?.(css);
224
+ }, onReset: () => emit([]) }) }));
225
+ }
226
+ const replace = (id, next, final = true) => emit(chain.map((fn) => (fn.id === id ? { ...next, id } : fn)), final);
227
+ const present = new Set(chain.map((fn) => fn.kind));
228
+ const addable = kinds.filter((kind) => kind === 'drop-shadow' || !present.has(kind));
229
+ return (_jsxs("div", { className: cn('flex flex-col gap-1.5', className), role: "group", "aria-label": label, children: [chain.map((fn) => (_jsxs("div", { className: "flex items-center gap-1.5", children: [_jsx("span", { className: "w-20 shrink-0 truncate text-xs text-fg-muted", children: fn.kind === 'drop-shadow' ? 'Drop shadow' : FILTER_SPECS[fn.kind].label }), fn.kind === 'drop-shadow' ? (_jsxs("div", { className: "flex min-w-0 flex-1 items-center gap-1", children: [_jsx(ColorPicker, { "aria-label": "Drop shadow color", trigger: "swatch", value: fn.shadow.color === 'currentcolor' ? null : fn.shadow.color, fallback: "#000000", swatches: swatches, onChange: (color) => replace(fn.id, { kind: 'drop-shadow', shadow: { ...fn.shadow, color } }, false), onCommit: (color) => replace(fn.id, { kind: 'drop-shadow', shadow: { ...fn.shadow, color } }) }), ['x', 'y', 'blur'].map((key) => (_jsx(ScrubNumberField, { "aria-label": `Drop shadow ${key}`, label: key === 'x' ? 'X' : key === 'y' ? 'Y' : 'B', value: fn.shadow[key], min: key === 'blur' ? 0 : undefined, onChange: (n) => replace(fn.id, { kind: 'drop-shadow', shadow: { ...fn.shadow, [key]: n } }, false), onCommit: (n) => replace(fn.id, { kind: 'drop-shadow', shadow: { ...fn.shadow, [key]: n } }), className: "min-w-0 flex-1" }, key)))] })) : (_jsx(ScrubNumberField, { "aria-label": FILTER_SPECS[fn.kind].label, label: _jsx("span", { "aria-hidden": "true", children: "\u2194" }), value: fn.value, unit: FILTER_SPECS[fn.kind].unit === 'deg' ? '°' : FILTER_SPECS[fn.kind].unit, min: FILTER_SPECS[fn.kind].min, max: FILTER_SPECS[fn.kind].max, step: FILTER_SPECS[fn.kind].step, onChange: (n) => replace(fn.id, { kind: fn.kind, value: n }, false), onCommit: (n) => replace(fn.id, { kind: fn.kind, value: n }), className: "min-w-0 flex-1" })), _jsx(IconButton, { size: "sm", label: "Remove filter", icon: _jsx(Minus, { className: "size-3.5" }), onClick: () => emit(chain.filter((item) => item.id !== fn.id)) })] }, fn.id))), addable.length ? (_jsx(Menu, { trigger: _jsxs("button", { type: "button", className: "flex h-7 items-center justify-center gap-1.5 rounded-control border border-dashed border-line-strong text-xs text-fg-muted outline-none hover:bg-hover hover:text-fg focus-visible:outline-2 focus-visible:outline-accent", children: [_jsx(Plus, { className: "size-3.5" }), "Add filter"] }), children: addable.map((kind) => (_jsx(MenuItem, { onSelect: () => emit([...chain, ...withIds([newFilter(kind)])]), children: kind === 'drop-shadow' ? 'Drop shadow' : FILTER_SPECS[kind].label }, kind))) })) : null] }));
230
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Splits `text` at top-level separators (outside parentheses): commas, or
3
+ * runs of whitespace. Empty pieces are dropped for whitespace and kept for
4
+ * commas (so `a,,b` can be rejected).
5
+ */
6
+ export declare function splitTopLevel(text: string, separator: ',' | 'space'): string[];
7
+ /** One `box-shadow` layer; lengths in px. */
8
+ export interface ShadowLayer {
9
+ x: number;
10
+ y: number;
11
+ blur: number;
12
+ spread: number;
13
+ /** Any CSS color text; `currentcolor` when the value named none. */
14
+ color: string;
15
+ inset: boolean;
16
+ }
17
+ /** A `drop-shadow()` filter or `text-shadow` layer: no spread, no inset. */
18
+ export interface DropShadow {
19
+ x: number;
20
+ y: number;
21
+ blur: number;
22
+ color: string;
23
+ }
24
+ /** A new shadow layer: a soft drop shadow. */
25
+ export declare const DEFAULT_SHADOW: ShadowLayer;
26
+ /**
27
+ * Parses a `box-shadow` value into layers (front first). `none` or empty
28
+ * gives `[]`; null when a layer is not `[inset] x y [blur [spread]] [color]`
29
+ * in px.
30
+ */
31
+ export declare function parseBoxShadow(css: string): ShadowLayer[] | null;
32
+ /** Writes one box-shadow layer: `[inset] x y blur spread color`. */
33
+ export declare function formatShadowLayer(layer: ShadowLayer): string;
34
+ /** Writes a `box-shadow` value; `none` for no layers. */
35
+ export declare function formatBoxShadow(layers: readonly ShadowLayer[]): string;
36
+ /** Parses the argument of `drop-shadow()` (or one `text-shadow` layer). */
37
+ export declare function parseDropShadow(text: string): DropShadow | null;
38
+ /** Writes the argument of `drop-shadow()`: `x y blur color`. */
39
+ export declare function formatDropShadow(shadow: DropShadow): string;
40
+ /** Filter functions whose argument is one number. */
41
+ export type FilterAmountKind = 'blur' | 'brightness' | 'contrast' | 'grayscale' | 'saturate' | 'hue-rotate' | 'invert' | 'sepia' | 'opacity';
42
+ /** Every filter function the editor handles. */
43
+ export type FilterKind = FilterAmountKind | 'drop-shadow';
44
+ /** One function of a `filter` chain. Amounts: blur in px, hue-rotate in degrees, the rest in percent. */
45
+ export type FilterFunction = {
46
+ kind: FilterAmountKind;
47
+ value: number;
48
+ } | {
49
+ kind: 'drop-shadow';
50
+ shadow: DropShadow;
51
+ };
52
+ /** How a numeric filter is edited. */
53
+ export interface FilterSpec {
54
+ label: string;
55
+ unit: 'px' | '%' | 'deg';
56
+ min: number;
57
+ /** Upper bound of the field (CSS clamps grayscale, invert, sepia and opacity at 100%). */
58
+ max: number;
59
+ step: number;
60
+ /** The value that changes nothing. */
61
+ identity: number;
62
+ /** The value a newly added filter starts at. */
63
+ initial: number;
64
+ }
65
+ /** Field settings of the numeric filters, in menu order. */
66
+ export declare const FILTER_SPECS: Readonly<Record<FilterAmountKind, FilterSpec>>;
67
+ /** The drop shadow a newly added `drop-shadow()` starts as. */
68
+ export declare const DEFAULT_DROP_SHADOW: DropShadow;
69
+ /** A new filter function of `kind` at its initial value. */
70
+ export declare function newFilter(kind: FilterKind): FilterFunction;
71
+ /**
72
+ * Parses a `filter` chain. `none` or empty gives `[]`; null for `url()`,
73
+ * unknown functions or arguments it cannot read.
74
+ */
75
+ export declare function parseFilter(css: string): FilterFunction[] | null;
76
+ /** Writes one filter function. */
77
+ export declare function formatFilterFunction(fn: FilterFunction): string;
78
+ /** Writes a `filter` value; `none` for an empty chain. */
79
+ export declare function formatFilter(chain: readonly FilterFunction[]): string;
80
+ /** One color stop; position in percent along the gradient line. */
81
+ export interface GradientStop {
82
+ color: string;
83
+ position: number;
84
+ }
85
+ /** A linear or radial gradient. */
86
+ export interface Gradient {
87
+ type: 'linear' | 'radial';
88
+ repeating: boolean;
89
+ /** Direction of a linear gradient in degrees (CSS: 0 points up, 90 right). Kept while radial. */
90
+ angle: number;
91
+ /** Shape of a radial gradient. */
92
+ shape: 'circle' | 'ellipse';
93
+ /** Radial size as written (`closest-side`, `200px`…), when not the default. */
94
+ size?: string;
95
+ /** Radial center as written after `at` (`top left`, `30% 40%`), when not `center`. */
96
+ position?: string;
97
+ /** Sorted by position. */
98
+ stops: GradientStop[];
99
+ }
100
+ /** A new gradient: left-to-right, from `from` to `to`. */
101
+ export declare function defaultGradient(from?: string, to?: string): Gradient;
102
+ /**
103
+ * Parses a `linear-gradient()` / `radial-gradient()` (or `repeating-`) value.
104
+ * Null for other images, color hints, stop positions not in %, or a value
105
+ * with several layers. `to <side>` directions become angles (corners as 45°
106
+ * steps, which matches CSS for square boxes).
107
+ */
108
+ export declare function parseGradient(css: string): Gradient | null;
109
+ /** Writes a gradient as CSS; stops are written in position order. */
110
+ export declare function formatGradient(gradient: Gradient): string;
111
+ /** Stops sorted by position (stable). */
112
+ export declare function sortStops(stops: readonly GradientStop[]): GradientStop[];
113
+ /**
114
+ * The color the gradient shows at `position` (percent), interpolated in
115
+ * premultiplied sRGB like browsers do by default, written like the nearer
116
+ * stop. Stops whose colors cannot be read (a `var()`) are taken as-is.
117
+ */
118
+ export declare function colorAtPosition(stops: readonly GradientStop[], position: number): string;