@zag-js/color-picker 1.31.1 → 1.33.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
@@ -848,12 +848,19 @@ function prefixHex(value) {
848
848
 
849
849
  // src/color-picker.machine.ts
850
850
  var { and } = core.createGuards();
851
+ var hashObject = (obj) => {
852
+ let hash = "";
853
+ for (const key in obj) hash += `${key}:${obj[key] ?? ""};`;
854
+ return hash;
855
+ };
856
+ var DEFAULT_COLOR = parse("#000000");
851
857
  var machine = core.createMachine({
852
858
  props({ props: props2 }) {
859
+ const color = props2.value ?? props2.defaultValue ?? DEFAULT_COLOR;
853
860
  return {
854
861
  dir: "ltr",
855
- defaultValue: parse("#000000"),
856
- defaultFormat: "rgba",
862
+ defaultValue: DEFAULT_COLOR,
863
+ defaultFormat: color.getFormat(),
857
864
  openAutoFocus: true,
858
865
  ...props2,
859
866
  positioning: {
@@ -872,15 +879,15 @@ var machine = core.createMachine({
872
879
  defaultValue: prop("defaultValue"),
873
880
  value: prop("value"),
874
881
  isEqual(a, b) {
875
- return a.toString("css") === b?.toString("css");
882
+ return b != null && a.isEqual(b);
876
883
  },
877
884
  hash(a) {
878
- return a.toString("css");
885
+ return hashObject(a.toJSON());
879
886
  },
880
887
  onChange(value) {
881
888
  const ctx = getContext();
882
- const valueAsString = value.toString(ctx.get("format"));
883
- prop("onValueChange")?.({ value, valueAsString });
889
+ const format = ctx.get("format");
890
+ prop("onValueChange")?.({ value, valueAsString: value.toString(format) });
884
891
  }
885
892
  })),
886
893
  format: bindable(() => ({
@@ -916,7 +923,7 @@ var machine = core.createMachine({
916
923
  action(["syncInputElements", "dispatchChangeEvent"]);
917
924
  });
918
925
  track([() => context.get("format")], () => {
919
- action(["syncFormatSelectElement"]);
926
+ action(["syncFormatSelectElement", "syncValueWithFormat"]);
920
927
  });
921
928
  track([() => prop("open")], () => {
922
929
  action(["toggleVisibility"]);
@@ -1421,6 +1428,12 @@ var machine = core.createMachine({
1421
1428
  syncFormatSelectElement({ context, scope }) {
1422
1429
  syncFormatSelect(scope, context.get("format"));
1423
1430
  },
1431
+ syncValueWithFormat({ context }) {
1432
+ const value = context.get("value");
1433
+ const newValue = value.toFormat(context.get("format"));
1434
+ if (newValue.isEqual(value)) return;
1435
+ context.set("value", newValue);
1436
+ },
1424
1437
  invokeOnOpen({ prop, context }) {
1425
1438
  if (prop("inline")) return;
1426
1439
  prop("onOpenChange")?.({ open: true, value: context.get("value") });
package/dist/index.mjs CHANGED
@@ -846,12 +846,19 @@ function prefixHex(value) {
846
846
 
847
847
  // src/color-picker.machine.ts
848
848
  var { and } = createGuards();
849
+ var hashObject = (obj) => {
850
+ let hash = "";
851
+ for (const key in obj) hash += `${key}:${obj[key] ?? ""};`;
852
+ return hash;
853
+ };
854
+ var DEFAULT_COLOR = parse("#000000");
849
855
  var machine = createMachine({
850
856
  props({ props: props2 }) {
857
+ const color = props2.value ?? props2.defaultValue ?? DEFAULT_COLOR;
851
858
  return {
852
859
  dir: "ltr",
853
- defaultValue: parse("#000000"),
854
- defaultFormat: "rgba",
860
+ defaultValue: DEFAULT_COLOR,
861
+ defaultFormat: color.getFormat(),
855
862
  openAutoFocus: true,
856
863
  ...props2,
857
864
  positioning: {
@@ -870,15 +877,15 @@ var machine = createMachine({
870
877
  defaultValue: prop("defaultValue"),
871
878
  value: prop("value"),
872
879
  isEqual(a, b) {
873
- return a.toString("css") === b?.toString("css");
880
+ return b != null && a.isEqual(b);
874
881
  },
875
882
  hash(a) {
876
- return a.toString("css");
883
+ return hashObject(a.toJSON());
877
884
  },
878
885
  onChange(value) {
879
886
  const ctx = getContext();
880
- const valueAsString = value.toString(ctx.get("format"));
881
- prop("onValueChange")?.({ value, valueAsString });
887
+ const format = ctx.get("format");
888
+ prop("onValueChange")?.({ value, valueAsString: value.toString(format) });
882
889
  }
883
890
  })),
884
891
  format: bindable(() => ({
@@ -914,7 +921,7 @@ var machine = createMachine({
914
921
  action(["syncInputElements", "dispatchChangeEvent"]);
915
922
  });
916
923
  track([() => context.get("format")], () => {
917
- action(["syncFormatSelectElement"]);
924
+ action(["syncFormatSelectElement", "syncValueWithFormat"]);
918
925
  });
919
926
  track([() => prop("open")], () => {
920
927
  action(["toggleVisibility"]);
@@ -1419,6 +1426,12 @@ var machine = createMachine({
1419
1426
  syncFormatSelectElement({ context, scope }) {
1420
1427
  syncFormatSelect(scope, context.get("format"));
1421
1428
  },
1429
+ syncValueWithFormat({ context }) {
1430
+ const value = context.get("value");
1431
+ const newValue = value.toFormat(context.get("format"));
1432
+ if (newValue.isEqual(value)) return;
1433
+ context.set("value", newValue);
1434
+ },
1422
1435
  invokeOnOpen({ prop, context }) {
1423
1436
  if (prop("inline")) return;
1424
1437
  prop("onOpenChange")?.({ open: true, value: context.get("value") });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/color-picker",
3
- "version": "1.31.1",
3
+ "version": "1.33.0",
4
4
  "description": "Core logic for the color-picker widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -27,14 +27,14 @@
27
27
  "url": "https://github.com/chakra-ui/zag/issues"
28
28
  },
29
29
  "dependencies": {
30
- "@zag-js/core": "1.31.1",
31
- "@zag-js/anatomy": "1.31.1",
32
- "@zag-js/dom-query": "1.31.1",
33
- "@zag-js/dismissable": "1.31.1",
34
- "@zag-js/utils": "1.31.1",
35
- "@zag-js/color-utils": "1.31.1",
36
- "@zag-js/popper": "1.31.1",
37
- "@zag-js/types": "1.31.1"
30
+ "@zag-js/core": "1.33.0",
31
+ "@zag-js/anatomy": "1.33.0",
32
+ "@zag-js/dom-query": "1.33.0",
33
+ "@zag-js/dismissable": "1.33.0",
34
+ "@zag-js/utils": "1.33.0",
35
+ "@zag-js/color-utils": "1.33.0",
36
+ "@zag-js/popper": "1.33.0",
37
+ "@zag-js/types": "1.33.0"
38
38
  },
39
39
  "devDependencies": {
40
40
  "clean-package": "2.2.0"