@elcrm/form 0.0.87 → 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 (68) hide show
  1. package/README.md +87 -47
  2. package/dist/Form.js +1 -11
  3. package/dist/FormInitContext.js +1 -20
  4. package/dist/assets/styles/Field.module.css +1 -1
  5. package/dist/assets/styles/Form.module.css +1 -1
  6. package/dist/assets/styles/Select.module.css +1 -1
  7. package/dist/core/Field.d.ts +14 -2
  8. package/dist/core/Field.js +2 -35
  9. package/dist/dark.css +22 -3
  10. package/dist/events/onEvent.js +1 -67
  11. package/dist/fields/Check.js +1 -86
  12. package/dist/fields/Code.js +1 -94
  13. package/dist/fields/Color.js +1 -495
  14. package/dist/fields/Date.js +1 -450
  15. package/dist/fields/Display.d.ts +1 -1
  16. package/dist/fields/Display.js +1 -49
  17. package/dist/fields/DragDrop.js +1 -189
  18. package/dist/fields/Email.d.ts +4 -2
  19. package/dist/fields/Email.js +1 -80
  20. package/dist/fields/File.js +1 -115
  21. package/dist/fields/Hidden.js +1 -33
  22. package/dist/fields/Input.d.ts +3 -1
  23. package/dist/fields/Input.js +1 -149
  24. package/dist/fields/Mask.js +1 -119
  25. package/dist/fields/Modal.js +1 -68
  26. package/dist/fields/Money.js +1 -125
  27. package/dist/fields/NativeTextField.d.ts +15 -0
  28. package/dist/fields/NativeTextField.js +1 -0
  29. package/dist/fields/Numeric.d.ts +12 -1
  30. package/dist/fields/Numeric.js +1 -105
  31. package/dist/fields/Options.js +1 -69
  32. package/dist/fields/Password.d.ts +1 -1
  33. package/dist/fields/Password.js +1 -143
  34. package/dist/fields/Percent.d.ts +8 -1
  35. package/dist/fields/Percent.js +1 -100
  36. package/dist/fields/Phone.d.ts +2 -2
  37. package/dist/fields/Phone.js +1 -128
  38. package/dist/fields/Radio.js +1 -88
  39. package/dist/fields/Range.js +1 -109
  40. package/dist/fields/Rating.js +1 -82
  41. package/dist/fields/RichText.js +1 -134
  42. package/dist/fields/Select.d.ts +3 -1
  43. package/dist/fields/Select.js +1 -159
  44. package/dist/fields/Tags.js +1 -97
  45. package/dist/fields/Textarea.d.ts +1 -1
  46. package/dist/fields/Textarea.js +1 -68
  47. package/dist/fields/Time.js +1 -444
  48. package/dist/fields/Url.d.ts +4 -2
  49. package/dist/fields/Url.js +1 -80
  50. package/dist/fields/emit.d.ts +9 -0
  51. package/dist/fields/emit.js +1 -0
  52. package/dist/fields/type.d.ts +1 -1
  53. package/dist/fields/visibility.js +1 -6
  54. package/dist/hooks/use.js +1 -57
  55. package/dist/index.js +2 -42
  56. package/dist/index.umd.js +2 -2
  57. package/dist/light.css +23 -3
  58. package/dist/mask/MaskPhone.js +1 -1384
  59. package/dist/mask/phoneDefaults.d.ts +8 -0
  60. package/dist/mask/phoneDefaults.js +1 -0
  61. package/dist/overlayZ.js +1 -25
  62. package/dist/package.js +1 -130
  63. package/dist/style.css +3 -3
  64. package/dist/styles/Color.module.js +1 -27
  65. package/dist/styles/Field.module.js +1 -77
  66. package/dist/styles/Form.module.js +1 -12
  67. package/dist/styles/Select.module.js +1 -9
  68. package/package.json +2 -2
