@zag-js/color-picker 0.46.0 → 0.48.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.mjs CHANGED
@@ -33,11 +33,10 @@ import {
33
33
  getEventStep,
34
34
  getNativeEvent,
35
35
  isLeftClick,
36
- isModifiedEvent
36
+ isModifierKey
37
37
  } from "@zag-js/dom-event";
38
- import { dataAttr, query } from "@zag-js/dom-query";
38
+ import { dataAttr, query, visuallyHiddenStyle } from "@zag-js/dom-query";
39
39
  import { getPlacementStyles } from "@zag-js/popper";
40
- import { visuallyHiddenStyle } from "@zag-js/visually-hidden";
41
40
 
42
41
  // src/color-picker.dom.ts
43
42
  import { getRelativePoint } from "@zag-js/dom-event";
@@ -228,11 +227,11 @@ function connect(state, send, normalize) {
228
227
  const value = state.context.value;
229
228
  const areaValue = state.context.areaValue;
230
229
  const valueAsString = state.context.valueAsString;
231
- const isDisabled = state.context.isDisabled;
232
- const isInteractive = state.context.isInteractive;
233
- const isDragging = state.hasTag("dragging");
234
- const isOpen = state.hasTag("open");
235
- const isFocused = state.hasTag("focused");
230
+ const disabled = state.context.isDisabled;
231
+ const interactive = state.context.isInteractive;
232
+ const dragging = state.hasTag("dragging");
233
+ const open = state.hasTag("open");
234
+ const focused = state.hasTag("focused");
236
235
  const getAreaChannels = (props) => {
237
236
  const channels = areaValue.getChannels();
238
237
  return {
@@ -250,20 +249,19 @@ function connect(state, send, normalize) {
250
249
  return {
251
250
  value: color,
252
251
  valueAsString: color.toString("hex"),
253
- isChecked: color.isEqual(value),
254
- isDisabled: props.disabled || !isInteractive
252
+ checked: color.isEqual(value),
253
+ disabled: props.disabled || !interactive
255
254
  };
256
255
  }
257
256
  return {
258
- isDragging,
259
- isOpen,
257
+ dragging,
258
+ open,
260
259
  valueAsString,
261
260
  value,
262
- open() {
263
- send({ type: "OPEN" });
264
- },
265
- close() {
266
- send({ type: "CLOSE" });
261
+ setOpen(_open) {
262
+ if (_open === open)
263
+ return;
264
+ send({ type: _open ? "OPEN" : "CLOSE" });
267
265
  },
268
266
  setValue(value2) {
269
267
  send({ type: "VALUE.SET", value: normalizeColor(value2), src: "set-color" });
@@ -289,7 +287,7 @@ function connect(state, send, normalize) {
289
287
  ...parts.root.attrs,
290
288
  dir: state.context.dir,
291
289
  id: dom.getRootId(state.context),
292
- "data-disabled": dataAttr(isDisabled),
290
+ "data-disabled": dataAttr(disabled),
293
291
  "data-readonly": dataAttr(state.context.readOnly),
294
292
  style: {
295
293
  "--value": value.toString("css")
@@ -300,9 +298,9 @@ function connect(state, send, normalize) {
300
298
  dir: state.context.dir,
301
299
  id: dom.getLabelId(state.context),
302
300
  htmlFor: dom.getHiddenInputId(state.context),
303
- "data-disabled": dataAttr(isDisabled),
301
+ "data-disabled": dataAttr(disabled),
304
302
  "data-readonly": dataAttr(state.context.readOnly),
305
- "data-focus": dataAttr(isFocused),
303
+ "data-focus": dataAttr(focused),
306
304
  onClick(event) {
307
305
  event.preventDefault();
308
306
  const inputEl = query(dom.getControlEl(state.context), "[data-channel=hex]");
@@ -313,33 +311,33 @@ function connect(state, send, normalize) {
313
311
  ...parts.control.attrs,
314
312
  id: dom.getControlId(state.context),
315
313
  dir: state.context.dir,
316
- "data-disabled": dataAttr(isDisabled),
314
+ "data-disabled": dataAttr(disabled),
317
315
  "data-readonly": dataAttr(state.context.readOnly),
318
- "data-state": isOpen ? "open" : "closed",
319
- "data-focus": dataAttr(isFocused)
316
+ "data-state": open ? "open" : "closed",
317
+ "data-focus": dataAttr(focused)
320
318
  }),
321
319
  triggerProps: normalize.button({
322
320
  ...parts.trigger.attrs,
323
321
  id: dom.getTriggerId(state.context),
324
322
  dir: state.context.dir,
325
- disabled: isDisabled,
323
+ disabled,
326
324
  "aria-label": `select color. current color is ${valueAsString}`,
327
325
  "aria-controls": dom.getContentId(state.context),
328
326
  "aria-labelledby": dom.getLabelId(state.context),
329
- "data-disabled": dataAttr(isDisabled),
327
+ "data-disabled": dataAttr(disabled),
330
328
  "data-readonly": dataAttr(state.context.readOnly),
331
329
  "data-placement": currentPlacement,
332
- "aria-expanded": dataAttr(isOpen),
333
- "data-state": isOpen ? "open" : "closed",
334
- "data-focus": dataAttr(isFocused),
330
+ "aria-expanded": dataAttr(open),
331
+ "data-state": open ? "open" : "closed",
332
+ "data-focus": dataAttr(focused),
335
333
  type: "button",
336
334
  onClick() {
337
- if (!isInteractive)
335
+ if (!interactive)
338
336
  return;
339
337
  send({ type: "TRIGGER.CLICK" });
340
338
  },
341
339
  onBlur() {
342
- if (!isInteractive)
340
+ if (!interactive)
343
341
  return;
344
342
  send({ type: "TRIGGER.BLUR" });
345
343
  },
@@ -358,8 +356,8 @@ function connect(state, send, normalize) {
358
356
  id: dom.getContentId(state.context),
359
357
  dir: state.context.dir,
360
358
  "data-placement": currentPlacement,
361
- "data-state": isOpen ? "open" : "closed",
362
- hidden: !isOpen
359
+ "data-state": open ? "open" : "closed",
360
+ hidden: !open
363
361
  }),
364
362
  getAreaProps(props = {}) {
365
363
  const { xChannel, yChannel } = getAreaChannels(props);
@@ -373,10 +371,10 @@ function connect(state, send, normalize) {
373
371
  id: dom.getAreaId(state.context),
374
372
  role: "group",
375
373
  onPointerDown(event) {
376
- if (!isInteractive)
374
+ if (!interactive)
377
375
  return;
378
376
  const evt = getNativeEvent(event);
379
- if (!isLeftClick(evt) || isModifiedEvent(evt))
377
+ if (!isLeftClick(evt) || isModifierKey(evt))
380
378
  return;
381
379
  const point = getEventPoint(evt);
382
380
  const channel = { xChannel, yChannel };
@@ -420,8 +418,8 @@ function connect(state, send, normalize) {
420
418
  ...parts.areaThumb.attrs,
421
419
  id: dom.getAreaThumbId(state.context),
422
420
  dir: state.context.dir,
423
- tabIndex: isDisabled ? void 0 : 0,
424
- "data-disabled": dataAttr(isDisabled),
421
+ tabIndex: disabled ? void 0 : 0,
422
+ "data-disabled": dataAttr(disabled),
425
423
  role: "slider",
426
424
  "aria-valuemin": 0,
427
425
  "aria-valuemax": 100,
@@ -439,12 +437,14 @@ function connect(state, send, normalize) {
439
437
  background: areaValue.withChannelValue("alpha", 1).toString("css")
440
438
  },
441
439
  onFocus() {
442
- if (!isInteractive)
440
+ if (!interactive)
443
441
  return;
444
442
  send({ type: "AREA.FOCUS", id: "area", channel });
445
443
  },
446
444
  onKeyDown(event) {
447
- if (!isInteractive)
445
+ if (event.defaultPrevented)
446
+ return;
447
+ if (!interactive)
448
448
  return;
449
449
  const step = getEventStep(event);
450
450
  const keyMap = {
@@ -504,10 +504,10 @@ function connect(state, send, normalize) {
504
504
  "data-orientation": orientation,
505
505
  role: "presentation",
506
506
  onPointerDown(event) {
507
- if (!isInteractive)
507
+ if (!interactive)
508
508
  return;
509
509
  const evt = getNativeEvent(event);
510
- if (!isLeftClick(evt) || isModifiedEvent(evt))
510
+ if (!isLeftClick(evt) || isModifierKey(evt))
511
511
  return;
512
512
  const point = getEventPoint(evt);
513
513
  send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, point, id: channel, orientation });
@@ -550,11 +550,11 @@ function connect(state, send, normalize) {
550
550
  id: dom.getChannelSliderThumbId(state.context, channel),
551
551
  role: "slider",
552
552
  "aria-label": channel,
553
- tabIndex: isDisabled ? void 0 : 0,
553
+ tabIndex: disabled ? void 0 : 0,
554
554
  "data-channel": channel,
555
- "data-disabled": dataAttr(isDisabled),
555
+ "data-disabled": dataAttr(disabled),
556
556
  "data-orientation": orientation,
557
- "aria-disabled": dataAttr(isDisabled),
557
+ "aria-disabled": dataAttr(disabled),
558
558
  "aria-orientation": orientation,
559
559
  "aria-valuemax": maxValue,
560
560
  "aria-valuemin": minValue,
@@ -567,12 +567,14 @@ function connect(state, send, normalize) {
567
567
  ...placementStyles
568
568
  },
569
569
  onFocus() {
570
- if (!isInteractive)
570
+ if (!interactive)
571
571
  return;
572
572
  send({ type: "CHANNEL_SLIDER.FOCUS", channel });
573
573
  },
574
574
  onKeyDown(event) {
575
- if (!isInteractive)
575
+ if (event.defaultPrevented)
576
+ return;
577
+ if (!interactive)
576
578
  return;
577
579
  const step = getEventStep(event) * stepValue;
578
580
  const keyMap = {
@@ -624,15 +626,15 @@ function connect(state, send, normalize) {
624
626
  "aria-label": channel,
625
627
  spellCheck: false,
626
628
  autoComplete: "off",
627
- disabled: isDisabled,
628
- "data-disabled": dataAttr(isDisabled),
629
+ disabled,
630
+ "data-disabled": dataAttr(disabled),
629
631
  readOnly: state.context.readOnly,
630
632
  defaultValue: getChannelValue(value, channel),
631
633
  min: range?.minValue,
632
634
  max: range?.maxValue,
633
635
  step: range?.step,
634
636
  onBeforeInput(event) {
635
- if (isTextField || !isInteractive)
637
+ if (isTextField || !interactive)
636
638
  return;
637
639
  const value2 = event.currentTarget.value;
638
640
  if (value2.match(/[^0-9.]/g)) {
@@ -640,19 +642,21 @@ function connect(state, send, normalize) {
640
642
  }
641
643
  },
642
644
  onFocus(event) {
643
- if (!isInteractive)
645
+ if (!interactive)
644
646
  return;
645
647
  send({ type: "CHANNEL_INPUT.FOCUS", channel });
646
648
  event.target.select();
647
649
  },
648
650
  onBlur(event) {
649
- if (!isInteractive)
651
+ if (!interactive)
650
652
  return;
651
653
  const value2 = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
652
654
  send({ type: "CHANNEL_INPUT.BLUR", channel, value: value2, isTextField });
653
655
  },
654
656
  onKeyDown(event) {
655
- if (!isInteractive)
657
+ if (event.defaultPrevented)
658
+ return;
659
+ if (!interactive)
656
660
  return;
657
661
  if (event.key === "Enter") {
658
662
  const value2 = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
@@ -669,7 +673,7 @@ function connect(state, send, normalize) {
669
673
  },
670
674
  hiddenInputProps: normalize.input({
671
675
  type: "text",
672
- disabled: isDisabled,
676
+ disabled,
673
677
  name: state.context.name,
674
678
  id: dom.getHiddenInputId(state.context),
675
679
  style: visuallyHiddenStyle,
@@ -679,11 +683,11 @@ function connect(state, send, normalize) {
679
683
  ...parts.eyeDropperTrigger.attrs,
680
684
  type: "button",
681
685
  dir: state.context.dir,
682
- disabled: isDisabled,
683
- "data-disabled": dataAttr(isDisabled),
686
+ disabled,
687
+ "data-disabled": dataAttr(disabled),
684
688
  "aria-label": "Pick a color from the screen",
685
689
  onClick() {
686
- if (!isInteractive)
690
+ if (!interactive)
687
691
  return;
688
692
  send("EYEDROPPER.CLICK");
689
693
  }
@@ -697,15 +701,15 @@ function connect(state, send, normalize) {
697
701
  const triggerState = getSwatchTriggerState(props);
698
702
  return normalize.button({
699
703
  ...parts.swatchTrigger.attrs,
700
- disabled: triggerState.isDisabled,
704
+ disabled: triggerState.disabled,
701
705
  dir: state.context.dir,
702
706
  type: "button",
703
707
  "aria-label": `select ${triggerState.valueAsString} as the color`,
704
- "data-state": triggerState.isChecked ? "checked" : "unchecked",
708
+ "data-state": triggerState.checked ? "checked" : "unchecked",
705
709
  "data-value": triggerState.valueAsString,
706
- "data-disabled": dataAttr(triggerState.isDisabled),
710
+ "data-disabled": dataAttr(triggerState.disabled),
707
711
  onClick() {
708
- if (triggerState.isDisabled)
712
+ if (triggerState.disabled)
709
713
  return;
710
714
  send({ type: "SWATCH_TRIGGER.CLICK", value: triggerState.value });
711
715
  },
@@ -719,7 +723,7 @@ function connect(state, send, normalize) {
719
723
  return normalize.element({
720
724
  ...parts.swatchIndicator.attrs,
721
725
  dir: state.context.dir,
722
- hidden: !triggerState.isChecked
726
+ hidden: !triggerState.checked
723
727
  });
724
728
  },
725
729
  getSwatchProps(props) {
@@ -728,7 +732,7 @@ function connect(state, send, normalize) {
728
732
  return normalize.element({
729
733
  ...parts.swatch.attrs,
730
734
  dir: state.context.dir,
731
- "data-state": triggerState.isChecked ? "checked" : "unchecked",
735
+ "data-state": triggerState.checked ? "checked" : "unchecked",
732
736
  "data-value": triggerState.valueAsString,
733
737
  style: {
734
738
  position: "relative",
@@ -753,7 +757,7 @@ function connect(state, send, normalize) {
753
757
  "aria-label": "change color format",
754
758
  dir: state.context.dir,
755
759
  defaultValue: state.context.format,
756
- disabled: isDisabled,
760
+ disabled,
757
761
  onChange(event) {
758
762
  const format = assertFormat(event.currentTarget.value);
759
763
  send({ type: "FORMAT.SET", format, src: "format-select" });