@destyler/color-picker 0.2.10 → 0.2.12

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
@@ -1,13 +1,10 @@
1
- import * as _destyler_anatomy0 from "@destyler/anatomy";
2
1
  import { Color, Color as Color$1, ColorAxes, ColorChannel, ColorFormat, ColorType } from "@destyler/color";
3
2
  import { PositioningOptions } from "@destyler/popper";
4
3
  import { FocusOutsideEvent, InteractOutsideEvent, InteractOutsideHandlers, PointerDownOutsideEvent } from "@destyler/dismissable";
5
- import * as _destyler_xstate0 from "@destyler/xstate";
6
4
  import { AnyEventObject, Machine, XSend, XState } from "@destyler/xstate";
7
5
  import { CommonProperties, DirectionProperty, NormalizeProps, Orientation, PropTypes, RequiredBy } from "@destyler/types";
8
-
9
6
  //#region src/anatomy.d.ts
10
- declare const anatomy: _destyler_anatomy0.AnatomyInstance<"root" | "label" | "control" | "trigger" | "positioner" | "content" | "area" | "areaThumb" | "valueText" | "areaBackground" | "channelSlider" | "channelSliderLabel" | "channelSliderTrack" | "channelSliderThumb" | "channelSliderValueText" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
7
+ export declare const anatomy: import("@destyler/anatomy").AnatomyInstance<"root" | "label" | "control" | "trigger" | "positioner" | "content" | "area" | "areaThumb" | "valueText" | "areaBackground" | "channelSlider" | "channelSliderLabel" | "channelSliderTrack" | "channelSliderThumb" | "channelSliderValueText" | "channelInput" | "transparencyGrid" | "swatchGroup" | "swatchTrigger" | "swatchIndicator" | "swatch" | "eyeDropperTrigger" | "formatTrigger" | "formatSelect">;
11
8
  //#endregion
12
9
  //#region src/types.d.ts
13
10
  type ExtendedColorChannel = ColorChannel | 'hex' | 'css';
@@ -331,12 +328,12 @@ interface MachineApi<T extends PropTypes = PropTypes> {
331
328
  }
332
329
  //#endregion
333
330
  //#region src/connect.d.ts
334
- declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
331
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
335
332
  //#endregion
336
333
  //#region src/machine.d.ts
337
- declare function machine(userContext: UserDefinedContext): _destyler_xstate0.Machine<MachineContext, MachineState, _destyler_xstate0.AnyEventObject>;
334
+ export declare function machine(userContext: UserDefinedContext): import("@destyler/xstate").Machine<MachineContext, MachineState, import("@destyler/xstate").AnyEventObject>;
338
335
  //#endregion
339
336
  //#region src/parse.d.ts
340
- declare function parse(colorString: string): Color$1;
337
+ export declare function parse(colorString: string): Color$1;
341
338
  //#endregion
342
- export { type MachineApi as Api, type AreaProps, type ChannelInputProps, type ChannelProps, type ChannelSliderProps, type Color, type ColorAxes, type ColorChannel, type ColorFormat, type ColorType, type UserDefinedContext as Context, type ElementIds, type FocusOutsideEvent, type FormatChangeDetails, type InteractOutsideEvent, type MachineState, type OpenChangeDetails, type PointerDownOutsideEvent, type PositioningOptions, type Service, type SwatchProps, type SwatchTriggerProps, type SwatchTriggerState, type TransparencyGridProps, type ValueChangeDetails, anatomy, connect, machine, parse };
339
+ export type { MachineApi as Api, AreaProps, ChannelInputProps, ChannelProps, ChannelSliderProps, Color, ColorAxes, ColorChannel, ColorFormat, ColorType, UserDefinedContext as Context, ElementIds, FocusOutsideEvent, FormatChangeDetails, InteractOutsideEvent, MachineState, OpenChangeDetails, PointerDownOutsideEvent, PositioningOptions, Service, SwatchProps, SwatchTriggerProps, SwatchTriggerState, TransparencyGridProps, ValueChangeDetails };
package/dist/index.mjs CHANGED
@@ -5,11 +5,9 @@ import { getPlacement, getPlacementStyles } from "@destyler/popper";
5
5
  import { trackDismissableElement } from "@destyler/dismissable";
6
6
  import { compact, isControlled, isPropUserProvided, resolveControllableOpen, resolveControllableProp, tryCatch, withControllableProvided } from "@destyler/utils";
7
7
  import { createMachine, guards } from "@destyler/xstate";
8
-
9
8
  //#region src/anatomy.ts
10
9
  const anatomy = createAnatomy("color-picker").parts("root", "label", "control", "trigger", "positioner", "content", "area", "areaThumb", "valueText", "areaBackground", "channelSlider", "channelSliderLabel", "channelSliderTrack", "channelSliderThumb", "channelSliderValueText", "channelInput", "transparencyGrid", "swatchGroup", "swatchTrigger", "swatchIndicator", "swatch", "eyeDropperTrigger", "formatTrigger", "formatSelect");
11
10
  const parts = anatomy.build();
12
-
13
11
  //#endregion
14
12
  //#region src/dom.ts
