@zag-js/color-picker 0.15.0 → 0.17.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 +13 -18
- package/dist/index.d.ts +13 -18
- package/dist/index.js +57 -47
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +57 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +12 -12
- package/src/color-picker.connect.ts +3 -3
- package/src/color-picker.machine.ts +56 -45
- package/src/color-picker.types.ts +17 -1
- package/src/index.ts +1 -1
- package/src/utils/get-channel-input-value.ts +3 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
2
|
import { Orientation, RequiredBy, PropTypes, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
|
|
3
|
-
import { ColorChannel, Color, ColorFormat
|
|
3
|
+
import { ColorChannel, Color, ColorFormat } from '@zag-js/color-utils';
|
|
4
4
|
export { Color, ColorAxes, ColorChannel, ColorFormat, ColorType } from '@zag-js/color-utils';
|
|
5
5
|
import * as _zag_js_core from '@zag-js/core';
|
|
6
6
|
import { StateMachine } from '@zag-js/core';
|
|
@@ -72,33 +72,28 @@ type PublicContext = CommonProperties & {
|
|
|
72
72
|
*/
|
|
73
73
|
name?: string;
|
|
74
74
|
};
|
|
75
|
-
type PrivateContext = Context<{
|
|
76
|
-
/**
|
|
77
|
-
* The id of the thumb that is currently being dragged
|
|
78
|
-
*/
|
|
79
|
-
activeId: string | null;
|
|
80
|
-
/**
|
|
81
|
-
* The channel that is currently being interacted with
|
|
82
|
-
*/
|
|
83
|
-
activeChannel: Partial<ColorAxes> | null;
|
|
84
|
-
/**
|
|
85
|
-
* The orientation of the channel that is currently being interacted with
|
|
86
|
-
*/
|
|
87
|
-
activeOrientation: Orientation | null;
|
|
88
|
-
}>;
|
|
75
|
+
type PrivateContext = Context<{}>;
|
|
89
76
|
type ComputedContext = Readonly<{
|
|
90
77
|
/**
|
|
78
|
+
* @computed
|
|
91
79
|
* Whether the color picker is in RTL mode
|
|
92
80
|
*/
|
|
93
81
|
isRtl: boolean;
|
|
94
82
|
/**
|
|
83
|
+
* @computed
|
|
95
84
|
* Whether the color picker is interactive
|
|
96
85
|
*/
|
|
97
86
|
isInteractive: boolean;
|
|
98
87
|
/**
|
|
88
|
+
* @computed
|
|
99
89
|
* The color value as a Color object
|
|
100
90
|
*/
|
|
101
91
|
valueAsColor: Color;
|
|
92
|
+
/**
|
|
93
|
+
* @computed
|
|
94
|
+
* Whether the color picker is disabled
|
|
95
|
+
*/
|
|
96
|
+
isDisabled: boolean;
|
|
102
97
|
}>;
|
|
103
98
|
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
104
99
|
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
@@ -107,7 +102,7 @@ type MachineState = {
|
|
|
107
102
|
};
|
|
108
103
|
type State = StateMachine.State<MachineContext, MachineState>;
|
|
109
104
|
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
110
|
-
type
|
|
105
|
+
type MachineApi<T extends PropTypes = PropTypes> = {
|
|
111
106
|
/**
|
|
112
107
|
* Whether the color picker is being dragged
|
|
113
108
|
*/
|
|
@@ -158,8 +153,8 @@ type PublicApi<T extends PropTypes = PropTypes> = {
|
|
|
158
153
|
getSwatchProps(props: ColorSwatchProps): T["element"];
|
|
159
154
|
};
|
|
160
155
|
|
|
161
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>):
|
|
156
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
162
157
|
|
|
163
158
|
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
164
159
|
|
|
165
|
-
export { ColorAreaProps, ColorChannelInputProps, ColorChannelProps, ColorSwatchProps, UserDefinedContext as Context,
|
|
160
|
+
export { MachineApi as Api, ColorAreaProps, ColorChannelInputProps, ColorChannelProps, ColorSwatchProps, UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as _zag_js_anatomy from '@zag-js/anatomy';
|
|
2
2
|
import { Orientation, RequiredBy, PropTypes, CommonProperties, Context, NormalizeProps } from '@zag-js/types';
|
|
3
|
-
import { ColorChannel, Color, ColorFormat
|
|
3
|
+
import { ColorChannel, Color, ColorFormat } from '@zag-js/color-utils';
|
|
4
4
|
export { Color, ColorAxes, ColorChannel, ColorFormat, ColorType } from '@zag-js/color-utils';
|
|
5
5
|
import * as _zag_js_core from '@zag-js/core';
|
|
6
6
|
import { StateMachine } from '@zag-js/core';
|
|
@@ -72,33 +72,28 @@ type PublicContext = CommonProperties & {
|
|
|
72
72
|
*/
|
|
73
73
|
name?: string;
|
|
74
74
|
};
|
|
75
|
-
type PrivateContext = Context<{
|
|
76
|
-
/**
|
|
77
|
-
* The id of the thumb that is currently being dragged
|
|
78
|
-
*/
|
|
79
|
-
activeId: string | null;
|
|
80
|
-
/**
|
|
81
|
-
* The channel that is currently being interacted with
|
|
82
|
-
*/
|
|
83
|
-
activeChannel: Partial<ColorAxes> | null;
|
|
84
|
-
/**
|
|
85
|
-
* The orientation of the channel that is currently being interacted with
|
|
86
|
-
*/
|
|
87
|
-
activeOrientation: Orientation | null;
|
|
88
|
-
}>;
|
|
75
|
+
type PrivateContext = Context<{}>;
|
|
89
76
|
type ComputedContext = Readonly<{
|
|
90
77
|
/**
|
|
78
|
+
* @computed
|
|
91
79
|
* Whether the color picker is in RTL mode
|
|
92
80
|
*/
|
|
93
81
|
isRtl: boolean;
|
|
94
82
|
/**
|
|
83
|
+
* @computed
|
|
95
84
|
* Whether the color picker is interactive
|
|
96
85
|
*/
|
|
97
86
|
isInteractive: boolean;
|
|
98
87
|
/**
|
|
88
|
+
* @computed
|
|
99
89
|
* The color value as a Color object
|
|
100
90
|
*/
|
|
101
91
|
valueAsColor: Color;
|
|
92
|
+
/**
|
|
93
|
+
* @computed
|
|
94
|
+
* Whether the color picker is disabled
|
|
95
|
+
*/
|
|
96
|
+
isDisabled: boolean;
|
|
102
97
|
}>;
|
|
103
98
|
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
104
99
|
type MachineContext = PublicContext & PrivateContext & ComputedContext;
|
|
@@ -107,7 +102,7 @@ type MachineState = {
|
|
|
107
102
|
};
|
|
108
103
|
type State = StateMachine.State<MachineContext, MachineState>;
|
|
109
104
|
type Send = StateMachine.Send<StateMachine.AnyEventObject>;
|
|
110
|
-
type
|
|
105
|
+
type MachineApi<T extends PropTypes = PropTypes> = {
|
|
111
106
|
/**
|
|
112
107
|
* Whether the color picker is being dragged
|
|
113
108
|
*/
|
|
@@ -158,8 +153,8 @@ type PublicApi<T extends PropTypes = PropTypes> = {
|
|
|
158
153
|
getSwatchProps(props: ColorSwatchProps): T["element"];
|
|
159
154
|
};
|
|
160
155
|
|
|
161
|
-
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>):
|
|
156
|
+
declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T>;
|
|
162
157
|
|
|
163
158
|
declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
|
|
164
159
|
|
|
165
|
-
export { ColorAreaProps, ColorChannelInputProps, ColorChannelProps, ColorSwatchProps, UserDefinedContext as Context,
|
|
160
|
+
export { MachineApi as Api, ColorAreaProps, ColorChannelInputProps, ColorChannelProps, ColorSwatchProps, UserDefinedContext as Context, anatomy, connect, machine };
|
package/dist/index.js
CHANGED
|
@@ -165,6 +165,8 @@ function getChannelDisplayColor(color, channel) {
|
|
|
165
165
|
|
|
166
166
|
// src/utils/get-channel-input-value.ts
|
|
167
167
|
function getChannelInputValue(color, channel) {
|
|
168
|
+
if (channel == null)
|
|
169
|
+
return;
|
|
168
170
|
switch (channel) {
|
|
169
171
|
case "hex":
|
|
170
172
|
return color.toString("hex");
|
|
@@ -407,7 +409,7 @@ var getSliderBgImage = (ctx, props) => {
|
|
|
407
409
|
function connect(state, send, normalize) {
|
|
408
410
|
const valueAsColor = state.context.valueAsColor;
|
|
409
411
|
const value = state.context.value;
|
|
410
|
-
const isDisabled = state.context.
|
|
412
|
+
const isDisabled = state.context.isDisabled;
|
|
411
413
|
const isInteractive = state.context.isInteractive;
|
|
412
414
|
const isDragging = state.matches("dragging");
|
|
413
415
|
const channels = valueAsColor.getColorChannels();
|
|
@@ -782,15 +784,18 @@ function machine(userContext) {
|
|
|
782
784
|
initial: "idle",
|
|
783
785
|
context: {
|
|
784
786
|
dir: "ltr",
|
|
787
|
+
value: "#D9D9D9",
|
|
788
|
+
disabled: false,
|
|
789
|
+
...ctx,
|
|
785
790
|
activeId: null,
|
|
786
791
|
activeChannel: null,
|
|
787
792
|
activeOrientation: null,
|
|
788
|
-
|
|
789
|
-
...ctx
|
|
793
|
+
fieldsetDisabled: false
|
|
790
794
|
},
|
|
791
795
|
computed: {
|
|
792
796
|
isRtl: (ctx2) => ctx2.dir === "rtl",
|
|
793
|
-
|
|
797
|
+
isDisabled: (ctx2) => !!ctx2.disabled || ctx2.fieldsetDisabled,
|
|
798
|
+
isInteractive: (ctx2) => !(ctx2.isDisabled || ctx2.readOnly),
|
|
794
799
|
valueAsColor: (ctx2) => (0, import_color_utils3.parseColor)(ctx2.value)
|
|
795
800
|
},
|
|
796
801
|
on: {
|
|
@@ -800,7 +805,7 @@ function machine(userContext) {
|
|
|
800
805
|
},
|
|
801
806
|
activities: ["trackFormControl"],
|
|
802
807
|
watch: {
|
|
803
|
-
value: ["syncInputElements"
|
|
808
|
+
value: ["syncInputElements"]
|
|
804
809
|
},
|
|
805
810
|
states: {
|
|
806
811
|
idle: {
|
|
@@ -937,8 +942,8 @@ function machine(userContext) {
|
|
|
937
942
|
trackFormControl(ctx2, _evt, { send, initialContext }) {
|
|
938
943
|
const inputEl = dom.getHiddenInputEl(ctx2);
|
|
939
944
|
return (0, import_form_utils.trackFormControl)(inputEl, {
|
|
940
|
-
|
|
941
|
-
ctx2.
|
|
945
|
+
onFieldsetDisabledChange(disabled) {
|
|
946
|
+
ctx2.fieldsetDisabled = disabled;
|
|
942
947
|
},
|
|
943
948
|
onFormReset() {
|
|
944
949
|
send({ type: "VALUE.SET", value: initialContext.value, src: "form.reset" });
|
|
@@ -971,7 +976,7 @@ function machine(userContext) {
|
|
|
971
976
|
picker.open().then(({ sRGBHex }) => {
|
|
972
977
|
const format = ctx2.valueAsColor.getColorSpace();
|
|
973
978
|
const color = (0, import_color_utils3.parseColor)(sRGBHex).toFormat(format);
|
|
974
|
-
|
|
979
|
+
set.value(ctx2, color);
|
|
975
980
|
ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: color });
|
|
976
981
|
}).catch(() => void 0);
|
|
977
982
|
},
|
|
@@ -998,7 +1003,7 @@ function machine(userContext) {
|
|
|
998
1003
|
const color = getColorFromPoint(percent.x, percent.y);
|
|
999
1004
|
if (!color)
|
|
1000
1005
|
return;
|
|
1001
|
-
|
|
1006
|
+
set.value(ctx2, color);
|
|
1002
1007
|
},
|
|
1003
1008
|
setChannelColorFromPoint(ctx2, evt) {
|
|
1004
1009
|
const channel = evt.channel || ctx2.activeId;
|
|
@@ -1011,90 +1016,80 @@ function machine(userContext) {
|
|
|
1011
1016
|
const channelValue = (0, import_numeric_range2.getPercentValue)(point, minValue, maxValue, step);
|
|
1012
1017
|
const value = (0, import_numeric_range2.snapValueToStep)(channelValue - step, minValue, maxValue, step);
|
|
1013
1018
|
const newColor = ctx2.valueAsColor.withChannelValue(channel, value);
|
|
1014
|
-
|
|
1019
|
+
set.value(ctx2, newColor);
|
|
1015
1020
|
},
|
|
1016
1021
|
setValue(ctx2, evt) {
|
|
1017
|
-
|
|
1022
|
+
set.value(ctx2, evt.value);
|
|
1018
1023
|
},
|
|
1019
1024
|
syncInputElements(ctx2) {
|
|
1020
1025
|
const inputs = dom.getChannelInputEls(ctx2);
|
|
1021
1026
|
inputs.forEach((input) => {
|
|
1022
1027
|
const channel = input.dataset.channel;
|
|
1023
|
-
|
|
1024
|
-
return;
|
|
1025
|
-
input.value = getChannelInputValue(ctx2.valueAsColor, channel);
|
|
1028
|
+
dom.setValue(input, getChannelInputValue(ctx2.valueAsColor, channel));
|
|
1026
1029
|
});
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1030
|
+
dom.setValue(dom.getHiddenInputEl(ctx2), ctx2.value);
|
|
1031
|
+
},
|
|
1032
|
+
invokeOnChangeEnd(ctx2) {
|
|
1033
|
+
invoke.changeEnd(ctx2);
|
|
1031
1034
|
},
|
|
1032
1035
|
setChannelColorFromInput(ctx2, evt) {
|
|
1033
1036
|
const { channel, isTextField, value } = evt;
|
|
1034
1037
|
try {
|
|
1035
1038
|
const format = ctx2.valueAsColor.getColorSpace();
|
|
1036
1039
|
const newColor = isTextField ? (0, import_color_utils3.parseColor)(value).toFormat(format) : ctx2.valueAsColor.withChannelValue(channel, value);
|
|
1037
|
-
|
|
1040
|
+
set.value(ctx2, newColor);
|
|
1038
1041
|
} catch {
|
|
1039
|
-
const
|
|
1040
|
-
|
|
1041
|
-
return;
|
|
1042
|
-
input.value = getChannelInputValue(ctx2.valueAsColor, channel);
|
|
1042
|
+
const inputEl = dom.getChannelInputEl(ctx2, channel);
|
|
1043
|
+
dom.setValue(inputEl, getChannelInputValue(ctx2.valueAsColor, channel));
|
|
1043
1044
|
}
|
|
1044
1045
|
},
|
|
1045
1046
|
incrementChannel(ctx2, evt) {
|
|
1046
1047
|
const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
|
|
1047
1048
|
const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
|
|
1048
1049
|
const value = (0, import_numeric_range2.snapValueToStep)(channelValue + evt.step, minValue, maxValue, step);
|
|
1049
|
-
const
|
|
1050
|
-
|
|
1050
|
+
const color = ctx2.valueAsColor.withChannelValue(evt.channel, (0, import_numeric_range2.clampValue)(value, minValue, maxValue));
|
|
1051
|
+
set.value(ctx2, color);
|
|
1051
1052
|
},
|
|
1052
1053
|
decrementChannel(ctx2, evt) {
|
|
1053
1054
|
const { minValue, maxValue, step } = ctx2.valueAsColor.getChannelRange(evt.channel);
|
|
1054
1055
|
const channelValue = ctx2.valueAsColor.getChannelValue(evt.channel);
|
|
1055
1056
|
const value = (0, import_numeric_range2.snapValueToStep)(channelValue - evt.step, minValue, maxValue, step);
|
|
1056
|
-
const
|
|
1057
|
-
|
|
1057
|
+
const color = ctx2.valueAsColor.withChannelValue(evt.channel, (0, import_numeric_range2.clampValue)(value, minValue, maxValue));
|
|
1058
|
+
set.value(ctx2, color);
|
|
1058
1059
|
},
|
|
1059
1060
|
incrementXChannel(ctx2, evt) {
|
|
1060
1061
|
const { xChannel, yChannel } = evt.channel;
|
|
1061
1062
|
const { incrementX } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
|
|
1062
|
-
const
|
|
1063
|
-
|
|
1063
|
+
const color = ctx2.valueAsColor.withChannelValue(xChannel, incrementX(evt.step));
|
|
1064
|
+
set.value(ctx2, color);
|
|
1064
1065
|
},
|
|
1065
1066
|
decrementXChannel(ctx2, evt) {
|
|
1066
1067
|
const { xChannel, yChannel } = evt.channel;
|
|
1067
1068
|
const { decrementX } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
|
|
1068
|
-
const
|
|
1069
|
-
|
|
1069
|
+
const color = ctx2.valueAsColor.withChannelValue(xChannel, decrementX(evt.step));
|
|
1070
|
+
set.value(ctx2, color);
|
|
1070
1071
|
},
|
|
1071
1072
|
incrementYChannel(ctx2, evt) {
|
|
1072
1073
|
const { xChannel, yChannel } = evt.channel;
|
|
1073
1074
|
const { incrementY } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
|
|
1074
|
-
const
|
|
1075
|
-
|
|
1075
|
+
const color = ctx2.valueAsColor.withChannelValue(yChannel, incrementY(evt.step));
|
|
1076
|
+
set.value(ctx2, color);
|
|
1076
1077
|
},
|
|
1077
1078
|
decrementYChannel(ctx2, evt) {
|
|
1078
1079
|
const { xChannel, yChannel } = evt.channel;
|
|
1079
1080
|
const { decrementY } = getChannelDetails(ctx2.valueAsColor, xChannel, yChannel);
|
|
1080
|
-
const
|
|
1081
|
-
|
|
1081
|
+
const color = ctx2.valueAsColor.withChannelValue(yChannel, decrementY(evt.step));
|
|
1082
|
+
set.value(ctx2, color);
|
|
1082
1083
|
},
|
|
1083
1084
|
setChannelToMax(ctx2, evt) {
|
|
1084
1085
|
const { maxValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
|
|
1085
|
-
const
|
|
1086
|
-
|
|
1086
|
+
const color = ctx2.valueAsColor.withChannelValue(evt.channel, maxValue);
|
|
1087
|
+
set.value(ctx2, color);
|
|
1087
1088
|
},
|
|
1088
1089
|
setChannelToMin(ctx2, evt) {
|
|
1089
1090
|
const { minValue } = ctx2.valueAsColor.getChannelRange(evt.channel);
|
|
1090
|
-
const
|
|
1091
|
-
|
|
1092
|
-
},
|
|
1093
|
-
invokeOnChangeEnd(ctx2) {
|
|
1094
|
-
ctx2.onChangeEnd?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
|
|
1095
|
-
},
|
|
1096
|
-
invokeOnChange(ctx2) {
|
|
1097
|
-
ctx2.onChange?.({ value: ctx2.value, valueAsColor: ctx2.valueAsColor });
|
|
1091
|
+
const color = ctx2.valueAsColor.withChannelValue(evt.channel, minValue);
|
|
1092
|
+
set.value(ctx2, color);
|
|
1098
1093
|
},
|
|
1099
1094
|
focusAreaThumb(ctx2) {
|
|
1100
1095
|
(0, import_dom_query3.raf)(() => {
|
|
@@ -1110,8 +1105,23 @@ function machine(userContext) {
|
|
|
1110
1105
|
}
|
|
1111
1106
|
);
|
|
1112
1107
|
}
|
|
1113
|
-
var
|
|
1114
|
-
ctx.value
|
|
1108
|
+
var getDetails = (ctx) => ({
|
|
1109
|
+
value: ctx.value,
|
|
1110
|
+
valueAsColor: ctx.valueAsColor
|
|
1111
|
+
});
|
|
1112
|
+
var invoke = {
|
|
1113
|
+
changeEnd(ctx) {
|
|
1114
|
+
ctx.onChangeEnd?.(getDetails(ctx));
|
|
1115
|
+
},
|
|
1116
|
+
change(ctx) {
|
|
1117
|
+
ctx.onChange?.(getDetails(ctx));
|
|
1118
|
+
}
|
|
1119
|
+
};
|
|
1120
|
+
var set = {
|
|
1121
|
+
value(ctx, color) {
|
|
1122
|
+
ctx.value = color.toString("css");
|
|
1123
|
+
invoke.change(ctx);
|
|
1124
|
+
}
|
|
1115
1125
|
};
|
|
1116
1126
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1117
1127
|
0 && (module.exports = {
|