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