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