@octanejs/colorful 0.0.1

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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +31 -0
  3. package/UPSTREAM.md +56 -0
  4. package/package.json +63 -0
  5. package/src/components/HexAlphaColorPicker.tsrx +14 -0
  6. package/src/components/HexColorInput.tsrx +26 -0
  7. package/src/components/HexColorPicker.tsrx +14 -0
  8. package/src/components/HslColorPicker.tsrx +14 -0
  9. package/src/components/HslStringColorPicker.tsrx +14 -0
  10. package/src/components/HslaColorPicker.tsrx +14 -0
  11. package/src/components/HslaStringColorPicker.tsrx +14 -0
  12. package/src/components/HsvColorPicker.tsrx +14 -0
  13. package/src/components/HsvStringColorPicker.tsrx +14 -0
  14. package/src/components/HsvaColorPicker.tsrx +14 -0
  15. package/src/components/HsvaStringColorPicker.tsrx +14 -0
  16. package/src/components/RgbColorPicker.tsrx +14 -0
  17. package/src/components/RgbStringColorPicker.tsrx +14 -0
  18. package/src/components/RgbaColorPicker.tsrx +14 -0
  19. package/src/components/RgbaStringColorPicker.tsrx +14 -0
  20. package/src/components/common/Alpha.tsrx +43 -0
  21. package/src/components/common/AlphaColorPicker.tsrx +75 -0
  22. package/src/components/common/ColorInput.tsrx +59 -0
  23. package/src/components/common/ColorPicker.tsrx +73 -0
  24. package/src/components/common/Hue.tsrx +41 -0
  25. package/src/components/common/Interactive.tsrx +167 -0
  26. package/src/components/common/Pointer.tsrx +16 -0
  27. package/src/components/common/Saturation.tsrx +46 -0
  28. package/src/css/styles.css +108 -0
  29. package/src/css/styles.ts +107 -0
  30. package/src/hooks/useColorManipulation.tsrx +55 -0
  31. package/src/hooks/useEventCallback.tsrx +12 -0
  32. package/src/hooks/useStyleSheet.tsrx +28 -0
  33. package/src/index.ts +18 -0
  34. package/src/types.ts +61 -0
  35. package/src/utils/clamp.ts +6 -0
  36. package/src/utils/compare.ts +32 -0
  37. package/src/utils/convert.ts +209 -0
  38. package/src/utils/format.ts +1 -0
  39. package/src/utils/nonce.ts +13 -0
  40. package/src/utils/round.ts +3 -0
  41. package/src/utils/validate.ts +13 -0