@@ -1,495 +1 @@
1
- import e from "../styles/Field.module.js";
2
- import t from "../core/Field.js";
3
- import { useFormFieldState as n } from "../hooks/use.js";
4
- import { acquireOverlayZIndex as r, releaseOverlayZIndex as i } from "../overlayZ.js";
5
- import a from "../styles/Color.module.js";
6
- import o from "react";
7
- import { Fragment as s, jsx as c, jsxs as l } from "react/jsx-runtime";
8
- import { createPortal as u } from "react-dom";
9
- //#region src/lib/fields/Color.tsx
10
- var d = /* @__PURE__ */ "#FF2D55.#FF3B30.#FF453A.#FF6961.#FF8A80.#C11F3E.#AF52DE.#BF5AF2.#DA8FFF.#5856D6.#5E5CE6.#7D7AFF.#007AFF.#0A84FF.#409CFF.#64D2FF.#70D7FF.#5AC8FA.#32ADE6.#64D2FF.#40C8E0.#00C7BE.#63E6E2.#66D4CF.#34C759.#30D158.#30DB5B.#4CD964.#6DE6A0.#8EEDB3.#A8E10C.#B5F200.#D0FF00.#E4FF66.#F2FF99.#C6FF00.#FFCC00.#FFD60A.#FFEA00.#FFE066.#FFF3A3.#FFEB3B.#FF9500.#FF9F0A.#FFB340.#FFB340.#FFD580.#FF8C00.#A2845E.#AC8E68.#C4A484.#8E8E93.#636366.#48484A.#3A3A3C.#2C2C2E.#1C1C1E".split(".");
11
- function f(e, t, n) {
12
- return Math.min(n, Math.max(t, e));
13
- }
14
- function p(e) {
15
- return Math.round(f(e, 0, 255)).toString(16).padStart(2, "0").toUpperCase();
16
- }
17
- function m(e, t, n) {
18
- let r = (e % 360 + 360) % 360, i = n * t, a = i * (1 - Math.abs(r / 60 % 2 - 1)), o = n - i, s = 0, c = 0, l = 0;
19
- return r < 60 ? [s, c, l] = [
20
- i,
21
- a,
22
- 0
23
- ] : r < 120 ? [s, c, l] = [
24
- a,
25
- i,
26
- 0
27
- ] : r < 180 ? [s, c, l] = [
28
- 0,
29
- i,
30
- a
31
- ] : r < 240 ? [s, c, l] = [
32
- 0,
33
- a,
34
- i
35
- ] : r < 300 ? [s, c, l] = [
36
- a,
37
- 0,
38
- i
39
- ] : [s, c, l] = [
40
- i,
41
- 0,
42
- a
43
- ], {
44
- r: (s + o) * 255,
45
- g: (c + o) * 255,
46
- b: (l + o) * 255
47
- };
48
- }
49
- function h(e, t, n) {
50
- let r = e / 255, i = t / 255, a = n / 255, o = Math.max(r, i, a), s = o - Math.min(r, i, a), c = 0;
51
- s !== 0 && (c = o === r ? (i - a) / s % 6 : o === i ? (a - r) / s + 2 : (r - i) / s + 4, c *= 60, c < 0 && (c += 360));
52
- let l = o === 0 ? 0 : s / o;
53
- return {
54
- h: c,
55
- s: l,
56
- v: o
57
- };
58
- }
59
- function g(e) {
60
- if (typeof e != "string") return null;
61
- let t = e.trim();
62
- if (!t || (t[0] === "#" && (t = t.slice(1)), ![
63
- 3,
64
- 4,
65
- 6,
66
- 8
67
- ].includes(t.length)) || !/^[0-9a-fA-F]+$/.test(t)) return null;
68
- let n = 0, r = 0, i = 0, a = 1;
69
- return t.length === 3 || t.length === 4 ? (n = parseInt(t[0] + t[0], 16), r = parseInt(t[1] + t[1], 16), i = parseInt(t[2] + t[2], 16), t.length === 4 && (a = parseInt(t[3] + t[3], 16) / 255)) : (n = parseInt(t.slice(0, 2), 16), r = parseInt(t.slice(2, 4), 16), i = parseInt(t.slice(4, 6), 16), t.length === 8 && (a = parseInt(t.slice(6, 8), 16) / 255)), {
70
- ...h(n, r, i),
71
- a
72
- };
73
- }
74
- function _(e, t) {
75
- let { r: n, g: r, b: i } = m(e.h, e.s, e.v), a = `#${p(n)}${p(r)}${p(i)}`;
76
- return !t || e.a >= .999 ? a : `${a}${p(e.a * 255)}`;
77
- }
78
- function v(e) {
79
- let { r: t, g: n, b: r } = m(e.h, e.s, e.v), i = Math.round(t), a = Math.round(n), o = Math.round(r);
80
- return e.a >= .999 ? `rgb(${i}, ${a}, ${o})` : `rgba(${i}, ${a}, ${o}, ${Number(e.a.toFixed(3))})`;
81
- }
82
- function y(e) {
83
- let { r: t, g: n, b: r } = m(e, 1, 1);
84
- return `rgb(${Math.round(t)},${Math.round(n)},${Math.round(r)})`;
85
- }
86
- function b(e) {
87
- let t = g(e);
88
- return t ? _(t, t.a < .999) : e.toUpperCase();
89
- }
90
- function x(e) {
91
- return e == null ? !0 : typeof e == "string" ? e.trim() === "" : !1;
92
- }
93
- function S(e) {
94
- if (!(e instanceof HTMLElement)) return !1;
95
- let t = e.tagName;
96
- return t === "INPUT" || t === "TEXTAREA" || t === "SELECT" ? !0 : !!e.isContentEditable;
97
- }
98
- function C(e) {
99
- let t = (e || "").trim();
100
- if (!t) return null;
101
- let n = g(t) ?? g(t[0] === "#" ? t : `#${t.replace(/^#/, "")}`);
102
- if (n) return n;
103
- let r = /^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(?:\s*,\s*([\d.]+))?\s*\)$/i.exec(t);
104
- if (r) {
105
- let e = f(Number(r[1]), 0, 255), t = f(Number(r[2]), 0, 255), n = f(Number(r[3]), 0, 255), i = r[4] === void 0 ? 1 : f(Number(r[4]), 0, 1);
106
- return {
107
- ...h(e, t, n),
108
- a: i
109
- };
110
- }
111
- return null;
112
- }
113
- function w() {
114
- return typeof window < "u" && "EyeDropper" in window;
115
- }
116
- var T = {
117
- h: 0,
118
- s: 1,
119
- v: .76,
120
- a: 1
121
- };
122
- function E({ value: r = "", form: i, onValue: f, onBlur: p, name: m = "", placeholder: h = "#000000", label: v = "", error: y = "", hidden: b = !1, disabled: w = !1, className: E = "", after: O = "", before: k = "", colors: A, alpha: j = !0 }) {
123
- let { value: M } = n(i, m, r), N = typeof M == "string" ? M : M == null ? "" : String(M), [P, F] = o.useState(!1), [I, L] = o.useState(null), [R, z] = o.useState(() => g(N) ?? { ...T }), [B, V] = o.useState(() => _(g(N) ?? T, j)), H = A?.length ? A : d, U = g(N), W = U ?? (x(N) ? g(h) : null), G = U ? _(U, j) : "", K = W ? _(W, j) : "transparent";
124
- o.useEffect(() => {
125
- let e = g(N);
126
- e && (z(e), V(_(e, j)));
127
- }, [N, j]);
128
- let q = o.useCallback((e, t) => {
129
- let n = _(e, j), r = {
130
- value: n,
131
- name: m || ""
132
- };
133
- i && m && (t === "live" ? i.onValue(r) : i.setValue(r)), f && f !== i?.onValue && f(r), V(n);
134
- }, [
135
- j,
136
- i,
137
- m,
138
- f
139
- ]), J = (e) => {
140
- if (w) return;
141
- let t = g(N) ?? { ...R };
142
- z(t), V(_(t, j)), L(e.getBoundingClientRect()), F(!0);
143
- }, Y = () => {
144
- F(!1), p?.({
145
- value: _(R, j),
146
- name: m || ""
147
- });
148
- };
149
- o.useEffect(() => {
150
- if (!P || w) return;
151
- let e = "", t = (t) => {
152
- let n = C(t);
153
- if (!n) return !1;
154
- let r = {
155
- ...n,
156
- a: j ? n.a : 1
157
- }, i = _(r, j);
158
- return i === e ? !0 : (e = i, z(r), q(r, "commit"), !0);
159
- }, n = (e) => {
160
- S(e.target) || t(e.clipboardData?.getData("text/plain") ?? "") && (e.preventDefault(), e.stopPropagation());
161
- }, r = (e) => {
162
- (e.ctrlKey || e.metaKey) && (e.key !== "v" && e.key !== "V" || S(e.target) || navigator.clipboard?.readText && (e.preventDefault(), navigator.clipboard.readText().then((e) => {
163
- t(e);
164
- })));
165
- };
166
- return document.addEventListener("paste", n, !0), window.addEventListener("keydown", r, !0), () => {
167
- document.removeEventListener("paste", n, !0), window.removeEventListener("keydown", r, !0);
168
- };
169
- }, [
170
- P,
171
- w,
172
- j,
173
- q
174
- ]);
175
- let X = (e) => {
176
- if (!w) {
177
- if (!P && (e.key === "Enter" || e.key === " " || e.key === "ArrowDown")) {
178
- e.preventDefault(), J(e.currentTarget);
179
- return;
180
- }
181
- P && e.key === "Escape" && (e.preventDefault(), Y());
182
- }
183
- };
184
- return b && x(N) ? null : /* @__PURE__ */ l(s, { children: [/* @__PURE__ */ l(t, {
185
- type: "color",
186
- label: v,
187
- error: y,
188
- element_blok: { className: [E || ""] },
189
- element_input: {
190
- className: [e.w],
191
- props: { "data-disabled": w ? "true" : void 0 }
192
- },
193
- children: [
194
- k ? /* @__PURE__ */ c("span", {
195
- className: e.affix,
196
- children: k
197
- }) : null,
198
- /* @__PURE__ */ l("div", {
199
- className: a.trigger,
200
- role: "combobox",
201
- tabIndex: w ? -1 : 0,
202
- "aria-expanded": P,
203
- "aria-haspopup": "dialog",
204
- "aria-disabled": w,
205
- "aria-label": v || "Цвет",
206
- onClick: (e) => J(e.currentTarget),
207
- onKeyDown: X,
208
- children: [/* @__PURE__ */ c("span", {
209
- className: a.preview,
210
- style: { "--preview-color": K }
211
- }), /* @__PURE__ */ c("span", {
212
- className: [a.hexText, G ? "" : a.placeholder].filter(Boolean).join(" "),
213
- children: G || h
214
- })]
215
- }),
216
- O ? /* @__PURE__ */ c("span", {
217
- className: e.affix,
218
- children: O
219
- }) : null
220
- ]
221
- }), P && u(/* @__PURE__ */ c(D, {
222
- parent: I || void 0,
223
- hsva: R,
224
- setHsva: z,
225
- hexDraft: B,
226
- setHexDraft: V,
227
- alpha: j,
228
- palette: H,
229
- onLive: (e) => q(e, "live"),
230
- onCommit: (e) => q(e, "commit"),
231
- onClose: Y
232
- }), document.body)] });
233
- }
234
- E.displayName = "ColorField";
235
- function D({ parent: e, hsva: t, setHsva: n, hexDraft: u, setHexDraft: d, alpha: p, palette: m, onLive: h, onCommit: y, onClose: x }) {
236
- let [S] = o.useState(() => r());
237
- if (o.useEffect(() => () => i(S), [S]), !e) return null;
238
- let C = p ? 340 : 300, T = window.innerHeight - e.bottom < C + 8 ? Math.max(8, e.top - C - 8) : Math.min(window.innerHeight - C - 8, e.bottom + 8), E = Math.min(Math.max(8, e.left), window.innerWidth - 260 - 8), D = v({
239
- ...t,
240
- a: 1
241
- }), j = b(_(t, p)), N = (e, t) => {
242
- n(e), t === "live" ? h(e) : y(e);
243
- }, P = () => {
244
- let e = g(u);
245
- if (!e) {
246
- d(_(t, p));
247
- return;
248
- }
249
- N(e, "commit");
250
- }, F = (e) => {
251
- let n = e.replace(/[^\d]/g, ""), r = f(Number(n || 0), 0, 100);
252
- N({
253
- ...t,
254
- a: r / 100
255
- }, "commit");
256
- }, I = async () => {
257
- if (w()) try {
258
- let e = window.EyeDropper, n = g((await new e().open()).sRGBHex);
259
- n && N({
260
- ...n,
261
- a: t.a
262
- }, "commit");
263
- } catch {}
264
- };
265
- return /* @__PURE__ */ l(s, { children: [/* @__PURE__ */ c("div", {
266
- className: a.o,
267
- style: { zIndex: S },
268
- onClick: x,
269
- "aria-hidden": !0
270
- }), /* @__PURE__ */ l("div", {
271
- className: a.panel,
272
- role: "dialog",
273
- "aria-label": "Выбор цвета",
274
- style: {
275
- top: T,
276
- left: E,
277
- width: 260,
278
- zIndex: S + 1
279
- },
280
- children: [
281
- /* @__PURE__ */ c(O, {
282
- hsva: t,
283
- onChange: (e, n, r) => N({
284
- ...t,
285
- s: e,
286
- v: n
287
- }, r)
288
- }),
289
- /* @__PURE__ */ l("div", {
290
- className: a.slidersRow,
291
- children: [w() ? /* @__PURE__ */ c("button", {
292
- type: "button",
293
- className: a.eyedropper,
294
- "aria-label": "Пипетка",
295
- onClick: I,
296
- children: /* @__PURE__ */ c(M, {})
297
- }) : null, /* @__PURE__ */ l("div", {
298
- className: a.sliders,
299
- children: [/* @__PURE__ */ c(k, {
300
- h: t.h,
301
- onChange: (e, n) => N({
302
- ...t,
303
- h: e
304
- }, n)
305
- }), p ? /* @__PURE__ */ c(A, {
306
- a: t.a,
307
- color: D,
308
- onChange: (e, n) => N({
309
- ...t,
310
- a: e
311
- }, n)
312
- }) : null]
313
- })]
314
- }),
315
- /* @__PURE__ */ l("div", {
316
- className: a.inputs,
317
- children: [
318
- /* @__PURE__ */ c("span", {
319
- className: a.format,
320
- children: "Hex"
321
- }),
322
- /* @__PURE__ */ c("input", {
323
- className: a.input,
324
- value: u.replace(/^#/, ""),
325
- spellCheck: !1,
326
- "aria-label": "Hex",
327
- onChange: (e) => {
328
- let t = e.target.value.replace(/[^0-9a-fA-F]/g, "");
329
- d(t ? `#${t}` : "");
330
- },
331
- onBlur: P,
332
- onKeyDown: (e) => {
333
- e.key === "Enter" && (e.preventDefault(), e.target.blur());
334
- }
335
- }),
336
- p ? /* @__PURE__ */ c("input", {
337
- className: `${a.input} ${a.inputAlpha}`,
338
- value: `${Math.round(t.a * 100)} %`,
339
- "aria-label": "Прозрачность",
340
- onChange: (e) => F(e.target.value),
341
- onFocus: (e) => e.target.select()
342
- }) : null
343
- ]
344
- }),
345
- /* @__PURE__ */ c("div", {
346
- className: a.palette,
347
- role: "listbox",
348
- "aria-label": "Палитра",
349
- children: m.map((e) => {
350
- let t = b(e) === j;
351
- return /* @__PURE__ */ c("button", {
352
- type: "button",
353
- role: "option",
354
- "aria-checked": t,
355
- "aria-label": e,
356
- className: a.swatchBtn,
357
- style: { "--swatch": e },
358
- onClick: () => {
359
- let t = g(e);
360
- t && N({
361
- ...t,
362
- a: p ? t.a : 1
363
- }, "commit");
364
- }
365
- }, e);
366
- })
367
- })
368
- ]
369
- })] });
370
- }
371
- function O({ hsva: e, onChange: t }) {
372
- let n = o.useRef(null), r = o.useRef(!1), i = o.useRef(t);
373
- i.current = t;
374
- let s = (t, r) => {
375
- let i = n.current;
376
- if (!i) return {
377
- s: e.s,
378
- v: e.v
379
- };
380
- let a = i.getBoundingClientRect();
381
- return {
382
- s: f((t - a.left) / a.width, 0, 1),
383
- v: f(1 - (r - a.top) / a.height, 0, 1)
384
- };
385
- };
386
- return o.useEffect(() => {
387
- let e = (e) => {
388
- if (!r.current) return;
389
- let { s: t, v: n } = s(e.clientX, e.clientY);
390
- i.current(t, n, "live");
391
- }, t = (e) => {
392
- if (!r.current) return;
393
- r.current = !1;
394
- let { s: t, v: n } = s(e.clientX, e.clientY);
395
- i.current(t, n, "commit");
396
- };
397
- return window.addEventListener("pointermove", e), window.addEventListener("pointerup", t), () => {
398
- window.removeEventListener("pointermove", e), window.removeEventListener("pointerup", t);
399
- };
400
- }, []), /* @__PURE__ */ l("div", {
401
- ref: n,
402
- className: a.sv,
403
- style: { background: y(e.h) },
404
- onPointerDown: (e) => {
405
- r.current = !0;
406
- let { s: t, v: n } = s(e.clientX, e.clientY);
407
- i.current(t, n, "live");
408
- },
409
- children: [
410
- /* @__PURE__ */ c("div", { className: a.svWhite }),
411
- /* @__PURE__ */ c("div", { className: a.svBlack }),
412
- /* @__PURE__ */ c("div", {
413
- className: a.handle,
414
- style: {
415
- left: `${e.s * 100}%`,
416
- top: `${(1 - e.v) * 100}%`,
417
- background: v({
418
- ...e,
419
- a: 1
420
- })
421
- }
422
- })
423
- ]
424
- });
425
- }
426
- function k({ h: e, onChange: t }) {
427
- return /* @__PURE__ */ c(j, {
428
- className: `${a.slider} ${a.sliderHue}`,
429
- ratio: e / 360,
430
- onRatio: (e, n) => t(e * 360, n),
431
- handleColor: y(e)
432
- });
433
- }
434
- function A({ a: e, color: t, onChange: n }) {
435
- return /* @__PURE__ */ c(j, {
436
- className: `${a.slider} ${a.sliderAlpha}`,
437
- style: { "--alpha-color": t },
438
- ratio: e,
439
- onRatio: n,
440
- handleColor: t
441
- });
442
- }
443
- function j({ className: e, style: t, ratio: n, onRatio: r, handleColor: i }) {
444
- let s = o.useRef(null), l = o.useRef(!1), u = (e, t) => {
445
- let n = s.current;
446
- if (!n) return;
447
- let i = n.getBoundingClientRect();
448
- r(f((e - i.left) / i.width, 0, 1), t);
449
- };
450
- return o.useEffect(() => {
451
- let e = (e) => {
452
- l.current && u(e.clientX, "live");
453
- }, t = (e) => {
454
- l.current && (l.current = !1, u(e.clientX, "commit"));
455
- };
456
- return window.addEventListener("pointermove", e), window.addEventListener("pointerup", t), () => {
457
- window.removeEventListener("pointermove", e), window.removeEventListener("pointerup", t);
458
- };
459
- }, [r]), /* @__PURE__ */ c("div", {
460
- ref: s,
461
- className: e,
462
- style: t,
463
- onPointerDown: (e) => {
464
- l.current = !0, u(e.clientX, "live");
465
- },
466
- children: /* @__PURE__ */ c("div", {
467
- className: a.sliderHandle,
468
- style: {
469
- left: `${f(n, 0, 1) * 100}%`,
470
- background: i
471
- }
472
- })
473
- });
474
- }
475
- function M() {
476
- return /* @__PURE__ */ l("svg", {
477
- width: "16",
478
- height: "16",
479
- viewBox: "0 0 24 24",
480
- fill: "none",
481
- stroke: "currentColor",
482
- strokeWidth: "2",
483
- strokeLinecap: "round",
484
- strokeLinejoin: "round",
485
- "aria-hidden": !0,
486
- children: [
487
- /* @__PURE__ */ c("path", { d: "m2 22 1-1h3l9-9" }),
488
- /* @__PURE__ */ c("path", { d: "M3 21v-3l9-9" }),
489
- /* @__PURE__ */ c("path", { d: "m15 5 3 3" }),
490
- /* @__PURE__ */ c("path", { d: "M17.5 2.5a2.12 2.12 0 0 1 3 3L12 14l-4 1 1-4Z" })
491
- ]
492
- });
493
- }
494
- //#endregion
495
- export { d as DEFAULT_COLOR_PALETTE, E as default };
1
+ import e from"../styles/Field.module.js";import t from"../core/Field.js";import{useFormFieldState as n}from"../hooks/use.js";import{acquireOverlayZIndex as r,releaseOverlayZIndex as i}from"../overlayZ.js";import a from"../styles/Color.module.js";import o from"react";import{Fragment as s,jsx as c,jsxs as l}from"react/jsx-runtime";import{createPortal as u}from"react-dom";var d=`#FF2D55.#FF3B30.#FF453A.#FF6961.#FF8A80.#C11F3E.#AF52DE.#BF5AF2.#DA8FFF.#5856D6.#5E5CE6.#7D7AFF.#007AFF.#0A84FF.#409CFF.#64D2FF.#70D7FF.#5AC8FA.#32ADE6.#64D2FF.#40C8E0.#00C7BE.#63E6E2.#66D4CF.#34C759.#30D158.#30DB5B.#4CD964.#6DE6A0.#8EEDB3.#A8E10C.#B5F200.#D0FF00.#E4FF66.#F2FF99.#C6FF00.#FFCC00.#FFD60A.#FFEA00.#FFE066.#FFF3A3.#FFEB3B.#FF9500.#FF9F0A.#FFB340.#FFB340.#FFD580.#FF8C00.#A2845E.#AC8E68.#C4A484.#8E8E93.#636366.#48484A.#3A3A3C.#2C2C2E.#1C1C1E`.split(`.`);function f(e,t,n){return Math.min(n,Math.max(t,e))}function p(e){return Math.round(f(e,0,255)).toString(16).padStart(2,`0`).toUpperCase()}function m(e,t,n){let r=(e%360+360)%360,i=n*t,a=i*(1-Math.abs(r/60%2-1)),o=n-i,s=0,c=0,l=0;return r<60?[s,c,l]=[i,a,0]:r<120?[s,c,l]=[a,i,0]:r<180?[s,c,l]=[0,i,a]:r<240?[s,c,l]=[0,a,i]:r<300?[s,c,l]=[a,0,i]:[s,c,l]=[i,0,a],{r:(s+o)*255,g:(c+o)*255,b:(l+o)*255}}function h(e,t,n){let r=e/255,i=t/255,a=n/255,o=Math.max(r,i,a),s=o-Math.min(r,i,a),c=0;s!==0&&(c=o===r?(i-a)/s%6:o===i?(a-r)/s+2:(r-i)/s+4,c*=60,c<0&&(c+=360));let l=o===0?0:s/o;return{h:c,s:l,v:o}}function g(e){if(typeof e!=`string`)return null;let t=e.trim();if(!t||(t[0]===`#`&&(t=t.slice(1)),![3,4,6,8].includes(t.length))||!/^[0-9a-fA-F]+$/.test(t))return null;let n=0,r=0,i=0,a=1;return t.length===3||t.length===4?(n=parseInt(t[0]+t[0],16),r=parseInt(t[1]+t[1],16),i=parseInt(t[2]+t[2],16),t.length===4&&(a=parseInt(t[3]+t[3],16)/255)):(n=parseInt(t.slice(0,2),16),r=parseInt(t.slice(2,4),16),i=parseInt(t.slice(4,6),16),t.length===8&&(a=parseInt(t.slice(6,8),16)/255)),{...h(n,r,i),a}}function _(e,t){let{r:n,g:r,b:i}=m(e.h,e.s,e.v),a=`#${p(n)}${p(r)}${p(i)}`;return!t||e.a>=.999?a:`${a}${p(e.a*255)}`}function v(e){let{r:t,g:n,b:r}=m(e.h,e.s,e.v),i=Math.round(t),a=Math.round(n),o=Math.round(r);return e.a>=.999?`rgb(${i}, ${a}, ${o})`:`rgba(${i}, ${a}, ${o}, ${Number(e.a.toFixed(3))})`}function y(e){let{r:t,g:n,b:r}=m(e,1,1);return`rgb(${Math.round(t)},${Math.round(n)},${Math.round(r)})`}function b(e){let t=g(e);return t?_(t,t.a<.999):e.toUpperCase()}function x(e){return e==null?!0:typeof e==`string`?e.trim()===``:!1}function S(e){if(!(e instanceof HTMLElement))return!1;let t=e.tagName;return t===`INPUT`||t===`TEXTAREA`||t===`SELECT`?!0:!!e.isContentEditable}function C(e){let t=(e||``).trim();if(!t)return null;let n=g(t)??g(t[0]===`#`?t:`#${t.replace(/^#/,``)}`);if(n)return n;let r=/^rgba?\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})(?:\s*,\s*([\d.]+))?\s*\)$/i.exec(t);if(r){let e=f(Number(r[1]),0,255),t=f(Number(r[2]),0,255),n=f(Number(r[3]),0,255),i=r[4]===void 0?1:f(Number(r[4]),0,1);return{...h(e,t,n),a:i}}return null}function w(){return typeof window<`u`&&`EyeDropper`in window}var T={h:0,s:1,v:.76,a:1};function E({value:r=``,form:i,onValue:f,onBlur:p,name:m=``,placeholder:h=`#000000`,label:v=``,error:y=``,hidden:b=!1,disabled:w=!1,className:E=``,after:O=``,before:k=``,colors:A,alpha:j=!0}){let{value:M}=n(i,m,r),N=typeof M==`string`?M:M==null?``:String(M),[P,F]=o.useState(!1),[I,L]=o.useState(null),[R,z]=o.useState(()=>g(N)??{...T}),[B,V]=o.useState(()=>_(g(N)??T,j)),H=A?.length?A:d,U=g(N),W=U??(x(N)?g(h):null),G=U?_(U,j):``,K=W?_(W,j):`transparent`;o.useEffect(()=>{let e=g(N);e&&(z(e),V(_(e,j)))},[N,j]);let q=o.useCallback((e,t)=>{let n=_(e,j),r={value:n,name:m||``};i&&m&&(t===`live`?i.onValue(r):i.setValue(r)),f&&f!==i?.onValue&&f(r),V(n)},[j,i,m,f]),J=e=>{if(w)return;let t=g(N)??{...R};z(t),V(_(t,j)),L(e.getBoundingClientRect()),F(!0)},Y=()=>{F(!1),p?.({value:_(R,j),name:m||``})};o.useEffect(()=>{if(!P||w)return;let e=``,t=t=>{let n=C(t);if(!n)return!1;let r={...n,a:j?n.a:1},i=_(r,j);return i===e?!0:(e=i,z(r),q(r,`commit`),!0)},n=e=>{S(e.target)||t(e.clipboardData?.getData(`text/plain`)??``)&&(e.preventDefault(),e.stopPropagation())},r=e=>{(e.ctrlKey||e.metaKey)&&(e.key!==`v`&&e.key!==`V`||S(e.target)||navigator.clipboard?.readText&&(e.preventDefault(),navigator.clipboard.readText().then(e=>{t(e)})))};return document.addEventListener(`paste`,n,!0),window.addEventListener(`keydown`,r,!0),()=>{document.removeEventListener(`paste`,n,!0),window.removeEventListener(`keydown`,r,!0)}},[P,w,j,q]);let X=e=>{if(!w){if(!P&&(e.key===`Enter`||e.key===` `||e.key===`ArrowDown`)){e.preventDefault(),J(e.currentTarget);return}P&&e.key===`Escape`&&(e.preventDefault(),Y())}};return b&&x(N)?null:l(s,{children:[l(t,{type:`color`,label:v,error:y,element_blok:{className:[E||``]},element_input:{className:[e.w],props:{"data-disabled":w?`true`:void 0}},children:[k?c(`span`,{className:e.affix,children:k}):null,l(`div`,{className:a.trigger,role:`combobox`,tabIndex:w?-1:0,"aria-expanded":P,"aria-haspopup":`dialog`,"aria-disabled":w,"aria-label":v||`Цвет`,onClick:e=>J(e.currentTarget),onKeyDown:X,children:[c(`span`,{className:a.preview,style:{"--preview-color":K}}),c(`span`,{className:[a.hexText,G?``:a.placeholder].filter(Boolean).join(` `),children:G||h})]}),O?c(`span`,{className:e.affix,children:O}):null]}),P&&u(c(D,{parent:I||void 0,hsva:R,setHsva:z,hexDraft:B,setHexDraft:V,alpha:j,palette:H,onLive:e=>q(e,`live`),onCommit:e=>q(e,`commit`),onClose:Y}),document.body)]})}E.displayName=`ColorField`;function D({parent:e,hsva:t,setHsva:n,hexDraft:u,setHexDraft:d,alpha:p,palette:m,onLive:h,onCommit:y,onClose:x}){let[S]=o.useState(()=>r());if(o.useEffect(()=>()=>i(S),[S]),!e)return null;let C=p?340:300,T=window.innerHeight-e.bottom<C+8?Math.max(8,e.top-C-8):Math.min(window.innerHeight-C-8,e.bottom+8),E=Math.min(Math.max(8,e.left),window.innerWidth-260-8),D=v({...t,a:1}),j=b(_(t,p)),N=(e,t)=>{n(e),t===`live`?h(e):y(e)},P=()=>{let e=g(u);if(!e){d(_(t,p));return}N(e,`commit`)},F=e=>{let n=e.replace(/[^\d]/g,``),r=f(Number(n||0),0,100);N({...t,a:r/100},`commit`)},I=async()=>{if(w())try{let e=window.EyeDropper,n=g((await new e().open()).sRGBHex);n&&N({...n,a:t.a},`commit`)}catch{}};return l(s,{children:[c(`div`,{className:a.o,style:{zIndex:S},onClick:x,"aria-hidden":!0}),l(`div`,{className:a.panel,role:`dialog`,"aria-label":`Выбор цвета`,style:{top:T,left:E,width:260,zIndex:S+1},children:[c(O,{hsva:t,onChange:(e,n,r)=>N({...t,s:e,v:n},r)}),l(`div`,{className:a.slidersRow,children:[w()?c(`button`,{type:`button`,className:a.eyedropper,"aria-label":`Пипетка`,onClick:I,children:c(M,{})}):null,l(`div`,{className:a.sliders,children:[c(k,{h:t.h,onChange:(e,n)=>N({...t,h:e},n)}),p?c(A,{a:t.a,color:D,onChange:(e,n)=>N({...t,a:e},n)}):null]})]}),l(`div`,{className:a.inputs,children:[c(`span`,{className:a.format,children:`Hex`}),c(`input`,{className:a.input,value:u.replace(/^#/,``),spellCheck:!1,"aria-label":`Hex`,onChange:e=>{let t=e.target.value.replace(/[^0-9a-fA-F]/g,``);d(t?`#${t}`:``)},onBlur:P,onKeyDown:e=>{e.key===`Enter`&&(e.preventDefault(),e.target.blur())}}),p?c(`input`,{className:`${a.input} ${a.inputAlpha}`,value:`${Math.round(t.a*100)} %`,"aria-label":`Прозрачность`,onChange:e=>F(e.target.value),onFocus:e=>e.target.select()}):null]}),c(`div`,{className:a.palette,role:`listbox`,"aria-label":`Палитра`,children:m.map(e=>{let t=b(e)===j;return c(`button`,{type:`button`,role:`option`,"aria-checked":t,"aria-label":e,className:a.swatchBtn,style:{"--swatch":e},onClick:()=>{let t=g(e);t&&N({...t,a:p?t.a:1},`commit`)}},e)})})]})]})}function O({hsva:e,onChange:t}){let n=o.useRef(null),r=o.useRef(!1),i=o.useRef(t);i.current=t;let s=(t,r)=>{let i=n.current;if(!i)return{s:e.s,v:e.v};let a=i.getBoundingClientRect();return{s:f((t-a.left)/a.width,0,1),v:f(1-(r-a.top)/a.height,0,1)}};return o.useEffect(()=>{let e=e=>{if(!r.current)return;let{s:t,v:n}=s(e.clientX,e.clientY);i.current(t,n,`live`)},t=e=>{if(!r.current)return;r.current=!1;let{s:t,v:n}=s(e.clientX,e.clientY);i.current(t,n,`commit`)};return window.addEventListener(`pointermove`,e),window.addEventListener(`pointerup`,t),()=>{window.removeEventListener(`pointermove`,e),window.removeEventListener(`pointerup`,t)}},[]),l(`div`,{ref:n,className:a.sv,style:{background:y(e.h)},onPointerDown:e=>{r.current=!0;let{s:t,v:n}=s(e.clientX,e.clientY);i.current(t,n,`live`)},children:[c(`div`,{className:a.svWhite}),c(`div`,{className:a.svBlack}),c(`div`,{className:a.handle,style:{left:`${e.s*100}%`,top:`${(1-e.v)*100}%`,background:v({...e,a:1})}})]})}function k({h:e,onChange:t}){return c(j,{className:`${a.slider} ${a.sliderHue}`,ratio:e/360,onRatio:(e,n)=>t(e*360,n),handleColor:y(e)})}function A({a:e,color:t,onChange:n}){return c(j,{className:`${a.slider} ${a.sliderAlpha}`,style:{"--alpha-color":t},ratio:e,onRatio:n,handleColor:t})}function j({className:e,style:t,ratio:n,onRatio:r,handleColor:i}){let s=o.useRef(null),l=o.useRef(!1),u=(e,t)=>{let n=s.current;if(!n)return;let i=n.getBoundingClientRect();r(f((e-i.left)/i.width,0,1),t)};return o.useEffect(()=>{let e=e=>{l.current&&u(e.clientX,`live`)},t=e=>{l.current&&(l.current=!1,u(e.clientX,`commit`))};return window.addEventListener(`pointermove`,e),window.addEventListener(`pointerup`,t),()=>{window.removeEventListener(`pointermove`,e),window.removeEventListener(`pointerup`,t)}},[r]),c(`div`,{ref:s,className:e,style:t,onPointerDown:e=>{l.current=!0,u(e.clientX,`live`)},children:c(`div`,{className:a.sliderHandle,style:{left:`${f(n,0,1)*100}%`,background:i}})})}function M(){return l(`svg`,{width:`16`,height:`16`,viewBox:`0 0 24 24`,fill:`none`,stroke:`currentColor`,strokeWidth:`2`,strokeLinecap:`round`,strokeLinejoin:`round`,"aria-hidden":!0,children:[c(`path`,{d:`m2 22 1-1h3l9-9`}),c(`path`,{d:`M3 21v-3l9-9`}),c(`path`,{d:`m15 5 3 3`}),c(`path`,{d:`M17.5 2.5a2.12 2.12 0 0 1 3 3L12 14l-4 1 1-4Z`})]})}export{d as DEFAULT_COLOR_PALETTE,E as default};