@zag-js/color-picker 0.21.0 → 0.22.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
@@ -172,6 +172,16 @@ function getChannelInputValue(color, channel) {
172
172
  return color.toString("hex");
173
173
  case "css":
174
174
  return color.toString("css");
175
+ case "hue":
176
+ case "saturation":
177
+ case "lightness":
178
+ return color.toFormat("hsl").getChannelValue("lightness").toString();
179
+ case "brightness":
180
+ return color.toFormat("hsb").getChannelValue("brightness").toString();
181
+ case "red":
182
+ case "green":
183
+ case "blue":
184
+ return color.toFormat("rgb").getChannelValue(channel).toString();
175
185
  default:
176
186
  return color.getChannelValue(channel).toString();
177
187
  }
@@ -181,191 +191,21 @@ function getChannelInputRange(color, channel) {
181
191
  case "hex":
182
192
  case "css":
183
193
  return void 0;
194
+ case "hue":
195
+ case "saturation":
196
+ case "lightness":
197
+ return color.toFormat("hsl").getChannelRange(channel);
198
+ case "brightness":
199
+ return color.toFormat("hsb").getChannelRange(channel);
200
+ case "red":
201
+ case "green":
202
+ case "blue":
203
+ return color.toFormat("rgb").getChannelRange(channel);
184
204
  default:
185
205
  return color.getChannelRange(channel);
186
206
  }
187
207
  }
188
208
 
