@code0-tech/pictor 0.15.0 → 0.16.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.
@@ -0,0 +1,355 @@
1
+ import { jsx as c, jsxs as C, Fragment as ae } from "react/jsx-runtime";
2
+ import { c as v } from "../../_virtual/compiler-runtime.js";
3
+ import { ColorPicker as g, parseColor as w } from "@ark-ui/react";
4
+ import "../../utils/contextStore.js";
5
+ import b from "react";
6
+ import { mergeComponentProps as x } from "../../utils/component.js";
7
+ import "js-md5";
8
+ import { InputWrapper as se } from "./InputWrapper.js";
9
+ import { Flex as ce } from "../flex/Flex.js";
10
+ import { Badge as ee } from "../badge/Badge.js";
11
+ import { Button as te } from "../button/Button.js";
12
+ import { MenuTrigger as ie, Menu as ue, MenuPortal as he, MenuContent as me } from "../menu/Menu.js";
13
+ import { SegmentedControlItem as q, SegmentedControl as de } from "../segmented-control/SegmentedControl.js";
14
+ import { useCopyToClipboard as fe } from "@uidotdev/usehooks";
15
+ import { useHotkeys as le } from "react-hotkeys-hook";
16
+ import '../../assets/components/form/ColorInput.style.css';/* empty css */
17
+ const L = b.createContext({
18
+ hasValue: !1,
19
+ placeholder: null
20
+ }), pe = /^(#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})|(rgba?|hsla?|hsba?)\(\s*-?[\d.]+%?(\s*,\s*-?[\d.]+%?){2,3}\s*\))$/i, J = (o) => {
21
+ const e = o.trim();
22
+ return pe.test(e) ? w(e) : void 0;
23
+ }, ne = (o) => {
24
+ if (typeof o == "string") return J(o);
25
+ if (o != null)
26
+ return o;
27
+ }, _e = (o) => {
28
+ const e = o.trim().replace(/°/g, "").replace(/deg\b/gi, "");
29
+ if (!e) return null;
30
+ const t = J(e);
31
+ if (t) {
32
+ const a = e.toLowerCase(), s = a.startsWith("#") ? "hexa" : a.startsWith("hsl") ? "hsla" : "rgba";
33
+ return {
34
+ color: t,
35
+ notation: s
36
+ };
37
+ }
38
+ if (/^[0-9a-f]+$/i.test(e) && (e.length === 6 || e.length === 8 || (e.length === 3 || e.length === 4) && /[a-f]/i.test(e))) return {
39
+ color: w(`#${e}`),
40
+ notation: "hexa"
41
+ };
42
+ const l = e.split(/[\s,]+/).filter(Boolean), r = l.map((a) => Number(a.replace("%", "")));
43
+ if ((l.length === 3 || l.length === 4) && r.every((a) => !Number.isNaN(a))) {
44
+ const a = l.length === 4 ? r[3] : 1;
45
+ return l[1].includes("%") || l[2].includes("%") ? {
46
+ color: w(`hsla(${r[0]}, ${r[1]}%, ${r[2]}%, ${a})`),
47
+ notation: "hsla"
48
+ } : {
49
+ color: w(`rgba(${r[0]}, ${r[1]}, ${r[2]}, ${a})`),
50
+ notation: "rgba"
51
+ };
52
+ }
53
+ return null;
54
+ }, Be = (o) => {
55
+ const e = v.c(42), {
56
+ title: t,
57
+ description: n,
58
+ left: l,
59
+ leftType: r,
60
+ rightType: a,
61
+ right: s,
62
+ formValidation: i,
63
+ initialValue: h,
64
+ defaultValue: m,
65
+ value: u,
66
+ placeholder: f,
67
+ name: d,
68
+ disabled: p,
69
+ readOnly: _,
70
+ closeOnSelect: k,
71
+ onValueChange: G,
72
+ children: K
73
+ } = o, W = f === void 0 ? "Pick a color" : f, j = u ?? h ?? m;
74
+ let H;
75
+ e[0] !== j ? (H = ne(j), e[0] = j, e[1] = H) : H = e[1];
76
+ const S = H;
77
+ let A;
78
+ e[2] !== S ? (A = () => S ?? w("hsla(0, 0%, 0%, 1)"), e[2] = S, e[3] = A) : A = e[3];
79
+ const [z, Q] = b.useState(A);
80
+ let E;
81
+ e[4] !== S ? (E = () => S !== void 0, e[4] = S, e[5] = E) : E = e[5];
82
+ const [D, Y] = b.useState(E), [y, Z] = b.useState(!1);
83
+ let O, P;
84
+ e[6] !== u ? (O = () => {
85
+ if (u === void 0)
86
+ return;
87
+ const N = ne(u);
88
+ Y(N !== void 0), N && Q(N);
89
+ }, P = [u], e[6] = u, e[7] = O, e[8] = P) : (O = e[7], P = e[8]), b.useEffect(O, P);
90
+ let R;
91
+ e[9] !== i || e[10] !== G ? (R = (N) => {
92
+ Q(N.value), Y(!0), i?.setValue?.(N.value.toString("hsla")), G?.(N);
93
+ }, e[9] = i, e[10] = G, e[11] = R) : R = e[11];
94
+ const X = R;
95
+ let B;
96
+ e[12] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (B = (N) => Z(N.open), e[12] = B) : B = e[12];
97
+ let M;
98
+ e[13] !== k || e[14] !== z || e[15] !== p || e[16] !== X || e[17] !== d || e[18] !== y || e[19] !== _ ? (M = x("color-input", {
99
+ value: z,
100
+ onValueChange: X,
101
+ open: y,
102
+ onOpenChange: B,
103
+ name: d,
104
+ disabled: p,
105
+ readOnly: _,
106
+ closeOnSelect: k
107
+ }), e[13] = k, e[14] = z, e[15] = p, e[16] = X, e[17] = d, e[18] = y, e[19] = _, e[20] = M) : M = e[20];
108
+ const U = M;
109
+ let T;
110
+ e[21] !== D || e[22] !== W ? (T = {
111
+ hasValue: D,
112
+ placeholder: W
113
+ }, e[21] = D, e[22] = W, e[23] = T) : T = e[23];
114
+ let V;
115
+ e[24] !== K || e[25] !== n || e[26] !== i || e[27] !== l || e[28] !== r || e[29] !== s || e[30] !== a || e[31] !== t ? (V = /* @__PURE__ */ c(se, { title: t, description: n, left: l, leftType: r, right: s, rightType: a, formValidation: i, children: K }), e[24] = K, e[25] = n, e[26] = i, e[27] = l, e[28] = r, e[29] = s, e[30] = a, e[31] = t, e[32] = V) : V = e[32];
116
+ let $;
117
+ e[33] !== y || e[34] !== V ? ($ = /* @__PURE__ */ c(ue, { open: y, onOpenChange: Z, children: V }), e[33] = y, e[34] = V, e[35] = $) : $ = e[35];
118
+ let I;
119
+ e[36] !== T || e[37] !== $ ? (I = /* @__PURE__ */ c(L.Provider, { value: T, children: $ }), e[36] = T, e[37] = $, e[38] = I) : I = e[38];
120
+ let F;
121
+ return e[39] !== U || e[40] !== I ? (F = /* @__PURE__ */ c(g.Root, { ...U, children: I }), e[39] = U, e[40] = I, e[41] = F) : F = e[41], F;
122
+ }, Me = (o) => {
123
+ const e = v.c(9);
124
+ let t, n, l;
125
+ if (e[0] !== o) {
126
+ const {
127
+ children: i,
128
+ ...h
129
+ } = o;
130
+ n = i, t = g.Control, l = x("color-input__control", h), e[0] = o, e[1] = t, e[2] = n, e[3] = l;
131
+ } else
132
+ t = e[1], n = e[2], l = e[3];
133
+ const r = l;
134
+ let a;
135
+ e[4] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (a = /* @__PURE__ */ c(g.HiddenInput, {}), e[4] = a) : a = e[4];
136
+ let s;
137
+ return e[5] !== t || e[6] !== n || e[7] !== r ? (s = /* @__PURE__ */ C(t, { ...r, children: [
138
+ n,
139
+ a
140
+ ] }), e[5] = t, e[6] = n, e[7] = r, e[8] = s) : s = e[8], s;
141
+ }, Fe = (o) => {
142
+ const e = v.c(3), {
143
+ hasValue: t
144
+ } = b.useContext(L);
145
+ let n;
146
+ return e[0] !== t || e[1] !== o ? (n = /* @__PURE__ */ c("div", { className: "color-input__swatch-wrapper", children: t ? /* @__PURE__ */ C(ae, { children: [
147
+ /* @__PURE__ */ c(g.TransparencyGrid, { className: "color-input__transparency-grid" }),
148
+ /* @__PURE__ */ c(g.ValueSwatch, { ...x("color-input__swatch", o) })
149
+ ] }) : /* @__PURE__ */ c("div", { className: "color-input__swatch color-input__swatch--empty" }) }), e[0] = t, e[1] = o, e[2] = n) : n = e[2], n;
150
+ }, Le = (o) => {
151
+ const e = v.c(7), {
152
+ hasValue: t,
153
+ placeholder: n
154
+ } = b.useContext(L);
155
+ if (!t) {
156
+ let r;
157
+ e[0] !== o ? (r = x("color-input__value-text color-input__value-text--placeholder", o), e[0] = o, e[1] = r) : r = e[1];
158
+ let a;
159
+ return e[2] !== n || e[3] !== r ? (a = /* @__PURE__ */ c("span", { ...r, children: n }), e[2] = n, e[3] = r, e[4] = a) : a = e[4], a;
160
+ }
161
+ let l;
162
+ return e[5] !== o ? (l = /* @__PURE__ */ c(g.Context, { children: (r) => /* @__PURE__ */ c("span", { ...x("color-input__value-text", o), children: r.value.toString("hsla") }) }), e[5] = o, e[6] = l) : l = e[6], l;
163
+ }, Ge = (o) => {
164
+ const e = v.c(4);
165
+ let t;
166
+ e[0] !== o ? (t = x("color-input__trigger", o), e[0] = o, e[1] = t) : t = e[1];
167
+ const n = t;
168
+ let l;
169
+ return e[2] !== n ? (l = /* @__PURE__ */ c(ie, { ...n }), e[2] = n, e[3] = l) : l = e[3], l;
170
+ }, Ke = (o) => {
171
+ const e = v.c(21);
172
+ let t, n, l, r, a, s;
173
+ if (e[0] !== o) {
174
+ const {
175
+ defaultNotation: _,
176
+ ...k
177
+ } = o;
178
+ l = _, n = he, t = me, a = "end", s = 8, r = x("color-input__content", k), e[0] = o, e[1] = t, e[2] = n, e[3] = l, e[4] = r, e[5] = a, e[6] = s;
179
+ } else
180
+ t = e[1], n = e[2], l = e[3], r = e[4], a = e[5], s = e[6];
181
+ const i = r;
182
+ let h;
183
+ e[7] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (h = /* @__PURE__ */ C(g.Area, { className: "color-input__area", children: [
184
+ /* @__PURE__ */ c(g.AreaBackground, { className: "color-input__area-background" }),
185
+ /* @__PURE__ */ c(g.AreaThumb, { className: "color-input__area-thumb" })
186
+ ] }), e[7] = h) : h = e[7];
187
+ let m;
188
+ e[8] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (m = /* @__PURE__ */ C(g.ChannelSlider, { channel: "hue", className: "color-input__channel-slider", children: [
189
+ /* @__PURE__ */ c(g.ChannelSliderTrack, { className: "color-input__channel-slider-track" }),
190
+ /* @__PURE__ */ c(g.ChannelSliderThumb, { className: "color-input__channel-slider-thumb" })
191
+ ] }), e[8] = m) : m = e[8];
192
+ let u;
193
+ e[9] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (u = /* @__PURE__ */ C("div", { className: "color-input__sliders", children: [
194
+ m,
195
+ /* @__PURE__ */ C(g.ChannelSlider, { channel: "alpha", className: "color-input__channel-slider", children: [
196
+ /* @__PURE__ */ c(g.TransparencyGrid, { className: "color-input__channel-slider-grid" }),
197
+ /* @__PURE__ */ c(g.ChannelSliderTrack, { className: "color-input__channel-slider-track" }),
198
+ /* @__PURE__ */ c(g.ChannelSliderThumb, { className: "color-input__channel-slider-thumb" })
199
+ ] })
200
+ ] }), e[9] = u) : u = e[9];
201
+ let f;
202
+ e[10] !== l ? (f = /* @__PURE__ */ C("div", { className: "color-input__body", children: [
203
+ h,
204
+ u,
205
+ /* @__PURE__ */ c(g.Context, { children: (_) => /* @__PURE__ */ c(ge, { api: _, defaultNotation: l ?? "hsla" }) })
206
+ ] }), e[10] = l, e[11] = f) : f = e[11];
207
+ let d;
208
+ e[12] !== t || e[13] !== a || e[14] !== s || e[15] !== i || e[16] !== f ? (d = /* @__PURE__ */ c(t, { align: a, sideOffset: s, ...i, children: f }), e[12] = t, e[13] = a, e[14] = s, e[15] = i, e[16] = f, e[17] = d) : d = e[17];
209
+ let p;
210
+ return e[18] !== n || e[19] !== d ? (p = /* @__PURE__ */ c(n, { children: d }), e[18] = n, e[19] = d, e[20] = p) : p = e[20], p;
211
+ }, ge = (o) => {
212
+ const e = v.c(14), {
213
+ api: t,
214
+ defaultNotation: n
215
+ } = o, [l, r] = b.useState(n);
216
+ let a;
217
+ e[0] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (a = (_) => {
218
+ (_ === "rgba" || _ === "hsla" || _ === "hexa") && r(_);
219
+ }, e[0] = a) : a = e[0];
220
+ const s = a;
221
+ let i, h, m;
222
+ e[1] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (i = /* @__PURE__ */ c(q, { value: "rgba", children: "RGBA" }), h = /* @__PURE__ */ c(q, { value: "hsla", children: "HSLA" }), m = /* @__PURE__ */ c(q, { value: "hexa", children: "HEXA" }), e[1] = i, e[2] = h, e[3] = m) : (i = e[1], h = e[2], m = e[3]);
223
+ let u;
224
+ e[4] !== l ? (u = /* @__PURE__ */ C(de, { type: "single", value: l, onValueChange: s, className: "segmented-control color-input__notation", children: [
225
+ i,
226
+ h,
227
+ m
228
+ ] }), e[4] = l, e[5] = u) : u = e[5];
229
+ let f, d;
230
+ e[6] !== t || e[7] !== l ? (f = l === "hexa" ? /* @__PURE__ */ C("label", { className: "color-input__channel color-input__channel--hex", children: [
231
+ /* @__PURE__ */ c("span", { className: "color-input__channel-label", children: "HEXA" }),
232
+ /* @__PURE__ */ c(be, { api: t })
233
+ ] }) : /* @__PURE__ */ C("div", { className: "color-input__channel-row", children: [
234
+ (l === "rgba" ? ["red", "green", "blue"] : ["hue", "saturation", "lightness"]).map((_) => /* @__PURE__ */ c(oe, { api: t, format: l, channel: _ }, _)),
235
+ /* @__PURE__ */ c(oe, { api: t, format: l, channel: "alpha" })
236
+ ] }), d = /* @__PURE__ */ c(Ce, { api: t, notation: l, setNotation: r }), e[6] = t, e[7] = l, e[8] = f, e[9] = d) : (f = e[8], d = e[9]);
237
+ let p;
238
+ return e[10] !== u || e[11] !== f || e[12] !== d ? (p = /* @__PURE__ */ C("div", { className: "color-input__channels", onKeyDown: ve, children: [
239
+ u,
240
+ f,
241
+ d
242
+ ] }), e[10] = u, e[11] = f, e[12] = d, e[13] = p) : p = e[13], p;
243
+ }, Ce = ({
244
+ api: o,
245
+ notation: e,
246
+ setNotation: t
247
+ }) => {
248
+ const {
249
+ hasValue: n
250
+ } = b.useContext(L), [, l] = fe(), r = b.useRef(o);
251
+ r.current = o;
252
+ const a = () => {
253
+ n && l(e === "hexa" ? o.value.toString("hexa") : o.value.toString(e));
254
+ }, s = () => {
255
+ navigator.clipboard.readText().then((h) => {
256
+ const m = _e(h);
257
+ m && (r.current.setValue(m.color), t(m.notation), document.activeElement instanceof HTMLElement && document.activeElement.blur());
258
+ }, () => {
259
+ });
260
+ };
261
+ le("mod+c", a, {
262
+ enabled: n,
263
+ enableOnFormTags: !0
264
+ }, [n, e, o]), le("mod+v", s, {
265
+ enableOnFormTags: !0
266
+ });
267
+ const i = /mac|iphone|ipad|ipod/i.test(navigator.userAgent) ? "⌘" : "Ctrl";
268
+ return /* @__PURE__ */ C(ce, { className: "color-input__actions", align: "center", children: [
269
+ /* @__PURE__ */ C(te, { paddingSize: "xxs", onClick: s, children: [
270
+ "Paste",
271
+ /* @__PURE__ */ C(ee, { children: [
272
+ i,
273
+ "V"
274
+ ] })
275
+ ] }),
276
+ /* @__PURE__ */ C(te, { paddingSize: "xxs", onClick: a, disabled: !n, children: [
277
+ "Copy",
278
+ /* @__PURE__ */ C(ee, { children: [
279
+ i,
280
+ "C"
281
+ ] })
282
+ ] })
283
+ ] });
284
+ }, oe = (o) => {
285
+ const e = v.c(18), {
286
+ channel: t,
287
+ format: n,
288
+ api: l
289
+ } = o;
290
+ let r;
291
+ e[0] !== t[0] ? (r = t[0].toUpperCase(), e[0] = t[0], e[1] = r) : r = e[1];
292
+ let a;
293
+ e[2] !== r ? (a = /* @__PURE__ */ c("span", { className: "color-input__channel-label", children: r }), e[2] = r, e[3] = a) : a = e[3];
294
+ let s;
295
+ e[4] !== l || e[5] !== t ? (s = l.getChannelValue(t), e[4] = l, e[5] = t, e[6] = s) : s = e[6];
296
+ const i = t === "alpha" ? "decimal" : "numeric";
297
+ let h;
298
+ e[7] !== l || e[8] !== t || e[9] !== n ? (h = (f) => {
299
+ const d = Number(f);
300
+ f.trim() === "" || Number.isNaN(d) || l.setValue(l.value.toFormat(n).withChannelValue(t, d));
301
+ }, e[7] = l, e[8] = t, e[9] = n, e[10] = h) : h = e[10];
302
+ let m;
303
+ e[11] !== s || e[12] !== i || e[13] !== h ? (m = /* @__PURE__ */ c(re, { committed: s, inputMode: i, commit: h }), e[11] = s, e[12] = i, e[13] = h, e[14] = m) : m = e[14];
304
+ let u;
305
+ return e[15] !== a || e[16] !== m ? (u = /* @__PURE__ */ C("label", { className: "color-input__channel", children: [
306
+ a,
307
+ m
308
+ ] }), e[15] = a, e[16] = m, e[17] = u) : u = e[17], u;
309
+ }, be = (o) => {
310
+ const e = v.c(7), {
311
+ api: t
312
+ } = o;
313
+ let n;
314
+ e[0] !== t.value ? (n = t.value.toString("hexa"), e[0] = t.value, e[1] = n) : n = e[1];
315
+ let l;
316
+ e[2] !== t ? (l = (a) => {
317
+ const s = J(a);
318
+ s && t.setValue(s);
319
+ }, e[2] = t, e[3] = l) : l = e[3];
320
+ let r;
321
+ return e[4] !== n || e[5] !== l ? (r = /* @__PURE__ */ c(re, { committed: n, commit: l }), e[4] = n, e[5] = l, e[6] = r) : r = e[6], r;
322
+ }, re = (o) => {
323
+ const e = v.c(13), {
324
+ committed: t,
325
+ commit: n,
326
+ inputMode: l
327
+ } = o, r = l === void 0 ? "text" : l, [a, s] = b.useState(t), i = b.useRef(!1);
328
+ let h, m;
329
+ e[0] !== t ? (h = () => {
330
+ i.current || s(t);
331
+ }, m = [t], e[0] = t, e[1] = h, e[2] = m) : (h = e[1], m = e[2]), b.useEffect(h, m);
332
+ let u;
333
+ e[3] === /* @__PURE__ */ Symbol.for("react.memo_cache_sentinel") ? (u = () => i.current = !0, e[3] = u) : u = e[3];
334
+ let f;
335
+ e[4] !== n ? (f = (_) => {
336
+ s(_.target.value), n(_.target.value);
337
+ }, e[4] = n, e[5] = f) : f = e[5];
338
+ let d;
339
+ e[6] !== t ? (d = () => {
340
+ i.current = !1, s(t);
341
+ }, e[6] = t, e[7] = d) : d = e[7];
342
+ let p;
343
+ return e[8] !== a || e[9] !== r || e[10] !== f || e[11] !== d ? (p = /* @__PURE__ */ c("input", { className: "color-input__channel-input", type: "text", inputMode: r, value: a, spellCheck: !1, autoComplete: "off", onFocus: u, onChange: f, onBlur: d }), e[8] = a, e[9] = r, e[10] = f, e[11] = d, e[12] = p) : p = e[12], p;
344
+ };
345
+ function ve(o) {
346
+ o.key !== "Escape" && !o.metaKey && !o.ctrlKey && o.stopPropagation();
347
+ }
348
+ export {
349
+ Be as ColorInput,
350
+ Ke as ColorInputContent,
351
+ Me as ColorInputControl,
352
+ Fe as ColorInputSwatch,
353
+ Ge as ColorInputTrigger,
354
+ Le as ColorInputValueText
355
+ };