15
13
  const dom = createScope({
@@ -58,7 +56,6 @@ const dom = createScope({
58
56
  return [...queryAll(dom.getContentEl(ctx), "input[data-channel]"), ...queryAll(dom.getControlEl(ctx), "input[data-channel]")];
59
57
  }
60
58
  });
61
-
62
59
  //#endregion
63
60
  //#region src/utils/get-channel-display-color.ts
64
61
  function getChannelDisplayColor(color, channel) {
@@ -74,7 +71,6 @@ function getChannelDisplayColor(color, channel) {
74
71
  default: throw new Error(`Unknown color channel: ${channel}`);
75
72
  }
76
73
  }
77
-
78
74
  //#endregion
79
75
  //#region src/utils/get-channel-input-value.ts
80
76
  function getChannelValue(color, channel) {
@@ -116,7 +112,12 @@ function getChannelRange(color, channel) {
116
112
  default: return color.getChannelRange(channel);
117
113
  }
118
114
  }
119
-
115
+ //#endregion
116
+ //#region src/utils/get-resolved-color.ts
117
+ function getResolvedColor(value) {
118
+ if (value === void 0) throw new TypeError("Color picker value must be resolved before use");
119
+ return value;
120
+ }
120
121
  //#endregion
121
122
  //#region src/utils/get-slider-background.ts
122
123
  function getSliderBackgroundDirection(orientation, dir) {
@@ -140,11 +141,10 @@ function getSliderBackground(props) {
140
141
  default: throw new Error(`Unknown color channel: ${channel}`);
141
142
  }
142
143
  }
143
-
144
144
  //#endregion
145
145
  //#region src/connect.ts
146
146
  function connect(state, send, normalize) {
147
- const value = state.context.value;
147
+ const value = getResolvedColor(state.context.value);
148
148
  const areaValue = state.context.areaValue;
149
149
  const valueAsString = state.context.valueAsString;
150
150
  const disabled = state.context.isDisabled;
@@ -182,10 +182,10 @@ function connect(state, send, normalize) {
182
182
  if (nextOpen === open) return;
183
183
  send({ type: nextOpen ? "OPEN" : "CLOSE" });
184
184
  },
185
- setValue(value$1) {
185
+ setValue(value) {
186
186
  send({
187
187
  type: "VALUE.SET",
188
- value: normalizeColor(value$1),
188
+ value: normalizeColor(value),
189
189
  src: "set-color"
190
190
  });
191
191
  },
@@ -205,8 +205,8 @@ function connect(state, send, normalize) {
205
205
  format: state.context.format,
206
206
  setFormat(format) {
207
207
  send({
208
- type: "VALUE.SET",
209
- value: value.toFormat(format),
208
+ type: "FORMAT.SET",
209
+ format: assertFormat(format),
210
210
  src: "set-format"
211
211
  });
212
212
  },
@@ -460,8 +460,8 @@ function connect(state, send, normalize) {
460
460
  step
461
461
  });
462
462
  },
463
- Escape(event$1) {
464
- event$1.stopPropagation();
463
+ Escape(event) {
464
+ event.stopPropagation();
465
465
  }
466
466
  }[getEventKey(event, state.context)];
467
467
  if (exec) {
@@ -500,11 +500,12 @@ function connect(state, send, normalize) {
500
500
  if (!interactive) return;
501
501
  if (!isLeftClick(event)) return;
502
502
  if (isModifierKey(event)) return;
503
+ const point = getEventPoint(event);
503
504
  send({
504
505
  type: "CHANNEL_SLIDER.POINTER_DOWN",
505
506
  channel,
506
507
  format,
507
- point: getEventPoint(event),
508
+ point,
508
509
  id: channel,
509
510
  orientation
510
511
  });
@@ -610,6 +611,7 @@ function connect(state, send, normalize) {
610
611
  send({
611
612
  type: "CHANNEL_SLIDER.ARROW_UP",
612
613
  channel,
614
+ format,
613
615
  step
614
616
  });
615
617
  },
@@ -617,6 +619,7 @@ function connect(state, send, normalize) {
617
619
  send({
618
620
  type: "CHANNEL_SLIDER.ARROW_DOWN",
619
621
  channel,
622
+ format,
620
623
  step
621
624
  });
622
625
  },
@@ -624,6 +627,7 @@ function connect(state, send, normalize) {
624
627
  send({
625
628
  type: "CHANNEL_SLIDER.ARROW_LEFT",
626
629
  channel,
630
+ format,
627
631
  step
628
632
  });
629
633
  },
@@ -631,35 +635,42 @@ function connect(state, send, normalize) {
631
635
  send({
632
636
  type: "CHANNEL_SLIDER.ARROW_RIGHT",
633
637
  channel,
638
+ format,
634
639
  step
635
640
  });
636
641
  },
637
642
  PageUp() {
638
643
  send({
639
644
  type: "CHANNEL_SLIDER.PAGE_UP",
640
- channel
645
+ channel,
646
+ format,
647
+ step
641
648
  });
642
649
  },
643
650
  PageDown() {
644
651
  send({
645
652
  type: "CHANNEL_SLIDER.PAGE_DOWN",
646
- channel
653
+ channel,
654
+ format,
655
+ step
647
656
  });
648
657
  },
649
658
  Home() {
650
659
  send({
651
660
  type: "CHANNEL_SLIDER.HOME",
652
- channel
661
+ channel,
662
+ format
653
663
  });
654
664
  },
655
665
  End() {
656
666
  send({
657
667
  type: "CHANNEL_SLIDER.END",
658
- channel
668
+ channel,
669
+ format
659
670
  });
660
671
  },
661
- Escape(event$1) {
662
- event$1.stopPropagation();
672
+ Escape(event) {
673
+ event.stopPropagation();
663
674
  }
664
675
  }[getEventKey(event, state.context)];
665
676
  if (exec) {
@@ -692,7 +703,8 @@ function connect(state, send, normalize) {
692
703
  "step": channelRange?.step,
693
704
  onBeforeInput(event) {
694
705
  if (isTextField || !interactive) return;
695
- if (event.currentTarget.value.match(/[^0-9.]/g)) event.preventDefault();
706
+ const value = event.currentTarget.value;
707
+ if (/[^0-9.]/.test(value)) event.preventDefault();
696
708
  },
697
709
  onFocus(event) {
698
710
  if (!interactive) return;
@@ -704,10 +716,11 @@ function connect(state, send, normalize) {
704
716
  },
705
717
  onBlur(event) {
706
718
  if (!interactive) return;
719
+ const value = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
707
720
  send({
708
721
  type: "CHANNEL_INPUT.BLUR",
709
722
  channel,
710
- value: isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber,
723
+ value,
711
724
  isTextField
712
725
  });
713
726
  },
@@ -715,10 +728,11 @@ function connect(state, send, normalize) {
715
728
  if (event.defaultPrevented) return;
716
729
  if (!interactive) return;
717
730
  if (event.key === "Enter") {
731
+ const value = isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber;
718
732
  send({
719
733
  type: "CHANNEL_INPUT.CHANGE",
720
734
  channel,
721
- value: isTextField ? event.currentTarget.value : event.currentTarget.valueAsNumber,
735
+ value,
722
736
  isTextField
723
737
  });
724
738
  event.preventDefault();
@@ -854,21 +868,20 @@ const formats = [
854
868
  "hsla",
855
869
  "rgba"
856
870
  ];
857
- const formatRegex = /* @__PURE__ */ new RegExp(`^(${formats.join("|")})$`);
871
+ const formatRegex = new RegExp(`^(${formats.join("|")})$`);
858
872
  function getNextFormat(format) {
859
- return formats[formats.indexOf(format) + 1] ?? formats[0];
873
+ const index = formats.indexOf(format);
874
+ return formats[index + 1] ?? formats[0];
860
875
  }
861
876
  function assertFormat(format) {
862
- if (formatRegex.test(format)) return format;
877
+ if (typeof format === "string" && formatRegex.test(format)) return format;
863
878
  throw new Error(`Unsupported color format: ${format}`);
864
879
  }
865
-
866
880
  //#endregion
867
881
  //#region src/parse.ts
868
882
  function parse(colorString) {
869
883
  return parseColor(colorString);
870
884
  }
871
-
872
885
  //#endregion
873
886
  //#region src/machine.ts
874
887
  const { and } = guards;
@@ -878,7 +891,7 @@ const sync = {
878
891
  raf(() => {
879
892
  channelInputs.forEach((inputEl) => {
880
893
  const channel = inputEl.dataset.channel;
881
- dom.setValue(inputEl, getChannelValue(color || ctx.value, channel));
894
+ dom.setValue(inputEl, getChannelValue(getResolvedColor(color || ctx.value), channel));
882
895
  });
883
896
  });
884
897
  },
@@ -889,20 +902,26 @@ const sync = {
889
902
  });
890
903
  }
891
904
  };
905
+ function getChannelColor(ctx, channel, format) {
906
+ const value = ctx.value;
907
+ if (channel === "alpha") return value;
908
+ return format ? value.toFormat(format) : ctx.areaValue;
909
+ }
892
910
  const invoke = {
893
911
  changeEnd(ctx) {
894
- const value = ctx.value.toFormat(ctx.format);
912
+ const value = getResolvedColor(ctx.value).toFormat(ctx.format);
895
913
  ctx.onValueChangeEnd?.({
896
914
  value,
897
915
  valueAsString: ctx.valueAsString
898
916
  });
899
917
  },
900
- change(ctx) {
901
- const value = ctx.value.toFormat(ctx.format);
918
+ change(ctx, isCurrent) {
919
+ const value = getResolvedColor(ctx.value).toFormat(ctx.format);
902
920
  ctx.onValueChange?.({
903
921
  value,
904
922
  valueAsString: ctx.valueAsString
905
923
  });
924
+ if (isCurrent && !isCurrent()) return;
906
925
  dispatchInputValueEvent(dom.getHiddenInputEl(ctx), { value: ctx.valueAsString });
907
926
  },
908
927
  formatChange(ctx) {
@@ -910,8 +929,8 @@ const invoke = {
910
929
  }
911
930
  };
912
931
  const set = {
913
- value(ctx, color) {
914
- if (!color || ctx.value.isEqual(color)) return;
932
+ value(ctx, color, isCurrent) {
933
+ if (!color || getResolvedColor(ctx.value).isEqual(color)) return;
915
934
  if (isControlled(ctx, "value")) {
916
935
  const value = color.toFormat(ctx.format);
917
936
  ctx.onValueChange?.({
@@ -921,7 +940,8 @@ const set = {
921
940
  return;
922
941
  }
923
942
  ctx.value = color;
924
- invoke.change(ctx);
943
+ if (isCurrent && !isCurrent()) return;
944
+ invoke.change(ctx, isCurrent);
925
945
  },
926
946
  format(ctx, format) {
927
947
  if (ctx.format === format) return;
@@ -938,16 +958,31 @@ function machine(userContext) {
938
958
  valueProvided: isPropUserProvided(ctx, "value"),
939
959
  fallback: parse("#000000")
940
960
  });
961
+ const eyeDropperOwners = /* @__PURE__ */ new WeakMap();
962
+ const getEyeDropperOwner = (ctx) => {
963
+ let owner = eyeDropperOwners.get(ctx);
964
+ if (!owner) {
965
+ owner = {
966
+ generation: 0,
967
+ controllers: /* @__PURE__ */ new Set()
968
+ };
969
+ eyeDropperOwners.set(ctx, owner);
970
+ }
971
+ return owner;
972
+ };
973
+ const defaults = {
974
+ dir: "ltr",
975
+ value: parse("#000000"),
976
+ format: "rgba",
977
+ disabled: false,
978
+ closeOnSelect: false,
979
+ openAutoFocus: true
980
+ };
941
981
  return createMachine({
942
982
  id: "color-picker",
943
983
  initial: initialOpen ? "open" : "idle",
944
984
  context: {
945
- dir: "ltr",
946
- value: parse("#000000"),
947
- format: "rgba",
948
- disabled: false,
949
- closeOnSelect: false,
950
- openAutoFocus: true,
985
+ ...defaults,
951
986
  ...ctx,
952
987
  value: initialValue,
953
988
  activeId: null,
@@ -961,16 +996,16 @@ function machine(userContext) {
961
996
  }
962
997
  },
963
998
  computed: {
964
- isRtl: (ctx$1) => ctx$1.dir === "rtl",
965
- isDisabled: (ctx$1) => !!ctx$1.disabled || ctx$1.fieldsetDisabled,
966
- isInteractive: (ctx$1) => !(ctx$1.isDisabled || ctx$1.readOnly),
967
- valueAsString: (ctx$1) => ctx$1.value.toString(ctx$1.format),
968
- areaValue: (ctx$1) => {
969
- const format = ctx$1.format.startsWith("hsl") ? "hsla" : "hsba";
970
- return ctx$1.value.toFormat(format);
999
+ isRtl: (ctx) => ctx.dir === "rtl",
1000
+ isDisabled: (ctx) => !!ctx.disabled || ctx.fieldsetDisabled,
1001
+ isInteractive: (ctx) => !(ctx.isDisabled || ctx.readOnly),
1002
+ valueAsString: (ctx) => getResolvedColor(ctx.value).toString(ctx.format),
1003
+ areaValue: (ctx) => {
1004
+ const format = ctx.format.startsWith("hsl") ? "hsla" : "hsba";
1005
+ return getResolvedColor(ctx.value).toFormat(format);
971
1006
  }
972
1007
  },
973
- activities: ["trackFormControl"],
1008
+ activities: ["trackFormControl", "trackEyeDropper"],
974
1009
  watch: {
975
1010
  value: ["syncInputElements"],
976
1011
  format: ["syncFormatSelectElement"],
@@ -1181,44 +1216,54 @@ function machine(userContext) {
1181
1216
  }
1182
1217
  }, {
1183
1218
  guards: {
1184
- closeOnSelect: (ctx$1) => !!ctx$1.closeOnSelect,
1185
- isOpenControlled: (ctx$1) => isControlled(ctx$1, "open"),
1186
- shouldRestoreFocus: (ctx$1) => !!ctx$1.restoreFocus
1219
+ closeOnSelect: (ctx) => !!ctx.closeOnSelect,
1220
+ isOpenControlled: (ctx) => isControlled(ctx, "open"),
1221
+ shouldRestoreFocus: (ctx) => !!ctx.restoreFocus
1187
1222
  },
1188
1223
  activities: {
1189
- trackPositioning(ctx$1) {
1190
- ctx$1.currentPlacement ||= ctx$1.positioning.placement;
1191
- const anchorEl = dom.getTriggerEl(ctx$1);
1192
- const getPositionerEl = () => dom.getPositionerEl(ctx$1);
1224
+ trackEyeDropper(ctx) {
1225
+ const owner = getEyeDropperOwner(ctx);
1226
+ owner.generation++;
1227
+ return () => {
1228
+ owner.generation++;
1229
+ owner.controllers.forEach((controller) => controller.abort());
1230
+ owner.controllers.clear();
1231
+ };
1232
+ },
1233
+ trackPositioning(ctx) {
1234
+ ctx.currentPlacement ||= ctx.positioning.placement;
1235
+ const anchorEl = dom.getTriggerEl(ctx);
1236
+ const getPositionerEl = () => dom.getPositionerEl(ctx);
1193
1237
  return getPlacement(anchorEl, getPositionerEl, {
1194
- ...ctx$1.positioning,
1238
+ ...ctx.positioning,
1195
1239
  defer: true,
1196
1240
  onComplete(data) {
1197
- ctx$1.currentPlacement = data.placement;
1241
+ ctx.currentPlacement = data.placement;
1198
1242
  }
1199
1243
  });
1200
1244
  },
1201
- trackDismissableElement(ctx$1, _evt, { send }) {
1202
- const getContentEl = () => dom.getContentEl(ctx$1);
1245
+ trackDismissableElement(ctx, _evt, { send }) {
1246
+ const getContentEl = () => dom.getContentEl(ctx);
1203
1247
  return trackDismissableElement(getContentEl, {
1204
- exclude: dom.getTriggerEl(ctx$1),
1248
+ exclude: dom.getTriggerEl(ctx),
1205
1249
  defer: true,
1206
1250
  onInteractOutside(event) {
1207
- ctx$1.onInteractOutside?.(event);
1251
+ ctx.onInteractOutside?.(event);
1208
1252
  if (event.defaultPrevented) return;
1209
- ctx$1.restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1253
+ ctx.restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1210
1254
  },
1211
- onPointerDownOutside: ctx$1.onPointerDownOutside,
1212
- onFocusOutside: ctx$1.onFocusOutside,
1255
+ onPointerDownOutside: ctx.onPointerDownOutside,
1256
+ onFocusOutside: ctx.onFocusOutside,
1213
1257
  onDismiss() {
1214
1258
  send({ type: "INTERACT_OUTSIDE" });
1215
1259
  }
1216
1260
  });
1217
1261
  },
1218
- trackFormControl(ctx$1, _evt, { send, initialContext }) {
1219
- return trackFormControl(dom.getHiddenInputEl(ctx$1), {
1262
+ trackFormControl(ctx, _evt, { send, initialContext }) {
1263
+ const inputEl = dom.getHiddenInputEl(ctx);
1264
+ return trackFormControl(inputEl, {
1220
1265
  onFieldsetDisabledChange(disabled) {
1221
- ctx$1.fieldsetDisabled = disabled;
1266
+ ctx.fieldsetDisabled = disabled;
1222
1267
  },
1223
1268
  onFormReset() {
1224
1269
  send({
@@ -1229,180 +1274,195 @@ function machine(userContext) {
1229
1274
  }
1230
1275
  });
1231
1276
  },
1232
- trackPointerMove(ctx$1, evt, { send }) {
1233
- return trackPointerMove(dom.getDoc(ctx$1), {
1277
+ trackPointerMove(ctx, evt, { send }) {
1278
+ return trackPointerMove(dom.getDoc(ctx), {
1234
1279
  onPointerMove({ point }) {
1235
1280
  send({
1236
- type: ctx$1.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE",
1281
+ type: ctx.activeId === "area" ? "AREA.POINTER_MOVE" : "CHANNEL_SLIDER.POINTER_MOVE",
1237
1282
  point,
1238
1283
  format: evt.format
1239
1284
  });
1240
1285
  },
1241
1286
  onPointerUp() {
1242
- send({ type: ctx$1.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP" });
1287
+ send({ type: ctx.activeId === "area" ? "AREA.POINTER_UP" : "CHANNEL_SLIDER.POINTER_UP" });
1243
1288
  }
1244
1289
  });
1245
1290
  },
1246
- disableTextSelection(ctx$1) {
1291
+ disableTextSelection(ctx) {
1247
1292
  return disableTextSelection({
1248
- doc: dom.getDoc(ctx$1),
1249
- target: dom.getContentEl(ctx$1)
1293
+ doc: dom.getDoc(ctx),
1294
+ target: dom.getContentEl(ctx)
1250
1295
  });
1251
1296
  }
1252
1297
  },
1253
1298
  actions: {
1254
- openEyeDropper(ctx$1) {
1255
- if (!("EyeDropper" in dom.getWin(ctx$1))) return;
1256
- new (dom.getWin(ctx$1)).EyeDropper().open().then(({ sRGBHex }) => {
1257
- const format = ctx$1.value.getFormat();
1258
- const color = parseColor(sRGBHex).toFormat(format);
1259
- set.value(ctx$1, color);
1260
- ctx$1.onValueChangeEnd?.({
1261
- value: ctx$1.value,
1262
- valueAsString: ctx$1.valueAsString
1263
- });
1264
- }).catch(() => void 0);
1299
+ openEyeDropper(ctx) {
1300
+ if (!("EyeDropper" in dom.getWin(ctx))) return;
1301
+ const win = dom.getWin(ctx);
1302
+ const picker = new win.EyeDropper();
1303
+ const controller = new win.AbortController();
1304
+ const owner = getEyeDropperOwner(ctx);
1305
+ const generation = owner.generation;
1306
+ const isCurrent = () => generation === owner.generation && !controller.signal.aborted;
1307
+ owner.controllers.add(controller);
1308
+ try {
1309
+ picker.open({ signal: controller.signal }).then(({ sRGBHex }) => {
1310
+ if (!isCurrent()) return;
1311
+ const format = getResolvedColor(ctx.value).getFormat();
1312
+ const color = parseColor(sRGBHex).toFormat(format);
1313
+ set.value(ctx, color, isCurrent);
1314
+ if (!isCurrent()) return;
1315
+ ctx.onValueChangeEnd?.({
1316
+ value: getResolvedColor(ctx.value),
1317
+ valueAsString: ctx.valueAsString
1318
+ });
1319
+ }).catch(() => void 0).finally(() => owner.controllers.delete(controller));
1320
+ } catch (error) {
1321
+ owner.controllers.delete(controller);
1322
+ throw error;
1323
+ }
1265
1324
  },
1266
- setActiveChannel(ctx$1, evt) {
1267
- ctx$1.activeId = evt.id;
1268
- if (evt.channel) ctx$1.activeChannel = evt.channel;
1269
- if (evt.orientation) ctx$1.activeOrientation = evt.orientation;
1325
+ setActiveChannel(ctx, evt) {
1326
+ ctx.activeId = evt.id;
1327
+ if (evt.channel) ctx.activeChannel = evt.channel;
1328
+ if (evt.orientation) ctx.activeOrientation = evt.orientation;
1270
1329
  },
1271
- clearActiveChannel(ctx$1) {
1272
- ctx$1.activeChannel = null;
1273
- ctx$1.activeId = null;
1274
- ctx$1.activeOrientation = null;
1330
+ clearActiveChannel(ctx) {
1331
+ ctx.activeChannel = null;
1332
+ ctx.activeId = null;
1333
+ ctx.activeOrientation = null;
1275
1334
  },
1276
- setAreaColorFromPoint(ctx$1, evt) {
1277
- const normalizedValue = evt.format ? ctx$1.value.toFormat(evt.format) : ctx$1.areaValue;
1278
- const { xChannel, yChannel } = evt.channel || ctx$1.activeChannel;
1279
- const percent = dom.getAreaValueFromPoint(ctx$1, evt.point);
1335
+ setAreaColorFromPoint(ctx, evt) {
1336
+ const normalizedValue = evt.format ? getResolvedColor(ctx.value).toFormat(evt.format) : ctx.areaValue;
1337
+ const { xChannel, yChannel } = evt.channel || ctx.activeChannel;
1338
+ const percent = dom.getAreaValueFromPoint(ctx, evt.point);
1280
1339
  if (!percent) return;
1281
1340
  const xValue = normalizedValue.getChannelPercentValue(xChannel, percent.x);
1282
1341
  const yValue = normalizedValue.getChannelPercentValue(yChannel, 1 - percent.y);
1283
1342
  const color = normalizedValue.withChannelValue(xChannel, xValue).withChannelValue(yChannel, yValue);
1284
- set.value(ctx$1, color);
1343
+ set.value(ctx, color);
1285
1344
  },
1286
- setChannelColorFromPoint(ctx$1, evt) {
1287
- const channel = evt.channel || ctx$1.activeId;
1288
- const normalizedValue = evt.format ? ctx$1.value.toFormat(evt.format) : ctx$1.areaValue;
1289
- const percent = dom.getChannelSliderValueFromPoint(ctx$1, evt.point, channel);
1345
+ setChannelColorFromPoint(ctx, evt) {
1346
+ const channel = evt.channel || ctx.activeId;
1347
+ const normalizedValue = evt.format ? getResolvedColor(ctx.value).toFormat(evt.format) : ctx.areaValue;
1348
+ const percent = dom.getChannelSliderValueFromPoint(ctx, evt.point, channel);
1290
1349
  if (!percent) return;
1291
- const channelPercent = (ctx$1.activeOrientation || "horizontal") === "horizontal" ? percent.x : percent.y;
1350
+ const channelPercent = (ctx.activeOrientation || "horizontal") === "horizontal" ? percent.x : percent.y;
1292
1351
  const value = normalizedValue.getChannelPercentValue(channel, channelPercent);
1293
1352
  const color = normalizedValue.withChannelValue(channel, value);
1294
- set.value(ctx$1, color);
1353
+ set.value(ctx, color);
1295
1354
  },
1296
- setValue(ctx$1, evt) {
1297
- set.value(ctx$1, evt.value);
1355
+ setValue(ctx, evt) {
1356
+ set.value(ctx, evt.value);
1298
1357
  },
1299
- setFormat(ctx$1, evt) {
1300
- set.format(ctx$1, evt.format);
1358
+ setFormat(ctx, evt) {
1359
+ set.format(ctx, evt.format);
1301
1360
  },
1302
- syncInputElements(ctx$1) {
1303
- sync.inputs(ctx$1);
1361
+ syncInputElements(ctx) {
1362
+ sync.inputs(ctx);
1304
1363
  },
1305
- invokeOnChangeEnd(ctx$1) {
1306
- invoke.changeEnd(ctx$1);
1364
+ invokeOnChangeEnd(ctx) {
1365
+ invoke.changeEnd(ctx);
1307
1366
  },
1308
- setChannelColorFromInput(ctx$1, evt) {
1367
+ setChannelColorFromInput(ctx, evt) {
1309
1368
  const { channel, isTextField, value } = evt;
1310
- const currentAlpha = ctx$1.value.getChannelValue("alpha");
1369
+ const currentAlpha = getResolvedColor(ctx.value).getChannelValue("alpha");
1311
1370
  let color;
1312
1371
  if (channel === "alpha") {
1313
1372
  let valueAsNumber = Number.parseFloat(value);
1314
1373
  valueAsNumber = Number.isNaN(valueAsNumber) ? currentAlpha : valueAsNumber;
1315
- color = ctx$1.value.withChannelValue("alpha", valueAsNumber);
1316
- } else if (isTextField) color = tryCatch(() => parse(value).withChannelValue("alpha", currentAlpha), () => ctx$1.value);
1374
+ color = getResolvedColor(ctx.value).withChannelValue("alpha", valueAsNumber);
1375
+ } else if (isTextField) color = tryCatch(() => parse(value).withChannelValue("alpha", currentAlpha), () => getResolvedColor(ctx.value));
1317
1376
  else {
1318
- const current = ctx$1.value.toFormat(ctx$1.format);
1377
+ const current = getResolvedColor(ctx.value).toFormat(ctx.format);
1319
1378
  const valueAsNumber = Number.isNaN(value) ? current.getChannelValue(channel) : value;
1320
1379
  color = current.withChannelValue(channel, valueAsNumber);
1321
1380
  }
1322
- sync.inputs(ctx$1, color);
1323
- set.value(ctx$1, color);
1381
+ sync.inputs(ctx, color);
1382
+ set.value(ctx, color);
1324
1383
  },
1325
- incrementChannel(ctx$1, evt) {
1326
- const color = ctx$1.value.incrementChannel(evt.channel, evt.step);
1327
- set.value(ctx$1, color);
1384
+ incrementChannel(ctx, evt) {
1385
+ const color = getChannelColor(ctx, evt.channel, evt.format).incrementChannel(evt.channel, evt.step);
1386
+ set.value(ctx, color);
1328
1387
  },
1329
- decrementChannel(ctx$1, evt) {
1330
- const color = ctx$1.value.decrementChannel(evt.channel, evt.step);
1331
- set.value(ctx$1, color);
1388
+ decrementChannel(ctx, evt) {
1389
+ const color = getChannelColor(ctx, evt.channel, evt.format).decrementChannel(evt.channel, evt.step);
1390
+ set.value(ctx, color);
1332
1391
  },
1333
- incrementAreaXChannel(ctx$1, evt) {
1392
+ incrementAreaXChannel(ctx, evt) {
1334
1393
  const { xChannel } = evt.channel;
1335
- const color = ctx$1.areaValue.incrementChannel(xChannel, evt.step);
1336
- set.value(ctx$1, color);
1394
+ const color = ctx.areaValue.incrementChannel(xChannel, evt.step);
1395
+ set.value(ctx, color);
1337
1396
  },
1338
- decrementAreaXChannel(ctx$1, evt) {
1397
+ decrementAreaXChannel(ctx, evt) {
1339
1398
  const { xChannel } = evt.channel;
1340
- const color = ctx$1.areaValue.decrementChannel(xChannel, evt.step);
1341
- set.value(ctx$1, color);
1399
+ const color = ctx.areaValue.decrementChannel(xChannel, evt.step);
1400
+ set.value(ctx, color);
1342
1401
  },
1343
- incrementAreaYChannel(ctx$1, evt) {
1402
+ incrementAreaYChannel(ctx, evt) {
1344
1403
  const { yChannel } = evt.channel;
1345
- const color = ctx$1.areaValue.incrementChannel(yChannel, evt.step);
1346
- set.value(ctx$1, color);
1404
+ const color = ctx.areaValue.incrementChannel(yChannel, evt.step);
1405
+ set.value(ctx, color);
1347
1406
  },
1348
- decrementAreaYChannel(ctx$1, evt) {
1407
+ decrementAreaYChannel(ctx, evt) {
1349
1408
  const { yChannel } = evt.channel;
1350
- const color = ctx$1.areaValue.decrementChannel(yChannel, evt.step);
1351
- set.value(ctx$1, color);
1409
+ const color = ctx.areaValue.decrementChannel(yChannel, evt.step);
1410
+ set.value(ctx, color);
1352
1411
  },
1353
- setChannelToMax(ctx$1, evt) {
1354
- const range = ctx$1.value.getChannelRange(evt.channel);
1355
- const color = ctx$1.value.withChannelValue(evt.channel, range.maxValue);
1356
- set.value(ctx$1, color);
1412
+ setChannelToMax(ctx, evt) {
1413
+ const value = getChannelColor(ctx, evt.channel, evt.format);
1414
+ const range = value.getChannelRange(evt.channel);
1415
+ const color = value.withChannelValue(evt.channel, range.maxValue);
1416
+ set.value(ctx, color);
1357
1417
  },
1358
- setChannelToMin(ctx$1, evt) {
1359
- const range = ctx$1.value.getChannelRange(evt.channel);
1360
- const color = ctx$1.value.withChannelValue(evt.channel, range.minValue);
1361
- set.value(ctx$1, color);
1418
+ setChannelToMin(ctx, evt) {
1419
+ const value = getChannelColor(ctx, evt.channel, evt.format);
1420
+ const range = value.getChannelRange(evt.channel);
1421
+ const color = value.withChannelValue(evt.channel, range.minValue);
1422
+ set.value(ctx, color);
1362
1423
  },
1363
- focusAreaThumb(ctx$1) {
1424
+ focusAreaThumb(ctx) {
1364
1425
  raf(() => {
1365
- dom.getAreaThumbEl(ctx$1)?.focus({ preventScroll: true });
1426
+ dom.getAreaThumbEl(ctx)?.focus({ preventScroll: true });
1366
1427
  });
1367
1428
  },
1368
- focusChannelThumb(ctx$1, evt) {
1429
+ focusChannelThumb(ctx, evt) {
1369
1430
  raf(() => {
1370
- dom.getChannelSliderThumbEl(ctx$1, evt.channel)?.focus({ preventScroll: true });
1431
+ dom.getChannelSliderThumbEl(ctx, evt.channel)?.focus({ preventScroll: true });
1371
1432
  });
1372
1433
  },
1373
- setInitialFocus(ctx$1) {
1374
- if (!ctx$1.openAutoFocus) return;
1434
+ setInitialFocus(ctx) {
1435
+ if (!ctx.openAutoFocus) return;
1375
1436
  raf(() => {
1376
1437
  getInitialFocus({
1377
- root: dom.getContentEl(ctx$1),
1378
- getInitialEl: ctx$1.initialFocusEl
1438
+ root: dom.getContentEl(ctx),
1439
+ getInitialEl: ctx.initialFocusEl
1379
1440
  })?.focus({ preventScroll: true });
1380
1441
  });
1381
1442
  },
1382
- setReturnFocus(ctx$1) {
1443
+ setReturnFocus(ctx) {
1383
1444
  raf(() => {
1384
- dom.getTriggerEl(ctx$1)?.focus({ preventScroll: true });
1445
+ dom.getTriggerEl(ctx)?.focus({ preventScroll: true });
1385
1446
  });
1386
1447
  },
1387
- syncFormatSelectElement(ctx$1) {
1388
- sync.formatSelect(ctx$1);
1448
+ syncFormatSelectElement(ctx) {
1449
+ sync.formatSelect(ctx);
1389
1450
  },
1390
- invokeOnOpen(ctx$1) {
1391
- ctx$1.onOpenChange?.({ open: true });
1451
+ invokeOnOpen(ctx) {
1452
+ ctx.onOpenChange?.({ open: true });
1392
1453
  },
1393
- invokeOnClose(ctx$1) {
1394
- ctx$1.onOpenChange?.({ open: false });
1454
+ invokeOnClose(ctx) {
1455
+ ctx.onOpenChange?.({ open: false });
1395
1456
  },
1396
- toggleVisibility(ctx$1, evt, { send }) {
1457
+ toggleVisibility(ctx, evt, { send }) {
1397
1458
  send({
1398
- type: ctx$1.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
1459
+ type: ctx.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE",
1399
1460
  previousEvent: evt
1400
1461
  });
1401
1462
  }
1402
1463
  },
1403
- compareFns: { value: (a, b) => a.isEqual(b) }
1464
+ compareFns: { value: (a, b) => getResolvedColor(a).isEqual(getResolvedColor(b)) }
1404
1465
  });
1405
1466
  }
1406
-
1407
1467
  //#endregion
1408
- export { anatomy, connect, machine, parse };
1468
+ export { anatomy, connect, machine, parse };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@destyler/color-picker",
3
3
  "type": "module",
4
- "version": "0.2.10",
4
+ "version": "0.2.12",
5
5
  "description": "a component that allows users to select a color from a color picker.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
@@ -22,14 +22,14 @@
22
22
  "dist"
23
23
  ],
24
24
  "dependencies": {
25
- "@destyler/anatomy": "0.2.10",
26
- "@destyler/color": "0.2.10",
27
- "@destyler/dom": "0.2.10",
28
- "@destyler/dismissable": "0.2.10",
29
- "@destyler/popper": "0.2.10",
30
- "@destyler/types": "0.2.10",
31
- "@destyler/utils": "0.2.10",
32
- "@destyler/xstate": "0.2.10"
25
+ "@destyler/anatomy": "0.2.12",
26
+ "@destyler/color": "0.2.12",
27
+ "@destyler/dismissable": "0.2.12",
28
+ "@destyler/dom": "0.2.12",
29
+ "@destyler/popper": "0.2.12",
30
+ "@destyler/types": "0.2.12",
31
+ "@destyler/utils": "0.2.12",
32
+ "@destyler/xstate": "0.2.12"
33
33
  },
34
34
  "publishConfig": {
35
35
  "access": "public"