@zag-js/color-picker 0.56.1 → 0.57.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.d.mts CHANGED
@@ -9,7 +9,7 @@ import { StateMachine } from '@zag-js/core';
9
9
  import { PositioningOptions } from '@zag-js/popper';
10
10
  export { PositioningOptions } from '@zag-js/popper';
11
11
 
12
- declare const anatomy: _zag_js_anatomy.Anatomy<"area" | "label" | "root" | "content" | "control" | "trigger" | "positioner" | "areaThumb" | "areaBackground" | "channelSlider" | "channelSliderTrack" | "channelSliderThumb" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
12
+ declare const anatomy: _zag_js_anatomy.Anatomy<"area" | "label" | "root" | "content" | "control" | "trigger" | "positioner" | "areaThumb" | "areaBackground" | "channelSlider" | "channelSliderLabel" | "channelSliderTrack" | "channelSliderThumb" | "channelSliderValueText" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
13
13
 
14
14
  type ExtendedColorChannel = ColorChannel | "hex" | "css";
15
15
  interface EyeDropper {
@@ -157,6 +157,9 @@ interface ChannelProps {
157
157
  channel: ColorChannel;
158
158
  orientation?: Orientation;
159
159
  }
160
+ interface ChannelSliderProps extends ChannelProps {
161
+ format?: ColorFormat;
162
+ }
160
163
  interface ChannelInputProps {
161
164
  channel: ExtendedColorChannel;
162
165
  orientation?: Orientation;
@@ -219,6 +222,10 @@ interface MachineApi<T extends PropTypes = PropTypes> {
219
222
  * Function to set the color value
220
223
  */
221
224
  getChannelValue(channel: ColorChannel): string;
225
+ /**
226
+ * Function to get the formatted and localized value of a specific channel
227
+ */
228
+ getChannelValueText(channel: ColorChannel, locale: string): string;
222
229
  /**
223
230
  * Function to set the color value of a specific channel
224
231
  */
@@ -253,10 +260,12 @@ interface MachineApi<T extends PropTypes = PropTypes> {
253
260
  getAreaProps(props?: AreaProps): T["element"];
254
261
  getAreaBackgroundProps(props?: AreaProps): T["element"];
255
262
  getAreaThumbProps(props?: AreaProps): T["element"];
256
- getChannelSliderProps(props: ChannelProps): T["element"];
257
- getChannelSliderTrackProps(props: ChannelProps): T["element"];
258
- getChannelSliderThumbProps(props: ChannelProps): T["element"];
259
263
  getChannelInputProps(props: ChannelInputProps): T["input"];
264
+ getChannelSliderProps(props: ChannelSliderProps): T["element"];
265
+ getChannelSliderTrackProps(props: ChannelSliderProps): T["element"];
266
+ getChannelSliderThumbProps(props: ChannelSliderProps): T["element"];
267
+ getChannelSliderLabelProps(props: ChannelProps): T["element"];
268
+ getChannelSliderValueTextProps(props: ChannelProps): T["element"];
260
269
  getTransparencyGridProps(props?: TransparencyGridProps): T["element"];
261
270
  getEyeDropperTriggerProps(): T["button"];
262
271
  getSwatchGroupProps(): T["element"];
@@ -274,4 +283,4 @@ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<
274
283
 
275
284
  declare const parse: (colorString: string) => Color;
276
285
 
277
- export { type MachineApi as Api, type AreaProps, type ChannelInputProps, type ChannelProps, type UserDefinedContext as Context, type ElementIds, type FormatChangeDetails, type OpenChangeDetails, type SwatchProps, type SwatchTriggerProps, type SwatchTriggerState, type TransparencyGridProps, type ValueChangeDetails, anatomy, connect, machine, parse };
286
+ export { type MachineApi as Api, type AreaProps, type ChannelInputProps, type ChannelProps, type ChannelSliderProps, type UserDefinedContext as Context, type ElementIds, type FormatChangeDetails, type OpenChangeDetails, type SwatchProps, type SwatchTriggerProps, type SwatchTriggerState, type TransparencyGridProps, type ValueChangeDetails, anatomy, connect, machine, parse };
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ import { StateMachine } from '@zag-js/core';
9
9
  import { PositioningOptions } from '@zag-js/popper';
10
10
  export { PositioningOptions } from '@zag-js/popper';
11
11
 
12
- declare const anatomy: _zag_js_anatomy.Anatomy<"area" | "label" | "root" | "content" | "control" | "trigger" | "positioner" | "areaThumb" | "areaBackground" | "channelSlider" | "channelSliderTrack" | "channelSliderThumb" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
12
+ declare const anatomy: _zag_js_anatomy.Anatomy<"area" | "label" | "root" | "content" | "control" | "trigger" | "positioner" | "areaThumb" | "areaBackground" | "channelSlider" | "channelSliderLabel" | "channelSliderTrack" | "channelSliderThumb" | "channelSliderValueText" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
13
13
 
14
14
  type ExtendedColorChannel = ColorChannel | "hex" | "css";
15
15
  interface EyeDropper {
@@ -157,6 +157,9 @@ interface ChannelProps {
157
157
  channel: ColorChannel;
158
158
  orientation?: Orientation;
159
159
  }
160
+ interface ChannelSliderProps extends ChannelProps {
161
+ format?: ColorFormat;
162
+ }
160
163
  interface ChannelInputProps {
161
164
  channel: ExtendedColorChannel;
162
165
  orientation?: Orientation;
@@ -219,6 +222,10 @@ interface MachineApi<T extends PropTypes = PropTypes> {
219
222
  * Function to set the color value
220
223
  */
221
224
  getChannelValue(channel: ColorChannel): string;
225
+ /**
226
+ * Function to get the formatted and localized value of a specific channel
227
+ */
228
+ getChannelValueText(channel: ColorChannel, locale: string): string;
222
229
  /**
223
230
  * Function to set the color value of a specific channel
224
231
  */
@@ -253,10 +260,12 @@ interface MachineApi<T extends PropTypes = PropTypes> {
253
260
  getAreaProps(props?: AreaProps): T["element"];
254
261
  getAreaBackgroundProps(props?: AreaProps): T["element"];
255
262
  getAreaThumbProps(props?: AreaProps): T["element"];
256
- getChannelSliderProps(props: ChannelProps): T["element"];
257
- getChannelSliderTrackProps(props: ChannelProps): T["element"];
258
- getChannelSliderThumbProps(props: ChannelProps): T["element"];
259
263
  getChannelInputProps(props: ChannelInputProps): T["input"];
264
+ getChannelSliderProps(props: ChannelSliderProps): T["element"];
265
+ getChannelSliderTrackProps(props: ChannelSliderProps): T["element"];
266
+ getChannelSliderThumbProps(props: ChannelSliderProps): T["element"];
267
+ getChannelSliderLabelProps(props: ChannelProps): T["element"];
268
+ getChannelSliderValueTextProps(props: ChannelProps): T["element"];
260
269
  getTransparencyGridProps(props?: TransparencyGridProps): T["element"];
261
270
  getEyeDropperTriggerProps(): T["button"];
262
271
  getSwatchGroupProps(): T["element"];
@@ -274,4 +283,4 @@ declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<
274
283
 
275
284
  declare const parse: (colorString: string) => Color;
276
285
 
277
- export { type MachineApi as Api, type AreaProps, type ChannelInputProps, type ChannelProps, type UserDefinedContext as Context, type ElementIds, type FormatChangeDetails, type OpenChangeDetails, type SwatchProps, type SwatchTriggerProps, type SwatchTriggerState, type TransparencyGridProps, type ValueChangeDetails, anatomy, connect, machine, parse };
286
+ export { type MachineApi as Api, type AreaProps, type ChannelInputProps, type ChannelProps, type ChannelSliderProps, type UserDefinedContext as Context, type ElementIds, type FormatChangeDetails, type OpenChangeDetails, type SwatchProps, type SwatchTriggerProps, type SwatchTriggerState, type TransparencyGridProps, type ValueChangeDetails, anatomy, connect, machine, parse };
package/dist/index.js CHANGED
@@ -40,8 +40,10 @@ var anatomy = (0, import_anatomy.createAnatomy)("color-picker", [
40
40
  "areaThumb",
41
41
  "areaBackground",
42
42
  "channelSlider",
43
+ "channelSliderLabel",
43
44
  "channelSliderTrack",
44
45
  "channelSliderThumb",
46
+ "channelSliderValueText",
45
47
  "channelInput",
46
48
  "transparencyGrid",
47
49
  "swatchGroup",
@@ -279,6 +281,9 @@ function connect(state, send, normalize) {
279
281
  getChannelValue(channel) {
280
282
  return getChannelValue(value, channel);
281
283
  },
284
+ getChannelValueText(channel, locale) {
285
+ return value.formatChannelValue(channel, locale);
286
+ },
282
287
  setChannelValue(channel, channelValue) {
283
288
  const color = value.withChannelValue(channel, channelValue);
284
289
  send({ type: "VALUE.SET", value: color, src: "set-channel" });
@@ -512,7 +517,7 @@ function connect(state, send, normalize) {
512
517
  });
513
518
  },
514
519
  getChannelSliderProps(props) {
515
- const { orientation = "horizontal", channel } = props;
520
+ const { orientation = "horizontal", channel, format } = props;
516
521
  return normalize.element({
517
522
  ...parts.channelSlider.attrs,
518
523
  "data-channel": channel,
@@ -523,7 +528,7 @@ function connect(state, send, normalize) {
523
528
  if (!(0, import_dom_event2.isLeftClick)(event)) return;
524
529
  if ((0, import_dom_event2.isModifierKey)(event)) return;
525
530
  const point = (0, import_dom_event2.getEventPoint)(event);
526
- send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, point, id: channel, orientation });
531
+ send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, format, point, id: channel, orientation });
527
532
  event.preventDefault();
528
533
  },
529
534
  style: {
@@ -533,7 +538,8 @@ function connect(state, send, normalize) {
533
538
  });
534
539
  },
535
540
  getChannelSliderTrackProps(props) {
536
- const { orientation = "horizontal", channel } = props;
541
+ const { orientation = "horizontal", channel, format } = props;
542
+ const normalizedValue = format ? value.toFormat(format) : areaValue;
537
543
  return normalize.element({
538
544
  ...parts.channelSliderTrack.attrs,
539
545
  id: dom.getChannelSliderId(state.context, channel),
@@ -547,16 +553,40 @@ function connect(state, send, normalize) {
547
553
  orientation,
548
554
  channel,
549
555
  dir: state.context.dir,
550
- value: areaValue
556
+ value: normalizedValue
551
557
  })
552
558
  }
553
559
  });
554
560
  },
561
+ getChannelSliderLabelProps(props) {
562
+ const { channel } = props;
563
+ return normalize.element({
564
+ ...parts.channelSliderLabel.attrs,
565
+ "data-channel": channel,
566
+ onClick(event) {
567
+ if (!interactive) return;
568
+ event.preventDefault();
569
+ const thumbId = dom.getChannelSliderThumbId(state.context, channel);
570
+ dom.getById(state.context, thumbId)?.focus({ preventScroll: true });
571
+ },
572
+ style: {
573
+ userSelect: "none",
574
+ WebkitUserSelect: "none"
575
+ }
576
+ });
577
+ },
578
+ getChannelSliderValueTextProps(props) {
579
+ return normalize.element({
580
+ ...parts.channelSliderValueText.attrs,
581
+ "data-channel": props.channel
582
+ });
583
+ },
555
584
  getChannelSliderThumbProps(props) {
556
- const { orientation = "horizontal", channel } = props;
557
- const { minValue, maxValue, step: stepValue } = areaValue.getChannelRange(channel);
558
- const channelValue = areaValue.getChannelValue(channel);
559
- const offset = (channelValue - minValue) / (maxValue - minValue);
585
+ const { orientation = "horizontal", channel, format } = props;
586
+ const normalizedValue = format ? value.toFormat(format) : areaValue;
587
+ const channelRange = normalizedValue.getChannelRange(channel);
588
+ const channelValue = normalizedValue.getChannelValue(channel);
589
+ const offset = (channelValue - channelRange.minValue) / (channelRange.maxValue - channelRange.minValue);
560
590
  const placementStyles = orientation === "horizontal" ? { left: `${offset * 100}%`, top: "50%" } : { top: `${offset * 100}%`, left: "50%" };
561
591
  return normalize.element({
562
592
  ...parts.channelSliderThumb.attrs,
@@ -569,8 +599,8 @@ function connect(state, send, normalize) {
569
599
  "data-orientation": orientation,
570
600
  "aria-disabled": (0, import_dom_query2.dataAttr)(disabled),
571
601
  "aria-orientation": orientation,
572
- "aria-valuemax": maxValue,
573
- "aria-valuemin": minValue,
602
+ "aria-valuemax": channelRange.maxValue,
603
+ "aria-valuemin": channelRange.minValue,
574
604
  "aria-valuenow": channelValue,
575
605
  "aria-valuetext": `${channel} ${channelValue}`,
576
606
  style: {
@@ -586,7 +616,7 @@ function connect(state, send, normalize) {
586
616
  onKeyDown(event) {
587
617
  if (event.defaultPrevented) return;
588
618
  if (!interactive) return;
589
- const step = (0, import_dom_event2.getEventStep)(event) * stepValue;
619
+ const step = (0, import_dom_event2.getEventStep)(event) * channelRange.step;
590
620
  const keyMap = {
591
621
  ArrowUp() {
592
622
  send({ type: "CHANNEL_SLIDER.ARROW_UP", channel, step });
@@ -627,7 +657,7 @@ function connect(state, send, normalize) {
627
657
  getChannelInputProps(props) {
628
658
  const { channel } = props;
629
659
  const isTextField = channel === "hex" || channel === "css";
630
- const range = getChannelRange(value, channel);
660
+ const channelRange = getChannelRange(value, channel);
631
661
  return normalize.input({
632
662
  ...parts.channelInput.attrs,
633
663
  dir: state.context.dir,
@@ -640,9 +670,9 @@ function connect(state, send, normalize) {
640
670
  "data-disabled": (0, import_dom_query2.dataAttr)(disabled),
641
671
  readOnly: state.context.readOnly,
642
672
  defaultValue: getChannelValue(value, channel),
643
- min: range?.minValue,
644
- max: range?.maxValue,
645
- step: range?.step,
673
+ min: channelRange?.minValue,
674
+ max: channelRange?.maxValue,
675
+ step: channelRange?.step,
646
676
  onBeforeInput(event) {
647
677
  if (isTextField || !interactive) return;
648
678
  const value2 = event.currentTarget.value;
@@ -972,22 +1002,22 @@ function machine(userContext) {
972
1002
  actions: ["setActiveChannel"]
973
1003
  },
974
1004
  "AREA.ARROW_LEFT": {
975
- actions: ["decrementXChannel"]
1005
+ actions: ["decrementAreaXChannel"]
976
1006
  },
977
1007
  "AREA.ARROW_RIGHT": {
978
- actions: ["incrementXChannel"]
1008
+ actions: ["incrementAreaXChannel"]
979
1009
  },
980
1010
  "AREA.ARROW_UP": {
981
- actions: ["incrementYChannel"]
1011
+ actions: ["incrementAreaYChannel"]
982
1012
  },
983
1013
  "AREA.ARROW_DOWN": {
984
- actions: ["decrementYChannel"]
1014
+ actions: ["decrementAreaYChannel"]
985
1015
  },
986
1016
  "AREA.PAGE_UP": {
987
- actions: ["incrementXChannel"]
1017
+ actions: ["incrementAreaXChannel"]
988
1018
  },
989
1019
  "AREA.PAGE_DOWN": {
990
- actions: ["decrementXChannel"]
1020
+ actions: ["decrementAreaXChannel"]
991
1021
  },
992
1022
  "CHANNEL_SLIDER.ARROW_LEFT": {
993
1023
  actions: ["decrementChannel"]
@@ -1162,11 +1192,11 @@ function machine(userContext) {
1162
1192
  }
1163
1193
  });
1164
1194
  },
1165
- trackPointerMove(ctx2, _evt, { send }) {
1195
+ trackPointerMove(ctx2, evt, { send }) {
1166
1196
  return (0, import_dom_event3.trackPointerMove)(dom.getDoc(ctx2), {
1167
1197
  onPointerMove({ point }) {
1168
1198
  const type = ctx2.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE";
1169
- send({ type, point });
1199
+ send({ type, point, format: evt.format });
1170
1200
  },
1171
1201
  onPointerUp() {
1172
1202
  const type = ctx2.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP";
@@ -1202,22 +1232,24 @@ function machine(userContext) {
1202
1232
  ctx2.activeOrientation = null;
1203
1233
  },
1204
1234
  setAreaColorFromPoint(ctx2, evt) {
1235
+ const normalizedValue = evt.format ? ctx2.value.toFormat(evt.format) : ctx2.areaValue;
1205
1236
  const { xChannel, yChannel } = evt.channel || ctx2.activeChannel;
1206
1237
  const percent = dom.getAreaValueFromPoint(ctx2, evt.point);
1207
1238
  if (!percent) return;
1208
- const xValue = ctx2.areaValue.getChannelPercentValue(xChannel, percent.x);
1209
- const yValue = ctx2.areaValue.getChannelPercentValue(yChannel, 1 - percent.y);
1210
- const color = ctx2.areaValue.withChannelValue(xChannel, xValue).withChannelValue(yChannel, yValue);
1239
+ const xValue = normalizedValue.getChannelPercentValue(xChannel, percent.x);
1240
+ const yValue = normalizedValue.getChannelPercentValue(yChannel, 1 - percent.y);
1241
+ const color = normalizedValue.withChannelValue(xChannel, xValue).withChannelValue(yChannel, yValue);
1211
1242
  set.value(ctx2, color);
1212
1243
  },
1213
1244
  setChannelColorFromPoint(ctx2, evt) {
1214
1245
  const channel = evt.channel || ctx2.activeId;
1246
+ const normalizedValue = evt.format ? ctx2.value.toFormat(evt.format) : ctx2.areaValue;
1215
1247
  const percent = dom.getChannelSliderValueFromPoint(ctx2, evt.point, channel);
1216
1248
  if (!percent) return;
1217
1249
  const orientation = ctx2.activeOrientation || "horizontal";
1218
1250
  const channelPercent = orientation === "horizontal" ? percent.x : percent.y;
1219
- const value = ctx2.areaValue.getChannelPercentValue(channel, channelPercent);
1220
- const color = ctx2.areaValue.withChannelValue(channel, value);
1251
+ const value = normalizedValue.getChannelPercentValue(channel, channelPercent);
1252
+ const color = normalizedValue.withChannelValue(channel, value);
1221
1253
  set.value(ctx2, color);
1222
1254
  },
1223
1255
  setValue(ctx2, evt) {
@@ -1261,22 +1293,22 @@ function machine(userContext) {
1261
1293
  const color = ctx2.value.decrementChannel(evt.channel, evt.step);
1262
1294
  set.value(ctx2, color);
1263
1295
  },
1264
- incrementXChannel(ctx2, evt) {
1296
+ incrementAreaXChannel(ctx2, evt) {
1265
1297
  const { xChannel } = evt.channel;
1266
1298
  const color = ctx2.areaValue.incrementChannel(xChannel, evt.step);
1267
1299
  set.value(ctx2, color);
1268
1300
  },
1269
- decrementXChannel(ctx2, evt) {
1301
+ decrementAreaXChannel(ctx2, evt) {
1270
1302
  const { xChannel } = evt.channel;
1271
1303
  const color = ctx2.areaValue.decrementChannel(xChannel, evt.step);
1272
1304
  set.value(ctx2, color);
1273
1305
  },
1274
- incrementYChannel(ctx2, evt) {
1306
+ incrementAreaYChannel(ctx2, evt) {
1275
1307
  const { yChannel } = evt.channel;
1276
1308
  const color = ctx2.areaValue.incrementChannel(yChannel, evt.step);
1277
1309
  set.value(ctx2, color);
1278
1310
  },
1279
- decrementYChannel(ctx2, evt) {
1311
+ decrementAreaYChannel(ctx2, evt) {
1280
1312
  const { yChannel } = evt.channel;
1281
1313
  const color = ctx2.areaValue.decrementChannel(yChannel, evt.step);
1282
1314
  set.value(ctx2, color);