@zag-js/color-picker 0.10.4 → 0.11.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.
Files changed (38) hide show
  1. package/dist/index.d.mts +150 -0
  2. package/dist/index.d.ts +150 -4
  3. package/dist/index.js +1086 -8
  4. package/dist/index.js.map +1 -0
  5. package/dist/index.mjs +1069 -3
  6. package/dist/index.mjs.map +1 -0
  7. package/package.json +11 -11
  8. package/dist/color-picker.anatomy.d.ts +0 -3
  9. package/dist/color-picker.anatomy.js +0 -24
  10. package/dist/color-picker.anatomy.mjs +0 -19
  11. package/dist/color-picker.connect.d.ts +0 -44
  12. package/dist/color-picker.connect.js +0 -372
  13. package/dist/color-picker.connect.mjs +0 -368
  14. package/dist/color-picker.dom.d.ts +0 -46
  15. package/dist/color-picker.dom.js +0 -37
  16. package/dist/color-picker.dom.mjs +0 -33
  17. package/dist/color-picker.machine.d.ts +0 -3
  18. package/dist/color-picker.machine.js +0 -340
  19. package/dist/color-picker.machine.mjs +0 -336
  20. package/dist/color-picker.types.d.ts +0 -99
  21. package/dist/utils/generate-format-background.d.ts +0 -66
  22. package/dist/utils/generate-format-background.js +0 -133
  23. package/dist/utils/generate-format-background.mjs +0 -121
  24. package/dist/utils/get-channel-details.d.ts +0 -19
  25. package/dist/utils/get-channel-details.js +0 -57
  26. package/dist/utils/get-channel-details.mjs +0 -53
  27. package/dist/utils/get-channel-display-color.d.ts +0 -3
  28. package/dist/utils/get-channel-display-color.js +0 -26
  29. package/dist/utils/get-channel-display-color.mjs +0 -22
  30. package/dist/utils/get-channel-input-value.d.ts +0 -5
  31. package/dist/utils/get-channel-input-value.js +0 -26
  32. package/dist/utils/get-channel-input-value.mjs +0 -21
  33. package/dist/utils/get-color-area-gradient.d.ts +0 -7
  34. package/dist/utils/get-color-area-gradient.js +0 -65
  35. package/dist/utils/get-color-area-gradient.mjs +0 -61
  36. package/dist/utils/get-slider-background.d.ts +0 -2
  37. package/dist/utils/get-slider-background.js +0 -43
  38. package/dist/utils/get-slider-background.mjs +0 -39
