@zag-js/color-picker 1.34.1 → 1.35.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 (49) hide show
  1. package/dist/color-picker.anatomy.d.mts +6 -0
  2. package/dist/color-picker.anatomy.d.ts +6 -0
  3. package/dist/color-picker.anatomy.js +59 -0
  4. package/dist/color-picker.anatomy.mjs +33 -0
  5. package/dist/color-picker.connect.d.mts +10 -0
  6. package/dist/color-picker.connect.d.ts +10 -0
  7. package/dist/color-picker.connect.js +681 -0
  8. package/dist/color-picker.connect.mjs +646 -0
  9. package/dist/color-picker.dom.d.mts +39 -0
  10. package/dist/color-picker.dom.d.ts +39 -0
  11. package/dist/color-picker.dom.js +136 -0
  12. package/dist/color-picker.dom.mjs +85 -0
  13. package/dist/color-picker.machine.d.mts +10 -0
  14. package/dist/color-picker.machine.d.ts +10 -0
  15. package/dist/color-picker.machine.js +636 -0
  16. package/dist/color-picker.machine.mjs +609 -0
  17. package/dist/color-picker.parse.d.mts +5 -0
  18. package/dist/color-picker.parse.d.ts +5 -0
  19. package/dist/color-picker.parse.js +33 -0
  20. package/dist/color-picker.parse.mjs +8 -0
  21. package/dist/color-picker.props.d.mts +21 -0
  22. package/dist/color-picker.props.d.ts +21 -0
  23. package/dist/color-picker.props.js +94 -0
  24. package/dist/color-picker.props.mjs +58 -0
  25. package/dist/color-picker.types.d.mts +303 -0
  26. package/dist/color-picker.types.d.ts +303 -0
  27. package/dist/color-picker.types.js +18 -0
  28. package/dist/color-picker.types.mjs +0 -0
  29. package/dist/index.d.mts +9 -324
  30. package/dist/index.d.ts +9 -324
  31. package/dist/index.js +37 -1517
  32. package/dist/index.mjs +11 -1504
  33. package/dist/utils/get-channel-display-color.d.mts +5 -0
  34. package/dist/utils/get-channel-display-color.d.ts +5 -0
  35. package/dist/utils/get-channel-display-color.js +48 -0
  36. package/dist/utils/get-channel-display-color.mjs +23 -0
  37. package/dist/utils/get-channel-input-value.d.mts +11 -0
  38. package/dist/utils/get-channel-input-value.d.ts +11 -0
  39. package/dist/utils/get-channel-input-value.js +88 -0
  40. package/dist/utils/get-channel-input-value.mjs +62 -0
  41. package/dist/utils/get-slider-background.d.mts +14 -0
  42. package/dist/utils/get-slider-background.d.ts +14 -0
  43. package/dist/utils/get-slider-background.js +65 -0
  44. package/dist/utils/get-slider-background.mjs +40 -0
  45. package/dist/utils/is-valid-hex.d.mts +4 -0
  46. package/dist/utils/is-valid-hex.d.ts +4 -0
  47. package/dist/utils/is-valid-hex.js +40 -0
  48. package/dist/utils/is-valid-hex.mjs +14 -0
  49. package/package.json +20 -10
package/dist/index.js CHANGED
@@ -1,1522 +1,42 @@
1
- 'use strict';
2
-
3
- var anatomy$1 = require('@zag-js/anatomy');
4
- var colorUtils = require('@zag-js/color-utils');
5
- var domQuery = require('@zag-js/dom-query');
6
- var popper = require('@zag-js/popper');
7
- var core = require('@zag-js/core');
8
- var dismissable = require('@zag-js/dismissable');
9
- var utils = require('@zag-js/utils');
10
- var types = require('@zag-js/types');
11
-
12
- // src/color-picker.anatomy.ts
13
- var anatomy = anatomy$1.createAnatomy("color-picker", [
14
- "root",
15
- "label",
16
- "control",
17
- "trigger",
18
- "positioner",
19
- "content",
20
- "area",
21
- "areaThumb",
22
- "valueText",
23
- "areaBackground",
24
- "channelSlider",
25
- "channelSliderLabel",
26
- "channelSliderTrack",
27
- "channelSliderThumb",
28
- "channelSliderValueText",
29
- "channelInput",
30
- "transparencyGrid",
31
- "swatchGroup",
32
- "swatchTrigger",
33
- "swatchIndicator",
34
- "swatch",
35
- "eyeDropperTrigger",
36
- "formatTrigger",
37
- "formatSelect"
38
- ]);
39
- var parts = anatomy.build();
40
- var getRootId = (ctx) => ctx.ids?.root ?? `color-picker:${ctx.id}`;
41
- var getLabelId = (ctx) => ctx.ids?.label ?? `color-picker:${ctx.id}:label`;
42
- var getHiddenInputId = (ctx) => ctx.ids?.hiddenInput ?? `color-picker:${ctx.id}:hidden-input`;
43
- var getControlId = (ctx) => ctx.ids?.control ?? `color-picker:${ctx.id}:control`;
44
- var getTriggerId = (ctx) => ctx.ids?.trigger ?? `color-picker:${ctx.id}:trigger`;
45
- var getContentId = (ctx) => ctx.ids?.content ?? `color-picker:${ctx.id}:content`;
46
- var getPositionerId = (ctx) => ctx.ids?.positioner ?? `color-picker:${ctx.id}:positioner`;
47
- var getFormatSelectId = (ctx) => ctx.ids?.formatSelect ?? `color-picker:${ctx.id}:format-select`;
48
- var getAreaId = (ctx) => ctx.ids?.area ?? `color-picker:${ctx.id}:area`;
49
- var getAreaGradientId = (ctx) => ctx.ids?.areaGradient ?? `color-picker:${ctx.id}:area-gradient`;
50
- var getAreaThumbId = (ctx) => ctx.ids?.areaThumb ?? `color-picker:${ctx.id}:area-thumb`;
51
- var getChannelSliderTrackId = (ctx, channel) => ctx.ids?.channelSliderTrack?.(channel) ?? `color-picker:${ctx.id}:slider-track:${channel}`;
52
- var getChannelSliderThumbId = (ctx, channel) => ctx.ids?.channelSliderThumb?.(channel) ?? `color-picker:${ctx.id}:slider-thumb:${channel}`;
53
- var getContentEl = (ctx) => ctx.getById(getContentId(ctx));
54
- var getAreaThumbEl = (ctx) => ctx.getById(getAreaThumbId(ctx));
55
- var getChannelSliderThumbEl = (ctx, channel) => ctx.getById(getChannelSliderThumbId(ctx, channel));
56
- var getFormatSelectEl = (ctx) => ctx.getById(getFormatSelectId(ctx));
57
- var getHiddenInputEl = (ctx) => ctx.getById(getHiddenInputId(ctx));
58
- var getAreaEl = (ctx) => ctx.getById(getAreaId(ctx));
59
- var getAreaValueFromPoint = (ctx, point, dir) => {
60
- const areaEl = getAreaEl(ctx);
61
- if (!areaEl) return;
62
- const { getPercentValue } = domQuery.getRelativePoint(point, areaEl);
63
- return {
64
- x: getPercentValue({ dir, orientation: "horizontal" }),
65
- y: getPercentValue({ orientation: "vertical" })
66
- };
67
- };
68
- var getControlEl = (ctx) => ctx.getById(getControlId(ctx));
69
- var getTriggerEl = (ctx) => ctx.getById(getTriggerId(ctx));
70
- var getPositionerEl = (ctx) => ctx.getById(getPositionerId(ctx));
71
- var getChannelSliderTrackEl = (ctx, channel) => ctx.getById(getChannelSliderTrackId(ctx, channel));
72
- var getChannelSliderValueFromPoint = (ctx, point, channel, dir) => {
73
- const trackEl = getChannelSliderTrackEl(ctx, channel);
74
- if (!trackEl) return;
75
- const { getPercentValue } = domQuery.getRelativePoint(point, trackEl);
76
- return {
77
- x: getPercentValue({ dir, orientation: "horizontal" }),
78
- y: getPercentValue({ orientation: "vertical" })
79
- };
80
- };
81
- var getChannelInputEls = (ctx) => {
82
- return [
83
- ...domQuery.queryAll(getContentEl(ctx), "input[data-channel]"),
84
- ...domQuery.queryAll(getControlEl(ctx), "input[data-channel]")
85
- ];
86
- };
87
- function getChannelDisplayColor(color, channel) {
88
- switch (channel) {
89
- case "hue":
90
- return colorUtils.parseColor(`hsl(${color.getChannelValue("hue")}, 100%, 50%)`);
91
- case "lightness":
92
- case "brightness":
93
- case "saturation":
94
- case "red":
95
- case "green":
96
- case "blue":
97
- return color.withChannelValue("alpha", 1);
98
- case "alpha": {
99
- return color;
100
- }
101
- default:
102
- throw new Error("Unknown color channel: " + channel);
103
- }
104
- }
105
- function getChannelValue(color, channel) {
106
- if (channel == null) return "";
107
- if (channel === "hex") {
108
- return color.toString("hex");
109
- }
110
- if (channel === "css") {
111
- return color.toString("css");
112
- }
113
- if (channel in color) {
114
- return color.getChannelValue(channel).toString();
115
- }
116
- const isHSL = color.getFormat() === "hsla";
117
- switch (channel) {
118
- case "hue":
119
- return isHSL ? color.toFormat("hsla").getChannelValue("hue").toString() : color.toFormat("hsba").getChannelValue("hue").toString();
120
- case "saturation":
121
- return isHSL ? color.toFormat("hsla").getChannelValue("saturation").toString() : color.toFormat("hsba").getChannelValue("saturation").toString();
122
- case "lightness":
123
- return color.toFormat("hsla").getChannelValue("lightness").toString();
124
- case "brightness":
125
- return color.toFormat("hsba").getChannelValue("brightness").toString();
126
- case "red":
127
- case "green":
128
- case "blue":
129
- return color.toFormat("rgba").getChannelValue(channel).toString();
130
- default:
131
- return color.getChannelValue(channel).toString();
132
- }
133
- }
134
- function getChannelRange(color, channel) {
135
- switch (channel) {
136
- case "hex":
137
- const minColor = colorUtils.parseColor("#000000");
138
- const maxColor = colorUtils.parseColor("#FFFFFF");
139
- return {
140
- minValue: minColor.toHexInt(),
141
- maxValue: maxColor.toHexInt(),
142
- pageSize: 10,
143
- step: 1
144
- };
145
- case "css":
146
- return void 0;
147
- case "hue":
148
- case "saturation":
149
- case "lightness":
150
- return color.toFormat("hsla").getChannelRange(channel);
151
- case "brightness":
152
- return color.toFormat("hsba").getChannelRange(channel);
153
- case "red":
154
- case "green":
155
- case "blue":
156
- return color.toFormat("rgba").getChannelRange(channel);
157
- default:
158
- return color.getChannelRange(channel);
159
- }
160
- }
161
-
162
- // src/utils/get-slider-background.ts
163
- function getSliderBackgroundDirection(orientation, dir) {
164
- if (orientation === "vertical") {
165
- return "top";
166
- } else if (dir === "ltr") {
167
- return "right";
168
- } else {
169
- return "left";
170
- }
171
- }
172
- var getSliderBackground = (props2) => {
173
- const { channel, value, dir, orientation } = props2;
174
- const bgDirection = getSliderBackgroundDirection(orientation, dir);
175
- const { minValue, maxValue } = value.getChannelRange(channel);
176
- switch (channel) {
177
- case "hue":
178
- return `linear-gradient(to ${bgDirection}, rgb(255, 0, 0) 0%, rgb(255, 255, 0) 17%, rgb(0, 255, 0) 33%, rgb(0, 255, 255) 50%, rgb(0, 0, 255) 67%, rgb(255, 0, 255) 83%, rgb(255, 0, 0) 100%)`;
179
- case "lightness": {
180
- let start = value.withChannelValue(channel, minValue).toString("css");
181
- let middle = value.withChannelValue(channel, (maxValue - minValue) / 2).toString("css");
182
- let end = value.withChannelValue(channel, maxValue).toString("css");
183
- return `linear-gradient(to ${bgDirection}, ${start}, ${middle}, ${end})`;
184
- }
185
- case "saturation":
186
- case "brightness":
187
- case "red":
188
- case "green":
189
- case "blue":
190
- case "alpha": {
191
- let start = value.withChannelValue(channel, minValue).toString("css");
192
- let end = value.withChannelValue(channel, maxValue).toString("css");
193
- return `linear-gradient(to ${bgDirection}, ${start}, ${end})`;
194
- }
195
- default:
196
- throw new Error("Unknown color channel: " + channel);
197
- }
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
198
9
  };
