@zag-js/color-picker 0.33.2 → 0.35.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
@@ -775,7 +775,7 @@ function assertFormat(format) {
775
775
 
776
776
  // src/color-picker.machine.ts
777
777
  import { parseColor as parseColor4 } from "@zag-js/color-utils";
778
- import { createMachine } from "@zag-js/core";
778
+ import { createMachine, guards } from "@zag-js/core";
779
779
  import { trackDismissableElement } from "@zag-js/dismissable";
780
780
  import { trackPointerMove } from "@zag-js/dom-event";
781
781
  import { raf } from "@zag-js/dom-query";
@@ -791,6 +791,7 @@ var parse = (colorString) => {
791
791
  };
792
792
 
793
793
  // src/color-picker.machine.ts
794
+ var { and } = guards;
794
795
  function machine(userContext) {
795
796
  const ctx = compact(userContext);
796
797
  return createMachine(
@@ -808,6 +809,7 @@ function machine(userContext) {
808
809
  activeChannel: null,
809
810
  activeOrientation: null,
810
811
  fieldsetDisabled: false,
812
+ restoreFocus: true,
811
813
  positioning: {
812
814
  ...ctx.positioning,
813
815
  placement: "bottom"
@@ -847,14 +849,30 @@ function machine(userContext) {
847
849
  idle: {
848
850
  tags: ["closed"],
849
851
  on: {
850
- OPEN: {
852
+ "CONTROLLED.OPEN": {
851
853
  target: "open",
852
- actions: ["setInitialFocus", "invokeOnOpen"]
853
- },
854
- "TRIGGER.CLICK": {
855
- target: "open",
856
- actions: ["setInitialFocus", "invokeOnOpen"]
854
+ actions: ["setInitialFocus"]
857
855
  },
856
+ OPEN: [
857
+ {
858
+ guard: "isOpenControlled",
859
+ actions: ["invokeOnOpen"]
860
+ },
861
+ {
862
+ target: "open",
863
+ actions: ["invokeOnOpen", "setInitialFocus"]
864
+ }
865
+ ],
866
+ "TRIGGER.CLICK": [
867
+ {
868
+ guard: "isOpenControlled",
869
+ actions: ["invokeOnOpen"]
870
+ },
871
+ {
872
+ target: "open",
873
+ actions: ["invokeOnOpen", "setInitialFocus"]
874
+ }
875
+ ],
858
876
  "CHANNEL_INPUT.FOCUS": {
859
877
  target: "focused",
860
878
  actions: ["setActiveChannel"]
@@ -864,14 +882,30 @@ function machine(userContext) {
864
882
  focused: {
865
883
  tags: ["closed", "focused"],
866
884
  on: {
867
- OPEN: {
885
+ "CONTROLLED.OPEN": {
868
886
  target: "open",
869
- actions: ["setInitialFocus", "invokeOnOpen"]
870
- },
871
- "TRIGGER.CLICK": {
872
- target: "open",
873
- actions: ["setInitialFocus", "invokeOnOpen"]
887
+ actions: ["setInitialFocus"]
874
888
  },
889
+ OPEN: [
890
+ {
891
+ guard: "isOpenControlled",
892
+ actions: ["invokeOnOpen"]
893
+ },
894
+ {
895
+ target: "open",
896
+ actions: ["invokeOnOpen", "setInitialFocus"]
897
+ }
898
+ ],
899
+ "TRIGGER.CLICK": [
900
+ {
901
+ guard: "isOpenControlled",
902
+ actions: ["invokeOnOpen"]
903
+ },
904
+ {
905
+ target: "open",
906
+ actions: ["invokeOnOpen", "setInitialFocus"]
907
+ }
908
+ ],
875
909
  "CHANNEL_INPUT.FOCUS": {
876
910
  actions: ["setActiveChannel"]
877
911
  },
@@ -888,10 +922,26 @@ function machine(userContext) {
888
922
  tags: ["open"],
889
923
  activities: ["trackPositioning", "trackDismissableElement"],
890
924
  on: {
891
- "TRIGGER.CLICK": {
892
- target: "idle",
893
- actions: ["invokeOnClose"]
894
- },
925
+ "CONTROLLED.CLOSE": [
926
+ {
927
+ guard: "shouldRestoreFocus",
928
+ target: "focused",
929
+ actions: ["setReturnFocus"]
930
+ },
931
+ {
932
+ target: "idle"
933
+ }
934
+ ],
935
+ "TRIGGER.CLICK": [
936
+ {
937
+ guard: "isOpenControlled",
938
+ actions: ["invokeOnClose"]
939
+ },
940
+ {
941
+ target: "idle",
942
+ actions: ["invokeOnClose"]
943
+ }
944
+ ],
895
945
  "AREA.POINTER_DOWN": {
896
946
  target: "open:dragging",
897
947
  actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
@@ -952,25 +1002,39 @@ function machine(userContext) {
952
1002
  actions: ["setChannelColorFromInput"]
953
1003
  },
954
1004
  INTERACT_OUTSIDE: [
1005
+ {
1006
+ guard: "isOpenControlled",
1007
+ actions: ["invokeOnClose"]
1008
+ },
955
1009
  {
956
1010
  guard: "shouldRestoreFocus",
957
1011
  target: "focused",
958
- actions: ["setReturnFocus", "invokeOnClose"]
1012
+ actions: ["invokeOnClose", "setReturnFocus"]
1013
+ },
1014
+ {
1015
+ target: "idle",
1016
+ actions: ["invokeOnClose"]
1017
+ }
1018
+ ],
1019
+ CLOSE: [
1020
+ {
1021
+ guard: "isOpenControlled",
1022
+ actions: ["invokeOnClose"]
959
1023
  },
960
1024
  {
961
1025
  target: "idle",
962
1026
  actions: ["invokeOnClose"]
963
1027
  }
964
1028
  ],
965
- CLOSE: {
966
- target: "idle",
967
- actions: ["invokeOnClose"]
968
- },
969
1029
  "SWATCH_TRIGGER.CLICK": [
1030
+ {
1031
+ guard: and("isOpenControlled", "closeOnSelect"),
1032
+ actions: ["setValue", "invokeOnClose"]
1033
+ },
970
1034
  {
971
1035
  guard: "closeOnSelect",
972
1036
  target: "focused",
973
- actions: ["setValue", "setReturnFocus", "invokeOnClose"]
1037
+ actions: ["setValue", "invokeOnClose", "setReturnFocus"]
974
1038
  },
975
1039
  {
976
1040
  actions: ["setValue"]
@@ -983,6 +1047,16 @@ function machine(userContext) {
983
1047
  exit: ["clearActiveChannel"],
984
1048
  activities: ["trackPointerMove", "disableTextSelection", "trackPositioning", "trackDismissableElement"],
985
1049
  on: {
1050
+ "CONTROLLED.CLOSE": [
1051
+ {
1052
+ guard: "shouldRestoreFocus",
1053
+ target: "focused",
1054
+ actions: ["setReturnFocus"]
1055
+ },
1056
+ {
1057
+ target: "idle"
1058
+ }
1059
+ ],
986
1060
  "AREA.POINTER_MOVE": {
987
1061
  actions: ["setAreaColorFromPoint", "focusAreaThumb"]
988
1062
  },
@@ -998,28 +1072,39 @@ function machine(userContext) {
998
1072
  actions: ["invokeOnChangeEnd"]
999
1073
  },
1000
1074
  INTERACT_OUTSIDE: [
1075
+ {
1076
+ guard: "isOpenControlled",
1077
+ actions: ["invokeOnClose"]
1078
+ },
1001
1079
  {
1002
1080
  guard: "shouldRestoreFocus",
1003
1081
  target: "focused",
1004
- actions: ["setReturnFocus", "invokeOnClose"]
1082
+ actions: ["invokeOnClose", "setReturnFocus"]
1005
1083
  },
1006
1084
  {
1007
1085
  target: "idle",
1008
1086
  actions: ["invokeOnClose"]
1009
1087
  }
1010
1088
  ],
1011
- CLOSE: {
1012
- target: "idle",
1013
- actions: ["invokeOnClose"]
1014
- }
1089
+ CLOSE: [
1090
+ {
1091
+ guard: "isOpenControlled",
1092
+ actions: ["invokeOnClose"]
1093
+ },
1094
+ {
1095
+ target: "idle",
1096
+ actions: ["invokeOnClose"]
1097
+ }
1098
+ ]
1015
1099
  }
1016
1100
  }
1017
1101
  }
1018
1102
  },
1019
1103
  {
1020
1104
  guards: {
1021
- isTargetFocusable: (_ctx, evt) => evt.restoreFocus,
1022
- closeOnSelect: (ctx2) => !!ctx2.closeOnSelect
1105
+ closeOnSelect: (ctx2) => !!ctx2.closeOnSelect,
1106
+ isOpenControlled: (ctx2) => !!ctx2["open.controlled"],
1107
+ shouldRestoreFocus: (ctx2) => !!ctx2.restoreFocus
1023
1108
  },
1024
1109
  activities: {
1025
1110
  trackPositioning(ctx2) {
@@ -1031,15 +1116,11 @@ function machine(userContext) {
1031
1116
  defer: true,
1032
1117
  onComplete(data) {
1033
1118
  ctx2.currentPlacement = data.placement;
1034
- },
1035
- onCleanup() {
1036
- ctx2.currentPlacement = void 0;
1037
1119
  }
1038
1120
  });
1039
1121
  },
1040
1122
  trackDismissableElement(ctx2, _evt, { send }) {
1041
1123
  const getContentEl = () => dom.getContentEl(ctx2);
1042
- let restoreFocus = true;
1043
1124
  return trackDismissableElement(getContentEl, {
1044
1125
  exclude: dom.getTriggerEl(ctx2),
1045
1126
  defer: true,
@@ -1047,12 +1128,12 @@ function machine(userContext) {
1047
1128
  ctx2.onInteractOutside?.(event);
1048
1129
  if (event.defaultPrevented)
1049
1130
  return;
1050
- restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1131
+ ctx2.restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1051
1132
  },
1052
1133
  onPointerDownOutside: ctx2.onPointerDownOutside,
1053
1134
  onFocusOutside: ctx2.onFocusOutside,
1054
1135
  onDismiss() {
1055
- send({ type: "INTERACT_OUTSIDE", restoreFocus });
1136
+ send({ type: "INTERACT_OUTSIDE" });
1056
1137
  }
1057
1138
  });
1058
1139
  },
@@ -1230,8 +1311,8 @@ function machine(userContext) {
1230
1311
  invokeOnClose(ctx2) {
1231
1312
  ctx2.onOpenChange?.({ open: false });
1232
1313
  },
1233
- toggleVisibility(ctx2, _evt, { send }) {
1234
- send({ type: ctx2.open ? "OPEN" : "CLOSE", src: "controlled" });
1314
+ toggleVisibility(ctx2, evt, { send }) {
1315
+ send({ type: ctx2.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt });
1235
1316
  }
1236
1317
  },
1237
1318
  compareFns: {