@zag-js/color-picker 0.28.1 → 0.30.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 +47 -10
- package/dist/index.d.ts +47 -10
- package/dist/index.js +220 -89
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +220 -89
- package/dist/index.mjs.map +1 -1
- package/package.json +14 -14
- package/src/color-picker.anatomy.ts +3 -0
- package/src/color-picker.connect.ts +134 -28
- package/src/color-picker.dom.ts +3 -2
- package/src/color-picker.machine.ts +90 -49
- package/src/color-picker.parse.ts +2 -10
- package/src/color-picker.types.ts +49 -8
- package/src/index.ts +5 -1
- package/src/utils/get-channel-input-value.ts +5 -5
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import { parseColor, type Color } from "@zag-js/color-utils"
|
|
2
2
|
|
|
3
|
-
export const parse = (
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
const colorFormat = value.getFormat()
|
|
7
|
-
const lockedFormat = colorFormat === "hsla" ? "hsla" : "hsba"
|
|
8
|
-
|
|
9
|
-
const result = value.toFormat(lockedFormat)
|
|
10
|
-
result.outputFormat = colorFormat
|
|
11
|
-
|
|
12
|
-
return result
|
|
3
|
+
export const parse = (colorString: string): Color => {
|
|
4
|
+
return parseColor(colorString)
|
|
13
5
|
}
|
|
@@ -5,6 +5,8 @@ import type { PositioningOptions } from "@zag-js/popper"
|
|
|
5
5
|
import type { CommonProperties, Context, MaybeElement, Orientation, PropTypes, RequiredBy } from "@zag-js/types"
|
|
6
6
|
import type { MaybeFunction } from "@zag-js/utils"
|
|
7
7
|
|
|
8
|
+
export type ExtendedColorChannel = ColorChannel | "hex" | "css"
|
|
9
|
+
|
|
8
10
|
/* -----------------------------------------------------------------------------
|
|
9
11
|
* Callback details
|
|
10
12
|
* -----------------------------------------------------------------------------*/
|
|
@@ -18,13 +20,15 @@ export interface OpenChangeDetails {
|
|
|
18
20
|
open: boolean
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
export interface FormatChangeDetails {
|
|
24
|
+
format: ColorFormat
|
|
25
|
+
}
|
|
26
|
+
|
|
21
27
|
/* -----------------------------------------------------------------------------
|
|
22
28
|
* Machine context
|
|
23
29
|
* -----------------------------------------------------------------------------*/
|
|
24
30
|
|
|
25
|
-
export type
|
|
26
|
-
|
|
27
|
-
type ElementIds = Partial<{
|
|
31
|
+
export type ElementIds = Partial<{
|
|
28
32
|
root: string
|
|
29
33
|
control: string
|
|
30
34
|
trigger: string
|
|
@@ -80,11 +84,7 @@ interface PublicContext extends CommonProperties, InteractOutsideHandlers {
|
|
|
80
84
|
* The positioning options for the color picker
|
|
81
85
|
*/
|
|
82
86
|
positioning: PositioningOptions
|
|
83
|
-
|
|
84
|
-
* Whether to automatically set focus on the first focusable
|
|
85
|
-
* content within the color picker when opened.
|
|
86
|
-
*/
|
|
87
|
-
autoFocus?: boolean
|
|
87
|
+
|
|
88
88
|
/**
|
|
89
89
|
* The initial focus element when the color picker is opened.
|
|
90
90
|
*/
|
|
@@ -93,6 +93,18 @@ interface PublicContext extends CommonProperties, InteractOutsideHandlers {
|
|
|
93
93
|
* Whether the color picker is open
|
|
94
94
|
*/
|
|
95
95
|
open?: boolean
|
|
96
|
+
/**
|
|
97
|
+
* The color format to use
|
|
98
|
+
*/
|
|
99
|
+
format: ColorFormat
|
|
100
|
+
/**
|
|
101
|
+
* Function called when the color format changes
|
|
102
|
+
*/
|
|
103
|
+
onFormatChange?: (details: FormatChangeDetails) => void
|
|
104
|
+
/**
|
|
105
|
+
* Whether to close the color picker when a swatch is selected
|
|
106
|
+
*/
|
|
107
|
+
closeOnSelect?: boolean
|
|
96
108
|
}
|
|
97
109
|
|
|
98
110
|
type PrivateContext = Context<{
|
|
@@ -144,6 +156,11 @@ type ComputedContext = Readonly<{
|
|
|
144
156
|
* Whether the color picker is disabled
|
|
145
157
|
*/
|
|
146
158
|
isDisabled: boolean
|
|
159
|
+
/**
|
|
160
|
+
* @computed
|
|
161
|
+
* The area value as a Color object
|
|
162
|
+
*/
|
|
163
|
+
areaValue: Color
|
|
147
164
|
}>
|
|
148
165
|
|
|
149
166
|
export type UserDefinedContext = RequiredBy<PublicContext, "id">
|
|
@@ -183,6 +200,17 @@ export interface SwatchTriggerProps {
|
|
|
183
200
|
* The color value
|
|
184
201
|
*/
|
|
185
202
|
value: string | Color
|
|
203
|
+
/**
|
|
204
|
+
* Whether the swatch trigger is disabled
|
|
205
|
+
*/
|
|
206
|
+
disabled?: boolean
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export interface SwatchTriggerState {
|
|
210
|
+
value: Color
|
|
211
|
+
valueAsString: string
|
|
212
|
+
isChecked: boolean
|
|
213
|
+
isDisabled: boolean
|
|
186
214
|
}
|
|
187
215
|
|
|
188
216
|
export interface SwatchProps {
|
|
@@ -245,6 +273,14 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
245
273
|
* Function to set the color alpha
|
|
246
274
|
*/
|
|
247
275
|
setAlpha(value: number): void
|
|
276
|
+
/**
|
|
277
|
+
* Function to open the color picker
|
|
278
|
+
*/
|
|
279
|
+
open(): void
|
|
280
|
+
/**
|
|
281
|
+
* Function to close the color picker
|
|
282
|
+
*/
|
|
283
|
+
close(): void
|
|
248
284
|
|
|
249
285
|
rootProps: T["element"]
|
|
250
286
|
labelProps: T["element"]
|
|
@@ -269,7 +305,12 @@ export interface MachineApi<T extends PropTypes = PropTypes> {
|
|
|
269
305
|
|
|
270
306
|
swatchGroupProps: T["element"]
|
|
271
307
|
getSwatchTriggerProps(props: SwatchTriggerProps): T["button"]
|
|
308
|
+
getSwatchTriggerState(props: SwatchTriggerProps): SwatchTriggerState
|
|
272
309
|
getSwatchProps(props: SwatchProps): T["element"]
|
|
310
|
+
getSwatchIndicatorProps(props: SwatchProps): T["element"]
|
|
311
|
+
|
|
312
|
+
formatSelectProps: T["select"]
|
|
313
|
+
formatTriggerProps: T["button"]
|
|
273
314
|
}
|
|
274
315
|
|
|
275
316
|
/* -----------------------------------------------------------------------------
|
package/src/index.ts
CHANGED
|
@@ -13,7 +13,11 @@ export type {
|
|
|
13
13
|
ColorFormat,
|
|
14
14
|
ColorType,
|
|
15
15
|
UserDefinedContext as Context,
|
|
16
|
+
ElementIds,
|
|
17
|
+
OpenChangeDetails,
|
|
16
18
|
SwatchProps,
|
|
17
|
-
TransparencyGridProps,
|
|
18
19
|
SwatchTriggerProps,
|
|
20
|
+
SwatchTriggerState,
|
|
21
|
+
TransparencyGridProps,
|
|
22
|
+
ValueChangeDetails,
|
|
19
23
|
} from "./color-picker.types"
|
|
@@ -26,8 +26,8 @@ export function getChannelValue(color: Color, channel: ExtendedColorChannel | nu
|
|
|
26
26
|
|
|
27
27
|
case "saturation":
|
|
28
28
|
return isHSL
|
|
29
|
-
? color.toFormat("
|
|
30
|
-
: color.toFormat("
|
|
29
|
+
? color.toFormat("hsla").getChannelValue("saturation").toString()
|
|
30
|
+
: color.toFormat("hsba").getChannelValue("saturation").toString()
|
|
31
31
|
|
|
32
32
|
case "lightness":
|
|
33
33
|
return color.toFormat("hsla").getChannelValue("lightness").toString()
|
|
@@ -63,15 +63,15 @@ export function getChannelRange(color: Color, channel: ExtendedColorChannel): Co
|
|
|
63
63
|
case "hue":
|
|
64
64
|
case "saturation":
|
|
65
65
|
case "lightness":
|
|
66
|
-
return color.toFormat("
|
|
66
|
+
return color.toFormat("hsla").getChannelRange(channel)
|
|
67
67
|
|
|
68
68
|
case "brightness":
|
|
69
|
-
return color.toFormat("
|
|
69
|
+
return color.toFormat("hsba").getChannelRange(channel)
|
|
70
70
|
|
|
71
71
|
case "red":
|
|
72
72
|
case "green":
|
|
73
73
|
case "blue":
|
|
74
|
-
return color.toFormat("
|
|
74
|
+
return color.toFormat("rgba").getChannelRange(channel)
|
|
75
75
|
|
|
76
76
|
default:
|
|
77
77
|
return color.getChannelRange(channel)
|