199
-
200
- // src/color-picker.connect.ts
201
- function connect(service, normalize) {
202
- const { context, send, prop, computed, state, scope } = service;
203
- const value = context.get("value");
204
- const format = context.get("format");
205
- const areaValue = computed("areaValue");
206
- const valueAsString = computed("valueAsString");
207
- const disabled = computed("disabled");
208
- const readOnly = !!prop("readOnly");
209
- const invalid = !!prop("invalid");
210
- const required = !!prop("required");
211
- const interactive = computed("interactive");
212
- const dragging = state.hasTag("dragging");
213
- const open = state.hasTag("open");
214
- const focused = state.hasTag("focused");
215
- const getAreaChannels = (props2) => {
216
- const channels = areaValue.getChannels();
217
- return {
218
- xChannel: props2.xChannel ?? channels[1],
219
- yChannel: props2.yChannel ?? channels[2]
220
- };
221
- };
222
- const currentPlacement = context.get("currentPlacement");
223
- const popperStyles = popper.getPlacementStyles({
224
- ...prop("positioning"),
225
- placement: currentPlacement
226
- });
227
- function getSwatchTriggerState(props2) {
228
- const color = colorUtils.normalizeColor(props2.value).toFormat(context.get("format"));
229
- return {
230
- value: color,
231
- valueAsString: color.toString("hex"),
232
- checked: color.isEqual(value),
233
- disabled: props2.disabled || !interactive
234
- };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
235
15
  }
236
- return {
237
- dragging,
238
- open,
239
- valueAsString,
240
- value,
241
- inline: !!prop("inline"),
242
- setOpen(nextOpen) {
243
- if (prop("inline")) return;
244
- const open2 = state.hasTag("open");
245
- if (open2 === nextOpen) return;
246
- send({ type: nextOpen ? "OPEN" : "CLOSE" });
247
- },
248
- setValue(value2) {
249
- send({ type: "VALUE.SET", value: colorUtils.normalizeColor(value2), src: "set-color" });
250
- },
251
- getChannelValue(channel) {
252
- return getChannelValue(value, channel);
253
- },
254
- getChannelValueText(channel, locale) {
255
- return value.formatChannelValue(channel, locale);
256
- },
257
- setChannelValue(channel, channelValue) {
258
- const color = value.withChannelValue(channel, channelValue);
259
- send({ type: "VALUE.SET", value: color, src: "set-channel" });
260
- },
261
- format: context.get("format"),
262
- setFormat(format2) {
263
- const formatValue = value.toFormat(format2);
264
- send({ type: "VALUE.SET", value: formatValue, src: "set-format" });
265
- },
266
- alpha: value.getChannelValue("alpha"),
267
- setAlpha(alphaValue) {
268
- const color = value.withChannelValue("alpha", alphaValue);
269
- send({ type: "VALUE.SET", value: color, src: "set-alpha" });
270
- },
271
- getRootProps() {
272
- return normalize.element({
273
- ...parts.root.attrs,
274
- dir: prop("dir"),
275
- id: getRootId(scope),
276
- "data-disabled": domQuery.dataAttr(disabled),
277
- "data-readonly": domQuery.dataAttr(readOnly),
278
- "data-invalid": domQuery.dataAttr(invalid),
279
- style: {
280
- "--value": value.toString("css")
281
- }
282
- });
283
- },
284
- getLabelProps() {
285
- return normalize.element({
286
- ...parts.label.attrs,
287
- dir: prop("dir"),
288
- id: getLabelId(scope),
289
- htmlFor: getHiddenInputId(scope),
290
- "data-disabled": domQuery.dataAttr(disabled),
291
- "data-readonly": domQuery.dataAttr(readOnly),
292
- "data-invalid": domQuery.dataAttr(invalid),
293
- "data-required": domQuery.dataAttr(required),
294
- "data-focus": domQuery.dataAttr(focused),
295
- onClick(event) {
296
- event.preventDefault();
297
- const inputEl = domQuery.query(getControlEl(scope), "[data-channel=hex]");
298
- inputEl?.focus({ preventScroll: true });
299
- }
300
- });
301
- },
302
- getControlProps() {
303
- return normalize.element({
304
- ...parts.control.attrs,
305
- id: getControlId(scope),
306
- dir: prop("dir"),
307
- "data-disabled": domQuery.dataAttr(disabled),
308
- "data-readonly": domQuery.dataAttr(readOnly),
309
- "data-invalid": domQuery.dataAttr(invalid),
310
- "data-state": open ? "open" : "closed",
311
- "data-focus": domQuery.dataAttr(focused)
312
- });
313
- },
314
- getTriggerProps() {
315
- return normalize.button({
316
- ...parts.trigger.attrs,
317
- id: getTriggerId(scope),
318
- dir: prop("dir"),
319
- disabled,
320
- "aria-label": `select color. current color is ${valueAsString}`,
321
- "aria-controls": getContentId(scope),
322
- "aria-labelledby": getLabelId(scope),
323
- "aria-haspopup": prop("inline") ? void 0 : "dialog",
324
- "data-disabled": domQuery.dataAttr(disabled),
325
- "data-readonly": domQuery.dataAttr(readOnly),
326
- "data-invalid": domQuery.dataAttr(invalid),
327
- "data-placement": currentPlacement,
328
- "aria-expanded": open,
329
- "data-state": open ? "open" : "closed",
330
- "data-focus": domQuery.dataAttr(focused),
331
- type: "button",
332
- onClick() {
333
- if (!interactive) return;
334
- send({ type: "TRIGGER.CLICK" });
335
- },
336
- onBlur() {
337
- if (!interactive) return;
338
- send({ type: "TRIGGER.BLUR" });
339
- },
340
- style: {
341
- position: "relative"
342
- }
343
- });
344
- },
345
- getPositionerProps() {
346
- return normalize.element({
347
- ...parts.positioner.attrs,
348
- id: getPositionerId(scope),
349
- dir: prop("dir"),
350
- style: popperStyles.floating
351
- });
352
- },
353
- getContentProps() {
354
- return normalize.element({
355
- ...parts.content.attrs,
356
- id: getContentId(scope),
357
- dir: prop("dir"),
358
- role: prop("inline") ? void 0 : "dialog",
359
- tabIndex: -1,
360
- "data-placement": currentPlacement,
361
- "data-state": open ? "open" : "closed",
362
- hidden: !open
363
- });
364
- },
365
- getValueTextProps() {
366
- return normalize.element({
367
- ...parts.valueText.attrs,
368
- dir: prop("dir"),
369
- "data-disabled": domQuery.dataAttr(disabled),
370
- "data-focus": domQuery.dataAttr(focused)
371
- });
372
- },
373
- getAreaProps(props2 = {}) {
374
- const { xChannel, yChannel } = getAreaChannels(props2);
375
- const { areaStyles } = colorUtils.getColorAreaGradient(areaValue, {
376
- xChannel,
377
- yChannel,
378
- dir: prop("dir")
379
- });
380
- return normalize.element({
381
- ...parts.area.attrs,
382
- id: getAreaId(scope),
383
- role: "group",
384
- "data-invalid": domQuery.dataAttr(invalid),
385
- "data-disabled": domQuery.dataAttr(disabled),
386
- "data-readonly": domQuery.dataAttr(readOnly),
387
- onPointerDown(event) {
388
- if (!interactive) return;
389
- if (!domQuery.isLeftClick(event)) return;
390
- if (domQuery.isModifierKey(event)) return;
391
- const point = domQuery.getEventPoint(event);
392
- const channel = { xChannel, yChannel };
393
- send({ type: "AREA.POINTER_DOWN", point, channel, id: "area" });
394
- event.preventDefault();
395
- },
396
- style: {
397
- position: "relative",
398
- touchAction: "none",
399
- forcedColorAdjust: "none",
400
- ...areaStyles
401
- }
402
- });
403
- },
404
- getAreaBackgroundProps(props2 = {}) {
405
- const { xChannel, yChannel } = getAreaChannels(props2);
406
- const { areaGradientStyles } = colorUtils.getColorAreaGradient(areaValue, {
407
- xChannel,
408
- yChannel,
409
- dir: prop("dir")
410
- });
411
- return normalize.element({
412
- ...parts.areaBackground.attrs,
413
- id: getAreaGradientId(scope),
414
- "data-invalid": domQuery.dataAttr(invalid),
415
- "data-disabled": domQuery.dataAttr(disabled),
416
- "data-readonly": domQuery.dataAttr(readOnly),
417
- style: {
418
- position: "relative",
419
- touchAction: "none",
420
- forcedColorAdjust: "none",
421
- ...areaGradientStyles
422
- }
423
- });
424
- },
425
- getAreaThumbProps(props2 = {}) {
426
- const { xChannel, yChannel } = getAreaChannels(props2);
427
- const channel = { xChannel, yChannel };
428
- const xPercent = areaValue.getChannelValuePercent(xChannel);
429
- const yPercent = 1 - areaValue.getChannelValuePercent(yChannel);
430
- const isRtl = prop("dir") === "rtl";
431
- const finalXPercent = isRtl ? 1 - xPercent : xPercent;
432
- const xValue = areaValue.getChannelValue(xChannel);
433
- const yValue = areaValue.getChannelValue(yChannel);
434
- const color = areaValue.withChannelValue("alpha", 1).toString("css");
435
- return normalize.element({
436
- ...parts.areaThumb.attrs,
437
- id: getAreaThumbId(scope),
438
- dir: prop("dir"),
439
- tabIndex: disabled ? void 0 : 0,
440
- "data-disabled": domQuery.dataAttr(disabled),
441
- "data-invalid": domQuery.dataAttr(invalid),
442
- "data-readonly": domQuery.dataAttr(readOnly),
443
- role: "slider",
444
- "aria-valuemin": 0,
445
- "aria-valuemax": 100,
446
- "aria-valuenow": xValue,
447
- "aria-label": `${xChannel} and ${yChannel}`,
448
- "aria-roledescription": "2d slider",
449
- "aria-valuetext": `${xChannel} ${xValue}, ${yChannel} ${yValue}`,
450
- style: {
451
- position: "absolute",
452
- left: `${finalXPercent * 100}%`,
453
- top: `${yPercent * 100}%`,
454
- transform: "translate(-50%, -50%)",
455
- touchAction: "none",
456
- forcedColorAdjust: "none",
457
- "--color": color,
458
- background: color
459
- },
460
- onFocus() {
461
- if (!interactive) return;
462
- send({ type: "AREA.FOCUS", id: "area", channel });
463
- },
464
- onKeyDown(event) {
465
- if (event.defaultPrevented) return;
466
- if (!interactive) return;
467
- const step = domQuery.getEventStep(event);
468
- const keyMap = {
469
- ArrowUp() {
470
- send({ type: "AREA.ARROW_UP", channel, step });
471
- },
472
- ArrowDown() {
473
- send({ type: "AREA.ARROW_DOWN", channel, step });
474
- },
475
- ArrowLeft() {
476
- send({ type: "AREA.ARROW_LEFT", channel, step });
477
- },
478
- ArrowRight() {
479
- send({ type: "AREA.ARROW_RIGHT", channel, step });
480
- },
481
- PageUp() {
482
- send({ type: "AREA.PAGE_UP", channel, step });
483
- },
484
- PageDown() {
485
- send({ type: "AREA.PAGE_DOWN", channel, step });
486
- },
487
- Escape(event2) {
488
- event2.stopPropagation();
489
- }
490
- };
491
- const exec = keyMap[domQuery.getEventKey(event, {
492
- dir: prop("dir")
493
- })];
494
- if (exec) {
495
- exec(event);
496
- event.preventDefault();
497
- }
498
- }
499
- });
500
- },
501
- getTransparencyGridProps(props2 = {}) {
502
- const { size = "12px" } = props2;
503
- return normalize.element({
504
- ...parts.transparencyGrid.attrs,
505
- style: {
506
- "--size": size,
507
- width: "100%",
508
- height: "100%",
509
- position: "absolute",
510
- backgroundColor: "#fff",
511
- backgroundImage: "conic-gradient(#eeeeee 0 25%, transparent 0 50%, #eeeeee 0 75%, transparent 0)",
512
- backgroundSize: "var(--size) var(--size)",
513
- inset: "0px",
514
- zIndex: "auto",
515
- pointerEvents: "none"
516
- }
517
- });
518
- },
519
- getChannelSliderProps(props2) {
520
- const { orientation = "horizontal", channel, format: format2 } = props2;
521
- return normalize.element({
522
- ...parts.channelSlider.attrs,
523
- "data-channel": channel,
524
- "data-orientation": orientation,
525
- role: "presentation",
526
- onPointerDown(event) {
527
- if (!interactive) return;
528
- if (!domQuery.isLeftClick(event)) return;
529
- if (domQuery.isModifierKey(event)) return;
530
- const point = domQuery.getEventPoint(event);
531
- send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, format: format2, point, id: channel, orientation });
532
- event.preventDefault();
533
- },
534
- style: {
535
- position: "relative",
536
- touchAction: "none"
537
- }
538
- });
539
- },
540
- getChannelSliderTrackProps(props2) {
541
- const { orientation = "horizontal", channel, format: format2 } = props2;
542
- const normalizedValue = format2 ? value.toFormat(format2) : areaValue;
543
- return normalize.element({
544
- ...parts.channelSliderTrack.attrs,
545
- id: getChannelSliderTrackId(scope, channel),
546
- role: "group",
547
- "data-channel": channel,
548
- "data-orientation": orientation,
549
- style: {
550
- position: "relative",
551
- forcedColorAdjust: "none",
552
- backgroundImage: getSliderBackground({
553
- orientation,
554
- channel,
555
- dir: prop("dir"),
556
- value: normalizedValue
557
- })
558
- }
559
- });
560
- },
561
- getChannelSliderLabelProps(props2) {
562
- const { channel } = props2;
563
- return normalize.element({
564
- ...parts.channelSliderLabel.attrs,
565
- "data-channel": channel,
566
- onClick(event) {
567
- if (!interactive) return;
568
- event.preventDefault();
569
- const thumbId = getChannelSliderThumbId(scope, channel);
570
- scope.getById(thumbId)?.focus({ preventScroll: true });
571
- },
572
- style: {
573
- userSelect: "none",
574
- WebkitUserSelect: "none"
575
- }
576
- });
577
- },
578
- getChannelSliderValueTextProps(props2) {
579
- return normalize.element({
580
- ...parts.channelSliderValueText.attrs,
581
- "data-channel": props2.channel
582
- });
583
- },
584
- getChannelSliderThumbProps(props2) {
585
- const { orientation = "horizontal", channel, format: format2 } = props2;
586
- const normalizedValue = format2 ? value.toFormat(format2) : areaValue;
587
- const channelRange = normalizedValue.getChannelRange(channel);
588
- const channelValue = normalizedValue.getChannelValue(channel);
589
- const offset = (channelValue - channelRange.minValue) / (channelRange.maxValue - channelRange.minValue);
590
- const isRtl = prop("dir") === "rtl";
591
- const finalOffset = orientation === "horizontal" && isRtl ? 1 - offset : offset;
592
- const placementStyles = orientation === "horizontal" ? { left: `${finalOffset * 100}%`, top: "50%" } : { top: `${offset * 100}%`, left: "50%" };
593
- return normalize.element({
594
- ...parts.channelSliderThumb.attrs,
595
- id: getChannelSliderThumbId(scope, channel),
596
- role: "slider",
597
- "aria-label": channel,
598
- tabIndex: disabled ? void 0 : 0,
599
- "data-channel": channel,
600
- "data-disabled": domQuery.dataAttr(disabled),
601
- "data-orientation": orientation,
602
- "aria-disabled": domQuery.dataAttr(disabled),
603
- "aria-orientation": orientation,
604
- "aria-valuemax": channelRange.maxValue,
605
- "aria-valuemin": channelRange.minValue,
606
- "aria-valuenow": channelValue,
607
- "aria-valuetext": `${channel} ${channelValue}`,
608
- style: {
609
- forcedColorAdjust: "none",
610
- position: "absolute",
611
- background: getChannelDisplayColor(areaValue, channel).toString("css"),
612
- ...placementStyles
613
- },
614
- onFocus() {
615
- if (!interactive) return;
616
- send({ type: "CHANNEL_SLIDER.FOCUS", channel });
617
- },
618
- onKeyDown(event) {
619
- if (event.defaultPrevented) return;
620
- if (!interactive) return;
621
- const step = domQuery.getEventStep(event) * channelRange.step;
622
- const keyMap = {
623
- ArrowUp() {
624
- send({ type: "CHANNEL_SLIDER.ARROW_UP", channel, step });
625
- },
626
- ArrowDown() {
627
- send({ type: "CHANNEL_SLIDER.ARROW_DOWN", channel, step });
628
- },
629
- ArrowLeft() {
630
- send({ type: "CHANNEL_SLIDER.ARROW_LEFT", channel, step });
631
- },
632
- ArrowRight() {
633
- send({ type: "CHANNEL_SLIDER.ARROW_RIGHT", channel, step });
634
- },
635
- PageUp() {
636
- send({ type: "CHANNEL_SLIDER.PAGE_UP", channel });
637
- },
638
- PageDown() {
639
- send({ type: "CHANNEL_SLIDER.PAGE_DOWN", channel });
640
- },
641
- Home() {
642
- send({ type: "CHANNEL_SLIDER.HOME", channel });
643
- },
644
- End() {
645
- send({ type: "CHANNEL_SLIDER.END", channel });
646
- },
647
- Escape(event2) {
648
- event2.stopPropagation();
649
- }
650
- };
651
- const exec = keyMap[domQuery.getEventKey(event, {
652
- dir: prop("dir")
653
- })];
654
- if (exec) {
655
- exec(event);
656
- event.preventDefault();
657
- }
658
- }
659
- });
660
- },
661
- getChannelInputProps(props2) {
662
- const { channel } = props2;
663
- const isTextField = channel === "hex" || channel === "css";
664
- const channelRange = getChannelRange(value, channel);
665
- return normalize.input({
666
- ...parts.channelInput.attrs,
667
- dir: prop("dir"),
668
- type: isTextField ? "text" : "number",
669
- "data-channel": channel,
670
- "aria-label": channel,
671
- spellCheck: false,
672
- autoComplete: "off",
673
- disabled,
674
- "data-disabled": domQuery.dataAttr(disabled),
675
- "data-invalid": domQuery.dataAttr(invalid),
676
- "data-readonly": domQuery.dataAttr(readOnly),
677
- readOnly,
678
- defaultValue: getChannelValue(value, channel),
679
- min: channelRange?.minValue,
680
- max: channelRange?.maxValue,
681
- step: channelRange?.step,
682
- onBeforeInput(event) {
683
- if (isTextField || !interactive) return;
684
- const value2 = event.currentTarget.value;
685
- if (value2.match(/[^0-9.]/g)) {
686
- event.preventDefault();
687
- }
688
- },
689
- onFocus(event) {
690
- if (!interactive) return;
691
- send({ type: "CHANNEL_INPUT.FOCUS", channel });
692
- event.currentTarget.select();
693
- },
694
- onBlur(event) {
695
- if (!interactive) return;
696
- const value2 = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
697
- send({ type: "CHANNEL_INPUT.BLUR", channel, value: value2, isTextField });
698
- },
699
- onKeyDown(event) {
700
- if (event.defaultPrevented) return;
701
- if (!interactive) return;
702
- if (event.key === "Enter") {
703
- const value2 = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
704
- send({ type: "CHANNEL_INPUT.CHANGE", channel, value: value2, isTextField });
705
- event.preventDefault();
706
- }
707
- },
708
- style: {
709
- appearance: "none",
710
- WebkitAppearance: "none",
711
- MozAppearance: "textfield"
712
- }
713
- });
714
- },
715
- getHiddenInputProps() {
716
- return normalize.input({
717
- type: "text",
718
- disabled,
719
- name: prop("name"),
720
- tabIndex: -1,
721
- readOnly,
722
- required,
723
- id: getHiddenInputId(scope),
724
- style: domQuery.visuallyHiddenStyle,
725
- defaultValue: valueAsString
726
- });
727
- },
728
- getEyeDropperTriggerProps() {
729
- return normalize.button({
730
- ...parts.eyeDropperTrigger.attrs,
731
- type: "button",
732
- dir: prop("dir"),
733
- disabled,
734
- "data-disabled": domQuery.dataAttr(disabled),
735
- "data-invalid": domQuery.dataAttr(invalid),
736
- "data-readonly": domQuery.dataAttr(readOnly),
737
- "aria-label": "Pick a color from the screen",
738
- onClick() {
739
- if (!interactive) return;
740
- send({ type: "EYEDROPPER.CLICK" });
741
- }
742
- });
743
- },
744
- getSwatchGroupProps() {
745
- return normalize.element({
746
- ...parts.swatchGroup.attrs,
747
- role: "group"
748
- });
749
- },
750
- getSwatchTriggerState,
751
- getSwatchTriggerProps(props2) {
752
- const swatchState = getSwatchTriggerState(props2);
753
- return normalize.button({
754
- ...parts.swatchTrigger.attrs,
755
- disabled: swatchState.disabled,
756
- dir: prop("dir"),
757
- type: "button",
758
- "aria-label": `select ${swatchState.valueAsString} as the color`,
759
- "data-state": swatchState.checked ? "checked" : "unchecked",
760
- "data-value": swatchState.valueAsString,
761
- "data-disabled": domQuery.dataAttr(swatchState.disabled),
762
- onClick() {
763
- if (swatchState.disabled) return;
764
- send({ type: "SWATCH_TRIGGER.CLICK", value: swatchState.value });
765
- },
766
- style: {
767
- "--color": swatchState.valueAsString,
768
- position: "relative"
769
- }
770
- });
771
- },
772
- getSwatchIndicatorProps(props2) {
773
- const swatchState = getSwatchTriggerState(props2);
774
- return normalize.element({
775
- ...parts.swatchIndicator.attrs,
776
- dir: prop("dir"),
777
- hidden: !swatchState.checked
778
- });
779
- },
780
- getSwatchProps(props2) {
781
- const { respectAlpha = true } = props2;
782
- const swatchState = getSwatchTriggerState(props2);
783
- const color = swatchState.value.toString(respectAlpha ? "css" : "hex");
784
- return normalize.element({
785
- ...parts.swatch.attrs,
786
- dir: prop("dir"),
787
- "data-state": swatchState.checked ? "checked" : "unchecked",
788
- "data-value": swatchState.valueAsString,
789
- style: {
790
- "--color": color,
791
- position: "relative",
792
- background: color
793
- }
794
- });
795
- },
796
- getFormatTriggerProps() {
797
- return normalize.button({
798
- ...parts.formatTrigger.attrs,
799
- dir: prop("dir"),
800
- type: "button",
801
- "aria-label": `change color format to ${getNextFormat(format)}`,
802
- onClick(event) {
803
- if (event.currentTarget.disabled) return;
804
- const nextFormat = getNextFormat(format);
805
- send({ type: "FORMAT.SET", format: nextFormat, src: "format-trigger" });
806
- }
807
- });
808
- },
809
- getFormatSelectProps() {
810
- return normalize.select({
811
- ...parts.formatSelect.attrs,
812
- "aria-label": "change color format",
813
- dir: prop("dir"),
814
- defaultValue: prop("format"),
815
- disabled,
816
- onChange(event) {
817
- const format2 = assertFormat(event.currentTarget.value);
818
- send({ type: "FORMAT.SET", format: format2, src: "format-select" });
819
- }
820
- });
821
- }
822
- };
823
- }
824
- var formats = ["hsba", "hsla", "rgba"];
825
- var formatRegex = new RegExp(`^(${formats.join("|")})$`);
826
- function getNextFormat(format) {
827
- const index = formats.indexOf(format);
828
- return formats[index + 1] ?? formats[0];
829
- }
830
- function assertFormat(format) {
831
- if (formatRegex.test(format)) return format;
832
- throw new Error(`Unsupported color format: ${format}`);
833
- }
834
- var parse = (colorString) => {
835
- return colorUtils.parseColor(colorString);
16
+ return to;
836
17
  };
