@zag-js/color-picker 0.34.0 → 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.d.mts CHANGED
@@ -84,6 +84,10 @@ interface PublicContext extends CommonProperties, InteractOutsideHandlers {
84
84
  * Whether the color picker is open
85
85
  */
86
86
  open?: boolean;
87
+ /**
88
+ * Whether the color picker open state is controlled by the user
89
+ */
90
+ "open.controlled"?: boolean;
87
91
  /**
88
92
  * The color format to use
89
93
  */
package/dist/index.d.ts CHANGED
@@ -84,6 +84,10 @@ interface PublicContext extends CommonProperties, InteractOutsideHandlers {
84
84
  * Whether the color picker is open
85
85
  */
86
86
  open?: boolean;
87
+ /**
88
+ * Whether the color picker open state is controlled by the user
89
+ */
90
+ "open.controlled"?: boolean;
87
91
  /**
88
92
  * The color format to use
89
93
  */
package/dist/index.js CHANGED
@@ -813,6 +813,7 @@ var parse = (colorString) => {
813
813
  };
814
814
 
815
815
  // src/color-picker.machine.ts
816
+ var { and } = import_core.guards;
816
817
  function machine(userContext) {
817
818
  const ctx = (0, import_utils2.compact)(userContext);
818
819
  return (0, import_core.createMachine)(
@@ -830,6 +831,7 @@ function machine(userContext) {
830
831
  activeChannel: null,
831
832
  activeOrientation: null,
832
833
  fieldsetDisabled: false,
834
+ restoreFocus: true,
833
835
  positioning: {
834
836
  ...ctx.positioning,
835
837
  placement: "bottom"
@@ -869,14 +871,30 @@ function machine(userContext) {
869
871
  idle: {
870
872
  tags: ["closed"],
871
873
  on: {
872
- OPEN: {
874
+ "CONTROLLED.OPEN": {
873
875
  target: "open",
874
- actions: ["setInitialFocus", "invokeOnOpen"]
875
- },
876
- "TRIGGER.CLICK": {
877
- target: "open",
878
- actions: ["setInitialFocus", "invokeOnOpen"]
876
+ actions: ["setInitialFocus"]
879
877
  },
878
+ OPEN: [
879
+ {
880
+ guard: "isOpenControlled",
881
+ actions: ["invokeOnOpen"]
882
+ },
883
+ {
884
+ target: "open",
885
+ actions: ["invokeOnOpen", "setInitialFocus"]
886
+ }
887
+ ],
888
+ "TRIGGER.CLICK": [
889
+ {
890
+ guard: "isOpenControlled",
891
+ actions: ["invokeOnOpen"]
892
+ },
893
+ {
894
+ target: "open",
895
+ actions: ["invokeOnOpen", "setInitialFocus"]
896
+ }
897
+ ],
880
898
  "CHANNEL_INPUT.FOCUS": {
881
899
  target: "focused",
882
900
  actions: ["setActiveChannel"]
@@ -886,14 +904,30 @@ function machine(userContext) {
886
904
  focused: {
887
905
  tags: ["closed", "focused"],
888
906
  on: {
889
- OPEN: {
907
+ "CONTROLLED.OPEN": {
890
908
  target: "open",
891
- actions: ["setInitialFocus", "invokeOnOpen"]
892
- },
893
- "TRIGGER.CLICK": {
894
- target: "open",
895
- actions: ["setInitialFocus", "invokeOnOpen"]
909
+ actions: ["setInitialFocus"]
896
910
  },
911
+ OPEN: [
912
+ {
913
+ guard: "isOpenControlled",
914
+ actions: ["invokeOnOpen"]
915
+ },
916
+ {
917
+ target: "open",
918
+ actions: ["invokeOnOpen", "setInitialFocus"]
919
+ }
920
+ ],
921
+ "TRIGGER.CLICK": [
922
+ {
923
+ guard: "isOpenControlled",
924
+ actions: ["invokeOnOpen"]
925
+ },
926
+ {
927
+ target: "open",
928
+ actions: ["invokeOnOpen", "setInitialFocus"]
929
+ }
930
+ ],
897
931
  "CHANNEL_INPUT.FOCUS": {
898
932
  actions: ["setActiveChannel"]
899
933
  },
@@ -910,10 +944,26 @@ function machine(userContext) {
910
944
  tags: ["open"],
911
945
  activities: ["trackPositioning", "trackDismissableElement"],
912
946
  on: {
913
- "TRIGGER.CLICK": {
914
- target: "idle",
915
- actions: ["invokeOnClose"]
916
- },
947
+ "CONTROLLED.CLOSE": [
948
+ {
949
+ guard: "shouldRestoreFocus",
950
+ target: "focused",
951
+ actions: ["setReturnFocus"]
952
+ },
953
+ {
954
+ target: "idle"
955
+ }
956
+ ],
957
+ "TRIGGER.CLICK": [
958
+ {
959
+ guard: "isOpenControlled",
960
+ actions: ["invokeOnClose"]
961
+ },
962
+ {
963
+ target: "idle",
964
+ actions: ["invokeOnClose"]
965
+ }
966
+ ],
917
967
  "AREA.POINTER_DOWN": {
918
968
  target: "open:dragging",
919
969
  actions: ["setActiveChannel", "setAreaColorFromPoint", "focusAreaThumb"]
@@ -974,25 +1024,39 @@ function machine(userContext) {
974
1024
  actions: ["setChannelColorFromInput"]
975
1025
  },
976
1026
  INTERACT_OUTSIDE: [
1027
+ {
1028
+ guard: "isOpenControlled",
1029
+ actions: ["invokeOnClose"]
1030
+ },
977
1031
  {
978
1032
  guard: "shouldRestoreFocus",
979
1033
  target: "focused",
980
- actions: ["setReturnFocus", "invokeOnClose"]
1034
+ actions: ["invokeOnClose", "setReturnFocus"]
1035
+ },
1036
+ {
1037
+ target: "idle",
1038
+ actions: ["invokeOnClose"]
1039
+ }
1040
+ ],
1041
+ CLOSE: [
1042
+ {
1043
+ guard: "isOpenControlled",
1044
+ actions: ["invokeOnClose"]
981
1045
  },
982
1046
  {
983
1047
  target: "idle",
984
1048
  actions: ["invokeOnClose"]
985
1049
  }
986
1050
  ],
987
- CLOSE: {
988
- target: "idle",
989
- actions: ["invokeOnClose"]
990
- },
991
1051
  "SWATCH_TRIGGER.CLICK": [
1052
+ {
1053
+ guard: and("isOpenControlled", "closeOnSelect"),
1054
+ actions: ["setValue", "invokeOnClose"]
1055
+ },
992
1056
  {
993
1057
  guard: "closeOnSelect",
994
1058
  target: "focused",
995
- actions: ["setValue", "setReturnFocus", "invokeOnClose"]
1059
+ actions: ["setValue", "invokeOnClose", "setReturnFocus"]
996
1060
  },
997
1061
  {
998
1062
  actions: ["setValue"]
@@ -1005,6 +1069,16 @@ function machine(userContext) {
1005
1069
  exit: ["clearActiveChannel"],
1006
1070
  activities: ["trackPointerMove", "disableTextSelection", "trackPositioning", "trackDismissableElement"],
1007
1071
  on: {
1072
+ "CONTROLLED.CLOSE": [
1073
+ {
1074
+ guard: "shouldRestoreFocus",
1075
+ target: "focused",
1076
+ actions: ["setReturnFocus"]
1077
+ },
1078
+ {
1079
+ target: "idle"
1080
+ }
1081
+ ],
1008
1082
  "AREA.POINTER_MOVE": {
1009
1083
  actions: ["setAreaColorFromPoint", "focusAreaThumb"]
1010
1084
  },
@@ -1020,28 +1094,39 @@ function machine(userContext) {
1020
1094
  actions: ["invokeOnChangeEnd"]
1021
1095
  },
1022
1096
  INTERACT_OUTSIDE: [
1097
+ {
1098
+ guard: "isOpenControlled",
1099
+ actions: ["invokeOnClose"]
1100
+ },
1023
1101
  {
1024
1102
  guard: "shouldRestoreFocus",
1025
1103
  target: "focused",
1026
- actions: ["setReturnFocus", "invokeOnClose"]
1104
+ actions: ["invokeOnClose", "setReturnFocus"]
1027
1105
  },
1028
1106
  {
1029
1107
  target: "idle",
1030
1108
  actions: ["invokeOnClose"]
1031
1109
  }
1032
1110
  ],
1033
- CLOSE: {
1034
- target: "idle",
1035
- actions: ["invokeOnClose"]
1036
- }
1111
+ CLOSE: [
1112
+ {
1113
+ guard: "isOpenControlled",
1114
+ actions: ["invokeOnClose"]
1115
+ },
1116
+ {
1117
+ target: "idle",
1118
+ actions: ["invokeOnClose"]
1119
+ }
1120
+ ]
1037
1121
  }
1038
1122
  }
1039
1123
  }
1040
1124
  },
1041
1125
  {
1042
1126
  guards: {
1043
- isTargetFocusable: (_ctx, evt) => evt.restoreFocus,
1044
- closeOnSelect: (ctx2) => !!ctx2.closeOnSelect
1127
+ closeOnSelect: (ctx2) => !!ctx2.closeOnSelect,
1128
+ isOpenControlled: (ctx2) => !!ctx2["open.controlled"],
1129
+ shouldRestoreFocus: (ctx2) => !!ctx2.restoreFocus
1045
1130
  },
1046
1131
  activities: {
1047
1132
  trackPositioning(ctx2) {
@@ -1058,7 +1143,6 @@ function machine(userContext) {
1058
1143
  },
1059
1144
  trackDismissableElement(ctx2, _evt, { send }) {
1060
1145
  const getContentEl = () => dom.getContentEl(ctx2);
1061
- let restoreFocus = true;
1062
1146
  return (0, import_dismissable.trackDismissableElement)(getContentEl, {
1063
1147
  exclude: dom.getTriggerEl(ctx2),
1064
1148
  defer: true,
@@ -1066,12 +1150,12 @@ function machine(userContext) {
1066
1150
  ctx2.onInteractOutside?.(event);
1067
1151
  if (event.defaultPrevented)
1068
1152
  return;
1069
- restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1153
+ ctx2.restoreFocus = !(event.detail.focusable || event.detail.contextmenu);
1070
1154
  },
1071
1155
  onPointerDownOutside: ctx2.onPointerDownOutside,
1072
1156
  onFocusOutside: ctx2.onFocusOutside,
1073
1157
  onDismiss() {
1074
- send({ type: "INTERACT_OUTSIDE", restoreFocus });
1158
+ send({ type: "INTERACT_OUTSIDE" });
1075
1159
  }
1076
1160
  });
1077
1161
  },
@@ -1249,8 +1333,8 @@ function machine(userContext) {
1249
1333
  invokeOnClose(ctx2) {
1250
1334
  ctx2.onOpenChange?.({ open: false });
1251
1335
  },
1252
- toggleVisibility(ctx2, _evt, { send }) {
1253
- send({ type: ctx2.open ? "OPEN" : "CLOSE", src: "controlled" });
1336
+ toggleVisibility(ctx2, evt, { send }) {
1337
+ send({ type: ctx2.open ? "CONTROLLED.OPEN" : "CONTROLLED.CLOSE", previousEvent: evt });
1254
1338
  }
1255
1339
  },
1256
1340
  compareFns: {