@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,294 @@
|
|
|
1
|
+
// src/use-saturation-slider.ts
|
|
2
|
+
import {
|
|
3
|
+
useFormControlProps,
|
|
4
|
+
formControlProperties,
|
|
5
|
+
getFormControlProperties
|
|
6
|
+
} from "@yamada-ui/form-control";
|
|
7
|
+
import { useControllableState } from "@yamada-ui/use-controllable-state";
|
|
8
|
+
import { useLatestRef } from "@yamada-ui/use-latest-ref";
|
|
9
|
+
import { usePanEvent } from "@yamada-ui/use-pan-event";
|
|
10
|
+
import { useSize } from "@yamada-ui/use-size";
|
|
11
|
+
import {
|
|
12
|
+
omitObject,
|
|
13
|
+
dataAttr,
|
|
14
|
+
handlerAll,
|
|
15
|
+
mergeRefs,
|
|
16
|
+
useCallbackRef,
|
|
17
|
+
runIfFunc,
|
|
18
|
+
clampNumber,
|
|
19
|
+
hsvTo,
|
|
20
|
+
roundNumberToStep,
|
|
21
|
+
pickObject,
|
|
22
|
+
useUpdateEffect
|
|
23
|
+
} from "@yamada-ui/utils";
|
|
24
|
+
import { useCallback, useMemo, useRef, useState } from "react";
|
|
25
|
+
var defaultOverlays = (withShadow) => {
|
|
26
|
+
let overlays = [
|
|
27
|
+
([h]) => ({
|
|
28
|
+
bg: `hsl(${h}, 100%, 50%)`,
|
|
29
|
+
bgImage: "linear-gradient(0deg, #000, transparent), linear-gradient(90deg, #fff, transparent)"
|
|
30
|
+
})
|
|
31
|
+
];
|
|
32
|
+
if (withShadow)
|
|
33
|
+
overlays = [
|
|
34
|
+
...overlays,
|
|
35
|
+
{
|
|
36
|
+
boxShadow: `rgba(0, 0, 0, .1) 0 0 0 1px inset, rgb(0, 0, 0, .15) 0 0 4px inset`
|
|
37
|
+
}
|
|
38
|
+
];
|
|
39
|
+
return overlays;
|
|
40
|
+
};
|
|
41
|
+
var useSaturationSlider = ({
|
|
42
|
+
focusThumbOnChange = true,
|
|
43
|
+
...props
|
|
44
|
+
}) => {
|
|
45
|
+
if (!focusThumbOnChange)
|
|
46
|
+
props.isReadOnly = true;
|
|
47
|
+
let {
|
|
48
|
+
id,
|
|
49
|
+
name,
|
|
50
|
+
value: valueProp,
|
|
51
|
+
defaultValue = [0, 0, 1],
|
|
52
|
+
onChange: onChangeProp,
|
|
53
|
+
onChangeStart: onChangeStartProp,
|
|
54
|
+
onChangeEnd: onChangeEndProp,
|
|
55
|
+
step = 0.01,
|
|
56
|
+
thumbColor,
|
|
57
|
+
required,
|
|
58
|
+
disabled,
|
|
59
|
+
readOnly,
|
|
60
|
+
withShadow = true,
|
|
61
|
+
overlays: overlaysProp = defaultOverlays(withShadow),
|
|
62
|
+
...rest
|
|
63
|
+
} = useFormControlProps(props);
|
|
64
|
+
const onChangeStart = useCallbackRef(onChangeStartProp);
|
|
65
|
+
const onChangeEnd = useCallbackRef(onChangeEndProp);
|
|
66
|
+
const [value, setValue] = useControllableState({
|
|
67
|
+
value: valueProp,
|
|
68
|
+
defaultValue,
|
|
69
|
+
onChange: onChangeProp
|
|
70
|
+
});
|
|
71
|
+
const [isDragging, setDragging] = useState(false);
|
|
72
|
+
const isInteractive = !(disabled || readOnly);
|
|
73
|
+
let [h, s, v] = value;
|
|
74
|
+
s = clampNumber(s, 0, 1);
|
|
75
|
+
v = clampNumber(v, 0, 1);
|
|
76
|
+
const containerRef = useRef(null);
|
|
77
|
+
const trackRef = useRef(null);
|
|
78
|
+
const thumbRef = useRef(null);
|
|
79
|
+
const latestRef = useLatestRef({
|
|
80
|
+
value,
|
|
81
|
+
step,
|
|
82
|
+
isInteractive,
|
|
83
|
+
eventSource: null,
|
|
84
|
+
focusThumbOnChange
|
|
85
|
+
});
|
|
86
|
+
const thumbSize = useSize(thumbRef);
|
|
87
|
+
const overlays = useMemo(
|
|
88
|
+
() => overlaysProp.map((propsOrFunc) => runIfFunc(propsOrFunc, [h, s, v])),
|
|
89
|
+
[overlaysProp, h, s, v]
|
|
90
|
+
);
|
|
91
|
+
const getValueFromPointer = useCallback(
|
|
92
|
+
(ev) => {
|
|
93
|
+
var _a, _b;
|
|
94
|
+
if (!trackRef.current)
|
|
95
|
+
return [];
|
|
96
|
+
const { step: step2 } = latestRef.current;
|
|
97
|
+
latestRef.current.eventSource = "pointer";
|
|
98
|
+
const { bottom, left, height, width } = trackRef.current.getBoundingClientRect();
|
|
99
|
+
const { clientX, clientY } = (_b = (_a = ev.touches) == null ? void 0 : _a[0]) != null ? _b : ev;
|
|
100
|
+
let s2 = clampNumber((clientX - left) / width, 0, 1);
|
|
101
|
+
let v2 = clampNumber((bottom - clientY) / height, 0, 1);
|
|
102
|
+
if (step2) {
|
|
103
|
+
s2 = parseFloat(roundNumberToStep(s2, 0, step2));
|
|
104
|
+
v2 = parseFloat(roundNumberToStep(v2, 0, step2));
|
|
105
|
+
}
|
|
106
|
+
return [s2, v2];
|
|
107
|
+
},
|
|
108
|
+
[latestRef]
|
|
109
|
+
);
|
|
110
|
+
const setValueFromPointer = (ev) => {
|
|
111
|
+
const { value: value2 } = latestRef.current;
|
|
112
|
+
const [nextS, nextV] = getValueFromPointer(ev);
|
|
113
|
+
if (nextS == null || nextV == null)
|
|
114
|
+
return;
|
|
115
|
+
const [, s2, v2] = value2;
|
|
116
|
+
if (nextS !== s2 || nextV !== v2)
|
|
117
|
+
setValue(([h2]) => [h2, nextS, nextV]);
|
|
118
|
+
};
|
|
119
|
+
const focusThumb = useCallback(() => {
|
|
120
|
+
const { focusThumbOnChange: focusThumbOnChange2 } = latestRef.current;
|
|
121
|
+
if (focusThumbOnChange2)
|
|
122
|
+
setTimeout(() => {
|
|
123
|
+
var _a;
|
|
124
|
+
return (_a = thumbRef.current) == null ? void 0 : _a.focus();
|
|
125
|
+
});
|
|
126
|
+
}, [latestRef]);
|
|
127
|
+
const constrain = useCallback(
|
|
128
|
+
([s2, v2]) => {
|
|
129
|
+
const { isInteractive: isInteractive2 } = latestRef.current;
|
|
130
|
+
if (!isInteractive2)
|
|
131
|
+
return;
|
|
132
|
+
s2 = clampNumber(s2, 0, 1);
|
|
133
|
+
v2 = clampNumber(v2, 0, 1);
|
|
134
|
+
setValue(([h2]) => [h2, s2, v2]);
|
|
135
|
+
},
|
|
136
|
+
[latestRef, setValue]
|
|
137
|
+
);
|
|
138
|
+
const onKeyDown = useCallback(
|
|
139
|
+
(ev) => {
|
|
140
|
+
const actions = {
|
|
141
|
+
ArrowRight: () => constrain([s + step, v]),
|
|
142
|
+
ArrowUp: () => constrain([s, v + step]),
|
|
143
|
+
ArrowLeft: () => constrain([s - step, v]),
|
|
144
|
+
ArrowDown: () => constrain([s, v - step])
|
|
145
|
+
};
|
|
146
|
+
const action = actions[ev.key];
|
|
147
|
+
if (!action)
|
|
148
|
+
return;
|
|
149
|
+
ev.preventDefault();
|
|
150
|
+
ev.stopPropagation();
|
|
151
|
+
action(ev);
|
|
152
|
+
latestRef.current.eventSource = "keyboard";
|
|
153
|
+
},
|
|
154
|
+
[latestRef, constrain, s, v, step]
|
|
155
|
+
);
|
|
156
|
+
usePanEvent(containerRef, {
|
|
157
|
+
onSessionStart: (ev) => {
|
|
158
|
+
const { isInteractive: isInteractive2, value: value2 } = latestRef.current;
|
|
159
|
+
if (!isInteractive2)
|
|
160
|
+
return;
|
|
161
|
+
setDragging(true);
|
|
162
|
+
focusThumb();
|
|
163
|
+
setValueFromPointer(ev);
|
|
164
|
+
onChangeStart(value2);
|
|
165
|
+
},
|
|
166
|
+
onSessionEnd: () => {
|
|
167
|
+
const { isInteractive: isInteractive2, value: value2 } = latestRef.current;
|
|
168
|
+
if (!isInteractive2)
|
|
169
|
+
return;
|
|
170
|
+
setDragging(false);
|
|
171
|
+
onChangeEnd(value2);
|
|
172
|
+
},
|
|
173
|
+
onMove: (ev) => {
|
|
174
|
+
const { isInteractive: isInteractive2 } = latestRef.current;
|
|
175
|
+
if (!isInteractive2)
|
|
176
|
+
return;
|
|
177
|
+
setValueFromPointer(ev);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
useUpdateEffect(() => {
|
|
181
|
+
const { eventSource, value: value2 } = latestRef.current;
|
|
182
|
+
if (eventSource === "keyboard")
|
|
183
|
+
onChangeEnd(value2);
|
|
184
|
+
}, [value, onChangeEnd]);
|
|
185
|
+
const getContainerProps = useCallback(
|
|
186
|
+
(props2 = {}, ref = null) => ({
|
|
187
|
+
...props2,
|
|
188
|
+
...omitObject(rest, ["aria-readonly"]),
|
|
189
|
+
ref: mergeRefs(ref, containerRef),
|
|
190
|
+
tabIndex: -1
|
|
191
|
+
}),
|
|
192
|
+
[rest]
|
|
193
|
+
);
|
|
194
|
+
const getInnerProps = useCallback(
|
|
195
|
+
(props2 = {}, ref = null) => {
|
|
196
|
+
const { width: w } = thumbSize != null ? thumbSize : { width: 0 };
|
|
197
|
+
const style = {
|
|
198
|
+
...props2.style,
|
|
199
|
+
...rest.style,
|
|
200
|
+
padding: `${w / 2}px`
|
|
201
|
+
};
|
|
202
|
+
return {
|
|
203
|
+
...props2,
|
|
204
|
+
ref,
|
|
205
|
+
style
|
|
206
|
+
};
|
|
207
|
+
},
|
|
208
|
+
[rest, thumbSize]
|
|
209
|
+
);
|
|
210
|
+
const getInputProps = useCallback(
|
|
211
|
+
(props2 = {}, ref = null) => ({
|
|
212
|
+
...pickObject(rest, formControlProperties),
|
|
213
|
+
...props2,
|
|
214
|
+
id,
|
|
215
|
+
ref,
|
|
216
|
+
type: "hidden",
|
|
217
|
+
name,
|
|
218
|
+
value: [h, s, v].toString(),
|
|
219
|
+
required,
|
|
220
|
+
disabled,
|
|
221
|
+
readOnly
|
|
222
|
+
}),
|
|
223
|
+
[disabled, id, name, readOnly, required, rest, h, s, v]
|
|
224
|
+
);
|
|
225
|
+
const getTrackProps = useCallback(
|
|
226
|
+
(props2 = {}, ref = null) => ({
|
|
227
|
+
...pickObject(
|
|
228
|
+
rest,
|
|
229
|
+
getFormControlProperties({ omit: ["aria-readonly"] })
|
|
230
|
+
),
|
|
231
|
+
...props2,
|
|
232
|
+
ref: mergeRefs(ref, trackRef)
|
|
233
|
+
}),
|
|
234
|
+
[rest]
|
|
235
|
+
);
|
|
236
|
+
const getThumbProps = useCallback(
|
|
237
|
+
(props2 = {}, ref = null) => {
|
|
238
|
+
const { width, height } = thumbSize != null ? thumbSize : { width: 0, height: 0 };
|
|
239
|
+
const x = s * 100;
|
|
240
|
+
const y = v * 100;
|
|
241
|
+
const style = {
|
|
242
|
+
...props2.style,
|
|
243
|
+
position: "absolute",
|
|
244
|
+
userSelect: "none",
|
|
245
|
+
touchAction: "none",
|
|
246
|
+
left: `calc(${x}% - ${width / 2}px)`,
|
|
247
|
+
bottom: `calc(${y}% - ${height / 2}px)`
|
|
248
|
+
};
|
|
249
|
+
return {
|
|
250
|
+
"aria-label": "Saturation and brightness thumb",
|
|
251
|
+
bg: thumbColor != null ? thumbColor : hsvTo([h, s, v])(),
|
|
252
|
+
...pickObject(rest, formControlProperties),
|
|
253
|
+
...props2,
|
|
254
|
+
ref: mergeRefs(ref, thumbRef),
|
|
255
|
+
tabIndex: isInteractive && focusThumbOnChange ? 0 : void 0,
|
|
256
|
+
role: "slider",
|
|
257
|
+
"aria-valuenow": s,
|
|
258
|
+
"aria-valuemin": 0,
|
|
259
|
+
"aria-valuemax": 100,
|
|
260
|
+
"aria-valuetext": `saturation ${s}, brightness ${v}`,
|
|
261
|
+
"data-active": dataAttr(isDragging && focusThumbOnChange),
|
|
262
|
+
onKeyDown: handlerAll(props2.onKeyDown, onKeyDown),
|
|
263
|
+
onFocus: handlerAll(props2.onFocus, rest.onFocus),
|
|
264
|
+
onBlur: handlerAll(props2.onBlur, rest.onBlur),
|
|
265
|
+
style
|
|
266
|
+
};
|
|
267
|
+
},
|
|
268
|
+
[
|
|
269
|
+
h,
|
|
270
|
+
s,
|
|
271
|
+
v,
|
|
272
|
+
thumbColor,
|
|
273
|
+
focusThumbOnChange,
|
|
274
|
+
isDragging,
|
|
275
|
+
isInteractive,
|
|
276
|
+
onKeyDown,
|
|
277
|
+
rest,
|
|
278
|
+
thumbSize
|
|
279
|
+
]
|
|
280
|
+
);
|
|
281
|
+
return {
|
|
282
|
+
value,
|
|
283
|
+
overlays,
|
|
284
|
+
getContainerProps,
|
|
285
|
+
getInnerProps,
|
|
286
|
+
getTrackProps,
|
|
287
|
+
getInputProps,
|
|
288
|
+
getThumbProps
|
|
289
|
+
};
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
export {
|
|
293
|
+
useSaturationSlider
|
|
294
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// ../../utils/src/object.ts
|
|
2
|
+
var pickObject = (obj, keys) => {
|
|
3
|
+
const result = {};
|
|
4
|
+
keys.forEach((key) => {
|
|
5
|
+
if (key in obj)
|
|
6
|
+
result[key] = obj[key];
|
|
7
|
+
});
|
|
8
|
+
return result;
|
|
9
|
+
};
|
|
10
|
+
var getObject = (obj, path, fallback, i) => {
|
|
11
|
+
const k = typeof path === "string" ? path.split(".") : [path];
|
|
12
|
+
for (i = 0; i < k.length; i += 1) {
|
|
13
|
+
if (!obj)
|
|
14
|
+
break;
|
|
15
|
+
obj = obj[k[i]];
|
|
16
|
+
}
|
|
17
|
+
return obj === void 0 ? fallback : obj;
|
|
18
|
+
};
|
|
19
|
+
var memoizeObject = (func) => {
|
|
20
|
+
const cache = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
const memoizedFunc = (obj, path, fallback, i) => {
|
|
22
|
+
if (typeof obj === "undefined")
|
|
23
|
+
return func(obj, path, fallback);
|
|
24
|
+
if (!cache.has(obj))
|
|
25
|
+
cache.set(obj, /* @__PURE__ */ new Map());
|
|
26
|
+
const map = cache.get(obj);
|
|
27
|
+
if (map.has(path))
|
|
28
|
+
return map.get(path);
|
|
29
|
+
const value = func(obj, path, fallback, i);
|
|
30
|
+
map.set(path, value);
|
|
31
|
+
return value;
|
|
32
|
+
};
|
|
33
|
+
return memoizedFunc;
|
|
34
|
+
};
|
|
35
|
+
var getMemoizedObject = memoizeObject(getObject);
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
pickObject
|
|
39
|
+
};
|