@yamada-ui/color-picker 1.0.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/LICENSE +21 -0
- package/README.md +28 -0
- package/dist/alpha-slider.d.mts +62 -0
- package/dist/alpha-slider.d.ts +62 -0
- package/dist/alpha-slider.js +438 -0
- package/dist/alpha-slider.mjs +8 -0
- package/dist/chunk-3EO2XBIZ.mjs +54 -0
- package/dist/chunk-3KASGDAY.mjs +122 -0
- package/dist/chunk-3N443XSV.mjs +105 -0
- package/dist/chunk-73BVTMII.mjs +73 -0
- package/dist/chunk-7QGCTVDG.mjs +66 -0
- package/dist/chunk-DTB2Z3IJ.mjs +278 -0
- package/dist/chunk-EMWFECJD.mjs +123 -0
- package/dist/chunk-HNVFOSAM.mjs +210 -0
- package/dist/chunk-KJS52X4M.mjs +330 -0
- package/dist/chunk-MAHWFXUG.mjs +294 -0
- package/dist/chunk-N2GG5L7Q.mjs +39 -0
- package/dist/chunk-NR2XCTSD.mjs +430 -0
- package/dist/chunk-PTTNJPGJ.mjs +98 -0
- package/dist/chunk-PVWIBTBL.mjs +67 -0
- package/dist/chunk-TFQ62YSQ.mjs +68 -0
- package/dist/chunk-U6OJ4TDG.mjs +127 -0
- package/dist/color-input.d.mts +79 -0
- package/dist/color-input.d.ts +79 -0
- package/dist/color-input.js +2283 -0
- package/dist/color-input.mjs +21 -0
- package/dist/color-picker-body.d.mts +38 -0
- package/dist/color-picker-body.d.ts +38 -0
- package/dist/color-picker-body.js +794 -0
- package/dist/color-picker-body.mjs +14 -0
- package/dist/color-picker-channels.d.mts +20 -0
- package/dist/color-picker-channels.d.ts +20 -0
- package/dist/color-picker-channels.js +101 -0
- package/dist/color-picker-channels.mjs +10 -0
- package/dist/color-picker-eye-dropper.d.mts +10 -0
- package/dist/color-picker-eye-dropper.d.ts +10 -0
- package/dist/color-picker-eye-dropper.js +88 -0
- package/dist/color-picker-eye-dropper.mjs +10 -0
- package/dist/color-picker-sliders.d.mts +30 -0
- package/dist/color-picker-sliders.d.ts +30 -0
- package/dist/color-picker-sliders.js +590 -0
- package/dist/color-picker-sliders.mjs +11 -0
- package/dist/color-picker-swatches.d.mts +34 -0
- package/dist/color-picker-swatches.d.ts +34 -0
- package/dist/color-picker-swatches.js +195 -0
- package/dist/color-picker-swatches.mjs +9 -0
- package/dist/color-picker.d.mts +59 -0
- package/dist/color-picker.d.ts +59 -0
- package/dist/color-picker.js +1790 -0
- package/dist/color-picker.mjs +19 -0
- package/dist/color-swatch.d.mts +36 -0
- package/dist/color-swatch.d.ts +36 -0
- package/dist/color-swatch.js +124 -0
- package/dist/color-swatch.mjs +6 -0
- package/dist/hue-slider.d.mts +56 -0
- package/dist/hue-slider.d.ts +56 -0
- package/dist/hue-slider.js +409 -0
- package/dist/hue-slider.mjs +8 -0
- package/dist/index.d.mts +23 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +2295 -0
- package/dist/index.mjs +36 -0
- package/dist/saturation-slider.d.mts +33 -0
- package/dist/saturation-slider.d.ts +33 -0
- package/dist/saturation-slider.js +414 -0
- package/dist/saturation-slider.mjs +7 -0
- package/dist/use-color-input.d.mts +90 -0
- package/dist/use-color-input.d.ts +90 -0
- package/dist/use-color-input.js +340 -0
- package/dist/use-color-input.mjs +10 -0
- package/dist/use-color-picker.d.mts +131 -0
- package/dist/use-color-picker.d.ts +131 -0
- package/dist/use-color-picker.js +472 -0
- package/dist/use-color-picker.mjs +11 -0
- package/dist/use-color-slider.d.mts +69 -0
- package/dist/use-color-slider.d.ts +69 -0
- package/dist/use-color-slider.js +321 -0
- package/dist/use-color-slider.mjs +7 -0
- package/dist/use-saturation-slider.d.mts +77 -0
- package/dist/use-saturation-slider.d.ts +77 -0
- package/dist/use-saturation-slider.js +302 -0
- package/dist/use-saturation-slider.mjs +6 -0
- package/package.json +88 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SaturationSlider
|
|
3
|
+
} from "./chunk-3KASGDAY.mjs";
|
|
4
|
+
import {
|
|
5
|
+
ColorPickerBody
|
|
6
|
+
} from "./chunk-73BVTMII.mjs";
|
|
7
|
+
import {
|
|
8
|
+
ColorPickerChannels
|
|
9
|
+
} from "./chunk-PVWIBTBL.mjs";
|
|
10
|
+
import {
|
|
11
|
+
ColorPickerSwatches
|
|
12
|
+
} from "./chunk-TFQ62YSQ.mjs";
|
|
13
|
+
import {
|
|
14
|
+
ColorPickerProvider,
|
|
15
|
+
useColorPicker
|
|
16
|
+
} from "./chunk-NR2XCTSD.mjs";
|
|
17
|
+
|
|
18
|
+
// src/color-picker.tsx
|
|
19
|
+
import {
|
|
20
|
+
ui,
|
|
21
|
+
forwardRef,
|
|
22
|
+
useMultiComponentStyle,
|
|
23
|
+
omitThemeProps
|
|
24
|
+
} from "@yamada-ui/core";
|
|
25
|
+
import { cx } from "@yamada-ui/utils";
|
|
26
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
27
|
+
var ColorPicker = forwardRef(
|
|
28
|
+
({ size, ...props }, ref) => {
|
|
29
|
+
const [styles, mergedProps] = useMultiComponentStyle("ColorPicker", {
|
|
30
|
+
size,
|
|
31
|
+
...props
|
|
32
|
+
});
|
|
33
|
+
const {
|
|
34
|
+
className,
|
|
35
|
+
withResult = true,
|
|
36
|
+
withPicker = true,
|
|
37
|
+
withChannel = true,
|
|
38
|
+
swatches,
|
|
39
|
+
swatchesColumns = 7,
|
|
40
|
+
inputProps,
|
|
41
|
+
withEyeDropper,
|
|
42
|
+
eyeDropperRef,
|
|
43
|
+
eyeDropperProps,
|
|
44
|
+
saturationSliderRef,
|
|
45
|
+
saturationSliderProps,
|
|
46
|
+
hueSliderRef,
|
|
47
|
+
hueSliderProps,
|
|
48
|
+
alphaSliderRef,
|
|
49
|
+
alphaSliderProps,
|
|
50
|
+
swatchesLabel,
|
|
51
|
+
swatchProps,
|
|
52
|
+
swatchesProps,
|
|
53
|
+
...computedProps
|
|
54
|
+
} = omitThemeProps(mergedProps);
|
|
55
|
+
const {
|
|
56
|
+
getContainerProps,
|
|
57
|
+
getInputProps,
|
|
58
|
+
getSaturationSliderProps,
|
|
59
|
+
...rest
|
|
60
|
+
} = useColorPicker(computedProps);
|
|
61
|
+
const css = {
|
|
62
|
+
display: "flex",
|
|
63
|
+
flexDirection: "column",
|
|
64
|
+
...styles.container
|
|
65
|
+
};
|
|
66
|
+
return /* @__PURE__ */ jsx(ColorPickerProvider, { value: { styles, size, ...rest }, children: /* @__PURE__ */ jsxs(
|
|
67
|
+
ui.div,
|
|
68
|
+
{
|
|
69
|
+
ref,
|
|
70
|
+
className: cx("ui-color-picker", className),
|
|
71
|
+
__css: css,
|
|
72
|
+
...getContainerProps(),
|
|
73
|
+
children: [
|
|
74
|
+
/* @__PURE__ */ jsx(ui.input, { ...getInputProps(inputProps, ref) }),
|
|
75
|
+
withPicker ? /* @__PURE__ */ jsx(
|
|
76
|
+
SaturationSlider,
|
|
77
|
+
{
|
|
78
|
+
size,
|
|
79
|
+
className: "ui-color-picker__saturation-slider",
|
|
80
|
+
__css: { ...styles.saturationSlider },
|
|
81
|
+
...getSaturationSliderProps(
|
|
82
|
+
saturationSliderProps,
|
|
83
|
+
saturationSliderRef
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
) : null,
|
|
87
|
+
withPicker ? /* @__PURE__ */ jsx(
|
|
88
|
+
ColorPickerBody,
|
|
89
|
+
{
|
|
90
|
+
...{
|
|
91
|
+
withResult,
|
|
92
|
+
withEyeDropper,
|
|
93
|
+
eyeDropperRef,
|
|
94
|
+
eyeDropperProps,
|
|
95
|
+
hueSliderRef,
|
|
96
|
+
hueSliderProps,
|
|
97
|
+
alphaSliderRef,
|
|
98
|
+
alphaSliderProps
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
) : null,
|
|
102
|
+
withPicker && withChannel ? /* @__PURE__ */ jsx(ColorPickerChannels, {}) : null,
|
|
103
|
+
/* @__PURE__ */ jsx(
|
|
104
|
+
ColorPickerSwatches,
|
|
105
|
+
{
|
|
106
|
+
...{
|
|
107
|
+
swatchesLabel,
|
|
108
|
+
swatches,
|
|
109
|
+
swatchesColumns,
|
|
110
|
+
swatchProps,
|
|
111
|
+
...swatchesProps
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
)
|
|
115
|
+
]
|
|
116
|
+
}
|
|
117
|
+
) });
|
|
118
|
+
}
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
export {
|
|
122
|
+
ColorPicker
|
|
123
|
+
};
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ColorInputProvider,
|
|
3
|
+
useColorInput,
|
|
4
|
+
useColorInputContext
|
|
5
|
+
} from "./chunk-KJS52X4M.mjs";
|
|
6
|
+
import {
|
|
7
|
+
ColorPicker
|
|
8
|
+
} from "./chunk-EMWFECJD.mjs";
|
|
9
|
+
import {
|
|
10
|
+
EyeDropperIcon
|
|
11
|
+
} from "./chunk-3EO2XBIZ.mjs";
|
|
12
|
+
import {
|
|
13
|
+
ColorSwatch
|
|
14
|
+
} from "./chunk-3N443XSV.mjs";
|
|
15
|
+
|
|
16
|
+
// src/color-input.tsx
|
|
17
|
+
import {
|
|
18
|
+
ui,
|
|
19
|
+
forwardRef,
|
|
20
|
+
useMultiComponentStyle,
|
|
21
|
+
omitThemeProps
|
|
22
|
+
} from "@yamada-ui/core";
|
|
23
|
+
import { Popover, PopoverContent, PopoverTrigger } from "@yamada-ui/popover";
|
|
24
|
+
import { Portal } from "@yamada-ui/portal";
|
|
25
|
+
import {
|
|
26
|
+
cx,
|
|
27
|
+
getValidChildren,
|
|
28
|
+
isValidElement,
|
|
29
|
+
omitObject
|
|
30
|
+
} from "@yamada-ui/utils";
|
|
31
|
+
import { cloneElement } from "react";
|
|
32
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
33
|
+
var ColorInput = forwardRef(
|
|
34
|
+
({ withSwatch = true, ...props }, ref) => {
|
|
35
|
+
const [styles, mergedProps] = useMultiComponentStyle("ColorInput", {
|
|
36
|
+
withSwatch,
|
|
37
|
+
...props
|
|
38
|
+
});
|
|
39
|
+
let {
|
|
40
|
+
className,
|
|
41
|
+
withEyeDropper = true,
|
|
42
|
+
color,
|
|
43
|
+
h,
|
|
44
|
+
height,
|
|
45
|
+
minH,
|
|
46
|
+
minHeight,
|
|
47
|
+
containerProps,
|
|
48
|
+
inputProps,
|
|
49
|
+
swatchProps,
|
|
50
|
+
eyeDropperProps,
|
|
51
|
+
portalProps = { isDisabled: true },
|
|
52
|
+
...computedProps
|
|
53
|
+
} = omitThemeProps(omitObject(mergedProps, ["withSwatch"]));
|
|
54
|
+
const {
|
|
55
|
+
allowInput,
|
|
56
|
+
getPopoverProps,
|
|
57
|
+
getContainerProps,
|
|
58
|
+
getFieldProps,
|
|
59
|
+
getPickerProps,
|
|
60
|
+
getEyeDropperProps,
|
|
61
|
+
...rest
|
|
62
|
+
} = useColorInput(computedProps);
|
|
63
|
+
h != null ? h : h = height;
|
|
64
|
+
minH != null ? minH : minH = minHeight;
|
|
65
|
+
const css = {
|
|
66
|
+
w: "100%",
|
|
67
|
+
h: "fit-content",
|
|
68
|
+
color,
|
|
69
|
+
...styles.container
|
|
70
|
+
};
|
|
71
|
+
return /* @__PURE__ */ jsx(ColorInputProvider, { value: { styles, ...rest }, children: /* @__PURE__ */ jsx(Popover, { ...getPopoverProps(), children: /* @__PURE__ */ jsxs(
|
|
72
|
+
ui.div,
|
|
73
|
+
{
|
|
74
|
+
className: cx("ui-color-input", className),
|
|
75
|
+
__css: css,
|
|
76
|
+
...getContainerProps(containerProps),
|
|
77
|
+
children: [
|
|
78
|
+
/* @__PURE__ */ jsxs(
|
|
79
|
+
ui.div,
|
|
80
|
+
{
|
|
81
|
+
className: "ui-color-input__inner",
|
|
82
|
+
__css: {
|
|
83
|
+
position: "relative",
|
|
84
|
+
cursor: !allowInput ? "pointer" : void 0,
|
|
85
|
+
...styles.inner
|
|
86
|
+
},
|
|
87
|
+
children: [
|
|
88
|
+
withSwatch ? /* @__PURE__ */ jsx(ColorInputSwatch, { ...swatchProps }) : null,
|
|
89
|
+
/* @__PURE__ */ jsx(PopoverTrigger, { children: /* @__PURE__ */ jsx(
|
|
90
|
+
ColorInputField,
|
|
91
|
+
{
|
|
92
|
+
h,
|
|
93
|
+
minH,
|
|
94
|
+
...getFieldProps(inputProps, ref)
|
|
95
|
+
}
|
|
96
|
+
) }),
|
|
97
|
+
withEyeDropper ? /* @__PURE__ */ jsx(
|
|
98
|
+
ColorInputEyeDropper,
|
|
99
|
+
{
|
|
100
|
+
...getEyeDropperProps(eyeDropperProps)
|
|
101
|
+
}
|
|
102
|
+
) : null
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
),
|
|
106
|
+
/* @__PURE__ */ jsx(Portal, { ...portalProps, children: /* @__PURE__ */ jsx(
|
|
107
|
+
PopoverContent,
|
|
108
|
+
{
|
|
109
|
+
className: "ui-color-input__popover",
|
|
110
|
+
__css: { ...styles.list },
|
|
111
|
+
children: /* @__PURE__ */ jsx(
|
|
112
|
+
ColorPicker,
|
|
113
|
+
{
|
|
114
|
+
className: "ui-color-input__picker",
|
|
115
|
+
...getPickerProps()
|
|
116
|
+
}
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
) })
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
) }) });
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
var ColorInputField = forwardRef(
|
|
126
|
+
({ className, h, minH, ...rest }, ref) => {
|
|
127
|
+
const { styles } = useColorInputContext();
|
|
128
|
+
const css = {
|
|
129
|
+
ps: "2rem",
|
|
130
|
+
pe: "2rem",
|
|
131
|
+
h,
|
|
132
|
+
minH,
|
|
133
|
+
display: "flex",
|
|
134
|
+
alignItems: "center",
|
|
135
|
+
...styles.field
|
|
136
|
+
};
|
|
137
|
+
return /* @__PURE__ */ jsx(
|
|
138
|
+
ui.input,
|
|
139
|
+
{
|
|
140
|
+
ref,
|
|
141
|
+
className: cx("ui-color-input__field", className),
|
|
142
|
+
__css: css,
|
|
143
|
+
...rest
|
|
144
|
+
}
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
);
|
|
148
|
+
var ColorInputSwatch = forwardRef(
|
|
149
|
+
({ className, ...rest }, ref) => {
|
|
150
|
+
const { value, styles } = useColorInputContext();
|
|
151
|
+
const css = {
|
|
152
|
+
position: "absolute",
|
|
153
|
+
top: "50%",
|
|
154
|
+
transform: "translateY(-50%)",
|
|
155
|
+
...styles.swatch
|
|
156
|
+
};
|
|
157
|
+
return /* @__PURE__ */ jsx(
|
|
158
|
+
ColorSwatch,
|
|
159
|
+
{
|
|
160
|
+
ref,
|
|
161
|
+
className: cx("ui-color-input__swatch", className),
|
|
162
|
+
__css: css,
|
|
163
|
+
color: value,
|
|
164
|
+
isRounded: true,
|
|
165
|
+
...rest
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
);
|
|
170
|
+
var ColorInputEyeDropper = forwardRef(
|
|
171
|
+
({ className, children, ...rest }, ref) => {
|
|
172
|
+
const { styles } = useColorInputContext();
|
|
173
|
+
const css = {
|
|
174
|
+
position: "absolute",
|
|
175
|
+
top: "50%",
|
|
176
|
+
transform: "translateY(-50%)",
|
|
177
|
+
display: "inline-flex",
|
|
178
|
+
alignItems: "center",
|
|
179
|
+
justifyContent: "center",
|
|
180
|
+
zIndex: 1,
|
|
181
|
+
...styles.eyeDropper
|
|
182
|
+
};
|
|
183
|
+
const validChildren = getValidChildren(children);
|
|
184
|
+
const cloneChildren = validChildren.map(
|
|
185
|
+
(child) => cloneElement(child, {
|
|
186
|
+
focusable: false,
|
|
187
|
+
"aria-hidden": true,
|
|
188
|
+
style: {
|
|
189
|
+
maxWidth: "1em",
|
|
190
|
+
maxHeight: "1em",
|
|
191
|
+
color: "currentColor"
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
);
|
|
195
|
+
return /* @__PURE__ */ jsx(
|
|
196
|
+
ui.button,
|
|
197
|
+
{
|
|
198
|
+
ref,
|
|
199
|
+
className: cx("ui-color-input__eye-dropper", className),
|
|
200
|
+
__css: css,
|
|
201
|
+
...rest,
|
|
202
|
+
children: isValidElement(children) ? cloneChildren : /* @__PURE__ */ jsx(EyeDropperIcon, { className: "ui-color-input__eye-dropper__icon" })
|
|
203
|
+
}
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
export {
|
|
209
|
+
ColorInput
|
|
210
|
+
};
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// src/use-color-input.ts
|
|
2
|
+
import { layoutStyleProperties } from "@yamada-ui/core";
|
|
3
|
+
import {
|
|
4
|
+
useFormControlProps,
|
|
5
|
+
getFormControlProperties
|
|
6
|
+
} from "@yamada-ui/form-control";
|
|
7
|
+
import { useControllableState } from "@yamada-ui/use-controllable-state";
|
|
8
|
+
import { useEyeDropper } from "@yamada-ui/use-eye-dropper";
|
|
9
|
+
import { useOutsideClick } from "@yamada-ui/use-outside-click";
|
|
10
|
+
import {
|
|
11
|
+
createContext,
|
|
12
|
+
dataAttr,
|
|
13
|
+
handlerAll,
|
|
14
|
+
mergeRefs,
|
|
15
|
+
pickObject,
|
|
16
|
+
splitObject,
|
|
17
|
+
omitObject,
|
|
18
|
+
getEventRelatedTarget,
|
|
19
|
+
isContains,
|
|
20
|
+
convertColor,
|
|
21
|
+
calcFormat,
|
|
22
|
+
useUpdateEffect
|
|
23
|
+
} from "@yamada-ui/utils";
|
|
24
|
+
import { useCallback, useRef, useState } from "react";
|
|
25
|
+
var [ColorInputProvider, useColorInputContext] = createContext({
|
|
26
|
+
name: "ColorInputContext",
|
|
27
|
+
errorMessage: `useColorInputContext returned is 'undefined'. Seems you forgot to wrap the components in "<ColorInput />"`
|
|
28
|
+
});
|
|
29
|
+
var useColorInput = ({
|
|
30
|
+
value: valueProp,
|
|
31
|
+
defaultValue,
|
|
32
|
+
fallbackValue,
|
|
33
|
+
defaultColor,
|
|
34
|
+
onChange: onChangeProp,
|
|
35
|
+
onChangeStart,
|
|
36
|
+
onChangeEnd,
|
|
37
|
+
onSwatchClick,
|
|
38
|
+
formatInput = (value) => value,
|
|
39
|
+
closeOnBlur = true,
|
|
40
|
+
closeOnEsc = true,
|
|
41
|
+
placement = "bottom-start",
|
|
42
|
+
duration = 0.2,
|
|
43
|
+
defaultIsOpen,
|
|
44
|
+
onOpen: onOpenProp,
|
|
45
|
+
onClose: onCloseProp,
|
|
46
|
+
allowInput = true,
|
|
47
|
+
closeOnSelectSwatch,
|
|
48
|
+
format,
|
|
49
|
+
swatchesLabel,
|
|
50
|
+
swatches,
|
|
51
|
+
swatchesColumns,
|
|
52
|
+
withPicker,
|
|
53
|
+
withChannel,
|
|
54
|
+
withResult = false,
|
|
55
|
+
withColorPickerEyeDropper = false,
|
|
56
|
+
colorPickerVariant,
|
|
57
|
+
colorPickerSize,
|
|
58
|
+
colorPickerColorScheme,
|
|
59
|
+
...rest
|
|
60
|
+
}) => {
|
|
61
|
+
rest = useFormControlProps(rest);
|
|
62
|
+
const formControlProps = pickObject(
|
|
63
|
+
rest,
|
|
64
|
+
getFormControlProperties({ omit: ["aria-readonly"] })
|
|
65
|
+
);
|
|
66
|
+
const { disabled, readOnly } = formControlProps;
|
|
67
|
+
const computedProps = splitObject(rest, layoutStyleProperties);
|
|
68
|
+
const containerRef = useRef(null);
|
|
69
|
+
const fieldRef = useRef(null);
|
|
70
|
+
const { supported: eyeDropperSupported, onOpen: onEyeDropperOpen } = useEyeDropper();
|
|
71
|
+
const [value, setValue] = useControllableState({
|
|
72
|
+
value: valueProp,
|
|
73
|
+
defaultValue,
|
|
74
|
+
onChange: onChangeProp
|
|
75
|
+
});
|
|
76
|
+
const formatRef = useRef(format != null ? format : calcFormat(value != null ? value : ""));
|
|
77
|
+
const isInputFocused = useRef(false);
|
|
78
|
+
const [inputValue, setInputValue] = useState(value != null ? value : "");
|
|
79
|
+
const [isOpen, setIsOpen] = useState(defaultIsOpen != null ? defaultIsOpen : false);
|
|
80
|
+
const isColorPickerFull = colorPickerSize === "full";
|
|
81
|
+
const onOpen = useCallback(() => {
|
|
82
|
+
if (disabled || readOnly)
|
|
83
|
+
return;
|
|
84
|
+
setIsOpen(true);
|
|
85
|
+
onOpenProp == null ? void 0 : onOpenProp();
|
|
86
|
+
}, [onOpenProp, disabled, readOnly]);
|
|
87
|
+
const onClose = useCallback(() => {
|
|
88
|
+
if (!isOpen)
|
|
89
|
+
return;
|
|
90
|
+
const next = convertColor(value, fallbackValue)(formatRef.current);
|
|
91
|
+
setValue((prev) => !next || prev === next ? prev : next);
|
|
92
|
+
setInputValue(formatInput(next != null ? next : ""));
|
|
93
|
+
setIsOpen(false);
|
|
94
|
+
onCloseProp == null ? void 0 : onCloseProp();
|
|
95
|
+
}, [
|
|
96
|
+
formatRef,
|
|
97
|
+
isOpen,
|
|
98
|
+
setValue,
|
|
99
|
+
onCloseProp,
|
|
100
|
+
value,
|
|
101
|
+
formatInput,
|
|
102
|
+
setInputValue,
|
|
103
|
+
fallbackValue
|
|
104
|
+
]);
|
|
105
|
+
const onContainerClick = useCallback(() => {
|
|
106
|
+
if (isOpen)
|
|
107
|
+
return;
|
|
108
|
+
onOpen();
|
|
109
|
+
}, [isOpen, onOpen]);
|
|
110
|
+
const onInputFocus = useCallback(() => {
|
|
111
|
+
isInputFocused.current = true;
|
|
112
|
+
if (isOpen)
|
|
113
|
+
return;
|
|
114
|
+
onOpen();
|
|
115
|
+
}, [isOpen, onOpen]);
|
|
116
|
+
const onInputBlur = useCallback(() => {
|
|
117
|
+
isInputFocused.current = false;
|
|
118
|
+
}, []);
|
|
119
|
+
const onContainerBlur = useCallback(
|
|
120
|
+
(ev) => {
|
|
121
|
+
const relatedTarget = getEventRelatedTarget(ev);
|
|
122
|
+
if (isContains(containerRef.current, relatedTarget))
|
|
123
|
+
return;
|
|
124
|
+
if (!closeOnBlur)
|
|
125
|
+
return;
|
|
126
|
+
if (isOpen)
|
|
127
|
+
onClose();
|
|
128
|
+
},
|
|
129
|
+
[closeOnBlur, isOpen, onClose]
|
|
130
|
+
);
|
|
131
|
+
const onInputKeyDown = useCallback(
|
|
132
|
+
(ev) => {
|
|
133
|
+
if (ev.key === " ")
|
|
134
|
+
ev.key = ev.code;
|
|
135
|
+
if (disabled || readOnly)
|
|
136
|
+
return;
|
|
137
|
+
const actions = {
|
|
138
|
+
Space: !isOpen ? onOpen : void 0,
|
|
139
|
+
Enter: !isOpen ? onOpen : void 0,
|
|
140
|
+
Escape: closeOnEsc ? onClose : void 0
|
|
141
|
+
};
|
|
142
|
+
const action = actions[ev.key];
|
|
143
|
+
if (!action)
|
|
144
|
+
return;
|
|
145
|
+
ev.preventDefault();
|
|
146
|
+
ev.stopPropagation();
|
|
147
|
+
action();
|
|
148
|
+
},
|
|
149
|
+
[disabled, readOnly, isOpen, onOpen, closeOnEsc, onClose]
|
|
150
|
+
);
|
|
151
|
+
const onInputChange = useCallback(
|
|
152
|
+
(ev) => {
|
|
153
|
+
const value2 = ev.target.value;
|
|
154
|
+
setInputValue(formatInput(value2));
|
|
155
|
+
setValue(value2);
|
|
156
|
+
},
|
|
157
|
+
[setInputValue, formatInput, setValue]
|
|
158
|
+
);
|
|
159
|
+
const onColorPickerChange = useCallback(
|
|
160
|
+
(value2) => {
|
|
161
|
+
setValue(value2);
|
|
162
|
+
if (!isInputFocused.current)
|
|
163
|
+
setInputValue(formatInput(value2));
|
|
164
|
+
},
|
|
165
|
+
[setValue, formatInput]
|
|
166
|
+
);
|
|
167
|
+
const onEyeDropperClick = useCallback(
|
|
168
|
+
async (ev) => {
|
|
169
|
+
var _a;
|
|
170
|
+
ev.preventDefault();
|
|
171
|
+
ev.stopPropagation();
|
|
172
|
+
try {
|
|
173
|
+
const { sRGBHex } = (_a = await onEyeDropperOpen()) != null ? _a : {};
|
|
174
|
+
if (!sRGBHex)
|
|
175
|
+
return;
|
|
176
|
+
onColorPickerChange(sRGBHex);
|
|
177
|
+
} catch {
|
|
178
|
+
}
|
|
179
|
+
},
|
|
180
|
+
[onEyeDropperOpen, onColorPickerChange]
|
|
181
|
+
);
|
|
182
|
+
useOutsideClick({
|
|
183
|
+
ref: containerRef,
|
|
184
|
+
handler: onClose,
|
|
185
|
+
enabled: closeOnBlur
|
|
186
|
+
});
|
|
187
|
+
useUpdateEffect(() => {
|
|
188
|
+
if (!format)
|
|
189
|
+
return;
|
|
190
|
+
formatRef.current = format;
|
|
191
|
+
const nextValue = convertColor(value, fallbackValue)(format);
|
|
192
|
+
if (!nextValue)
|
|
193
|
+
return;
|
|
194
|
+
setInputValue(formatInput(nextValue));
|
|
195
|
+
setValue(nextValue);
|
|
196
|
+
}, [format, fallbackValue]);
|
|
197
|
+
const getPopoverProps = useCallback(
|
|
198
|
+
(props) => ({
|
|
199
|
+
matchWidth: isColorPickerFull,
|
|
200
|
+
...rest,
|
|
201
|
+
...props,
|
|
202
|
+
isOpen,
|
|
203
|
+
onOpen,
|
|
204
|
+
onClose,
|
|
205
|
+
placement,
|
|
206
|
+
duration,
|
|
207
|
+
trigger: "never",
|
|
208
|
+
closeOnButton: false
|
|
209
|
+
}),
|
|
210
|
+
[isColorPickerFull, duration, onClose, onOpen, placement, rest, isOpen]
|
|
211
|
+
);
|
|
212
|
+
const getContainerProps = useCallback(
|
|
213
|
+
(props = {}, ref = null) => ({
|
|
214
|
+
ref: mergeRefs(containerRef, ref),
|
|
215
|
+
...computedProps[0],
|
|
216
|
+
...props,
|
|
217
|
+
...formControlProps,
|
|
218
|
+
onClick: handlerAll(props.onClick, rest.onClick, onContainerClick),
|
|
219
|
+
onBlur: handlerAll(props.onBlur, rest.onBlur, onContainerBlur)
|
|
220
|
+
}),
|
|
221
|
+
[computedProps, formControlProps, onContainerBlur, onContainerClick, rest]
|
|
222
|
+
);
|
|
223
|
+
const getFieldProps = useCallback(
|
|
224
|
+
(props = {}, ref = null) => ({
|
|
225
|
+
ref: mergeRefs(fieldRef, ref),
|
|
226
|
+
tabIndex: !allowInput ? -1 : 0,
|
|
227
|
+
...omitObject(computedProps[1], ["aria-readonly"]),
|
|
228
|
+
...props,
|
|
229
|
+
style: {
|
|
230
|
+
...props.style,
|
|
231
|
+
...!allowInput ? { pointerEvents: "none" } : {}
|
|
232
|
+
},
|
|
233
|
+
value: inputValue,
|
|
234
|
+
"data-active": dataAttr(isOpen),
|
|
235
|
+
"aria-expanded": dataAttr(isOpen),
|
|
236
|
+
onFocus: handlerAll(props.onFocus, rest.onFocus, onInputFocus),
|
|
237
|
+
onKeyDown: handlerAll(props.onKeyDown, rest.onKeyDown, onInputKeyDown),
|
|
238
|
+
onChange: handlerAll(props.onChange, onInputChange),
|
|
239
|
+
onBlur: handlerAll(props.onFocus, onInputBlur)
|
|
240
|
+
}),
|
|
241
|
+
[
|
|
242
|
+
allowInput,
|
|
243
|
+
computedProps,
|
|
244
|
+
inputValue,
|
|
245
|
+
isOpen,
|
|
246
|
+
rest,
|
|
247
|
+
onInputFocus,
|
|
248
|
+
onInputKeyDown,
|
|
249
|
+
onInputChange,
|
|
250
|
+
onInputBlur
|
|
251
|
+
]
|
|
252
|
+
);
|
|
253
|
+
const getEyeDropperProps = useCallback(
|
|
254
|
+
(props = {}, ref = null) => ({
|
|
255
|
+
disabled,
|
|
256
|
+
"aria-label": "Pick a color",
|
|
257
|
+
...props,
|
|
258
|
+
ref,
|
|
259
|
+
style: { ...props.style, pointerEvents: readOnly ? "none" : void 0 },
|
|
260
|
+
onClick: handlerAll(props.onClick, onEyeDropperClick)
|
|
261
|
+
}),
|
|
262
|
+
[disabled, onEyeDropperClick, readOnly]
|
|
263
|
+
);
|
|
264
|
+
const getPickerProps = useCallback(
|
|
265
|
+
(props) => ({
|
|
266
|
+
...formControlProps,
|
|
267
|
+
...props,
|
|
268
|
+
value,
|
|
269
|
+
defaultValue: defaultColor,
|
|
270
|
+
fallbackValue,
|
|
271
|
+
onChange: onColorPickerChange,
|
|
272
|
+
onChangeStart,
|
|
273
|
+
onChangeEnd,
|
|
274
|
+
onSwatchClick: handlerAll(
|
|
275
|
+
onSwatchClick,
|
|
276
|
+
closeOnSelectSwatch ? onClose : void 0
|
|
277
|
+
),
|
|
278
|
+
format: formatRef.current,
|
|
279
|
+
withPicker,
|
|
280
|
+
withChannel,
|
|
281
|
+
withResult,
|
|
282
|
+
withEyeDropper: withColorPickerEyeDropper,
|
|
283
|
+
swatchesLabel,
|
|
284
|
+
swatches,
|
|
285
|
+
swatchesColumns,
|
|
286
|
+
variant: colorPickerVariant,
|
|
287
|
+
size: colorPickerSize,
|
|
288
|
+
colorScheme: colorPickerColorScheme
|
|
289
|
+
}),
|
|
290
|
+
[
|
|
291
|
+
formControlProps,
|
|
292
|
+
value,
|
|
293
|
+
fallbackValue,
|
|
294
|
+
defaultColor,
|
|
295
|
+
onColorPickerChange,
|
|
296
|
+
onChangeStart,
|
|
297
|
+
onChangeEnd,
|
|
298
|
+
onSwatchClick,
|
|
299
|
+
onClose,
|
|
300
|
+
closeOnSelectSwatch,
|
|
301
|
+
formatRef,
|
|
302
|
+
withPicker,
|
|
303
|
+
withChannel,
|
|
304
|
+
withResult,
|
|
305
|
+
withColorPickerEyeDropper,
|
|
306
|
+
swatchesLabel,
|
|
307
|
+
swatches,
|
|
308
|
+
swatchesColumns,
|
|
309
|
+
colorPickerColorScheme,
|
|
310
|
+
colorPickerSize,
|
|
311
|
+
colorPickerVariant
|
|
312
|
+
]
|
|
313
|
+
);
|
|
314
|
+
return {
|
|
315
|
+
value,
|
|
316
|
+
eyeDropperSupported,
|
|
317
|
+
allowInput,
|
|
318
|
+
getPopoverProps,
|
|
319
|
+
getContainerProps,
|
|
320
|
+
getFieldProps,
|
|
321
|
+
getPickerProps,
|
|
322
|
+
getEyeDropperProps
|
|
323
|
+
};
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
export {
|
|
327
|
+
ColorInputProvider,
|
|
328
|
+
useColorInputContext,
|
|
329
|
+
useColorInput
|
|
330
|
+
};
|