189
- // src/utils/generate-format-background.ts
190
- var generateRGB_R = (orientation, dir, zValue) => {
191
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
192
- const result = {
193
- areaStyles: {
194
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,0),rgb(${zValue},255,0))`
195
- },
196
- areaGradientStyles: {
197
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(${zValue},0,255),rgb(${zValue},255,255))`,
198
- WebkitMaskImage: maskImage,
199
- maskImage
200
- }
201
- };
202
- return result;
203
- };
204
- var generateRGB_G = (orientation, dir, zValue) => {
205
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
206
- const result = {
207
- areaStyles: {
208
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},0),rgb(255,${zValue},0))`
209
- },
210
- areaGradientStyles: {
211
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,${zValue},255),rgb(255,${zValue},255))`,
212
- WebkitMaskImage: maskImage,
213
- maskImage
214
- }
215
- };
216
- return result;
217
- };
218
- var generateRGB_B = (orientation, dir, zValue) => {
219
- const maskImage = `linear-gradient(to ${orientation[Number(!dir)]}, transparent, #000)`;
220
- const result = {
221
- areaStyles: {
222
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,0,${zValue}),rgb(255,0,${zValue}))`
223
- },
224
- areaGradientStyles: {
225
- backgroundImage: `linear-gradient(to ${orientation[Number(dir)]},rgb(0,255,${zValue}),rgb(255,255,${zValue}))`,
226
- WebkitMaskImage: maskImage,
227
- maskImage
228
- }
229
- };
230
- return result;
231
- };
232
- var generateHSL_H = (orientation, dir, zValue) => {
233
- const result = {
234
- areaStyles: {},
235
- areaGradientStyles: {
236
- background: [
237
- `linear-gradient(to ${orientation[Number(dir)]}, hsla(0,0%,0%,1) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,1) 100%)`,
238
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,50%),hsla(0,0%,50%,0))`,
239
- `hsl(${zValue}, 100%, 50%)`
240
- ].join(",")
241
- }
242
- };
243
- return result;
244
- };
245
- var generateHSL_S = (orientation, dir, alphaValue) => {
246
- const result = {
247
- areaStyles: {},
248
- areaGradientStyles: {
249
- background: [
250
- `linear-gradient(to ${orientation[Number(!dir)]}, hsla(0,0%,0%,${alphaValue}) 0%, hsla(0,0%,0%,0) 50%, hsla(0,0%,100%,0) 50%, hsla(0,0%,100%,${alphaValue}) 100%)`,
251
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
252
- "hsl(0, 0%, 50%)"
253
- ].join(",")
254
- }
255
- };
256
- return result;
257
- };
258
- var generateHSL_L = (orientation, dir, zValue) => {
259
- const result = {
260
- areaStyles: {},
261
- areaGradientStyles: {
262
- backgroundImage: [
263
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,${zValue}%),hsla(0,0%,${zValue}%,0))`,
264
- `linear-gradient(to ${orientation[Number(dir)]},hsl(0,100%,${zValue}%),hsl(60,100%,${zValue}%),hsl(120,100%,${zValue}%),hsl(180,100%,${zValue}%),hsl(240,100%,${zValue}%),hsl(300,100%,${zValue}%),hsl(360,100%,${zValue}%))`
265
- ].join(",")
266
- }
267
- };
268
- return result;
269
- };
270
- var generateHSB_H = (orientation, dir, zValue) => {
271
- const result = {
272
- areaStyles: {},
273
- areaGradientStyles: {
274
- background: [
275
- `linear-gradient(to ${orientation[Number(dir)]},hsl(0,0%,0%),hsla(0,0%,0%,0))`,
276
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,100%),hsla(0,0%,100%,0))`,
277
- `hsl(${zValue}, 100%, 50%)`
278
- ].join(",")
279
- }
280
- };
281
- return result;
282
- };
283
- var generateHSB_S = (orientation, dir, alphaValue) => {
284
- const result = {
285
- areaStyles: {},
286
- areaGradientStyles: {
287
- background: [
288
- `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,0%,${alphaValue}),hsla(0,0%,0%,0))`,
289
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
290
- `linear-gradient(to ${orientation[Number(!dir)]},hsl(0,0%,0%),hsl(0,0%,100%))`
291
- ].join(",")
292
- }
293
- };
294
- return result;
295
- };
296
- var generateHSB_B = (orientation, dir, alphaValue) => {
297
- const result = {
298
- areaStyles: {},
299
- areaGradientStyles: {
300
- background: [
301
- `linear-gradient(to ${orientation[Number(!dir)]},hsla(0,0%,100%,${alphaValue}),hsla(0,0%,100%,0))`,
302
- `linear-gradient(to ${orientation[Number(dir)]},hsla(0,100%,50%,${alphaValue}),hsla(60,100%,50%,${alphaValue}),hsla(120,100%,50%,${alphaValue}),hsla(180,100%,50%,${alphaValue}),hsla(240,100%,50%,${alphaValue}),hsla(300,100%,50%,${alphaValue}),hsla(359,100%,50%,${alphaValue}))`,
303
- "#000"
304
- ].join(",")
305
- }
306
- };
307
- return result;
308
- };
309
-
310
- // src/utils/get-color-area-gradient.ts
311
- function getColorAreaGradient(ctx, xChannel, yChannel) {
312
- const value = ctx.valueAsColor;
313
- const { zChannel } = value.getColorSpaceAxes({ xChannel, yChannel });
314
- const zValue = value.getChannelValue(zChannel);
315
- const { minValue: zMin, maxValue: zMax } = value.getChannelRange(zChannel);
316
- const orientation = ["top", ctx.dir === "rtl" ? "left" : "right"];
317
- let dir = false;
318
- let background = { areaStyles: {}, areaGradientStyles: {} };
319
- let alphaValue = (zValue - zMin) / (zMax - zMin);
320
- let isHSL = value.getColorSpace() === "hsl";
321
- switch (zChannel) {
322
- case "red": {
323
- dir = xChannel === "green";
324
- background = generateRGB_R(orientation, dir, zValue);
325
- break;
326
- }
327
- case "green": {
328
- dir = xChannel === "red";
329
- background = generateRGB_G(orientation, dir, zValue);
330
- break;
331
- }
332
- case "blue": {
333
- dir = xChannel === "red";
334
- background = generateRGB_B(orientation, dir, zValue);
335
- break;
336
- }
337
- case "hue": {
338
- dir = xChannel !== "saturation";
339
- if (isHSL) {
340
- background = generateHSL_H(orientation, dir, zValue);
341
- } else {
342
- background = generateHSB_H(orientation, dir, zValue);
343
- }
344
- break;
345
- }
346
- case "saturation": {
347
- dir = xChannel === "hue";
348
- if (isHSL) {
349
- background = generateHSL_S(orientation, dir, alphaValue);
350
- } else {
351
- background = generateHSB_S(orientation, dir, alphaValue);
352
- }
353
- break;
354
- }
355
- case "brightness": {
356
- dir = xChannel === "hue";
357
- background = generateHSB_B(orientation, dir, alphaValue);
358
- break;
359
- }
360
- case "lightness": {
361
- dir = xChannel === "hue";
362
- background = generateHSL_L(orientation, dir, zValue);
363
- break;
364
- }
365
- }
366
- return background;
367
- }
368
-
369
209
  // src/utils/get-slider-background.ts
