@pixerate/editor-react 0.95.0 → 0.96.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 (47) hide show
  1. package/README.md +2 -0
  2. package/dist/animation-editor/index.cjs +1240 -195
  3. package/dist/animation-editor/index.js +4 -2
  4. package/dist/canvas/index.cjs +1418 -373
  5. package/dist/canvas/index.js +4 -2
  6. package/dist/chunk-3I7FOSLF.js +988 -0
  7. package/dist/{chunk-OTGJEYHV.js → chunk-5LFQ2UQZ.js} +75 -68
  8. package/dist/{chunk-5R67YSSD.js → chunk-GLSOQNQR.js} +1 -1
  9. package/dist/{chunk-6YRSQW2V.js → chunk-IY6YOY5B.js} +1 -1
  10. package/dist/chunk-KIRIU53A.js +72 -0
  11. package/dist/color/ColorPicker.d.cts +30 -0
  12. package/dist/color/ColorPicker.d.ts +30 -0
  13. package/dist/color/DualColorPicker.d.cts +29 -0
  14. package/dist/color/DualColorPicker.d.ts +29 -0
  15. package/dist/color/index.cjs +1067 -0
  16. package/dist/color/index.d.cts +7 -0
  17. package/dist/color/index.d.ts +7 -0
  18. package/dist/color/index.js +14 -0
  19. package/dist/color/useColorScheme.d.cts +16 -0
  20. package/dist/color/useColorScheme.d.ts +16 -0
  21. package/dist/effects/index.cjs +1169 -124
  22. package/dist/effects/index.js +4 -2
  23. package/dist/gradient/FillPicker.d.cts +27 -0
  24. package/dist/gradient/FillPicker.d.ts +27 -0
  25. package/dist/gradient/GradientEditor.d.cts +21 -0
  26. package/dist/gradient/GradientEditor.d.ts +21 -0
  27. package/dist/gradient/index.cjs +1601 -0
  28. package/dist/gradient/index.d.cts +6 -0
  29. package/dist/gradient/index.d.ts +6 -0
  30. package/dist/gradient/index.js +617 -0
  31. package/dist/gradient/useGradientController.d.cts +10 -0
  32. package/dist/gradient/useGradientController.d.ts +10 -0
  33. package/dist/image-editor/index.cjs +1742 -697
  34. package/dist/image-editor/index.js +5 -3
  35. package/dist/index.cjs +2160 -1115
  36. package/dist/index.js +5 -3
  37. package/dist/paint-editor/index.cjs +1233 -188
  38. package/dist/paint-editor/index.js +4 -2
  39. package/dist/parameters/fields.d.cts +1 -1
  40. package/dist/parameters/fields.d.ts +1 -1
  41. package/dist/parameters/index.cjs +1218 -173
  42. package/dist/parameters/index.js +3 -1
  43. package/dist/vector/index.cjs +1219 -174
  44. package/dist/vector/index.js +4 -2
  45. package/dist/video-editor/index.cjs +1230 -185
  46. package/dist/video-editor/index.js +4 -2
  47. package/package.json +29 -3
