@zag-js/color-picker 0.69.0 → 0.71.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.
@@ -1,53 +0,0 @@
1
- import { createProps } from "@zag-js/types"
2
- import { createSplitProps } from "@zag-js/utils"
3
- import type {
4
- AreaProps,
5
- ChannelProps,
6
- SwatchProps,
7
- SwatchTriggerProps,
8
- TransparencyGridProps,
9
- UserDefinedContext,
10
- } from "./color-picker.types"
11
-
12
- export const props = createProps<UserDefinedContext>()([
13
- "closeOnSelect",
14
- "dir",
15
- "disabled",
16
- "format",
17
- "getRootNode",
18
- "id",
19
- "ids",
20
- "initialFocusEl",
21
- "name",
22
- "name",
23
- "onFocusOutside",
24
- "onFormatChange",
25
- "onInteractOutside",
26
- "onOpenChange",
27
- "onPointerDownOutside",
28
- "onValueChange",
29
- "onValueChangeEnd",
30
- "open.controlled",
31
- "open",
32
- "positioning",
33
- "required",
34
- "readOnly",
35
- "value",
36
- ])
37
-
38
- export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)
39
-
40
- export const areaProps = createProps<AreaProps>()(["xChannel", "yChannel"])
41
- export const splitAreaProps = createSplitProps<AreaProps>(areaProps)
42
-
43
- export const channelProps = createProps<ChannelProps>()(["channel", "orientation"])
44
- export const splitChannelProps = createSplitProps<ChannelProps>(channelProps)
45
-
46
- export const swatchTriggerProps = createProps<SwatchTriggerProps>()(["value", "disabled"])
47
- export const splitSwatchTriggerProps = createSplitProps<SwatchTriggerProps>(swatchTriggerProps)
48
-
49
- export const swatchProps = createProps<SwatchProps>()(["value", "respectAlpha"])
50
- export const splitSwatchProps = createSplitProps<SwatchProps>(swatchProps)
51
-
52
- export const transparencyGridProps = createProps<TransparencyGridProps>()(["size"])
53
- export const splitTransparencyGridProps = createSplitProps<TransparencyGridProps>(transparencyGridProps)
@@ -1,355 +0,0 @@
1
- import type { Color, ColorAxes, ColorChannel, ColorFormat, ColorType } from "@zag-js/color-utils"
2
- import type { Machine, StateMachine as S } from "@zag-js/core"
3
- import type { InteractOutsideHandlers } from "@zag-js/dismissable"
4
- import type { PositioningOptions } from "@zag-js/popper"
5
- import type { CommonProperties, DirectionProperty, Orientation, PropTypes, RequiredBy } from "@zag-js/types"
6
-
7
- export type ExtendedColorChannel = ColorChannel | "hex" | "css"
8
-
9
- interface EyeDropper {
10
- new (): EyeDropper
11
- open: (options?: { signal?: AbortSignal }) => Promise<{ sRGBHex: string }>
12
- [Symbol.toStringTag]: "EyeDropper"
13
- }
14
-
15
- declare global {
16
- interface Window {
17
- EyeDropper: EyeDropper
18
- }
19
- }
20
-
21
- /* -----------------------------------------------------------------------------
22
- * Callback details
23
- * -----------------------------------------------------------------------------*/
24
-
25
- export interface ValueChangeDetails {
26
- value: Color
27
- valueAsString: string
28
- }
29
-
30
- export interface OpenChangeDetails {
31
- open: boolean
32
- }
33
-
34
- export interface FormatChangeDetails {
35
- format: ColorFormat
36
- }
37
-
38
- /* -----------------------------------------------------------------------------
39
- * Machine context
40
- * -----------------------------------------------------------------------------*/
41
-
42
- export type ElementIds = Partial<{
43
- root: string
44
- control: string
45
- trigger: string
46
- label: string
47
- input: string
48
- hiddenInput: string
49
- content: string
50
- area: string
51
- areaGradient: string
52
- positioner: string
53
- formatSelect: string
54
- areaThumb: string
55
- channelInput(id: string): string
56
- channelSliderTrack(id: ColorChannel): string
57
- channelSliderThumb(id: ColorChannel): string
58
- }>
59
-
60
- interface PublicContext extends CommonProperties, DirectionProperty, InteractOutsideHandlers {
61
- /**
62
- * The ids of the elements in the color picker. Useful for composition.
63
- */
64
- ids?: ElementIds
65
- /**
66
- * The current color value
67
- * @default #000000
68
- */
69
- value: Color
70
- /**
71
- * Whether the color picker is disabled
72
- */
73
- disabled?: boolean
74
- /**
75
- * Whether the color picker is read-only
76
- */
77
- readOnly?: boolean
78
- /**
79
- * Whether the color picker is required
80
- */
81
- required?: boolean
82
- /**
83
- * Handler that is called when the value changes, as the user drags.
84
- */
85
- onValueChange?: (details: ValueChangeDetails) => void
86
- /**
87
- * Handler that is called when the user stops dragging.
88
- */
89
- onValueChangeEnd?: (details: ValueChangeDetails) => void
90
- /**
91
- * Handler that is called when the user opens or closes the color picker.
92
- */
93
- onOpenChange?: (details: OpenChangeDetails) => void
94
- /**
95
- * The name for the form input
96
- */
97
- name?: string
98
- /**
99
- * The positioning options for the color picker
100
- */
101
- positioning: PositioningOptions
102
- /**
103
- * The initial focus element when the color picker is opened.
104
- */
105
- initialFocusEl?: () => HTMLElement | null
106
- /**
107
- * Whether the color picker is open
108
- */
109
- open?: boolean
110
- /**
111
- * Whether the color picker open state is controlled by the user
112
- */
113
- "open.controlled"?: boolean
114
- /**
115
- * The color format to use
116
- * @default "rgba"
117
- */
118
- format: ColorFormat
119
- /**
120
- * Function called when the color format changes
121
- */
122
- onFormatChange?: (details: FormatChangeDetails) => void
123
- /**
124
- * Whether to close the color picker when a swatch is selected
125
- * @default false
126
- */
127
- closeOnSelect?: boolean
128
- }
129
-
130
- interface PrivateContext {
131
- /**
132
- * @internal
133
- * The id of the thumb that is currently being dragged
134
- */
135
- activeId: string | null
136
- /**
137
- * @internal
138
- * The channel that is currently being interacted with
139
- */
140
- activeChannel: Partial<ColorAxes> | null
141
- /**
142
- * @internal
143
- * The orientation of the channel that is currently being interacted with
144
- */
145
- activeOrientation: Orientation | null
146
- /**
147
- * @internal
148
- * Whether the checkbox's fieldset is disabled
149
- */
150
- fieldsetDisabled: boolean
151
- /**
152
- * @internal
153
- * The current placement of the color picker
154
- */
155
- currentPlacement?: PositioningOptions["placement"]
156
- /**
157
- * @internal
158
- * Whether the color picker should return focus to the trigger when closed
159
- */
160
- restoreFocus?: boolean
161
- }
162
-
163
- type ComputedContext = Readonly<{
164
- /**
165
- * @computed
166
- * Whether the color picker is in RTL mode
167
- */
168
- isRtl: boolean
169
- /**
170
- * @computed
171
- * Whether the color picker is interactive
172
- */
173
- isInteractive: boolean
174
- /**
175
- * @computed
176
- * The color value as a Color object
177
- */
178
- valueAsString: string
179
- /**
180
- * @computed
181
- * Whether the color picker is disabled
182
- */
183
- isDisabled: boolean
184
- /**
185
- * @computed
186
- * The area value as a Color object
187
- */
188
- areaValue: Color
189
- }>
190
-
191
- export type UserDefinedContext = RequiredBy<PublicContext, "id">
192
-
193
- export interface MachineContext extends PublicContext, PrivateContext, ComputedContext {}
194
-
195
- export interface MachineState {
196
- tags: "open" | "closed" | "dragging" | "focused"
197
- value: "idle" | "focused" | "open" | "open:dragging"
198
- }
199
-
200
- export type State = S.State<MachineContext, MachineState>
201
-
202
- export type Send = S.Send<S.AnyEventObject>
203
-
204
- export type Service = Machine<MachineContext, MachineState, S.AnyEventObject>
205
-
206
- /* -----------------------------------------------------------------------------
207
- * Component API
208
- * -----------------------------------------------------------------------------*/
209
-
210
- export interface ChannelProps {
211
- channel: ColorChannel
212
- orientation?: Orientation
213
- }
214
-
215
- export interface ChannelSliderProps extends ChannelProps {
216
- format?: ColorFormat
217
- }
218
-
219
- export interface ChannelInputProps {
220
- channel: ExtendedColorChannel
221
- orientation?: Orientation
222
- }
223
-
224
- export interface AreaProps {
225
- xChannel?: ColorChannel
226
- yChannel?: ColorChannel
227
- }
228
-
229
- export interface SwatchTriggerProps {
230
- /**
231
- * The color value
232
- */
233
- value: string | Color
234
- /**
235
- * Whether the swatch trigger is disabled
236
- */
237
- disabled?: boolean
238
- }
239
-
240
- export interface SwatchTriggerState {
241
- value: Color
242
- valueAsString: string
243
- checked: boolean
244
- disabled: boolean
245
- }
246
-
247
- export interface SwatchProps {
248
- /**
249
- * The color value
250
- */
251
- value: string | Color
252
- /**
253
- * Whether to include the alpha channel in the color
254
- */
255
- respectAlpha?: boolean
256
- }
257
-
258
- export interface TransparencyGridProps {
259
- size?: string
260
- }
261
-
262
- export interface MachineApi<T extends PropTypes = PropTypes> {
263
- /**
264
- * Whether the color picker is being dragged
265
- */
266
- dragging: boolean
267
- /**
268
- * Whether the color picker is open
269
- */
270
- open: boolean
271
- /**
272
- * The current color value (as a string)
273
- */
274
- value: Color
275
- /**
276
- * The current color value (as a Color object)
277
- */
278
- valueAsString: string
279
- /**
280
- * Function to set the color value
281
- */
282
- setValue(value: string | Color): void
283
- /**
284
- * Function to set the color value
285
- */
286
- getChannelValue(channel: ColorChannel): string
287
- /**
288
- * Function to get the formatted and localized value of a specific channel
289
- */
290
- getChannelValueText(channel: ColorChannel, locale: string): string
291
- /**
292
- * Function to set the color value of a specific channel
293
- */
294
- setChannelValue(channel: ColorChannel, value: number): void
295
- /**
296
- * The current color format
297
- */
298
- format: ColorFormat
299
- /**
300
- * Function to set the color format
301
- */
302
- setFormat(format: ColorFormat): void
303
- /**
304
- * The alpha value of the color
305
- */
306
- alpha: number
307
- /**
308
- * Function to set the color alpha
309
- */
310
- setAlpha(value: number): void
311
- /**
312
- * Function to open or close the color picker
313
- */
314
- setOpen(open: boolean): void
315
-
316
- getRootProps(): T["element"]
317
- getLabelProps(): T["element"]
318
- getControlProps(): T["element"]
319
- getTriggerProps(): T["button"]
320
- getPositionerProps(): T["element"]
321
- getContentProps(): T["element"]
322
- getHiddenInputProps(): T["input"]
323
- getValueTextProps(): T["element"]
324
-
325
- getAreaProps(props?: AreaProps): T["element"]
326
- getAreaBackgroundProps(props?: AreaProps): T["element"]
327
- getAreaThumbProps(props?: AreaProps): T["element"]
328
-
329
- getChannelInputProps(props: ChannelInputProps): T["input"]
330
-
331
- getChannelSliderProps(props: ChannelSliderProps): T["element"]
332
- getChannelSliderTrackProps(props: ChannelSliderProps): T["element"]
333
- getChannelSliderThumbProps(props: ChannelSliderProps): T["element"]
334
- getChannelSliderLabelProps(props: ChannelProps): T["element"]
335
- getChannelSliderValueTextProps(props: ChannelProps): T["element"]
336
-
337
- getTransparencyGridProps(props?: TransparencyGridProps): T["element"]
338
-
339
- getEyeDropperTriggerProps(): T["button"]
340
-
341
- getSwatchGroupProps(): T["element"]
342
- getSwatchTriggerProps(props: SwatchTriggerProps): T["button"]
343
- getSwatchTriggerState(props: SwatchTriggerProps): SwatchTriggerState
344
- getSwatchProps(props: SwatchProps): T["element"]
345
- getSwatchIndicatorProps(props: SwatchProps): T["element"]
346
-
347
- getFormatSelectProps(): T["select"]
348
- getFormatTriggerProps(): T["button"]
349
- }
350
-
351
- /* -----------------------------------------------------------------------------
352
- * Re-exported types
353
- * -----------------------------------------------------------------------------*/
354
-
355
- export type { Color, ColorAxes, ColorChannel, ColorFormat, ColorType, PositioningOptions }
package/src/index.ts DELETED
@@ -1,28 +0,0 @@
1
- export type { FocusOutsideEvent, InteractOutsideEvent, PointerDownOutsideEvent } from "@zag-js/dismissable"
2
- export { anatomy } from "./color-picker.anatomy"
3
- export { connect } from "./color-picker.connect"
4
- export { machine } from "./color-picker.machine"
5
- export { parse } from "./color-picker.parse"
6
- export type {
7
- MachineApi as Api,
8
- AreaProps,
9
- ChannelSliderProps,
10
- ChannelInputProps,
11
- ChannelProps,
12
- Color,
13
- ColorAxes,
14
- ColorChannel,
15
- ColorFormat,
16
- ColorType,
17
- UserDefinedContext as Context,
18
- ElementIds,
19
- FormatChangeDetails,
20
- OpenChangeDetails,
21
- PositioningOptions,
22
- SwatchProps,
23
- SwatchTriggerProps,
24
- SwatchTriggerState,
25
- TransparencyGridProps,
26
- ValueChangeDetails,
27
- Service,
28
- } from "./color-picker.types"
@@ -1,20 +0,0 @@
1
- import { parseColor, type Color, type ColorChannel } from "@zag-js/color-utils"
2
-
3
- export function getChannelDisplayColor(color: Color, channel: ColorChannel) {
4
- switch (channel) {
5
- case "hue":
6
- return parseColor(`hsl(${color.getChannelValue("hue")}, 100%, 50%)`)
7
- case "lightness":
8
- case "brightness":
9
- case "saturation":
10
- case "red":
11
- case "green":
12
- case "blue":
13
- return color.withChannelValue("alpha", 1)
14
- case "alpha": {
15
- return color
16
- }
17
- default:
18
- throw new Error("Unknown color channel: " + channel)
19
- }
20
- }
@@ -1,79 +0,0 @@
1
- import { parseColor, type Color, type ColorChannelRange } from "@zag-js/color-utils"
2
- import type { ExtendedColorChannel } from "../color-picker.types"
3
-
4
- export function getChannelValue(color: Color, channel: ExtendedColorChannel | null | undefined): string {
5
- if (channel == null) return ""
6
-
7
- if (channel === "hex") {
8
- return color.toString("hex")
9
- }
10
-
11
- if (channel === "css") {
12
- return color.toString("css")
13
- }
14
-
15
- if (channel in color) {
16
- return color.getChannelValue(channel).toString()
17
- }
18
-
19
- const isHSL = color.getFormat() === "hsla"
20
-
21
- switch (channel) {
22
- case "hue":
23
- return isHSL
24
- ? color.toFormat("hsla").getChannelValue("hue").toString()
25
- : color.toFormat("hsba").getChannelValue("hue").toString()
26
-
27
- case "saturation":
28
- return isHSL
29
- ? color.toFormat("hsla").getChannelValue("saturation").toString()
30
- : color.toFormat("hsba").getChannelValue("saturation").toString()
31
-
32
- case "lightness":
33
- return color.toFormat("hsla").getChannelValue("lightness").toString()
34
-
35
- case "brightness":
36
- return color.toFormat("hsba").getChannelValue("brightness").toString()
37
-
38
- case "red":
39
- case "green":
40
- case "blue":
41
- return color.toFormat("rgba").getChannelValue(channel).toString()
42
-
43
- default:
44
- return color.getChannelValue(channel).toString()
45
- }
46
- }
47
-
48
- export function getChannelRange(color: Color, channel: ExtendedColorChannel): ColorChannelRange | undefined {
49
- switch (channel) {
50
- case "hex":
51
- const minColor = parseColor("#000000")
52
- const maxColor = parseColor("#FFFFFF")
53
- return {
54
- minValue: minColor.toHexInt(),
55
- maxValue: maxColor.toHexInt(),
56
- pageSize: 10,
57
- step: 1,
58
- }
59
-
60
- case "css":
61
- return undefined
62
-
63
- case "hue":
64
- case "saturation":
65
- case "lightness":
66
- return color.toFormat("hsla").getChannelRange(channel)
67
-
68
- case "brightness":
69
- return color.toFormat("hsba").getChannelRange(channel)
70
-
71
- case "red":
72
- case "green":
73
- case "blue":
74
- return color.toFormat("rgba").getChannelRange(channel)
75
-
76
- default:
77
- return color.getChannelRange(channel)
78
- }
79
- }
@@ -1,45 +0,0 @@
1
- import type { ChannelProps, Color, MachineContext } from "../color-picker.types"
2
-
3
- function getSliderBackgroundDirection(orientation: "vertical" | "horizontal", dir: "ltr" | "rtl") {
4
- if (orientation === "vertical") {
5
- return "top"
6
- } else if (dir === "ltr") {
7
- return "right"
8
- } else {
9
- return "left"
10
- }
11
- }
12
-
13
- interface SliderBackgroundProps extends Required<ChannelProps> {
14
- value: Color
15
- dir: MachineContext["dir"]
16
- }
17
-
18
- export const getSliderBackground = (props: SliderBackgroundProps) => {
19
- const { channel, value, dir } = props
20
- const bgDirection = getSliderBackgroundDirection(props.orientation, dir!)
21
- const { minValue, maxValue } = value.getChannelRange(channel)
22
-
23
- switch (channel) {
24
- case "hue":
25
- return `linear-gradient(to ${bgDirection}, rgb(255, 0, 0) 0%, rgb(255, 255, 0) 17%, rgb(0, 255, 0) 33%, rgb(0, 255, 255) 50%, rgb(0, 0, 255) 67%, rgb(255, 0, 255) 83%, rgb(255, 0, 0) 100%)`
26
- case "lightness": {
27
- let start = value.withChannelValue(channel, minValue).toString("css")
28
- let middle = value.withChannelValue(channel, (maxValue - minValue) / 2).toString("css")
29
- let end = value.withChannelValue(channel, maxValue).toString("css")
30
- return `linear-gradient(to ${bgDirection}, ${start}, ${middle}, ${end})`
31
- }
32
- case "saturation":
33
- case "brightness":
34
- case "red":
35
- case "green":
36
- case "blue":
37
- case "alpha": {
38
- let start = value.withChannelValue(channel, minValue).toString("css")
39
- let end = value.withChannelValue(channel, maxValue).toString("css")
40
- return `linear-gradient(to ${bgDirection}, ${start}, ${end})`
41
- }
42
- default:
43
- throw new Error("Unknown color channel: " + channel)
44
- }
45
- }