18
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
837
20
 
838
- // src/utils/is-valid-hex.ts
839
- var HEX_REGEX = /^[0-9a-fA-F]{3,8}$/;
840
- function isValidHex(value) {
841
- return HEX_REGEX.test(value);
842
- }
843
- function prefixHex(value) {
844
- if (value.startsWith("#")) return value;
845
- if (isValidHex(value)) return `#${value}`;
846
- return value;
847
- }
848
-
849
- // src/color-picker.machine.ts
850
- var { and } = core.createGuards();
851
- var hashObject = (obj) => {
852
- let hash = "";
853
- for (const key in obj) hash += `${key}:${obj[key] ?? ""};`;
854
- return hash;
855
- };
856
- var DEFAULT_COLOR = parse("#000000");
857
- var machine = core.createMachine({
858
- props({ props: props2 }) {
859
- const color = props2.value ?? props2.defaultValue ?? DEFAULT_COLOR;
860
- return {
861
- dir: "ltr",
862
- defaultValue: DEFAULT_COLOR,
863
- defaultFormat: color.getFormat(),
864
- openAutoFocus: true,
865
- ...props2,
866
- positioning: {
867
- placement: "bottom",
868
- ...props2.positioning
869
- }
870
- };
871
- },
872
- initialState({ prop }) {
873
- const open = prop("open") || prop("defaultOpen") || prop("inline");
874
- return open ? "open" : "idle";
875
- },
876
- context({ prop, bindable, getContext }) {
877
- return {
878
- value: bindable(() => ({
879
- defaultValue: prop("defaultValue"),
880
- value: prop("value"),
881
- isEqual(a, b) {
882
- return b != null && a.isEqual(b);
883
- },
884
- hash(a) {
885
- return hashObject(a.toJSON());
886
- },
887
- onChange(value) {
888
- const ctx = getContext();
889
- const format = ctx.get("format");
890
- prop("onValueChange")?.({ value, valueAsString: value.toString(format) });
891
- }
892
- })),
893
- format: bindable(() => ({
894
- defaultValue: prop("defaultFormat"),
895
- value: prop("format"),
896
- onChange(format) {
897
- prop("onFormatChange")?.({ format });
898
- }
899
- })),
900
- activeId: bindable(() => ({ defaultValue: null })),
901
- activeChannel: bindable(() => ({ defaultValue: null })),
902
- activeOrientation: bindable(() => ({ defaultValue: null })),
903
- fieldsetDisabled: bindable(() => ({ defaultValue: false })),
904
- restoreFocus: bindable(() => ({ defaultValue: true })),
905
- currentPlacement: bindable(() => ({
906
- defaultValue: void 0
907
- }))
908
- };
909
- },
910
- computed: {
911
- rtl: ({ prop }) => prop("dir") === "rtl",
912
- disabled: ({ prop, context }) => !!prop("disabled") || context.get("fieldsetDisabled"),
913
- interactive: ({ prop }) => !(prop("disabled") || prop("readOnly")),
914
- valueAsString: ({ context }) => context.get("value").toString(context.get("format")),
915
- areaValue: ({ context }) => {
916
- const format = context.get("format").startsWith("hsl") ? "hsla" : "hsba";
917
- return context.get("value").toFormat(format);
918
- }
919
- },
920
- effects: ["trackFormControl"],
921
- watch({ prop, context, action, track }) {
922
- track([() => context.hash("value")], () => {
923
- action(["syncInputElements", "dispatchChangeEvent"]);
924
- });
925
- track([() => context.get("format")], () => {
926
- action(["syncFormatSelectElement", "syncValueWithFormat"]);
927
- });
928
- track([() => prop("open")], () => {
929
- action(["toggleVisibility"]);
930
- });
931
- },
932
- on: {
933
- "VALUE.SET": {
934
- actions: ["setValue"]
935
- },
936
- "FORMAT.SET": {
937
- actions: ["setFormat"]
938
- },
939
- "CHANNEL_INPUT.CHANGE": {
940
- actions: ["setChannelColorFromInput"]
941
- },
942
- "EYEDROPPER.CLICK": {
943
- actions: ["openEyeDropper"]
944
- },
945
- "SWATCH_TRIGGER.CLICK": {
946
- actions: ["setValue"]
947
- }
948
- },
949
- states: {
950
- idle: {
951
- tags: ["closed"],
952
- on: {
953
- "CONTROLLED.OPEN": {
954
- target: "open",
955
- actions: ["setInitialFocus"]
956
- },
957
- OPEN: [
958
- {
959
- guard: "isOpenControlled",
960
- actions: ["invokeOnOpen"]
961
- },
962
- {
963
- target: "open",
964
- actions: ["invokeOnOpen", "setInitialFocus"]
965
- }
966
- ],
967
- "TRIGGER.CLICK": [
968
- {
969
- guard: "isOpenControlled",
970
- actions: ["invokeOnOpen"]
971
- },
972
- {
973
- target: "open",
974
- actions: ["invokeOnOpen", "setInitialFocus"]
975
- }
976
- ],
977
- "CHANNEL_INPUT.FOCUS": {
978
- target: "focused",
979
- actions: ["setActiveChannel"]
980
- }
981
- }
982
- },
983
- focused: {
984
- tags: ["closed", "focused"],
985
- on: {
986
- "CONTROLLED.OPEN": {
987
- target: "open",
988
- actions: ["setInitialFocus"]
989
- },
990
- OPEN: [
991
- {
992
- guard: "isOpenControlled",
993
- actions: ["invokeOnOpen"]
994
- },
995
- {
996
- target: "open",
997
- actions: ["invokeOnOpen", "setInitialFocus"]
998
- }
999
- ],
1000
- "TRIGGER.CLICK": [
1001
- {
1002
- guard: "isOpenControlled",
1003
- actions: ["invokeOnOpen"]
1004
- },
1005
- {
1006
- target: "open",
1007
- actions: ["invokeOnOpen", "setInitialFocus"]
1008
- }
1009
- ],
1010
- "CHANNEL_INPUT.FOCUS": {
1011
- actions: ["setActiveChannel"]
1012
- },
1013
- "CHANNEL_INPUT.BLUR": {
1014
- target: "idle",
1015
- actions: ["setChannelColorFromInput"]
1016
- },
1017
- "TRIGGER.BLUR": {
1018
- target: "idle"
1019
- }
1020
- }
1021
- },
1022
- open: {
1023
- tags: ["open"],
1024
- effects: ["trackPositioning", "trackDismissableElement"],
1025
- on: {
1026
- "CONTROLLED.CLOSE": [
1027
- {
1028
- guard: "shouldRestoreFocus",
1029
- target: "focused",
1030
- actions: ["setReturnFocus"]
1031
- },
1032
- {
1033
- target: "idle"
1034
- }
1035
- ],
1036
- "TRIGGER.CLICK": [
1037
- {
1038
- guard: "isOpenControlled",
1039
- actions: ["invokeOnClose"]
1040
- },
1041
- {
1042
- target: "idle",
1043
- actions: ["invokeOnClose"]
1044
- }
1045
- ],
1046
- "AREA.POINTER_DOWN": {
1047
- target: "open:dragging",
1048
- actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
1049
- },
1050
- "AREA.FOCUS": {
1051
- actions: ["setActiveChannel"]
1052
- },
1053
- "CHANNEL_SLIDER.POINTER_DOWN": {
1054
- target: "open:dragging",
1055
- actions: ["setActiveChannel", "setChannelColorFromPoint", "focusChannelThumb"]
1056
- },
1057
- "CHANNEL_SLIDER.FOCUS": {
1058
- actions: ["setActiveChannel"]
1059
- },
1060
- "AREA.ARROW_LEFT": {
1061
- actions: ["decrementAreaXChannel"]
1062
- },
1063
- "AREA.ARROW_RIGHT": {
1064
- actions: ["incrementAreaXChannel"]
1065
- },
1066
- "AREA.ARROW_UP": {
1067
- actions: ["incrementAreaYChannel"]
1068
- },
1069
- "AREA.ARROW_DOWN": {
1070
- actions: ["decrementAreaYChannel"]
1071
- },
1072
- "AREA.PAGE_UP": {
1073
- actions: ["incrementAreaXChannel"]
1074
- },
1075
- "AREA.PAGE_DOWN": {
1076
- actions: ["decrementAreaXChannel"]
1077
- },
1078
- "CHANNEL_SLIDER.ARROW_LEFT": {
1079
- actions: ["decrementChannel"]
1080
- },
1081
- "CHANNEL_SLIDER.ARROW_RIGHT": {
1082
- actions: ["incrementChannel"]
1083
- },
1084
- "CHANNEL_SLIDER.ARROW_UP": {
1085
- actions: ["incrementChannel"]
1086
- },
1087
- "CHANNEL_SLIDER.ARROW_DOWN": {
1088
- actions: ["decrementChannel"]
1089
- },
1090
- "CHANNEL_SLIDER.PAGE_UP": {
1091
- actions: ["incrementChannel"]
1092
- },
1093
- "CHANNEL_SLIDER.PAGE_DOWN": {
1094
- actions: ["decrementChannel"]
1095
- },
1096
- "CHANNEL_SLIDER.HOME": {
1097
- actions: ["setChannelToMin"]
1098
- },
1099
- "CHANNEL_SLIDER.END": {
1100
- actions: ["setChannelToMax"]
1101
- },
1102
- "CHANNEL_INPUT.BLUR": {
1103
- actions: ["setChannelColorFromInput"]
1104
- },
1105
- INTERACT_OUTSIDE: [
1106
- {
1107
- guard: "isOpenControlled",
1108
- actions: ["invokeOnClose"]
1109
- },
1110
- {
1111
- guard: "shouldRestoreFocus",
1112
- target: "focused",
1113
- actions: ["invokeOnClose", "setReturnFocus"]
1114
- },
1115
- {
1116
- target: "idle",
1117
- actions: ["invokeOnClose"]
1118
- }
1119
- ],
1120
- CLOSE: [
1121
- {
1122
- guard: "isOpenControlled",
1123
- actions: ["invokeOnClose"]
1124
- },
1125
- {
1126
- target: "idle",
1127
- actions: ["invokeOnClose"]
1128
- }
1129
- ],
1130
- "SWATCH_TRIGGER.CLICK": [
1131
- {
1132
- guard: and("isOpenControlled", "closeOnSelect"),
1133
- actions: ["setValue", "invokeOnClose"]
1134
- },
1135
- {
1136
- guard: "closeOnSelect",
1137
- target: "focused",
1138
- actions: ["setValue", "invokeOnClose", "setReturnFocus"]
1139
- },
1140
- {
1141
- actions: ["setValue"]
1142
- }
1143
- ]
1144
- }
1145
- },
1146
- "open:dragging": {
1147
- tags: ["open"],
1148
- exit: ["clearActiveChannel"],
1149
- effects: ["trackPointerMove", "disableTextSelection", "trackPositioning", "trackDismissableElement"],
1150
- on: {
1151
- "CONTROLLED.CLOSE": [
1152
- {
1153
- guard: "shouldRestoreFocus",
1154
- target: "focused",
1155
- actions: ["setReturnFocus"]
1156
- },
1157
- {
1158
- target: "idle"
1159
- }
1160
- ],
1161
- "AREA.POINTER_MOVE": {
1162
- actions: ["setAreaColorFromPoint", "focusAreaThumb"]
1163
- },
1164
- "AREA.POINTER_UP": {
1165
- target: "open",
1166
- actions: ["invokeOnChangeEnd"]
1167
- },
1168
- "CHANNEL_SLIDER.POINTER_MOVE": {
1169
- actions: ["setChannelColorFromPoint", "focusChannelThumb"]
1170
- },
1171
- "CHANNEL_SLIDER.POINTER_UP": {
1172
- target: "open",
1173
- actions: ["invokeOnChangeEnd"]
1174
- },
1175
- INTERACT_OUTSIDE: [
1176
- {
1177
- guard: "isOpenControlled",
1178
- actions: ["invokeOnClose"]
1179
- },
1180
- {
1181
- guard: "shouldRestoreFocus",
1182
- target: "focused",
1183
- actions: ["invokeOnClose", "setReturnFocus"]
1184
- },
1185
- {
1186
- target: "idle",
1187
- actions: ["invokeOnClose"]
1188
- }
1189
- ],
1190
- CLOSE: [
1191
- {
1192
- guard: "isOpenControlled",
1193
- actions: ["invokeOnClose"]
1194
- },
1195
- {
1196
- target: "idle",
1197
- actions: ["invokeOnClose"]
1198
- }
1199
- ]
1200
- }
1201
- }
1202
- },
1203
- implementations: {
1204
- guards: {
1205
- closeOnSelect: ({ prop }) => !!prop("closeOnSelect"),
1206
- isOpenControlled: ({ prop }) => prop("open") != null || !!prop("inline"),
1207
- shouldRestoreFocus: ({ context }) => !!context.get("restoreFocus")
1208
- },
1209
- effects: {
1210
- trackPositioning({ context, prop, scope }) {
1211
- if (prop("inline")) return;
1212
- if (!context.get("currentPlacement")) {
1213
- context.set("currentPlacement", prop("positioning")?.placement);
1214
- }
1215
- const anchorEl = getTriggerEl(scope);
1216
- const getPositionerEl2 = () => getPositionerEl(scope);
1217
- return popper.getPlacement(anchorEl, getPositionerEl2, {
1218
- ...prop("positioning"),
1219
- defer: true,
1220
- onComplete(data) {
1221
- context.set("currentPlacement", data.placement);
1222
- }
1223
- });
1224
- },
1225
- trackDismissableElement({ context, scope, prop, send }) {
1226
- if (prop("inline")) return;
1227
- const getContentEl2 = () => getContentEl(scope);
1228
- return dismissable.trackDismissableElement(getContentEl2, {
1229
- type: "popover",
1230
- exclude: getTriggerEl(scope),
1231
- defer: true,
1232
- onInteractOutside(event) {
1233
- prop("onInteractOutside")?.(event);
1234
- if (event.defaultPrevented) return;
1235
- context.set("restoreFocus", !(event.detail.focusable || event.detail.contextmenu));
1236
- },
1237
- onPointerDownOutside: prop("onPointerDownOutside"),
1238
- onFocusOutside: prop("onFocusOutside"),
1239
- onDismiss() {
1240
- send({ type: "INTERACT_OUTSIDE" });
1241
- }
1242
- });
1243
- },
1244
- trackFormControl({ context, scope, send }) {
1245
- const inputEl = getHiddenInputEl(scope);
1246
- return domQuery.trackFormControl(inputEl, {
1247
- onFieldsetDisabledChange(disabled) {
1248
- context.set("fieldsetDisabled", disabled);
1249
- },
1250
- onFormReset() {
1251
- send({ type: "VALUE.SET", value: context.initial("value"), src: "form.reset" });
1252
- }
1253
- });
1254
- },
1255
- trackPointerMove({ context, scope, event, send }) {
1256
- return domQuery.trackPointerMove(scope.getDoc(), {
1257
- onPointerMove({ point }) {
1258
- const type = context.get("activeId") === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE";
1259
- send({ type, point, format: event.format });
1260
- },
1261
- onPointerUp() {
1262
- const type = context.get("activeId") === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP";
1263
- send({ type });
1264
- }
1265
- });
1266
- },
1267
- disableTextSelection({ scope }) {
1268
- return domQuery.disableTextSelection({
1269
- doc: scope.getDoc(),
1270
- target: getContentEl(scope)
1271
- });
1272
- }
1273
- },
1274
- actions: {
1275
- openEyeDropper({ scope, context }) {
1276
- const win = scope.getWin();
1277
- const isSupported = "EyeDropper" in win;
1278
- if (!isSupported) return;
1279
- const picker = new win.EyeDropper();
1280
- picker.open().then(({ sRGBHex }) => {
1281
- const format = context.get("value").getFormat();
1282
- const color = colorUtils.parseColor(sRGBHex).toFormat(format);
1283
- context.set("value", color);
1284
- }).catch(() => void 0);
1285
- },
1286
- setActiveChannel({ context, event }) {
1287
- context.set("activeId", event.id);
1288
- if (event.channel) context.set("activeChannel", event.channel);
1289
- if (event.orientation) context.set("activeOrientation", event.orientation);
1290
- },
1291
- clearActiveChannel({ context }) {
1292
- context.set("activeChannel", null);
1293
- context.set("activeId", null);
1294
- context.set("activeOrientation", null);
1295
- },
1296
- setAreaColorFromPoint({ context, event, computed, scope, prop }) {
1297
- const v = event.format ? context.get("value").toFormat(event.format) : computed("areaValue");
1298
- const { xChannel, yChannel } = event.channel || context.get("activeChannel");
1299
- const percent = getAreaValueFromPoint(scope, event.point, prop("dir"));
1300
- if (!percent) return;
1301
- const xValue = v.getChannelPercentValue(xChannel, percent.x);
1302
- const yValue = v.getChannelPercentValue(yChannel, 1 - percent.y);
1303
- const color = v.withChannelValue(xChannel, xValue).withChannelValue(yChannel, yValue);
1304
- context.set("value", color);
1305
- },
1306
- setChannelColorFromPoint({ context, event, computed, scope, prop }) {
1307
- const channel = event.channel || context.get("activeId");
1308
- const normalizedValue = event.format ? context.get("value").toFormat(event.format) : computed("areaValue");
1309
- const percent = getChannelSliderValueFromPoint(scope, event.point, channel, prop("dir"));
1310
- if (!percent) return;
1311
- const orientation = context.get("activeOrientation") || "horizontal";
1312
- const channelPercent = orientation === "horizontal" ? percent.x : percent.y;
1313
- const value = normalizedValue.getChannelPercentValue(channel, channelPercent);
1314
- const color = normalizedValue.withChannelValue(channel, value);
1315
- context.set("value", color);
1316
- },
1317
- setValue({ context, event }) {
1318
- context.set("value", event.value);
1319
- },
1320
- setFormat({ context, event }) {
1321
- context.set("format", event.format);
1322
- },
1323
- dispatchChangeEvent({ scope, computed }) {
1324
- domQuery.dispatchInputValueEvent(getHiddenInputEl(scope), { value: computed("valueAsString") });
1325
- },
1326
- syncInputElements({ context, scope }) {
1327
- syncChannelInputs(scope, context.get("value"));
1328
- },
1329
- invokeOnChangeEnd({ context, prop, computed }) {
1330
- prop("onValueChangeEnd")?.({
1331
- value: context.get("value"),
1332
- valueAsString: computed("valueAsString")
1333
- });
1334
- },
1335
- setChannelColorFromInput({ context, event, scope, prop }) {
1336
- const { channel, isTextField, value } = event;
1337
- const currentAlpha = context.get("value").getChannelValue("alpha");
1338
- let color;
1339
- if (channel === "alpha") {
1340
- let valueAsNumber = parseFloat(value);
1341
- valueAsNumber = Number.isNaN(valueAsNumber) ? currentAlpha : valueAsNumber;
1342
- color = context.get("value").withChannelValue("alpha", valueAsNumber);
1343
- } else if (isTextField) {
1344
- color = utils.tryCatch(
1345
- () => {
1346
- const parseValue = channel === "hex" ? prefixHex(value) : value;
1347
- return parse(parseValue).withChannelValue("alpha", currentAlpha);
1348
- },
1349
- () => context.get("value")
1350
- );
1351
- } else {
1352
- const current = context.get("value").toFormat(context.get("format"));
1353
- const valueAsNumber = Number.isNaN(value) ? current.getChannelValue(channel) : value;
1354
- color = current.withChannelValue(channel, valueAsNumber);
1355
- }
1356
- syncChannelInputs(scope, context.get("value"), color);
1357
- context.set("value", color);
1358
- prop("onValueChangeEnd")?.({
1359
- value: color,
1360
- valueAsString: color.toString(context.get("format"))
1361
- });
1362
- },
1363
- incrementChannel({ context, event }) {
1364
- const color = context.get("value").incrementChannel(event.channel, event.step);
1365
- context.set("value", color);
1366
- },
1367
- decrementChannel({ context, event }) {
1368
- const color = context.get("value").decrementChannel(event.channel, event.step);
1369
- context.set("value", color);
1370
- },
1371
- incrementAreaXChannel({ context, event, computed }) {
1372
- const { xChannel } = event.channel;
1373
- const color = computed("areaValue").incrementChannel(xChannel, event.step);
1374
- context.set("value", color);
1375
- },
1376
- decrementAreaXChannel({ context, event, computed }) {
1377
- const { xChannel } = event.channel;
1378
- const color = computed("areaValue").decrementChannel(xChannel, event.step);
1379
- context.set("value", color);
1380
- },
1381
- incrementAreaYChannel({ context, event, computed }) {
1382
- const { yChannel } = event.channel;
1383
- const color = computed("areaValue").incrementChannel(yChannel, event.step);
1384
- context.set("value", color);
1385
- },
1386
- decrementAreaYChannel({ context, event, computed }) {
1387
- const { yChannel } = event.channel;
1388
- const color = computed("areaValue").decrementChannel(yChannel, event.step);
1389
- context.set("value", color);
1390
- },
1391
- setChannelToMax({ context, event }) {
1392
- const value = context.get("value");
1393
- const range = value.getChannelRange(event.channel);
1394
- const color = value.withChannelValue(event.channel, range.maxValue);
1395
- context.set("value", color);
1396
- },
1397
- setChannelToMin({ context, event }) {
1398
- const value = context.get("value");
1399
- const range = value.getChannelRange(event.channel);
1400
- const color = value.withChannelValue(event.channel, range.minValue);
1401
- context.set("value", color);
1402
- },
1403
- focusAreaThumb({ scope }) {
1404
- domQuery.raf(() => {
1405
- getAreaThumbEl(scope)?.focus({ preventScroll: true });
1406
- });
1407
- },
1408
- focusChannelThumb({ event, scope }) {
1409
- domQuery.raf(() => {
1410
- getChannelSliderThumbEl(scope, event.channel)?.focus({ preventScroll: true });
1411
- });
1412
- },
1413
- setInitialFocus({ prop, scope }) {
1414
- if (!prop("openAutoFocus")) return;
1415
- domQuery.raf(() => {
1416
- const element = domQuery.getInitialFocus({
1417
- root: getContentEl(scope),
1418
- getInitialEl: prop("initialFocusEl")
1419
- });
1420
- element?.focus({ preventScroll: true });
1421
- });
1422
- },
1423
- setReturnFocus({ scope }) {
1424
- domQuery.raf(() => {
1425
- getTriggerEl(scope)?.focus({ preventScroll: true });
1426
- });
1427
- },
1428
- syncFormatSelectElement({ context, scope }) {
1429
- syncFormatSelect(scope, context.get("format"));
1430
- },
1431
- syncValueWithFormat({ context }) {
1432
- const value = context.get("value");
1433
- const newValue = value.toFormat(context.get("format"));
1434
- if (newValue.isEqual(value)) return;
1435
- context.set("value", newValue);
1436
- },
1437
- invokeOnOpen({ prop, context }) {
1438
- if (prop("inline")) return;
1439
- prop("onOpenChange")?.({ open: true, value: context.get("value") });
1440
- },
1441
- invokeOnClose({ prop, context }) {
1442
- if (prop("inline")) return;
1443
- prop("onOpenChange")?.({ open: false, value: context.get("value") });
1444
- },
1445
- toggleVisibility({ prop, event, send }) {
1446
- send({ type: prop("open") ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: event });
1447
- }
1448
- }
1449
- }
21
+ // src/index.ts
22
+ var index_exports = {};
23
+ __export(index_exports, {
24
+ anatomy: () => import_color_picker.anatomy,
25
+ connect: () => import_color_picker2.connect,
26
+ machine: () => import_color_picker3.machine,
27
+ parse: () => import_color_picker4.parse
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+ var import_color_picker = require("./color-picker.anatomy.cjs");
31
+ var import_color_picker2 = require("./color-picker.connect.cjs");
32
+ var import_color_picker3 = require("./color-picker.machine.cjs");
33
+ var import_color_picker4 = require("./color-picker.parse.cjs");
34
+ __reExport(index_exports, require("./color-picker.props.cjs"), module.exports);
35
+ // Annotate the CommonJS export names for ESM import in node:
36
+ 0 && (module.exports = {
37
+ anatomy,
38
+ connect,
39
+ machine,
40
+ parse,
41
+ ...require("./color-picker.props.cjs")
1450
42
  });
1451
- function syncChannelInputs(scope, currentValue, nextValue) {
1452
- const channelInputEls = getChannelInputEls(scope);
1453
- domQuery.raf(() => {
1454
- channelInputEls.forEach((inputEl) => {
1455
- const channel = inputEl.dataset.channel;
1456
- domQuery.setElementValue(inputEl, getChannelValue(nextValue || currentValue, channel));
1457
- });
1458
- });
1459
- }
1460
- function syncFormatSelect(scope, format) {
1461
- const selectEl = getFormatSelectEl(scope);
1462
- if (!selectEl) return;
1463
- domQuery.raf(() => domQuery.setElementValue(selectEl, format));
1464
- }
1465
- var props = types.createProps()([
1466
- "closeOnSelect",
1467
- "dir",
1468
- "disabled",
1469
- "format",
1470
- "defaultFormat",
1471
- "getRootNode",
1472
- "id",
1473
- "ids",
1474
- "initialFocusEl",
1475
- "inline",
1476
- "name",
1477
- "positioning",
1478
- "onFocusOutside",
1479
- "onFormatChange",
1480
- "onInteractOutside",
1481
- "onOpenChange",
1482
- "onPointerDownOutside",
1483
- "onValueChange",
1484
- "onValueChangeEnd",
1485
- "defaultOpen",
1486
- "open",
1487
- "positioning",
1488
- "required",
1489
- "readOnly",
1490
- "value",
1491
- "defaultValue",
1492
- "invalid",
1493
- "openAutoFocus"
1494
- ]);
1495
- var splitProps = utils.createSplitProps(props);
1496
- var areaProps = types.createProps()(["xChannel", "yChannel"]);
1497
- var splitAreaProps = utils.createSplitProps(areaProps);
1498
- var channelProps = types.createProps()(["channel", "orientation"]);
1499
- var splitChannelProps = utils.createSplitProps(channelProps);
1500
- var swatchTriggerProps = types.createProps()(["value", "disabled"]);
1501
- var splitSwatchTriggerProps = utils.createSplitProps(swatchTriggerProps);
1502
- var swatchProps = types.createProps()(["value", "respectAlpha"]);
1503
- var splitSwatchProps = utils.createSplitProps(swatchProps);
1504
- var transparencyGridProps = types.createProps()(["size"]);
1505
- var splitTransparencyGridProps = utils.createSplitProps(transparencyGridProps);
1506
-
1507
- exports.anatomy = anatomy;
1508
- exports.areaProps = areaProps;
1509
- exports.channelProps = channelProps;
1510
- exports.connect = connect;
1511
- exports.machine = machine;
1512
- exports.parse = parse;
1513
- exports.props = props;
1514
- exports.splitAreaProps = splitAreaProps;
1515
- exports.splitChannelProps = splitChannelProps;
1516
- exports.splitProps = splitProps;
1517
- exports.splitSwatchProps = splitSwatchProps;
1518
- exports.splitSwatchTriggerProps = splitSwatchTriggerProps;
1519
- exports.splitTransparencyGridProps = splitTransparencyGridProps;
1520
- exports.swatchProps = swatchProps;
1521
- exports.swatchTriggerProps = swatchTriggerProps;
1522
- exports.transparencyGridProps = transparencyGridProps;