@@ -0,0 +1,73 @@
1
+ import { useRef } from 'octane';
2
+ import type { Octane } from 'octane/jsx-runtime';
3
+ import { useColorManipulation } from '../../hooks/useColorManipulation.tsrx';
4
+ import { useStyleSheet } from '../../hooks/useStyleSheet.tsrx';
5
+ import type { AnyColor, ColorModel, ColorPickerBaseProps } from '../../types';
6
+ import { Hue } from './Hue.tsrx';
7
+ import { Saturation } from './Saturation.tsrx';
8
+
9
+ interface Props<T extends AnyColor> extends Partial<ColorPickerBaseProps<T>> {
10
+ colorModel: ColorModel<T>;
11
+ }
12
+
13
+ interface RootProps {
14
+ hostProps: Octane.JSX.IntrinsicElements['div'];
15
+ nodeRef: { current: HTMLDivElement | null };
16
+ className: Octane.JSX.IntrinsicElements['div']['className'];
17
+ classValue: Octane.JSX.IntrinsicElements['div']['class'];
18
+ hsva: import('../../types').HsvaColor;
19
+ onChange: (color: Partial<import('../../types').HsvaColor>) => void;
20
+ onChangeEnd: () => void;
21
+ }
22
+
23
+ function ColorPickerRoot({
24
+ hostProps,
25
+ nodeRef,
26
+ className,
27
+ classValue,
28
+ hsva,
29
+ onChange,
30
+ onChangeEnd,
31
+ }: RootProps) {
32
+ return <div
33
+ {...hostProps}
34
+ ref={[hostProps.ref ?? null, nodeRef]}
35
+ class={['react-colorful', className, classValue]}
36
+ >
37
+ <Saturation hsva={hsva} onChange={onChange} onChangeEnd={onChangeEnd} />
38
+ <Hue
39
+ hue={hsva.h}
40
+ onChange={onChange}
41
+ onChangeEnd={onChangeEnd}
42
+ className="react-colorful__last-control"
43
+ />
44
+ </div>;
45
+ }
46
+
47
+ export const ColorPicker = <T extends AnyColor,>({
48
+ class: classValue,
49
+ className,
50
+ colorModel,
51
+ color = colorModel.defaultColor,
52
+ onChange,
53
+ onChangeEnd,
54
+ ...rest
55
+ }: Props<T>) => {
56
+ const nodeRef = useRef<HTMLDivElement | null>(null);
57
+ useStyleSheet(nodeRef);
58
+ const [hsva, updateHsva, commitChange] = useColorManipulation(
59
+ colorModel,
60
+ color,
61
+ onChange,
62
+ onChangeEnd,
63
+ );
64
+ return <ColorPickerRoot
65
+ hostProps={rest}
66
+ nodeRef={nodeRef}
67
+ className={className}
68
+ classValue={classValue}
69
+ hsva={hsva}
70
+ onChange={updateHsva}
71
+ onChangeEnd={commitChange}
72
+ />;
73
+ };
@@ -0,0 +1,41 @@
1
+ import { memo } from 'octane';
2
+ import { clamp } from '../../utils/clamp';
3
+ import { hsvaToHslString } from '../../utils/convert';
4
+ import { formatClassName } from '../../utils/format';
5
+ import { round } from '../../utils/round';
6
+ import { Interactive, type Interaction } from './Interactive.tsrx';
7
+ import { Pointer } from './Pointer.tsrx';
8
+
9
+ interface Props {
10
+ className?: string;
11
+ hue: number;
12
+ onChange: (newHue: { h: number }) => void;
13
+ onChangeEnd?: () => void;
14
+ }
15
+
16
+ const HueBase = ({ className, hue, onChange, onChangeEnd }: Props) => {
17
+ const handleMove = (interaction: Interaction) => onChange({ h: 360 * interaction.left });
18
+ const handleKey = (offset: Interaction) => onChange({
19
+ h: clamp(hue + offset.left * 360, 0, 360),
20
+ });
21
+ const nodeClassName = formatClassName(['react-colorful__hue', className]);
22
+ return <div class={nodeClassName}>
23
+ <Interactive
24
+ onMove={handleMove}
25
+ onKey={handleKey}
26
+ onEnd={onChangeEnd}
27
+ aria-label="Hue"
28
+ aria-valuenow={round(hue)}
29
+ aria-valuemax={360}
30
+ aria-valuemin={0}
31
+ >
32
+ <Pointer
33
+ className="react-colorful__hue-pointer"
34
+ left={hue / 360}
35
+ color={hsvaToHslString({ h: hue, s: 100, v: 100, a: 1 })}
36
+ />
37
+ </Interactive>
38
+ </div>;
39
+ };
40
+
41
+ export const Hue = memo(HueBase);
@@ -0,0 +1,167 @@
1
+ import { memo, useEffect, useMemo, useRef } from 'octane';
2
+ import type { OctaneNode } from 'octane';
3
+ import { useEventCallback } from '../../hooks/useEventCallback.tsrx';
4
+ import { clamp } from '../../utils/clamp';
5
+
6
+ export interface Interaction {
7
+ left: number;
8
+ top: number;
9
+ }
10
+
11
+ const isTouch = (event: MouseEvent | TouchEvent): event is TouchEvent => 'touches' in event;
12
+
13
+ const getTouchPoint = (touches: TouchList, touchId: null | number): Touch => {
14
+ for (let index = 0; index < touches.length; index++) {
15
+ if (touches[index].identifier === touchId) return touches[index];
16
+ }
17
+ return touches[0];
18
+ };
19
+
20
+ const getParentWindow = (node?: HTMLDivElement | null): Window =>
21
+ node && node.ownerDocument.defaultView || self;
22
+
23
+ const getRelativePosition = (
24
+ node: HTMLDivElement,
25
+ event: MouseEvent | TouchEvent,
26
+ touchId: null | number,
27
+ ): Interaction => {
28
+ const rect = node.getBoundingClientRect();
29
+ const pointer =
30
+ isTouch(event) ? getTouchPoint(event.touches, touchId) : event;
31
+ return {
32
+ left: clamp((pointer.pageX - (rect.left + getParentWindow(node).pageXOffset)) / rect.width),
33
+ top: clamp((pointer.pageY - (rect.top + getParentWindow(node).pageYOffset)) / rect.height),
34
+ };
35
+ };
36
+
37
+ const preventDefaultMove = (event: MouseEvent | TouchEvent): void => {
38
+ if (!isTouch(event)) event.preventDefault();
39
+ };
40
+
41
+ const isInvalid = (event: MouseEvent | TouchEvent, hasTouch: boolean): boolean =>
42
+ hasTouch && !isTouch(event);
43
+
44
+ interface Props {
45
+ onMove: (interaction: Interaction) => void;
46
+ onKey: (offset: Interaction) => void;
47
+ onEnd?: () => void;
48
+ children: OctaneNode;
49
+ 'aria-label'?: string;
50
+ 'aria-valuetext'?: string;
51
+ 'aria-valuenow'?: number;
52
+ 'aria-valuemax'?: number;
53
+ 'aria-valuemin'?: number;
54
+ }
55
+
56
+ const InteractiveBase = ({
57
+ onMove,
58
+ onKey,
59
+ onEnd,
60
+ children,
61
+ 'aria-label': ariaLabel,
62
+ 'aria-valuetext': ariaValueText,
63
+ 'aria-valuenow': ariaValueNow,
64
+ 'aria-valuemax': ariaValueMax,
65
+ 'aria-valuemin': ariaValueMin,
66
+ }: Props) => {
67
+ const container = useRef<HTMLDivElement | null>(null);
68
+ const onMoveCallback = useEventCallback<Interaction>(onMove);
69
+ const onKeyCallback = useEventCallback<Interaction>(onKey);
70
+ const onEndCallback = useEventCallback<void>(onEnd);
71
+ const touchId = useRef<null | number>(null);
72
+ const hasTouch = useRef(false);
73
+
74
+ const [handleMoveStart, handleKeyDown, handleKeyUp, toggleDocumentEvents] = useMemo(() => {
75
+ const handleMove = (event: MouseEvent | TouchEvent) => {
76
+ preventDefaultMove(event);
77
+ const isDown = isTouch(event) ? event.touches.length > 0 : event.buttons > 0;
78
+ if (isDown && container.current) {
79
+ onMoveCallback(getRelativePosition(container.current, event, touchId.current));
80
+ } else {
81
+ toggleDocumentEvents(false);
82
+ onEndCallback();
83
+ }
84
+ };
85
+
86
+ const handleMoveEnd = () => {
87
+ toggleDocumentEvents(false);
88
+ onEndCallback();
89
+ };
90
+
91
+ function toggleDocumentEvents(state?: boolean) {
92
+ const touch = hasTouch.current;
93
+ const parentWindow = getParentWindow(container.current);
94
+ const toggleEvent = state ? parentWindow.addEventListener : parentWindow.removeEventListener;
95
+ toggleEvent.call(
96
+ parentWindow,
97
+ touch ? 'touchmove' : 'mousemove',
98
+ handleMove as EventListener,
99
+ );
100
+ toggleEvent.call(
101
+ parentWindow,
102
+ touch ? 'touchend' : 'mouseup',
103
+ handleMoveEnd as EventListener,
104
+ );
105
+ }
106
+
107
+ const handleMoveStart = (nativeEvent: MouseEvent | TouchEvent) => {
108
+ const element = container.current;
109
+ if (!element) return;
110
+ preventDefaultMove(nativeEvent);
111
+ if (isInvalid(nativeEvent, hasTouch.current)) return;
112
+ if (isTouch(nativeEvent)) {
113
+ hasTouch.current = true;
114
+ const changedTouches = nativeEvent.changedTouches || [];
115
+ if (changedTouches.length) touchId.current = changedTouches[0].identifier;
116
+ }
117
+ element.focus();
118
+ onMoveCallback(getRelativePosition(element, nativeEvent, touchId.current));
119
+ toggleDocumentEvents(true);
120
+ };
121
+
122
+ const handleKeyDown = (event: KeyboardEvent) => {
123
+ const keyCode = event.which || event.keyCode;
124
+ if (keyCode < 37 || keyCode > 40) return;
125
+ event.preventDefault();
126
+ onKeyCallback({
127
+ left: keyCode === 39
128
+ ? 0.05
129
+ : keyCode === 37
130
+ ? -0.05
131
+ : 0,
132
+ top: keyCode === 40
133
+ ? 0.05
134
+ : keyCode === 38
135
+ ? -0.05
136
+ : 0,
137
+ });
138
+ };
139
+
140
+ const handleKeyUp = (event: KeyboardEvent) => {
141
+ const keyCode = event.which || event.keyCode;
142
+ if (keyCode >= 37 && keyCode <= 40) onEndCallback();
143
+ };
144
+
145
+ return [handleMoveStart, handleKeyDown, handleKeyUp, toggleDocumentEvents] as const;
146
+ }, [onKeyCallback, onMoveCallback, onEndCallback]);
147
+
148
+ useEffect(() => toggleDocumentEvents, [toggleDocumentEvents]);
149
+
150
+ return <div
151
+ aria-label={ariaLabel}
152
+ aria-valuetext={ariaValueText}
153
+ aria-valuenow={ariaValueNow}
154
+ aria-valuemax={ariaValueMax}
155
+ aria-valuemin={ariaValueMin}
156
+ onTouchStart={handleMoveStart}
157
+ onMouseDown={handleMoveStart}
158
+ class="react-colorful__interactive"
159
+ ref={container}
160
+ onKeyDown={handleKeyDown}
161
+ onKeyUp={handleKeyUp}
162
+ tabIndex={0}
163
+ role="slider"
164
+ >{children}</div>;
165
+ };
166
+
167
+ export const Interactive = memo(InteractiveBase);
@@ -0,0 +1,16 @@
1
+ import { formatClassName } from '../../utils/format';
2
+
3
+ interface Props {
4
+ className?: string;
5
+ top?: number;
6
+ left: number;
7
+ color: string;
8
+ }
9
+
10
+ export const Pointer = ({ className, color, left, top = 0.5 }: Props) => {
11
+ const nodeClassName = formatClassName(['react-colorful__pointer', className]);
12
+ const style = { top: `${top * 100}%`, left: `${left * 100}%` };
13
+ return <div class={nodeClassName} style={style}>
14
+ <div class="react-colorful__pointer-fill" style={{ backgroundColor: color }} />
15
+ </div>;
16
+ };
@@ -0,0 +1,46 @@
1
+ import { memo } from 'octane';
2
+ import type { HsvaColor } from '../../types';
3
+ import { clamp } from '../../utils/clamp';
4
+ import { hsvaToHslString } from '../../utils/convert';
5
+ import { round } from '../../utils/round';
6
+ import { Interactive, type Interaction } from './Interactive.tsrx';
7
+ import { Pointer } from './Pointer.tsrx';
8
+
9
+ interface Props {
10
+ hsva: HsvaColor;
11
+ onChange: (newColor: { s: number; v: number }) => void;
12
+ onChangeEnd?: () => void;
13
+ }
14
+
15
+ const SaturationBase = ({ hsva, onChange, onChangeEnd }: Props) => {
16
+ const handleMove = (interaction: Interaction) => {
17
+ onChange({ s: interaction.left * 100, v: 100 - interaction.top * 100 });
18
+ };
19
+ const handleKey = (offset: Interaction) => {
20
+ onChange({
21
+ s: clamp(hsva.s + offset.left * 100, 0, 100),
22
+ v: clamp(hsva.v - offset.top * 100, 0, 100),
23
+ });
24
+ };
25
+ const containerStyle = {
26
+ backgroundColor: hsvaToHslString({ h: hsva.h, s: 100, v: 100, a: 1 }),
27
+ };
28
+ return <div class="react-colorful__saturation" style={containerStyle}>
29
+ <Interactive
30
+ onMove={handleMove}
31
+ onKey={handleKey}
32
+ onEnd={onChangeEnd}
33
+ aria-label="Color"
34
+ aria-valuetext={`Saturation ${round(hsva.s)}%, Brightness ${round(hsva.v)}%`}
35
+ >
36
+ <Pointer
37
+ className="react-colorful__saturation-pointer"
38
+ top={1 - hsva.v / 100}
39
+ left={hsva.s / 100}
40
+ color={hsvaToHslString(hsva)}
41
+ />
42
+ </Interactive>
43
+ </div>;
44
+ };
45
+
46
+ export const Saturation = memo(SaturationBase);
@@ -0,0 +1,108 @@
1
+ .react-colorful {
2
+ position: relative;
3
+ display: flex;
4
+ flex-direction: column;
5
+ width: 200px;
6
+ height: 200px;
7
+ user-select: none;
8
+ cursor: default;
9
+ }
10
+
11
+ .react-colorful__saturation {
12
+ position: relative;
13
+ flex-grow: 1;
14
+ border-color: transparent; /* Fixes https://github.com/omgovich/react-colorful/issues/139 */
15
+ border-bottom: 12px solid #000;
16
+ border-radius: 8px 8px 0 0;
17
+ background-image:
18
+ linear-gradient(to top, #000, rgba(0, 0, 0, 0)),
19
+ linear-gradient(to right, #fff, rgba(255, 255, 255, 0));
20
+ }
21
+
22
+ .react-colorful__pointer-fill,
23
+ .react-colorful__alpha-gradient {
24
+ content: '';
25
+ position: absolute;
26
+ left: 0;
27
+ top: 0;
28
+ right: 0;
29
+ bottom: 0;
30
+ pointer-events: none;
31
+ border-radius: inherit;
32
+ }
33
+
34
+ /* Improve elements rendering on light backgrounds */
35
+ .react-colorful__alpha-gradient,
36
+ .react-colorful__saturation {
37
+ box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.05);
38
+ }
39
+
40
+ .react-colorful__hue,
41
+ .react-colorful__alpha {
42
+ position: relative;
43
+ height: 24px;
44
+ }
45
+
46
+ .react-colorful__hue {
47
+ background: linear-gradient(
48
+ to right,
49
+ #f00 0%,
50
+ #ff0 17%,
51
+ #0f0 33%,
52
+ #0ff 50%,
53
+ #00f 67%,
54
+ #f0f 83%,
55
+ #f00 100%
56
+ );
57
+ }
58
+
59
+ /* Round bottom corners of the last element: `Hue` for `ColorPicker` or `Alpha` for `AlphaColorPicker` */
60
+ .react-colorful__last-control {
61
+ border-radius: 0 0 8px 8px;
62
+ }
63
+
64
+ .react-colorful__interactive {
65
+ position: absolute;
66
+ left: 0;
67
+ top: 0;
68
+ right: 0;
69
+ bottom: 0;
70
+ border-radius: inherit;
71
+ outline: none;
72
+ /* Don't trigger the default scrolling behavior when the event is originating from this element */
73
+ touch-action: none;
74
+ }
75
+
76
+ .react-colorful__pointer {
77
+ position: absolute;
78
+ z-index: 1;
79
+ box-sizing: border-box;
80
+ width: 28px;
81
+ height: 28px;
82
+ transform: translate(-50%, -50%);
83
+ background-color: #fff;
84
+ border: 2px solid #fff;
85
+ border-radius: 50%;
86
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
87
+ }
88
+
89
+ .react-colorful__interactive:focus .react-colorful__pointer {
90
+ transform: translate(-50%, -50%) scale(1.1);
91
+ }
92
+
93
+ /* Chessboard-like pattern for alpha related elements */
94
+ .react-colorful__alpha,
95
+ .react-colorful__alpha-pointer {
96
+ background-color: #fff;
97
+ background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill-opacity=".05"><rect x="8" width="8" height="8"/><rect y="8" width="8" height="8"/></svg>');
98
+ }
99
+
100
+ /* Display the saturation pointer over the hue one */
101
+ .react-colorful__saturation-pointer {
102
+ z-index: 3;
103
+ }
104
+
105
+ /* Display the hue pointer over the alpha one */
106
+ .react-colorful__hue-pointer {
107
+ z-index: 2;
108
+ }
@@ -0,0 +1,107 @@
1
+ export const styles = `.react-colorful {
2
+ position: relative;
3
+ display: flex;
4
+ flex-direction: column;
5
+ width: 200px;
6
+ height: 200px;
7
+ user-select: none;
8
+ cursor: default;
9
+ }
10
+
11
+ .react-colorful__saturation {
12
+ position: relative;
13
+ flex-grow: 1;
14
+ border-color: transparent; /* Fixes https://github.com/omgovich/react-colorful/issues/139 */
15
+ border-bottom: 12px solid #000;
16
+ border-radius: 8px 8px 0 0;
17
+ background-image: linear-gradient(to top, #000, rgba(0, 0, 0, 0)),
18
+ linear-gradient(to right, #fff, rgba(255, 255, 255, 0));
19
+ }
20
+
21
+ .react-colorful__pointer-fill,
22
+ .react-colorful__alpha-gradient {
23
+ content: "";
24
+ position: absolute;
25
+ left: 0;
26
+ top: 0;
27
+ right: 0;
28
+ bottom: 0;
29
+ pointer-events: none;
30
+ border-radius: inherit;
31
+ }
32
+
33
+ /* Improve elements rendering on light backgrounds */
34
+ .react-colorful__alpha-gradient,
35
+ .react-colorful__saturation {
36
+ box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.05);
37
+ }
38
+
39
+ .react-colorful__hue,
40
+ .react-colorful__alpha {
41
+ position: relative;
42
+ height: 24px;
43
+ }
44
+
45
+ .react-colorful__hue {
46
+ background: linear-gradient(
47
+ to right,
48
+ #f00 0%,
49
+ #ff0 17%,
50
+ #0f0 33%,
51
+ #0ff 50%,
52
+ #00f 67%,
53
+ #f0f 83%,
54
+ #f00 100%
55
+ );
56
+ }
57
+
58
+ /* Round bottom corners of the last element: \`Hue\` for \`ColorPicker\` or \`Alpha\` for \`AlphaColorPicker\` */
59
+ .react-colorful__last-control {
60
+ border-radius: 0 0 8px 8px;
61
+ }
62
+
63
+ .react-colorful__interactive {
64
+ position: absolute;
65
+ left: 0;
66
+ top: 0;
67
+ right: 0;
68
+ bottom: 0;
69
+ border-radius: inherit;
70
+ outline: none;
71
+ /* Don't trigger the default scrolling behavior when the event is originating from this element */
72
+ touch-action: none;
73
+ }
74
+
75
+ .react-colorful__pointer {
76
+ position: absolute;
77
+ z-index: 1;
78
+ box-sizing: border-box;
79
+ width: 28px;
80
+ height: 28px;
81
+ transform: translate(-50%, -50%);
82
+ background-color: #fff;
83
+ border: 2px solid #fff;
84
+ border-radius: 50%;
85
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
86
+ }
87
+
88
+ .react-colorful__interactive:focus .react-colorful__pointer {
89
+ transform: translate(-50%, -50%) scale(1.1);
90
+ }
91
+
92
+ /* Chessboard-like pattern for alpha related elements */
93
+ .react-colorful__alpha,
94
+ .react-colorful__alpha-pointer {
95
+ background-color: #fff;
96
+ background-image: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill-opacity=".05"><rect x="8" width="8" height="8"/><rect y="8" width="8" height="8"/></svg>');
97
+ }
98
+
99
+ /* Display the saturation pointer over the hue one */
100
+ .react-colorful__saturation-pointer {
101
+ z-index: 3;
102
+ }
103
+
104
+ /* Display the hue pointer over the alpha one */
105
+ .react-colorful__hue-pointer {
106
+ z-index: 2;
107
+ }`;
@@ -0,0 +1,55 @@
1
+ import { useCallback, useEffect, useRef, useState } from 'octane';
2
+ import type { AnyColor, ColorModel, HsvaColor } from '../types';
3
+ import { equalColorObjects } from '../utils/compare';
4
+ import { useEventCallback } from './useEventCallback.tsrx';
5
+
6
+ export function useColorManipulation<T extends AnyColor>(
7
+ colorModel: ColorModel<T>,
8
+ color: T,
9
+ onChange?: (color: T) => void,
10
+ onChangeEnd?: (color: T) => void,
11
+ ): [HsvaColor, (color: Partial<HsvaColor>) => void, () => void] {
12
+ const resolvedOnChange =
13
+ typeof onChange === 'symbol' ? undefined : onChange;
14
+ const resolvedOnChangeEnd =
15
+ typeof onChangeEnd === 'symbol' ? undefined : onChangeEnd;
16
+ const onChangeCallback = useEventCallback<T>(resolvedOnChange);
17
+ const onChangeEndCallback = useEventCallback<T>(resolvedOnChangeEnd);
18
+ const [hsva, updateHsva] = useState<HsvaColor>(() => colorModel.toHsva(color));
19
+ const cache = useRef({ color, hsva });
20
+ const isDirty = useRef(false);
21
+
22
+ useEffect(() => {
23
+ if (!colorModel.equal(color, cache.current.color)) {
24
+ const newHsva = colorModel.toHsva(color);
25
+ cache.current = { hsva: newHsva, color };
26
+ updateHsva(newHsva);
27
+ isDirty.current = false;
28
+ }
29
+ }, [color, colorModel]);
30
+
31
+ useEffect(() => {
32
+ let newColor: T;
33
+ if (
34
+ !equalColorObjects(hsva, cache.current.hsva) &&
35
+ !colorModel.equal(newColor = colorModel.fromHsva(hsva), cache.current.color)
36
+ ) {
37
+ cache.current = { hsva, color: newColor };
38
+ onChangeCallback(newColor);
39
+ isDirty.current = true;
40
+ }
41
+ }, [hsva, colorModel, onChangeCallback]);
42
+
43
+ const handleChange = useCallback((params: Partial<HsvaColor>) => {
44
+ updateHsva((current) => Object.assign({}, current, params));
45
+ }, []);
46
+
47
+ const commitChange = useCallback(() => {
48
+ if (isDirty.current) {
49
+ isDirty.current = false;
50
+ onChangeEndCallback(cache.current.color);
51
+ }
52
+ }, [onChangeEndCallback]);
53
+
54
+ return [hsva, handleChange, commitChange];
55
+ }
@@ -0,0 +1,12 @@
1
+ import { useRef } from 'octane';
2
+
3
+ export function useEventCallback<T>(handler?: (value: T) => void): (value: T) => void {
4
+ const resolvedHandler =
5
+ typeof handler === 'symbol' ? undefined : handler;
6
+ const callbackRef = useRef(resolvedHandler);
7
+ const fn = useRef(function (value: T) {
8
+ callbackRef.current?.(value);
9
+ });
10
+ callbackRef.current = resolvedHandler;
11
+ return fn.current;
12
+ }
@@ -0,0 +1,28 @@
1
+ import { useLayoutEffect } from 'octane';
2
+ import { styles } from '../css/styles';
3
+ import { getNonce } from '../utils/nonce';
4
+
5
+ type Root = Document | ShadowRoot;
6
+
7
+ const styleElementMap: WeakMap<Root, HTMLStyleElement> = new WeakMap();
8
+
9
+ export const useStyleSheet = (nodeRef: { current: HTMLDivElement | null }): void => {
10
+ useLayoutEffect(() => {
11
+ const node = nodeRef.current;
12
+ if (typeof document === 'undefined' || !node) return;
13
+
14
+ const raw =
15
+ node.getRootNode ? node.getRootNode() : node.ownerDocument;
16
+ const root = (raw && ('head' in raw || 'host' in raw) ? raw : node.ownerDocument) as Root;
17
+ if (styleElementMap.has(root)) return;
18
+
19
+ const target =
20
+ 'head' in root ? root.head : root;
21
+ const styleElement = (target.ownerDocument || document).createElement('style');
22
+ styleElement.innerHTML = styles;
23
+ const nonce = getNonce();
24
+ if (nonce) styleElement.setAttribute('nonce', nonce);
25
+ styleElementMap.set(root, styleElement);
26
+ target.appendChild(styleElement);
27
+ }, []);
28
+ };
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ export { HexColorPicker } from './components/HexColorPicker.tsrx';
2
+ export { HexAlphaColorPicker } from './components/HexAlphaColorPicker.tsrx';
3
+ export { HslaColorPicker } from './components/HslaColorPicker.tsrx';
4
+ export { HslaStringColorPicker } from './components/HslaStringColorPicker.tsrx';
5
+ export { HslColorPicker } from './components/HslColorPicker.tsrx';
6
+ export { HslStringColorPicker } from './components/HslStringColorPicker.tsrx';
7
+ export { HsvaColorPicker } from './components/HsvaColorPicker.tsrx';
8
+ export { HsvaStringColorPicker } from './components/HsvaStringColorPicker.tsrx';
9
+ export { HsvColorPicker } from './components/HsvColorPicker.tsrx';
10
+ export { HsvStringColorPicker } from './components/HsvStringColorPicker.tsrx';
11
+ export { RgbaColorPicker } from './components/RgbaColorPicker.tsrx';
12
+ export { RgbaStringColorPicker } from './components/RgbaStringColorPicker.tsrx';
13
+ export { RgbColorPicker } from './components/RgbColorPicker.tsrx';
14
+ export { RgbStringColorPicker } from './components/RgbStringColorPicker.tsrx';
15
+ export { HexColorInput } from './components/HexColorInput.tsrx';
16
+
17
+ export type { HslColor, HslaColor, HsvColor, HsvaColor, RgbColor, RgbaColor } from './types';
18
+ export { setNonce } from './utils/nonce';