@@ -0,0 +1,6 @@
1
+ export { GradientEditor } from './GradientEditor.cjs';
2
+ export type { GradientEditorProps } from './GradientEditor.cjs';
3
+ export { FillPicker } from './FillPicker.cjs';
4
+ export type { FillPickerProps, FillMode } from './FillPicker.cjs';
5
+ export { useGradientController, useGradientState } from './useGradientController.cjs';
6
+ export * from '@pixerate/editor/gradient';
@@ -0,0 +1,6 @@
1
+ export { GradientEditor } from './GradientEditor.js';
2
+ export type { GradientEditorProps } from './GradientEditor.js';
3
+ export { FillPicker } from './FillPicker.js';
4
+ export type { FillPickerProps, FillMode } from './FillPicker.js';
5
+ export { useGradientController, useGradientState } from './useGradientController.js';
6
+ export * from '@pixerate/editor/gradient';
@@ -0,0 +1,617 @@
1
+ import {
2
+ ColorPicker,
3
+ DualColorPicker
4
+ } from "../chunk-3I7FOSLF.js";
5
+
6
+ // src/gradient/GradientEditor.tsx
7
+ import React, { useCallback, useRef as useRef2 } from "react";
8
+ import {
9
+ GRADIENT_PRESETS,
10
+ angleFromPointer,
11
+ gradientKeyStep,
12
+ gradientToCss,
13
+ gradientToCssFallback,
14
+ gradientTrackBackground,
15
+ isDualGradient
16
+ } from "@pixerate/editor/gradient";
17
+ import { formatDualColor } from "@pixerate/editor/color";
18
+
19
+ // src/gradient/useGradientController.ts
20
+ import { useRef, useSyncExternalStore } from "react";
21
+ import {
22
+ GradientController
23
+ } from "@pixerate/editor/gradient";
24
+ function useGradientController(options) {
25
+ const ref = useRef(null);
26
+ if (!ref.current) {
27
+ const resolvedOptions = typeof options === "function" ? options() : options;
28
+ ref.current = new GradientController(resolvedOptions);
29
+ }
30
+ return ref.current;
31
+ }
32
+ function useGradientState(controller) {
33
+ return useSyncExternalStore(
34
+ (onStoreChange) => controller.subscribe(onStoreChange),
35
+ () => controller.state,
36
+ () => controller.state
37
+ );
38
+ }
39
+
40
+ // src/gradient/GradientEditor.tsx
41
+ import { jsx, jsxs } from "react/jsx-runtime";
42
+ var DEFAULT_TYPES = ["linear", "radial", "conic"];
43
+ var INTERPOLATIONS = ["oklab", "oklch", "srgb", "srgb-linear"];
44
+ var buttonClass = "inline-flex h-7 items-center justify-center rounded border border-border bg-background px-2 text-xs font-medium text-foreground transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40 aria-pressed:bg-primary aria-pressed:text-primary-foreground";
45
+ var selectClass = "h-7 rounded border border-border bg-background px-2 text-xs text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40";
46
+ var inputClass = "h-7 w-16 rounded border border-border bg-background px-1.5 text-center text-xs text-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40";
47
+ function GradientEditor({
48
+ controller,
49
+ types = DEFAULT_TYPES,
50
+ showPresets = true,
51
+ showCss = false,
52
+ dual: controlledDual,
53
+ onGestureStart,
54
+ onGestureEnd,
55
+ className
56
+ }) {
57
+ const state = useGradientState(controller);
58
+ const beginGesture = (label) => {
59
+ controller.beginGesture(label);
60
+ onGestureStart?.();
61
+ };
62
+ const endGesture = () => {
63
+ controller.endGesture();
64
+ onGestureEnd?.();
65
+ };
66
+ const gradient = state.gradient;
67
+ const selectedStopId = state.selectedStopId;
68
+ const isDual = controlledDual ?? isDualGradient(gradient);
69
+ const [activeScheme, setActiveScheme] = React.useState("light");
70
+ const [copied, setCopied] = React.useState(false);
71
+ const trackRef = useRef2(null);
72
+ const angleDialRef = useRef2(null);
73
+ const selectedStop = gradient.stops.find((s) => s.id === selectedStopId) ?? gradient.stops[0];
74
+ const previewCss = isDual ? gradientToCssFallback(gradient, 16, activeScheme) : gradientToCss(gradient);
75
+ const trackCss = gradientTrackBackground(gradient);
76
+ const handleTrackClick = (e) => {
77
+ if (controller.isReadOnly) return;
78
+ const rect = e.currentTarget.getBoundingClientRect();
79
+ if (rect.width <= 0) return;
80
+ const offset = Math.max(0, Math.min(1, (e.clientX - rect.left) / rect.width));
81
+ controller.addStop(offset);
82
+ };
83
+ const handleStopPointerDown = (e, stopId) => {
84
+ if (controller.isReadOnly) return;
85
+ e.stopPropagation();
86
+ controller.selectStop(stopId);
87
+ beginGesture("Move Stop");
88
+ const startY = e.clientY;
89
+ const track = trackRef.current;
90
+ const rect = track?.getBoundingClientRect();
91
+ const onPointerMove = (moveEvent) => {
92
+ if (!rect || rect.width <= 0) return;
93
+ if (moveEvent.clientY - startY > 32 && controller.getGradient().stops.length > 2) {
94
+ cleanup();
95
+ controller.removeStop(stopId);
96
+ endGesture();
97
+ return;
98
+ }
99
+ const nextOffset = Math.max(0, Math.min(1, (moveEvent.clientX - rect.left) / rect.width));
100
+ controller.updateStop(stopId, { offset: Math.round(nextOffset * 1e3) / 1e3 });
101
+ };
102
+ const cleanup = () => {
103
+ window.removeEventListener("pointermove", onPointerMove);
104
+ window.removeEventListener("pointerup", onPointerUp);
105
+ window.removeEventListener("pointercancel", onPointerUp);
106
+ };
107
+ const onPointerUp = () => {
108
+ cleanup();
109
+ endGesture();
110
+ };
111
+ window.addEventListener("pointermove", onPointerMove);
112
+ window.addEventListener("pointerup", onPointerUp);
113
+ window.addEventListener("pointercancel", onPointerUp);
114
+ };
115
+ const handleStopKeyDown = (e, stopId) => {
116
+ if (controller.isReadOnly) return;
117
+ const res = gradientKeyStep(gradient, stopId, e.key, e.shiftKey);
118
+ if (!res) return;
119
+ e.preventDefault();
120
+ if (res.remove) {
121
+ if (gradient.stops.length > 2) {
122
+ controller.removeStop(stopId);
123
+ }
124
+ } else if (res.offset !== void 0) {
125
+ controller.updateStop(stopId, { offset: res.offset });
126
+ }
127
+ };
128
+ const handleAngleDialPointerDown = (e) => {
129
+ if (controller.isReadOnly) return;
130
+ const dial = angleDialRef.current;
131
+ if (!dial) return;
132
+ beginGesture("Change Angle");
133
+ const rect = dial.getBoundingClientRect();
134
+ const center = { x: rect.left + rect.width / 2, y: rect.top + rect.height / 2 };
135
+ const update = (pe) => {
136
+ const deg = angleFromPointer(center, { x: pe.clientX, y: pe.clientY }, pe.shiftKey ? 15 : 1);
137
+ controller.setAngle(deg);
138
+ };
139
+ update(e.nativeEvent);
140
+ const cleanup = () => {
141
+ window.removeEventListener("pointermove", onPointerMove);
142
+ window.removeEventListener("pointerup", onPointerUp);
143
+ window.removeEventListener("pointercancel", onPointerUp);
144
+ };
145
+ const onPointerMove = (pe) => update(pe);
146
+ const onPointerUp = () => {
147
+ cleanup();
148
+ endGesture();
149
+ };
150
+ window.addEventListener("pointermove", onPointerMove);
151
+ window.addEventListener("pointerup", onPointerUp);
152
+ window.addEventListener("pointercancel", onPointerUp);
153
+ };
154
+ const copyCss = useCallback(() => {
155
+ const text = gradientToCss(gradient);
156
+ navigator.clipboard?.writeText(text).then(() => {
157
+ setCopied(true);
158
+ setTimeout(() => setCopied(false), 1500);
159
+ });
160
+ }, [gradient]);
161
+ return /* @__PURE__ */ jsxs(
162
+ "div",
163
+ {
164
+ className: `flex flex-col gap-3 rounded-lg border border-border bg-card p-3 text-card-foreground shadow-sm ${className ?? ""}`,
165
+ "data-testid": "gradient-editor",
166
+ children: [
167
+ /* @__PURE__ */ jsxs("div", { className: "relative h-20 w-full overflow-hidden rounded-md border border-border", children: [
168
+ /* @__PURE__ */ jsx(
169
+ "div",
170
+ {
171
+ className: "absolute inset-0",
172
+ style: {
173
+ backgroundImage: "linear-gradient(45deg, #e0e0e0 25%, transparent 25%), linear-gradient(-45deg, #e0e0e0 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #e0e0e0 75%), linear-gradient(-45deg, transparent 75%, #e0e0e0 75%)",
174
+ backgroundSize: "16px 16px",
175
+ backgroundPosition: "0 0, 0 8px, 8px -8px, -8px 0px"
176
+ }
177
+ }
178
+ ),
179
+ /* @__PURE__ */ jsx(
180
+ "div",
181
+ {
182
+ className: "absolute inset-0 transition-all",
183
+ style: { background: previewCss },
184
+ "data-testid": "gradient-preview"
185
+ }
186
+ )
187
+ ] }),
188
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
189
+ /* @__PURE__ */ jsx(
190
+ "div",
191
+ {
192
+ ref: trackRef,
193
+ role: "button",
194
+ tabIndex: 0,
195
+ "aria-label": "Add gradient stop",
196
+ className: "relative h-6 w-full cursor-pointer rounded border border-border",
197
+ style: { background: trackCss },
198
+ onClick: handleTrackClick,
199
+ "data-testid": "gradient-track",
200
+ children: gradient.stops.map((stop, idx) => {
201
+ const isSelected = stop.id === selectedStopId;
202
+ const pct = Math.round(stop.offset * 100);
203
+ return /* @__PURE__ */ jsx(
204
+ "button",
205
+ {
206
+ type: "button",
207
+ role: "slider",
208
+ "aria-label": `Stop ${idx + 1}, ${stop.color} at ${pct}%`,
209
+ "aria-valuemin": 0,
210
+ "aria-valuemax": 100,
211
+ "aria-valuenow": pct,
212
+ "aria-pressed": isSelected,
213
+ disabled: controller.isReadOnly,
214
+ className: `absolute top-1/2 h-6 w-3.5 -translate-x-1/2 -translate-y-1/2 rounded border border-white shadow-sm transition-transform focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-primary ${isSelected ? "scale-110 ring-2 ring-primary z-10" : "hover:scale-105"}`,
215
+ style: {
216
+ left: `${stop.offset * 100}%`,
217
+ backgroundColor: stop.color.startsWith("light-dark(") ? "#888" : stop.color
218
+ },
219
+ onClick: (e) => {
220
+ e.stopPropagation();
221
+ controller.selectStop(stop.id);
222
+ },
223
+ onPointerDown: (e) => handleStopPointerDown(e, stop.id),
224
+ onKeyDown: (e) => handleStopKeyDown(e, stop.id),
225
+ "data-testid": `gradient-stop-${stop.id}`
226
+ },
227
+ stop.id
228
+ );
229
+ })
230
+ }
231
+ ),
232
+ /* @__PURE__ */ jsxs("div", { className: "flex justify-between px-0.5 text-[10px] text-muted-foreground", children: [
233
+ /* @__PURE__ */ jsx("span", { children: "0%" }),
234
+ /* @__PURE__ */ jsx("span", { children: "50%" }),
235
+ /* @__PURE__ */ jsx("span", { children: "100%" })
236
+ ] })
237
+ ] }),
238
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [
239
+ types.length > 1 && /* @__PURE__ */ jsx("div", { className: "flex rounded-md border border-border p-0.5", children: types.map((t) => /* @__PURE__ */ jsx(
240
+ "button",
241
+ {
242
+ type: "button",
243
+ className: buttonClass,
244
+ "aria-pressed": gradient.type === t,
245
+ disabled: controller.isReadOnly,
246
+ onClick: () => controller.setType(t),
247
+ children: t.charAt(0).toUpperCase() + t.slice(1)
248
+ },
249
+ t
250
+ )) }),
251
+ (gradient.type === "linear" || gradient.type === "conic") && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
252
+ /* @__PURE__ */ jsx(
253
+ "div",
254
+ {
255
+ ref: angleDialRef,
256
+ role: "slider",
257
+ tabIndex: 0,
258
+ "aria-label": "Gradient angle",
259
+ "aria-valuemin": 0,
260
+ "aria-valuemax": 360,
261
+ "aria-valuenow": gradient.angle ?? 180,
262
+ className: "relative h-7 w-7 cursor-crosshair rounded-full border border-border bg-muted/40 shadow-inner flex items-center justify-center focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary",
263
+ onPointerDown: handleAngleDialPointerDown,
264
+ children: /* @__PURE__ */ jsx(
265
+ "div",
266
+ {
267
+ className: "h-2.5 w-0.5 rounded-full bg-foreground",
268
+ style: {
269
+ transform: `rotate(${gradient.angle ?? 180}deg) translateY(-50%)`,
270
+ transformOrigin: "50% 100%"
271
+ }
272
+ }
273
+ )
274
+ }
275
+ ),
276
+ /* @__PURE__ */ jsx(
277
+ "input",
278
+ {
279
+ type: "number",
280
+ "aria-label": "Angle in degrees",
281
+ className: inputClass,
282
+ min: 0,
283
+ max: 360,
284
+ value: Math.round(gradient.angle ?? 180),
285
+ disabled: controller.isReadOnly,
286
+ onChange: (e) => controller.setAngle(Number(e.target.value) % 360)
287
+ }
288
+ ),
289
+ /* @__PURE__ */ jsx("span", { className: "text-xs text-muted-foreground", children: "\xB0" })
290
+ ] }),
291
+ gradient.type === "radial" && /* @__PURE__ */ jsx("div", { className: "flex items-center gap-1", children: /* @__PURE__ */ jsx(
292
+ "button",
293
+ {
294
+ type: "button",
295
+ className: buttonClass,
296
+ "aria-pressed": (gradient.shape ?? "ellipse") === "circle",
297
+ disabled: controller.isReadOnly,
298
+ onClick: () => controller.setShape((gradient.shape ?? "ellipse") === "circle" ? "ellipse" : "circle"),
299
+ children: (gradient.shape ?? "ellipse") === "circle" ? "Circle" : "Ellipse"
300
+ }
301
+ ) }),
302
+ /* @__PURE__ */ jsx(
303
+ "select",
304
+ {
305
+ "aria-label": "Interpolation space",
306
+ className: selectClass,
307
+ value: gradient.interpolation ?? "oklab",
308
+ disabled: controller.isReadOnly,
309
+ onChange: (e) => controller.setInterpolation(e.target.value),
310
+ children: INTERPOLATIONS.map((space) => /* @__PURE__ */ jsx("option", { value: space, children: space }, space))
311
+ }
312
+ ),
313
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1 ml-auto", children: [
314
+ /* @__PURE__ */ jsx(
315
+ "button",
316
+ {
317
+ type: "button",
318
+ className: buttonClass,
319
+ title: "Reverse stops",
320
+ "aria-label": "Reverse stops",
321
+ disabled: controller.isReadOnly,
322
+ onClick: () => controller.reverse(),
323
+ children: "\u21C4"
324
+ }
325
+ ),
326
+ /* @__PURE__ */ jsx(
327
+ "button",
328
+ {
329
+ type: "button",
330
+ className: buttonClass,
331
+ title: "Distribute stops evenly",
332
+ "aria-label": "Distribute stops evenly",
333
+ disabled: controller.isReadOnly,
334
+ onClick: () => controller.distribute(),
335
+ children: "\u2AF6\u2AF6"
336
+ }
337
+ ),
338
+ gradient.stops.length > 2 && selectedStop && /* @__PURE__ */ jsx(
339
+ "button",
340
+ {
341
+ type: "button",
342
+ className: buttonClass,
343
+ title: "Delete selected stop",
344
+ "aria-label": "Delete selected stop",
345
+ disabled: controller.isReadOnly,
346
+ onClick: () => controller.removeStop(selectedStop.id),
347
+ children: "\u2715"
348
+ }
349
+ )
350
+ ] })
351
+ ] }),
352
+ selectedStop && /* @__PURE__ */ jsxs("div", { className: "rounded-md border border-border p-2", children: [
353
+ /* @__PURE__ */ jsxs("div", { className: "mb-2 flex items-center justify-between text-xs text-muted-foreground", children: [
354
+ /* @__PURE__ */ jsxs("span", { children: [
355
+ "Selected Stop (",
356
+ Math.round(selectedStop.offset * 100),
357
+ "%)"
358
+ ] }),
359
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1", children: [
360
+ /* @__PURE__ */ jsx("span", { children: "Offset:" }),
361
+ /* @__PURE__ */ jsx(
362
+ "input",
363
+ {
364
+ type: "number",
365
+ "aria-label": "Stop offset percentage",
366
+ className: inputClass,
367
+ min: 0,
368
+ max: 100,
369
+ value: Math.round(selectedStop.offset * 100),
370
+ disabled: controller.isReadOnly,
371
+ onChange: (e) => controller.updateStop(selectedStop.id, {
372
+ offset: Math.max(0, Math.min(100, Number(e.target.value))) / 100
373
+ })
374
+ }
375
+ ),
376
+ /* @__PURE__ */ jsx("span", { children: "%" })
377
+ ] })
378
+ ] }),
379
+ isDual ? /* @__PURE__ */ jsx(
380
+ DualColorPicker,
381
+ {
382
+ value: selectedStop.color,
383
+ scheme: activeScheme,
384
+ onSchemeChange: setActiveScheme,
385
+ onChange: (dualColor) => {
386
+ controller.updateStop(selectedStop.id, {
387
+ color: formatDualColor(dualColor)
388
+ });
389
+ },
390
+ onGestureStart: () => beginGesture("Change Stop Color"),
391
+ onGestureEnd: () => endGesture(),
392
+ disabled: controller.isReadOnly
393
+ }
394
+ ) : /* @__PURE__ */ jsx(
395
+ ColorPicker,
396
+ {
397
+ value: selectedStop.color,
398
+ alpha: true,
399
+ onChange: (color) => {
400
+ controller.updateStop(selectedStop.id, { color });
401
+ },
402
+ onGestureStart: () => beginGesture("Change Stop Color"),
403
+ onGestureEnd: () => endGesture(),
404
+ disabled: controller.isReadOnly
405
+ }
406
+ )
407
+ ] }),
408
+ showPresets && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1", children: [
409
+ /* @__PURE__ */ jsx("span", { className: "text-[11px] font-medium text-muted-foreground", children: "Presets" }),
410
+ /* @__PURE__ */ jsx("div", { className: "flex flex-wrap gap-1.5", "data-testid": "gradient-presets", children: Object.entries(GRADIENT_PRESETS).map(([name, preset]) => /* @__PURE__ */ jsx(
411
+ "button",
412
+ {
413
+ type: "button",
414
+ className: "h-6 w-10 rounded border border-border shadow-sm transition hover:scale-105 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-primary",
415
+ style: { background: gradientToCss(preset) },
416
+ title: name,
417
+ "aria-label": `Preset ${name}`,
418
+ disabled: controller.isReadOnly,
419
+ onClick: () => controller.setGradient(preset, `Apply Preset ${name}`)
420
+ },
421
+ name
422
+ )) })
423
+ ] }),
424
+ showCss && /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1 border-t border-border pt-2", children: [
425
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between", children: [
426
+ /* @__PURE__ */ jsx("span", { className: "text-[11px] font-medium text-muted-foreground", children: "CSS Code" }),
427
+ /* @__PURE__ */ jsx("button", { type: "button", className: buttonClass, onClick: copyCss, children: copied ? "Copied!" : "Copy" })
428
+ ] }),
429
+ /* @__PURE__ */ jsx("pre", { className: "overflow-x-auto rounded bg-muted p-2 font-mono text-[11px] text-foreground select-all", children: gradientToCss(gradient) })
430
+ ] })
431
+ ]
432
+ }
433
+ );
434
+ }
435
+
436
+ // src/gradient/FillPicker.tsx
437
+ import { useEffect, useRef as useRef3, useState } from "react";
438
+ import {
439
+ GradientController as GradientController2,
440
+ createGradient,
441
+ gradientToCss as gradientToCss2,
442
+ parseCssGradient
443
+ } from "@pixerate/editor/gradient";
444
+ import { formatHex, mixColors, parseColor, resolveColor } from "@pixerate/editor/color";
445
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
446
+ var DEFAULT_MODES = ["solid", "gradient"];
447
+ var buttonClass2 = "inline-flex h-7 items-center justify-center rounded border border-border bg-background px-2.5 text-xs font-medium text-foreground transition-colors hover:bg-muted focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-40 aria-pressed:bg-primary aria-pressed:text-primary-foreground";
448
+ function createDefaultGradientFromColor(colorStr) {
449
+ const parsed = parseColor(colorStr);
450
+ const baseHex = parsed ? formatHex(parsed) : "#3b82f6";
451
+ const lighterHex = mixColors(baseHex, "#ffffff", 0.35, { space: "oklab" }) ?? "#ffffff";
452
+ return createGradient([baseHex, lighterHex]);
453
+ }
454
+ function FillPicker({
455
+ value,
456
+ onChange,
457
+ onGestureStart,
458
+ onGestureEnd,
459
+ modes = DEFAULT_MODES,
460
+ alpha = false,
461
+ swatches,
462
+ showPresets = true,
463
+ showCss = false,
464
+ className,
465
+ disabled = false
466
+ }) {
467
+ const allowsSolid = modes.includes("solid");
468
+ const allowsGradient = modes.includes("gradient");
469
+ const modeFor = (v) => parseCssGradient(v) && allowsGradient ? "gradient" : allowsSolid ? "solid" : "gradient";
470
+ const [mode, setModeState] = useState(() => modeFor(value));
471
+ const latest = useRef3({ onChange, mode, disabled });
472
+ latest.current.onChange = onChange;
473
+ latest.current.disabled = disabled;
474
+ const setMode = (next) => {
475
+ latest.current.mode = next;
476
+ setModeState(next);
477
+ };
478
+ const isDraggingRef = useRef3(false);
479
+ const emittedRef = useRef3(null);
480
+ const loadingRef = useRef3(false);
481
+ const controllerRef = useRef3(null);
482
+ if (!controllerRef.current) {
483
+ controllerRef.current = new GradientController2({
484
+ value: parseCssGradient(value) ?? createDefaultGradientFromColor(value),
485
+ readOnly: () => latest.current.disabled,
486
+ onChange: (g) => {
487
+ if (loadingRef.current || latest.current.mode !== "gradient") return;
488
+ const css = gradientToCss2(g);
489
+ emittedRef.current = css;
490
+ latest.current.onChange(css);
491
+ }
492
+ });
493
+ }
494
+ const controller = controllerRef.current;
495
+ const modesKey = modes.join(",");
496
+ useEffect(() => {
497
+ if (isDraggingRef.current) return;
498
+ const incoming = parseCssGradient(value);
499
+ if (incoming && allowsGradient) {
500
+ setMode("gradient");
501
+ if (value !== emittedRef.current) {
502
+ loadingRef.current = true;
503
+ try {
504
+ controller.load(incoming);
505
+ } finally {
506
+ loadingRef.current = false;
507
+ }
508
+ }
509
+ } else if (!incoming && allowsSolid) {
510
+ setMode("solid");
511
+ }
512
+ }, [value, modesKey, controller]);
513
+ const handleGestureStart = () => {
514
+ isDraggingRef.current = true;
515
+ onGestureStart?.();
516
+ };
517
+ const handleGestureEnd = () => {
518
+ isDraggingRef.current = false;
519
+ onGestureEnd?.();
520
+ };
521
+ const emit = (next) => {
522
+ emittedRef.current = next;
523
+ onChange(next);
524
+ };
525
+ const switchToSolid = () => {
526
+ if (latest.current.mode === "solid") return;
527
+ setMode("solid");
528
+ const first = controller.getGradient().stops[0]?.color ?? "#000000";
529
+ emit(resolveColor(first, "light") ?? "#000000");
530
+ };
531
+ const switchToGradient = () => {
532
+ if (latest.current.mode === "gradient") return;
533
+ setMode("gradient");
534
+ const next = createDefaultGradientFromColor(value);
535
+ loadingRef.current = true;
536
+ try {
537
+ controller.load(next);
538
+ } finally {
539
+ loadingRef.current = false;
540
+ }
541
+ emit(gradientToCss2(controller.getGradient()));
542
+ };
543
+ const gradientEditor = /* @__PURE__ */ jsx2(
544
+ GradientEditor,
545
+ {
546
+ controller,
547
+ showPresets,
548
+ showCss,
549
+ onGestureStart: handleGestureStart,
550
+ onGestureEnd: handleGestureEnd
551
+ }
552
+ );
553
+ if (modes.length === 1 && modes[0] === "solid") {
554
+ return /* @__PURE__ */ jsx2("div", { className, "data-testid": "fill-picker", children: /* @__PURE__ */ jsx2(
555
+ ColorPicker,
556
+ {
557
+ value,
558
+ onChange,
559
+ onGestureStart,
560
+ onGestureEnd,
561
+ alpha,
562
+ swatches,
563
+ disabled
564
+ }
565
+ ) });
566
+ }
567
+ if (modes.length === 1 && modes[0] === "gradient") {
568
+ return /* @__PURE__ */ jsx2("div", { className, "data-testid": "fill-picker", children: gradientEditor });
569
+ }
570
+ return /* @__PURE__ */ jsxs2("div", { className: `flex flex-col gap-2.5 ${className ?? ""}`, "data-testid": "fill-picker", children: [
571
+ /* @__PURE__ */ jsxs2("div", { className: "flex rounded-md border border-border p-0.5", children: [
572
+ /* @__PURE__ */ jsx2(
573
+ "button",
574
+ {
575
+ type: "button",
576
+ className: `flex-1 ${buttonClass2}`,
577
+ "aria-pressed": mode === "solid",
578
+ disabled,
579
+ onClick: switchToSolid,
580
+ children: "Solid"
581
+ }
582
+ ),
583
+ /* @__PURE__ */ jsx2(
584
+ "button",
585
+ {
586
+ type: "button",
587
+ className: `flex-1 ${buttonClass2}`,
588
+ "aria-pressed": mode === "gradient",
589
+ disabled,
590
+ onClick: switchToGradient,
591
+ children: "Gradient"
592
+ }
593
+ )
594
+ ] }),
595
+ mode === "solid" ? /* @__PURE__ */ jsx2(
596
+ ColorPicker,
597
+ {
598
+ value,
599
+ onChange,
600
+ onGestureStart: handleGestureStart,
601
+ onGestureEnd: handleGestureEnd,
602
+ alpha,
603
+ swatches,
604
+ disabled
605
+ }
606
+ ) : gradientEditor
607
+ ] });
608
+ }
609
+
610
+ // src/gradient/index.ts
611
+ export * from "@pixerate/editor/gradient";
612
+ export {
613
+ FillPicker,
614
+ GradientEditor,
615
+ useGradientController,
616
+ useGradientState
617
+ };
@@ -0,0 +1,10 @@
1
+ import { GradientController, type GradientControllerOptions, type GradientState } from '@pixerate/editor/gradient';
2
+ /**
3
+ * Creates and owns a GradientController instance for the component's lifetime,
4
+ * preserving it across re-renders.
5
+ */
6
+ export declare function useGradientController(options?: GradientControllerOptions | (() => GradientControllerOptions)): GradientController;
7
+ /**
8
+ * Subscribes to a GradientController's state and re-renders when it changes.
9
+ */
10
+ export declare function useGradientState(controller: GradientController): GradientState;
@@ -0,0 +1,10 @@
1
+ import { GradientController, type GradientControllerOptions, type GradientState } from '@pixerate/editor/gradient';
2
+ /**
3
+ * Creates and owns a GradientController instance for the component's lifetime,
4
+ * preserving it across re-renders.
5
+ */
6
+ export declare function useGradientController(options?: GradientControllerOptions | (() => GradientControllerOptions)): GradientController;
7
+ /**
8
+ * Subscribes to a GradientController's state and re-renders when it changes.
9
+ */
10
+ export declare function useGradientState(controller: GradientController): GradientState;