370
210
  function getSliderBgDirection(orientation, dir) {
371
211
  if (orientation === "vertical") {
@@ -436,11 +276,16 @@ function connect(state, send, normalize) {
436
276
  },
437
277
  contentProps: normalize.element({
438
278
  ...parts.content.attrs,
439
- id: dom.getContentId(state.context)
279
+ id: dom.getContentId(state.context),
280
+ dir: state.context.dir
440
281
  }),
441
282
  getAreaProps(props) {
442
283
  const { xChannel, yChannel } = props;
443
- const { areaStyles } = getColorAreaGradient(state.context, xChannel, yChannel);
284
+ const { areaStyles } = (0, import_color_utils2.getColorAreaGradient)(state.context.valueAsColor, {
285
+ xChannel,
286
+ yChannel,
287
+ dir: state.context.dir
288
+ });
444
289
  return normalize.element({
445
290
  ...parts.area.attrs,
446
291
  id: dom.getAreaId(state.context),
@@ -465,7 +310,11 @@ function connect(state, send, normalize) {
465
310
  },
466
311
  getAreaGradientProps(props) {
467
312
  const { xChannel, yChannel } = props;
468
- const { areaGradientStyles } = getColorAreaGradient(state.context, xChannel, yChannel);
313
+ const { areaGradientStyles } = (0, import_color_utils2.getColorAreaGradient)(valueAsColor, {
314
+ xChannel,
315
+ yChannel,
316
+ dir: state.context.dir
317
+ });
469
318
  return normalize.element({
470
319
  ...parts.areaGradient.attrs,
471
320
  id: dom.getAreaGradientId(state.context),
@@ -673,22 +522,15 @@ function connect(state, send, normalize) {
673
522
  min: range?.minValue,
674
523
  max: range?.maxValue,
675
524
  step: range?.step,
676
- onFocus() {
525
+ onFocus(event) {
677
526
  send({ type: "CHANNEL_INPUT.FOCUS", channel });
678
- },
679
- onChange(event) {
680
- if (isTextField)
681
- return;
682
- const value2 = event.currentTarget.value;
683
- send({ type: "CHANNEL_INPUT.CHANGE", channel, value: value2, isTextField });
527
+ (0, import_dom_query2.raf)(() => event.target.select());
684
528
  },
685
529
  onBlur(event) {
686
530
  const value2 = event.currentTarget.value;
687
- send({ type: "CHANNEL_INPUT.BLUR", channel, value: value2, isTextField });
531
+ send({ type: "CHANNEL_INPUT.CHANGE", channel, value: value2, isTextField });
688
532
  },
689
533
  onKeyDown(event) {
690
- if (!isTextField)
691
- return;
692
534
  if (event.key === "Enter") {
693
535
  const value2 = event.currentTarget.value;
694
536
  send({ type: "CHANNEL_INPUT.CHANGE", channel, value: value2, isTextField });
@@ -750,7 +592,7 @@ function connect(state, send, normalize) {
750
592
  },
751
593
  getSwatchProps(props) {
752
594
  const { value: value2, readOnly } = props;
753
- const color = (0, import_color_utils2.normalizeColor)(value2).toFormat(valueAsColor.getColorSpace());
595
+ const color = (0, import_color_utils2.normalizeColor)(value2).toFormat(valueAsColor.getColorFormat());
754
596
  return normalize.element({
755
597
  ...parts.swatch.attrs,
756
598
  onClick() {
@@ -896,14 +738,6 @@ function machine(userContext) {
896
738
  "CHANNEL_INPUT.CHANGE": {
897
739
  actions: ["setChannelColorFromInput"]
898
740
  },
899
- "CHANNEL_INPUT.BLUR": [
900
- {
901
- guard: "isTextField",
902
- target: "idle",
903
- actions: ["setChannelColorFromInput"]
904
- },
905
- { target: "idle" }
906
- ],
907
741
  "CHANNEL_SLIDER.BLUR": {
908
742
  target: "idle"
909
743
  },
@@ -935,9 +769,6 @@ function machine(userContext) {
935
769
  }
936
770
  },
937
771
  {
938
- guards: {
939
- isTextField: (_ctx, evt) => !!evt.isTextField
940
- },
941
772
  activities: {
942
773
  trackFormControl(ctx2, _evt, { send, initialContext }) {
943
774
  const inputEl = dom.getHiddenInputEl(ctx2);
@@ -974,7 +805,7 @@ function machine(userContext) {
974
805
  const win = dom.getWin(ctx2);
975
806
  const picker = new win.EyeDropper();
976
807
  picker.open().then(({ sRGBHex }) => {
977
- const format = ctx2.valueAsColor.getColorSpace();
808
+ const format = ctx2.valueAsColor.getColorFormat();
978
809
  const color = (0, import_color_utils3.parseColor)(sRGBHex).toFormat(format);
979
810
  set.value(ctx2, color);
980
811
  ctx2.onValueChangeEnd?.({ value: ctx2.value, valueAsColor: color });
@@ -1034,14 +865,25 @@ function machine(userContext) {
1034
865
  },
1035
866
  setChannelColorFromInput(ctx2, evt) {
1036
867
  const { channel, isTextField, value } = evt;
1037
- try {
1038
- const format = ctx2.valueAsColor.getColorSpace();
1039
- const newColor = isTextField ? (0, import_color_utils3.parseColor)(value).toFormat(format) : ctx2.valueAsColor.withChannelValue(channel, value);
1040
- set.value(ctx2, newColor);
1041
- } catch {
868
+ if (channel === "alpha") {
869
+ const newColor2 = ctx2.valueAsColor.withChannelValue("alpha", value);
870
+ set.value(ctx2, newColor2);
871
+ return;
872
+ }
873
+ if (isTextField) {
874
+ const format = "hsl";
875
+ const currentAlpha = ctx2.valueAsColor.getChannelValue("alpha");
876
+ const newColor2 = (0, import_utils.tryCatch)(
877
+ () => (0, import_color_utils3.parseColor)(value).toFormat(format).withChannelValue("alpha", currentAlpha),
878
+ () => ctx2.valueAsColor
879
+ );
1042
880
  const inputEl = dom.getChannelInputEl(ctx2, channel);
1043
881
  dom.setValue(inputEl, getChannelInputValue(ctx2.valueAsColor, channel));
882
+ set.value(ctx2, newColor2);
883
+ return;
1044
884
  }
885
+ const newColor = ctx2.valueAsColor.withChannelValue(channel, value);
886
+ set.value(ctx2, newColor);
1045
887
  },
1046
888
  incrementChannel(ctx2, evt) {
1047
889
  const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);