@@ -1,372 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
5
- const colorUtils = require('@zag-js/color-utils');
6
- const domEvent = require('@zag-js/dom-event');
7
- const domQuery = require('@zag-js/dom-query');
8
- const colorPicker_anatomy = require('./color-picker.anatomy.js');
9
- const colorPicker_dom = require('./color-picker.dom.js');
10
- const getChannelDetails = require('./utils/get-channel-details.js');
11
- const getChannelDisplayColor = require('./utils/get-channel-display-color.js');
12
- const getChannelInputValue = require('./utils/get-channel-input-value.js');
13
- const getColorAreaGradient = require('./utils/get-color-area-gradient.js');
14
- const getSliderBackground = require('./utils/get-slider-background.js');
15
-
16
- function connect(state, send, normalize) {
17
- const valueAsColor = state.context.valueAsColor;
18
- const isDisabled = state.context.disabled;
19
- const isInteractive = state.context.isInteractive;
20
- const isDragging = state.matches("dragging");
21
- const channels = valueAsColor.getColorChannels();
22
- return {
23
- /**
24
- * Whether the color picker is being dragged
25
- */
26
- isDragging,
27
- /**
28
- * The current color value (as a string)
29
- */
30
- value: state.context.value,
31
- /**
32
- * The current color value (as a Color object)
33
- */
34
- valueAsColor,
35
- /**
36
- * The current color channels of the color
37
- */
38
- channels,
39
- /**
40
- * Function to set the color value
41
- */
42
- setColor(value) {
43
- send({ type: "VALUE.SET", value: colorUtils.normalizeColor(value), src: "set-color" });
44
- },
45
- /**
46
- * Function to set the color value of a specific channel
47
- */
48
- setChannelValue(channel, value) {
49
- const color = valueAsColor.withChannelValue(channel, value);
50
- send({ type: "VALUE.SET", value: color, src: "set-channel" });
51
- },
52
- /**
53
- * Function to set the color format
54
- */
55
- setFormat(format) {
56
- const value = valueAsColor.toFormat(format);
57
- send({ type: "VALUE.SET", value, src: "set-format" });
58
- },
59
- contentProps: normalize.element({
60
- ...colorPicker_anatomy.parts.content.attrs,
61
- id: colorPicker_dom.dom.getContentId(state.context)
62
- }),
63
- getAreaProps(props) {
64
- const { xChannel, yChannel } = props;
65
- const { areaStyles } = getColorAreaGradient.getColorAreaGradient(state.context, xChannel, yChannel);
66
- return normalize.element({
67
- ...colorPicker_anatomy.parts.area.attrs,
68
- id: colorPicker_dom.dom.getAreaId(state.context),
69
- role: "group",
70
- onPointerDown(event) {
71
- if (!isInteractive)
72
- return;
73
- const evt = domEvent.getNativeEvent(event);
74
- if (!domEvent.isLeftClick(evt) || domEvent.isModifiedEvent(evt))
75
- return;
76
- const point = domEvent.getEventPoint(evt);
77
- const channel = { xChannel, yChannel };
78
- send({ type: "AREA.POINTER_DOWN", point, channel, id: "area" });
79
- },
80
- style: {
81
- position: "relative",
82
- touchAction: "none",
83
- forcedColorAdjust: "none",
84
- ...areaStyles
85
- }
86
- });
87
- },
88
- getAreaGradientProps(props) {
89
- const { xChannel, yChannel } = props;
90
- const { areaGradientStyles } = getColorAreaGradient.getColorAreaGradient(state.context, xChannel, yChannel);
91
- return normalize.element({
92
- ...colorPicker_anatomy.parts.areaGradient.attrs,
93
- id: colorPicker_dom.dom.getAreaGradientId(state.context),
94
- style: {
95
- position: "relative",
96
- touchAction: "none",
97
- forcedColorAdjust: "none",
98
- ...areaGradientStyles
99
- }
100
- });
101
- },
102
- getAreaThumbProps(props) {
103
- const { xChannel, yChannel } = props;
104
- const { getThumbPosition } = getChannelDetails.getChannelDetails(valueAsColor, xChannel, yChannel);
105
- const { x, y } = getThumbPosition();
106
- const channel = { xChannel, yChannel };
107
- return normalize.element({
108
- ...colorPicker_anatomy.parts.areaThumb.attrs,
109
- id: colorPicker_dom.dom.getAreaThumbId(state.context),
110
- tabIndex: isDisabled ? void 0 : 0,
111
- "data-disabled": domQuery.dataAttr(isDisabled),
112
- role: "presentation",
113
- style: {
114
- position: "absolute",
115
- left: `${x * 100}%`,
116
- top: `${y * 100}%`,
117
- transform: "translate(-50%, -50%)",
118
- touchAction: "none",
119
- forcedColorAdjust: "none",
120
- background: valueAsColor.withChannelValue("alpha", 1).toString("css")
121
- },
122
- onBlur() {
123
- send("AREA.BLUR");
124
- },
125
- onKeyDown(event) {
126
- if (!isInteractive)
127
- return;
128
- const step = domEvent.getEventStep(event);
129
- const keyMap = {
130
- ArrowUp() {
131
- send({ type: "AREA.ARROW_UP", channel, step });
132
- },
133
- ArrowDown() {
134
- send({ type: "AREA.ARROW_DOWN", channel, step });
135
- },
136
- ArrowLeft() {
137
- send({ type: "AREA.ARROW_LEFT", channel, step });
138
- },
139
- ArrowRight() {
140
- send({ type: "AREA.ARROW_RIGHT", channel, step });
141
- },
142
- PageUp() {
143
- send({ type: "AREA.PAGE_UP", channel, step });
144
- },
145
- PageDown() {
146
- send({ type: "AREA.PAGE_DOWN", channel, step });
147
- }
148
- };
149
- const exec = keyMap[domEvent.getEventKey(event, state.context)];
150
- if (exec) {
151
- exec(event);
152
- event.preventDefault();
153
- }
154
- }
155
- });
156
- },
157
- getChannelSliderTrackProps(props) {
158
- const { orientation = "horizontal", channel } = props;
159
- return normalize.element({
160
- ...colorPicker_anatomy.parts.channelSliderTrack.attrs,
161
- id: colorPicker_dom.dom.getChannelSliderTrackId(state.context, channel),
162
- role: "group",
163
- "data-channel": channel,
164
- "data-orientation": orientation,
165
- onPointerDown(event) {
166
- if (!isInteractive)
167
- return;
168
- const evt = domEvent.getNativeEvent(event);
169
- if (!domEvent.isLeftClick(evt) || domEvent.isModifiedEvent(evt))
170
- return;
171
- const point = domEvent.getEventPoint(evt);
172
- send({ type: "CHANNEL_SLIDER.POINTER_DOWN", channel, point, id: channel, orientation });
173
- },
174
- style: {
175
- position: "relative",
176
- touchAction: "none",
177
- forcedColorAdjust: "none",
178
- backgroundImage: getSliderBackground.getSliderBgImage(state.context, { orientation, channel })
179
- }
180
- });
181
- },
182
- getChannelSliderBackgroundProps(props) {
183
- const { orientation = "horizontal", channel } = props;
184
- return normalize.element({
185
- ...colorPicker_anatomy.parts.channelSliderTrackBg.attrs,
186
- "data-orientation": orientation,
187
- "data-channel": channel,
188
- style: {
189
- position: "absolute",
190
- backgroundColor: "#fff",
191
- backgroundImage: [
192
- "linear-gradient(-45deg,#0000 75.5%,#bcbcbc 75.5%)",
193
- "linear-gradient(45deg,#0000 75.5%,#bcbcbc 75.5%)",
194
- "linear-gradient(-45deg,#bcbcbc 25.5%,#0000 25.5%)",
195
- "linear-gradient(45deg,#bcbcbc 25.5%,#0000 25.5%)"
196
- ].join(","),
197
- backgroundSize: "16px 16px",
198
- backgroundPosition: "-2px -2px,-2px 6px,6px -10px,-10px -2px",
199
- inset: 0,
200
- zIndex: -1
201
- }
202
- });
203
- },
204
- getChannelSliderThumbProps(props) {
205
- const { orientation = "horizontal", channel } = props;
206
- const { minValue, maxValue, step: stepValue } = valueAsColor.getChannelRange(channel);
207
- const channelValue = valueAsColor.getChannelValue(channel);
208
- const offset = (channelValue - minValue) / (maxValue - minValue);
209
- const placementStyles = orientation === "horizontal" ? { left: `${offset * 100}%`, top: "50%" } : { top: `${offset * 100}%`, left: "50%" };
210
- return normalize.element({
211
- ...colorPicker_anatomy.parts.channelSliderThumb.attrs,
212
- id: colorPicker_dom.dom.getChannelSliderThumbId(state.context, channel),
213
- role: "slider",
214
- "aria-label": channel,
215
- tabIndex: isDisabled ? void 0 : 0,
216
- "data-channel": channel,
217
- "data-disabled": domQuery.dataAttr(isDisabled),
218
- "data-orientation": orientation,
219
- "aria-disabled": domQuery.dataAttr(isDisabled),
220
- "aria-orientation": orientation,
221
- "aria-valuemax": maxValue,
222
- "aria-valuemin": minValue,
223
- "aria-valuenow": channelValue,
224
- style: {
225
- forcedColorAdjust: "none",
226
- position: "absolute",
227
- background: getChannelDisplayColor.getChannelDisplayColor(valueAsColor, channel).toString("css"),
228
- ...placementStyles
229
- },
230
- onFocus() {
231
- if (!isInteractive)
232
- return;
233
- send({ type: "CHANNEL_SLIDER.FOCUS", channel });
234
- },
235
- onBlur() {
236
- if (!isInteractive)
237
- return;
238
- send({ type: "CHANNEL_SLIDER.BLUR", channel });
239
- },
240
- onKeyDown(event) {
241
- if (!isInteractive)
242
- return;
243
- const step = domEvent.getEventStep(event) * stepValue;
244
- const keyMap = {
245
- ArrowUp() {
246
- send({ type: "CHANNEL_SLIDER.ARROW_UP", channel, step });
247
- },
248
- ArrowDown() {
249
- send({ type: "CHANNEL_SLIDER.ARROW_DOWN", channel, step });
250
- },
251
- ArrowLeft() {
252
- send({ type: "CHANNEL_SLIDER.ARROW_LEFT", channel, step });
253
- },
254
- ArrowRight() {
255
- send({ type: "CHANNEL_SLIDER.ARROW_RIGHT", channel, step });
256
- },
257
- PageUp() {
258
- send({ type: "CHANNEL_SLIDER.PAGE_UP", channel });
259
- },
260
- PageDown() {
261
- send({ type: "CHANNEL_SLIDER.PAGE_DOWN", channel });
262
- },
263
- Home() {
264
- send({ type: "CHANNEL_SLIDER.HOME", channel });
265
- },
266
- End() {
267
- send({ type: "CHANNEL_SLIDER.END", channel });
268
- }
269
- };
270
- const exec = keyMap[domEvent.getEventKey(event, state.context)];
271
- if (exec) {
272
- exec(event);
273
- event.preventDefault();
274
- }
275
- }
276
- });
277
- },
278
- getChannelInputProps(props) {
279
- const { channel } = props;
280
- const isTextField = channel === "hex" || channel === "css";
281
- const range = getChannelInputValue.getChannelInputRange(valueAsColor, channel);
282
- return normalize.input({
283
- ...colorPicker_anatomy.parts.channelInput.attrs,
284
- type: isTextField ? "text" : "number",
285
- "data-channel": channel,
286
- "aria-label": channel,
287
- disabled: isDisabled,
288
- "data-disabled": domQuery.dataAttr(isDisabled),
289
- readOnly: state.context.readOnly,
290
- id: colorPicker_dom.dom.getChannelInputId(state.context, channel),
291
- defaultValue: getChannelInputValue.getChannelInputValue(valueAsColor, channel),
292
- min: range?.minValue,
293
- max: range?.maxValue,
294
- step: range?.step,
295
- onFocus() {
296
- send({ type: "CHANNEL_INPUT.FOCUS", channel });
297
- },
298
- onChange(event) {
299
- if (isTextField)
300
- return;
301
- const value = event.currentTarget.value;
302
- send({ type: "CHANNEL_INPUT.CHANGE", channel, value, isTextField });
303
- },
304
- onBlur(event) {
305
- const value = event.currentTarget.value;
306
- send({ type: "CHANNEL_INPUT.BLUR", channel, value, isTextField });
307
- },
308
- onKeyDown(event) {
309
- if (!isTextField)
310
- return;
311
- if (event.key === "Enter") {
312
- const value = event.currentTarget.value;
313
- send({ type: "CHANNEL_INPUT.CHANGE", channel, value, isTextField });
314
- }
315
- },
316
- style: {
317
- appearance: "none",
318
- WebkitAppearance: "none",
319
- MozAppearance: "textfield"
320
- }
321
- });
322
- },
323
- eyeDropperTriggerProps: normalize.button({
324
- ...colorPicker_anatomy.parts.eyeDropTrigger.attrs,
325
- onClick() {
326
- send("EYEDROPPER.CLICK");
327
- }
328
- }),
329
- getSwatchBackgroundProps(props) {
330
- const { value } = props;
331
- const alpha = colorUtils.normalizeColor(value).getChannelValue("alpha");
332
- return normalize.element({
333
- ...colorPicker_anatomy.parts.swatchBg.attrs,
334
- "data-alpha": alpha,
335
- style: {
336
- width: "100%",
337
- height: "100%",
338
- background: "#fff",
339
- backgroundImage: [
340
- "linear-gradient(-45deg,#0000 75.5%,#bcbcbc 75.5%)",
341
- "linear-gradient(45deg,#0000 75.5%,#bcbcbc 75.5%)",
342
- "linear-gradient(-45deg,#bcbcbc 25.5%,#0000 25.5%)",
343
- "linear-gradient(45deg,#bcbcbc 25.5%,#0000 25.5%)"
344
- ].join(","),
345
- backgroundPosition: "-2px -2px,-2px 6px,6px -10px,-10px -2px",
346
- backgroundSize: "16px 16px",
347
- position: "absolute",
348
- inset: "0px",
349
- zIndex: -1
350
- }
351
- });
352
- },
353
- getSwatchProps(props) {
354
- const { value, readOnly } = props;
355
- const color = colorUtils.normalizeColor(value).toFormat(valueAsColor.getColorSpace());
356
- return normalize.element({
357
- ...colorPicker_anatomy.parts.swatch.attrs,
358
- onClick() {
359
- if (readOnly)
360
- return;
361
- send({ type: "VALUE.SET", value: color });
362
- },
363
- style: {
364
- position: "relative",
365
- background: color.toString("css")
366
- }
367
- });
368
- }
369
- };
370
- }
371
-
372
- exports.connect = connect;