sh-ui-cli 0.48.0 → 0.50.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/data/changelog/versions.json +27 -0
- package/data/registry/react/components/accordion/index.vanilla-extract.tsx +97 -0
- package/data/registry/react/components/accordion/styles.css.ts +131 -0
- package/data/registry/react/components/avatar/index.vanilla-extract.tsx +73 -0
- package/data/registry/react/components/avatar/styles.css.ts +68 -0
- package/data/registry/react/components/badge/index.vanilla-extract.tsx +40 -0
- package/data/registry/react/components/badge/styles.css.ts +71 -0
- package/data/registry/react/components/breadcrumb/index.vanilla-extract.tsx +152 -0
- package/data/registry/react/components/breadcrumb/styles.css.ts +95 -0
- package/data/registry/react/components/button/index.vanilla-extract.tsx +45 -0
- package/data/registry/react/components/button/styles.css.ts +120 -0
- package/data/registry/react/components/calendar/index.vanilla-extract.tsx +806 -0
- package/data/registry/react/components/calendar/styles.css.ts +250 -0
- package/data/registry/react/components/card/index.vanilla-extract.tsx +63 -0
- package/data/registry/react/components/card/styles.css.ts +88 -0
- package/data/registry/react/components/carousel/index.vanilla-extract.tsx +430 -0
- package/data/registry/react/components/carousel/styles.css.ts +169 -0
- package/data/registry/react/components/checkbox/index.vanilla-extract.tsx +96 -0
- package/data/registry/react/components/checkbox/styles.css.ts +74 -0
- package/data/registry/react/components/code-editor/index.vanilla-extract.tsx +230 -0
- package/data/registry/react/components/code-editor/styles.css.ts +97 -0
- package/data/registry/react/components/code-panel/index.vanilla-extract.tsx +191 -0
- package/data/registry/react/components/code-panel/styles.css.ts +151 -0
- package/data/registry/react/components/color-picker/index.vanilla-extract.tsx +467 -0
- package/data/registry/react/components/color-picker/styles.css.ts +169 -0
- package/data/registry/react/components/combobox/index.vanilla-extract.tsx +165 -0
- package/data/registry/react/components/combobox/styles.css.ts +174 -0
- package/data/registry/react/components/context-menu/index.vanilla-extract.tsx +251 -0
- package/data/registry/react/components/context-menu/styles.css.ts +167 -0
- package/data/registry/react/components/date-picker/index.vanilla-extract.tsx +520 -0
- package/data/registry/react/components/date-picker/styles.css.ts +111 -0
- package/data/registry/react/components/dialog/index.vanilla-extract.tsx +95 -0
- package/data/registry/react/components/dialog/styles.css.ts +140 -0
- package/data/registry/react/components/dropdown-menu/index.vanilla-extract.tsx +255 -0
- package/data/registry/react/components/dropdown-menu/styles.css.ts +175 -0
- package/data/registry/react/components/file-upload/index.vanilla-extract.tsx +487 -0
- package/data/registry/react/components/file-upload/styles.css.ts +193 -0
- package/data/registry/react/components/form/index.vanilla-extract.tsx +61 -0
- package/data/registry/react/components/form/styles.css.ts +56 -0
- package/data/registry/react/components/header/index.vanilla-extract.tsx +805 -0
- package/data/registry/react/components/header/styles.css.ts +413 -0
- package/data/registry/react/components/input/index.vanilla-extract.tsx +425 -0
- package/data/registry/react/components/input/styles.css.ts +202 -0
- package/data/registry/react/components/label/index.vanilla-extract.tsx +52 -0
- package/data/registry/react/components/label/styles.css.ts +141 -0
- package/data/registry/react/components/markdown-editor/index.vanilla-extract.tsx +119 -0
- package/data/registry/react/components/markdown-editor/styles.css.ts +231 -0
- package/data/registry/react/components/menubar/index.vanilla-extract.tsx +32 -0
- package/data/registry/react/components/menubar/styles.css.ts +53 -0
- package/data/registry/react/components/numeric-input/index.vanilla-extract.tsx +148 -0
- package/data/registry/react/components/numeric-input/styles.css.ts +65 -0
- package/data/registry/react/components/page-toc/index.vanilla-extract.tsx +174 -0
- package/data/registry/react/components/page-toc/styles.css.ts +97 -0
- package/data/registry/react/components/pagination/index.vanilla-extract.tsx +269 -0
- package/data/registry/react/components/pagination/styles.css.ts +113 -0
- package/data/registry/react/components/popover/index.vanilla-extract.tsx +113 -0
- package/data/registry/react/components/popover/styles.css.ts +78 -0
- package/data/registry/react/components/progress/index.vanilla-extract.tsx +54 -0
- package/data/registry/react/components/progress/styles.css.ts +53 -0
- package/data/registry/react/components/radio/index.vanilla-extract.tsx +65 -0
- package/data/registry/react/components/radio/styles.css.ts +79 -0
- package/data/registry/react/components/rich-text-editor/index.vanilla-extract.tsx +348 -0
- package/data/registry/react/components/rich-text-editor/styles.css.ts +243 -0
- package/data/registry/react/components/select/index.vanilla-extract.tsx +234 -0
- package/data/registry/react/components/select/styles.css.ts +225 -0
- package/data/registry/react/components/separator/index.vanilla-extract.tsx +46 -0
- package/data/registry/react/components/separator/styles.css.ts +24 -0
- package/data/registry/react/components/sidebar/index.vanilla-extract.tsx +1067 -0
- package/data/registry/react/components/sidebar/styles.css.ts +578 -0
- package/data/registry/react/components/skeleton/index.vanilla-extract.tsx +22 -0
- package/data/registry/react/components/skeleton/styles.css.ts +30 -0
- package/data/registry/react/components/slider/index.vanilla-extract.tsx +298 -0
- package/data/registry/react/components/slider/styles.css.ts +75 -0
- package/data/registry/react/components/spinner/index.vanilla-extract.tsx +38 -0
- package/data/registry/react/components/spinner/styles.css.ts +60 -0
- package/data/registry/react/components/switch/index.vanilla-extract.tsx +39 -0
- package/data/registry/react/components/switch/styles.css.ts +87 -0
- package/data/registry/react/components/tabs/index.vanilla-extract.tsx +91 -0
- package/data/registry/react/components/tabs/styles.css.ts +145 -0
- package/data/registry/react/components/textarea/index.vanilla-extract.tsx +23 -0
- package/data/registry/react/components/textarea/styles.css.ts +55 -0
- package/data/registry/react/components/toast/index.vanilla-extract.tsx +258 -0
- package/data/registry/react/components/toast/styles.css.ts +307 -0
- package/data/registry/react/components/toggle/index.vanilla-extract.tsx +131 -0
- package/data/registry/react/components/toggle/styles.css.ts +109 -0
- package/data/registry/react/components/tooltip/index.vanilla-extract.tsx +83 -0
- package/data/registry/react/components/tooltip/styles.css.ts +59 -0
- package/data/registry/react/peer-versions.json +1 -0
- package/data/registry/react/registry.json +922 -42
- package/data/tokens/build.mjs +3 -0
- package/package.json +1 -1
- package/src/api.d.ts +4 -3
- package/src/constants.js +4 -3
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { style } from "@vanilla-extract/css";
|
|
2
|
+
|
|
3
|
+
export const code = style({
|
|
4
|
+
position: "relative",
|
|
5
|
+
border: "1px solid var(--border)",
|
|
6
|
+
borderRadius: "var(--radius)",
|
|
7
|
+
background: "var(--background-subtle)",
|
|
8
|
+
overflow: "hidden",
|
|
9
|
+
fontSize: "0.8125rem",
|
|
10
|
+
lineHeight: 1.6,
|
|
11
|
+
margin: "var(--space-4) 0",
|
|
12
|
+
selectors: {
|
|
13
|
+
[`&:hover ${codeCopyFloating}`]: {
|
|
14
|
+
opacity: 1,
|
|
15
|
+
},
|
|
16
|
+
[`&:focus-within ${codeCopyFloating}`]: {
|
|
17
|
+
opacity: 1,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
"@media": {
|
|
21
|
+
"(max-width: 640px)": {
|
|
22
|
+
fontSize: "var(--text-xs)",
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export const code__header = style({
|
|
28
|
+
display: "flex",
|
|
29
|
+
alignItems: "center",
|
|
30
|
+
justifyContent: "space-between",
|
|
31
|
+
gap: "var(--space-2)",
|
|
32
|
+
padding: "var(--space-2) var(--space-3) var(--space-2) var(--space-4)",
|
|
33
|
+
borderBottom: "1px solid var(--border)",
|
|
34
|
+
background: "var(--background-muted)",
|
|
35
|
+
fontSize: "var(--text-xs)",
|
|
36
|
+
color: "var(--foreground-muted)",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export const code__filename = style({
|
|
40
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
|
|
41
|
+
color: "var(--foreground)",
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const codeCopyFloating = style({
|
|
45
|
+
position: "absolute",
|
|
46
|
+
top: "var(--space-2)",
|
|
47
|
+
right: "var(--space-2)",
|
|
48
|
+
zIndex: 1,
|
|
49
|
+
opacity: 0,
|
|
50
|
+
transition: "opacity var(--duration-fast)",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export const code__copy = style({
|
|
54
|
+
display: "inline-flex",
|
|
55
|
+
alignItems: "center",
|
|
56
|
+
gap: "0.375rem",
|
|
57
|
+
padding: "var(--space-1) var(--space-2)",
|
|
58
|
+
background: "var(--background)",
|
|
59
|
+
color: "var(--foreground-muted)",
|
|
60
|
+
border: "1px solid var(--border)",
|
|
61
|
+
borderRadius: "calc(var(--radius) - 2px)",
|
|
62
|
+
fontSize: "var(--text-xs)",
|
|
63
|
+
lineHeight: 1,
|
|
64
|
+
cursor: "pointer",
|
|
65
|
+
transition: "color var(--duration-fast), border-color var(--duration-fast), background-color var(--duration-fast)",
|
|
66
|
+
WebkitTapHighlightColor: "transparent",
|
|
67
|
+
selectors: {
|
|
68
|
+
"&:hover": {
|
|
69
|
+
color: "var(--foreground)",
|
|
70
|
+
borderColor: "var(--border-strong)",
|
|
71
|
+
},
|
|
72
|
+
"&:focus-visible": {
|
|
73
|
+
outline: "var(--border-width-strong) solid var(--foreground)",
|
|
74
|
+
outlineOffset: "2px",
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
export const codeCopyLabel = style({
|
|
80
|
+
fontSize: "var(--text-xs)",
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
export const code__body = style({
|
|
84
|
+
overflowX: "auto",
|
|
85
|
+
selectors: {
|
|
86
|
+
"& pre": {
|
|
87
|
+
margin: 0,
|
|
88
|
+
padding: "var(--space-3) var(--space-4)",
|
|
89
|
+
background: "transparent !important",
|
|
90
|
+
fontFamily: "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace",
|
|
91
|
+
fontSize: "inherit",
|
|
92
|
+
lineHeight: "inherit",
|
|
93
|
+
border: "none",
|
|
94
|
+
borderRadius: 0,
|
|
95
|
+
},
|
|
96
|
+
"& code": {
|
|
97
|
+
background: "transparent",
|
|
98
|
+
padding: 0,
|
|
99
|
+
fontSize: "inherit",
|
|
100
|
+
display: "block",
|
|
101
|
+
},
|
|
102
|
+
"& .shiki": {
|
|
103
|
+
color: "var(--shiki-light) !important",
|
|
104
|
+
backgroundColor: "transparent !important",
|
|
105
|
+
},
|
|
106
|
+
"& .shiki span": {
|
|
107
|
+
color: "var(--shiki-light) !important",
|
|
108
|
+
backgroundColor: "transparent !important",
|
|
109
|
+
},
|
|
110
|
+
"&[data-line-numbers] pre code": {
|
|
111
|
+
counterReset: "step",
|
|
112
|
+
counterIncrement: "step 0",
|
|
113
|
+
},
|
|
114
|
+
"&[data-line-numbers] pre code .line::before": {
|
|
115
|
+
content: "counter(step)",
|
|
116
|
+
counterIncrement: "step",
|
|
117
|
+
display: "inline-block",
|
|
118
|
+
width: "1.75rem",
|
|
119
|
+
marginRight: "var(--space-4)",
|
|
120
|
+
textAlign: "right",
|
|
121
|
+
color: "var(--foreground-muted)",
|
|
122
|
+
opacity: 0.7,
|
|
123
|
+
userSelect: "none",
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
export const dark = style({
|
|
129
|
+
selectors: {
|
|
130
|
+
[`& ${code__body} .shiki`]: {
|
|
131
|
+
color: "var(--shiki-dark) !important",
|
|
132
|
+
backgroundColor: "transparent !important",
|
|
133
|
+
},
|
|
134
|
+
[`& ${code__body} .shiki span`]: {
|
|
135
|
+
color: "var(--shiki-dark) !important",
|
|
136
|
+
backgroundColor: "transparent !important",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
/** 동적 키로 클래스 참조용 — `byKey[\`badge--${variant}\`]` 같은 패턴 지원. */
|
|
142
|
+
export const byKey: Record<string, string> = {
|
|
143
|
+
"code": code,
|
|
144
|
+
"code__header": code__header,
|
|
145
|
+
"code__filename": code__filename,
|
|
146
|
+
"code__copy-floating": codeCopyFloating,
|
|
147
|
+
"code__copy": code__copy,
|
|
148
|
+
"code__copy-label": codeCopyLabel,
|
|
149
|
+
"code__body": code__body,
|
|
150
|
+
"dark": dark,
|
|
151
|
+
};
|
|
@@ -0,0 +1,467 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { cn } from "@SH_UI_UTILS@";
|
|
5
|
+
import { byKey, colorPicker, colorPickerSv, colorPickerSvSaturation, colorPickerSvValue, colorPickerSvThumb, colorPickerHue, colorPickerHueThumb, colorPickerAlpha, colorPickerAlphaTrack, colorPickerRow, colorPickerSwatch, colorPickerHex, colorPickerSwatches, colorPickerSwatchBtn } from "./styles.css";
|
|
6
|
+
|
|
7
|
+
/* ───────────── types ───────────── */
|
|
8
|
+
|
|
9
|
+
interface HSV {
|
|
10
|
+
h: number; // 0~360
|
|
11
|
+
s: number; // 0~1
|
|
12
|
+
v: number; // 0~1
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
interface HSVA extends HSV {
|
|
16
|
+
a: number; // 0~1
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface ColorPickerProps
|
|
20
|
+
extends Omit<
|
|
21
|
+
React.HTMLAttributes<HTMLDivElement>,
|
|
22
|
+
"onChange" | "defaultValue" | "children"
|
|
23
|
+
> {
|
|
24
|
+
/** 제어 모드 색상값 (hex, 예: `"#FF8800"`). 6자리 / 3자리 / `#` 생략 모두 허용. */
|
|
25
|
+
value?: string;
|
|
26
|
+
/** 색상 변경 콜백. 항상 6자리 대문자 hex(`"#RRGGBB"`)로 통일되어 전달된다. */
|
|
27
|
+
onChange?: (hex: string) => void;
|
|
28
|
+
/**
|
|
29
|
+
* 비제어 모드 초기값.
|
|
30
|
+
* @default "#000000"
|
|
31
|
+
*/
|
|
32
|
+
defaultValue?: string;
|
|
33
|
+
/**
|
|
34
|
+
* compound 모드. 미지정 시 기본 레이아웃(Saturation + Hue + Hex)이 자동 렌더된다.
|
|
35
|
+
* 직접 조립하려면 `ColorPickerSaturation`/`Hue`/`Alpha`/`Hex`/`Swatches`를 자식으로 넘긴다.
|
|
36
|
+
*/
|
|
37
|
+
children?: React.ReactNode;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/* ───────────── color math ───────────── */
|
|
41
|
+
|
|
42
|
+
function clamp(n: number, min: number, max: number) {
|
|
43
|
+
return Math.min(max, Math.max(min, n));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function hexToRgb(hex: string): [number, number, number] {
|
|
47
|
+
const m = hex.replace("#", "");
|
|
48
|
+
const full = m.length === 3 ? m.split("").map((c) => c + c).join("") : m;
|
|
49
|
+
const n = parseInt(full, 16);
|
|
50
|
+
return [(n >> 16) & 255, (n >> 8) & 255, n & 255];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function rgbToHex(r: number, g: number, b: number): string {
|
|
54
|
+
const toHex = (n: number) => clamp(Math.round(n), 0, 255).toString(16).padStart(2, "0");
|
|
55
|
+
return `#${toHex(r)}${toHex(g)}${toHex(b)}`.toUpperCase();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function rgbToHsv(r: number, g: number, b: number): HSV {
|
|
59
|
+
const rn = r / 255, gn = g / 255, bn = b / 255;
|
|
60
|
+
const max = Math.max(rn, gn, bn);
|
|
61
|
+
const min = Math.min(rn, gn, bn);
|
|
62
|
+
const d = max - min;
|
|
63
|
+
let h = 0;
|
|
64
|
+
if (d !== 0) {
|
|
65
|
+
if (max === rn) h = ((gn - bn) / d) % 6;
|
|
66
|
+
else if (max === gn) h = (bn - rn) / d + 2;
|
|
67
|
+
else h = (rn - gn) / d + 4;
|
|
68
|
+
h *= 60;
|
|
69
|
+
if (h < 0) h += 360;
|
|
70
|
+
}
|
|
71
|
+
const s = max === 0 ? 0 : d / max;
|
|
72
|
+
const v = max;
|
|
73
|
+
return { h, s, v };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function hsvToRgb({ h, s, v }: HSV): [number, number, number] {
|
|
77
|
+
const c = v * s;
|
|
78
|
+
const hh = h / 60;
|
|
79
|
+
const x = c * (1 - Math.abs((hh % 2) - 1));
|
|
80
|
+
let r = 0, g = 0, b = 0;
|
|
81
|
+
if (hh >= 0 && hh < 1) [r, g, b] = [c, x, 0];
|
|
82
|
+
else if (hh < 2) [r, g, b] = [x, c, 0];
|
|
83
|
+
else if (hh < 3) [r, g, b] = [0, c, x];
|
|
84
|
+
else if (hh < 4) [r, g, b] = [0, x, c];
|
|
85
|
+
else if (hh < 5) [r, g, b] = [x, 0, c];
|
|
86
|
+
else [r, g, b] = [c, 0, x];
|
|
87
|
+
const m = v - c;
|
|
88
|
+
return [(r + m) * 255, (g + m) * 255, (b + m) * 255];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function hexToHsv(hex: string): HSV {
|
|
92
|
+
const [r, g, b] = hexToRgb(hex);
|
|
93
|
+
return rgbToHsv(r, g, b);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function hsvToHex(hsv: HSV): string {
|
|
97
|
+
const [r, g, b] = hsvToRgb(hsv);
|
|
98
|
+
return rgbToHex(r, g, b);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const HEX_RE = /^#?[0-9a-f]{6}$/i;
|
|
102
|
+
|
|
103
|
+
/* ───────────── drag hook ───────────── */
|
|
104
|
+
|
|
105
|
+
function useDrag(onMove: (e: PointerEvent, el: HTMLElement) => void) {
|
|
106
|
+
const ref = React.useRef<HTMLDivElement>(null);
|
|
107
|
+
|
|
108
|
+
const onPointerDown = (e: React.PointerEvent<HTMLDivElement>) => {
|
|
109
|
+
const el = ref.current;
|
|
110
|
+
if (!el) return;
|
|
111
|
+
el.setPointerCapture(e.pointerId);
|
|
112
|
+
onMove(e.nativeEvent, el);
|
|
113
|
+
|
|
114
|
+
const onPointerMove = (ev: PointerEvent) => onMove(ev, el);
|
|
115
|
+
const onPointerUp = (ev: PointerEvent) => {
|
|
116
|
+
el.releasePointerCapture(ev.pointerId);
|
|
117
|
+
el.removeEventListener("pointermove", onPointerMove);
|
|
118
|
+
el.removeEventListener("pointerup", onPointerUp);
|
|
119
|
+
};
|
|
120
|
+
el.addEventListener("pointermove", onPointerMove);
|
|
121
|
+
el.addEventListener("pointerup", onPointerUp);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
return { ref, onPointerDown };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/* ───────────── context ───────────── */
|
|
128
|
+
|
|
129
|
+
interface ColorPickerContextValue {
|
|
130
|
+
hsva: HSVA;
|
|
131
|
+
hex: string;
|
|
132
|
+
/** 현재 hue에 해당하는 순색(pure) hex. SV 배경용. */
|
|
133
|
+
pureHueHex: string;
|
|
134
|
+
setHsv: (next: Partial<HSV>) => void;
|
|
135
|
+
setAlpha: (a: number) => void;
|
|
136
|
+
commitHex: (raw: string) => boolean;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const ColorPickerContext = React.createContext<ColorPickerContextValue | null>(null);
|
|
140
|
+
|
|
141
|
+
function useColorPicker() {
|
|
142
|
+
const ctx = React.useContext(ColorPickerContext);
|
|
143
|
+
if (!ctx) {
|
|
144
|
+
throw new Error(
|
|
145
|
+
"ColorPicker 하위 컴포넌트는 <ColorPicker> 내부에서만 사용할 수 있습니다.",
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return ctx;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/* ───────────── root ───────────── */
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* HSV 모델 기반 색상 선택기. children을 생략하면 기본 레이아웃(SV + Hue + Hex)이 자동 렌더되고,
|
|
155
|
+
* 직접 조립하려면 ColorPickerSaturation/Hue/Alpha/Hex/Swatches를 자식으로 넘긴다.
|
|
156
|
+
* 외부 노출값은 항상 6자리 대문자 hex(`#RRGGBB`).
|
|
157
|
+
*/
|
|
158
|
+
export function ColorPicker({
|
|
159
|
+
value: valueProp,
|
|
160
|
+
onChange,
|
|
161
|
+
defaultValue = "#000000",
|
|
162
|
+
className,
|
|
163
|
+
children,
|
|
164
|
+
...rest
|
|
165
|
+
}: ColorPickerProps) {
|
|
166
|
+
const isControlled = valueProp !== undefined;
|
|
167
|
+
const [internal, setInternal] = React.useState(defaultValue);
|
|
168
|
+
const value = isControlled ? valueProp! : internal;
|
|
169
|
+
|
|
170
|
+
const [hsva, setHsva] = React.useState<HSVA>(() => ({ ...hexToHsv(value), a: 1 }));
|
|
171
|
+
|
|
172
|
+
/* 외부 value 변경 시 hsv 동기화 (우리가 내놓은 hex는 무시 — 무한 루프 방지) */
|
|
173
|
+
const lastEmittedRef = React.useRef(value);
|
|
174
|
+
React.useEffect(() => {
|
|
175
|
+
if (value === lastEmittedRef.current) return;
|
|
176
|
+
setHsva((prev) => ({ ...hexToHsv(value), a: prev.a }));
|
|
177
|
+
}, [value]);
|
|
178
|
+
|
|
179
|
+
const emit = React.useCallback(
|
|
180
|
+
(next: HSVA) => {
|
|
181
|
+
const hex = hsvToHex(next);
|
|
182
|
+
lastEmittedRef.current = hex;
|
|
183
|
+
setHsva(next);
|
|
184
|
+
if (!isControlled) setInternal(hex);
|
|
185
|
+
onChange?.(hex);
|
|
186
|
+
},
|
|
187
|
+
[isControlled, onChange],
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const setHsv = React.useCallback(
|
|
191
|
+
(partial: Partial<HSV>) => {
|
|
192
|
+
const next: HSVA = { ...hsva, ...partial };
|
|
193
|
+
const hex = hsvToHex(next);
|
|
194
|
+
lastEmittedRef.current = hex;
|
|
195
|
+
setHsva(next);
|
|
196
|
+
if (!isControlled) setInternal(hex);
|
|
197
|
+
onChange?.(hex);
|
|
198
|
+
},
|
|
199
|
+
[hsva, isControlled, onChange],
|
|
200
|
+
);
|
|
201
|
+
|
|
202
|
+
const setAlpha = React.useCallback((a: number) => {
|
|
203
|
+
setHsva((prev) => ({ ...prev, a: clamp(a, 0, 1) }));
|
|
204
|
+
}, []);
|
|
205
|
+
|
|
206
|
+
const commitHex = React.useCallback(
|
|
207
|
+
(raw: string) => {
|
|
208
|
+
const v = raw.trim();
|
|
209
|
+
if (!HEX_RE.test(v)) return false;
|
|
210
|
+
const normalized = (v.startsWith("#") ? v : `#${v}`).toUpperCase();
|
|
211
|
+
const nextHsv = hexToHsv(normalized);
|
|
212
|
+
emit({ ...nextHsv, a: hsva.a });
|
|
213
|
+
return true;
|
|
214
|
+
},
|
|
215
|
+
[emit, hsva.a],
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const pureHueHex = React.useMemo(
|
|
219
|
+
() => hsvToHex({ h: hsva.h, s: 1, v: 1 }),
|
|
220
|
+
[hsva.h],
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
const ctx = React.useMemo<ColorPickerContextValue>(
|
|
224
|
+
() => ({
|
|
225
|
+
hsva,
|
|
226
|
+
hex: value,
|
|
227
|
+
pureHueHex,
|
|
228
|
+
setHsv,
|
|
229
|
+
setAlpha,
|
|
230
|
+
commitHex,
|
|
231
|
+
}),
|
|
232
|
+
[hsva, value, pureHueHex, setHsv, setAlpha, commitHex],
|
|
233
|
+
);
|
|
234
|
+
|
|
235
|
+
return (
|
|
236
|
+
<ColorPickerContext.Provider value={ctx}>
|
|
237
|
+
<div
|
|
238
|
+
className={cn(colorPicker, className)}
|
|
239
|
+
{...rest}
|
|
240
|
+
>
|
|
241
|
+
{children ?? (
|
|
242
|
+
<>
|
|
243
|
+
<ColorPickerSaturation />
|
|
244
|
+
<ColorPickerHue />
|
|
245
|
+
<ColorPickerHex />
|
|
246
|
+
</>
|
|
247
|
+
)}
|
|
248
|
+
</div>
|
|
249
|
+
</ColorPickerContext.Provider>
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* ───────────── parts ───────────── */
|
|
254
|
+
|
|
255
|
+
export interface ColorPickerSaturationProps
|
|
256
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onPointerDown"> {}
|
|
257
|
+
|
|
258
|
+
/** 채도(S)와 명도(V)를 동시에 조절하는 2D 박스. 포인터 드래그로 조작. */
|
|
259
|
+
export function ColorPickerSaturation({
|
|
260
|
+
className,
|
|
261
|
+
style,
|
|
262
|
+
...rest
|
|
263
|
+
}: ColorPickerSaturationProps) {
|
|
264
|
+
const { hsva, hex, pureHueHex, setHsv } = useColorPicker();
|
|
265
|
+
const drag = useDrag((e, el) => {
|
|
266
|
+
const r = el.getBoundingClientRect();
|
|
267
|
+
const x = clamp((e.clientX - r.left) / r.width, 0, 1);
|
|
268
|
+
const y = clamp((e.clientY - r.top) / r.height, 0, 1);
|
|
269
|
+
setHsv({ s: x, v: 1 - y });
|
|
270
|
+
});
|
|
271
|
+
return (
|
|
272
|
+
<div
|
|
273
|
+
ref={drag.ref}
|
|
274
|
+
onPointerDown={drag.onPointerDown}
|
|
275
|
+
className={cn(colorPickerSv, className)}
|
|
276
|
+
style={{ background: pureHueHex, ...style }}
|
|
277
|
+
role="slider"
|
|
278
|
+
aria-label="채도/명도"
|
|
279
|
+
aria-valuemin={0}
|
|
280
|
+
aria-valuemax={100}
|
|
281
|
+
aria-valuenow={Math.round(hsva.s * 100)}
|
|
282
|
+
{...rest}
|
|
283
|
+
>
|
|
284
|
+
<div className={colorPickerSvSaturation} />
|
|
285
|
+
<div className={colorPickerSvValue} />
|
|
286
|
+
<div
|
|
287
|
+
className={colorPickerSvThumb}
|
|
288
|
+
style={{
|
|
289
|
+
left: `${hsva.s * 100}%`,
|
|
290
|
+
top: `${(1 - hsva.v) * 100}%`,
|
|
291
|
+
background: hex,
|
|
292
|
+
}}
|
|
293
|
+
/>
|
|
294
|
+
</div>
|
|
295
|
+
);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
export interface ColorPickerHueProps
|
|
299
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onPointerDown"> {}
|
|
300
|
+
|
|
301
|
+
/** 색상(H, 0~360°) 슬라이더. 무지개 그라데이션 위에 thumb이 위치. */
|
|
302
|
+
export function ColorPickerHue({ className, ...rest }: ColorPickerHueProps) {
|
|
303
|
+
const { hsva, setHsv } = useColorPicker();
|
|
304
|
+
const drag = useDrag((e, el) => {
|
|
305
|
+
const r = el.getBoundingClientRect();
|
|
306
|
+
const x = clamp((e.clientX - r.left) / r.width, 0, 1);
|
|
307
|
+
setHsv({ h: x * 360 });
|
|
308
|
+
});
|
|
309
|
+
return (
|
|
310
|
+
<div
|
|
311
|
+
ref={drag.ref}
|
|
312
|
+
onPointerDown={drag.onPointerDown}
|
|
313
|
+
className={cn(colorPickerHue, className)}
|
|
314
|
+
role="slider"
|
|
315
|
+
aria-label="색조"
|
|
316
|
+
aria-valuemin={0}
|
|
317
|
+
aria-valuemax={360}
|
|
318
|
+
aria-valuenow={Math.round(hsva.h)}
|
|
319
|
+
{...rest}
|
|
320
|
+
>
|
|
321
|
+
<div
|
|
322
|
+
className={colorPickerHueThumb}
|
|
323
|
+
style={{ left: `${(hsva.h / 360) * 100}%` }}
|
|
324
|
+
/>
|
|
325
|
+
</div>
|
|
326
|
+
);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export interface ColorPickerAlphaProps
|
|
330
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onPointerDown"> {}
|
|
331
|
+
|
|
332
|
+
/** 투명도(A, 0~100%) 슬라이더. 외부에 알파를 노출하지 않는 hex 모드와는 시각 표시용. */
|
|
333
|
+
export function ColorPickerAlpha({ className, style, ...rest }: ColorPickerAlphaProps) {
|
|
334
|
+
const { hsva, hex, setAlpha } = useColorPicker();
|
|
335
|
+
const drag = useDrag((e, el) => {
|
|
336
|
+
const r = el.getBoundingClientRect();
|
|
337
|
+
const x = clamp((e.clientX - r.left) / r.width, 0, 1);
|
|
338
|
+
setAlpha(x);
|
|
339
|
+
});
|
|
340
|
+
const gradient = `linear-gradient(to right, rgba(0,0,0,0) 0%, ${hex} 100%)`;
|
|
341
|
+
return (
|
|
342
|
+
<div
|
|
343
|
+
ref={drag.ref}
|
|
344
|
+
onPointerDown={drag.onPointerDown}
|
|
345
|
+
className={cn(colorPickerAlpha, className)}
|
|
346
|
+
role="slider"
|
|
347
|
+
aria-label="투명도"
|
|
348
|
+
aria-valuemin={0}
|
|
349
|
+
aria-valuemax={100}
|
|
350
|
+
aria-valuenow={Math.round(hsva.a * 100)}
|
|
351
|
+
style={style}
|
|
352
|
+
{...rest}
|
|
353
|
+
>
|
|
354
|
+
<div
|
|
355
|
+
className={colorPickerAlphaTrack}
|
|
356
|
+
style={{ backgroundImage: gradient }}
|
|
357
|
+
/>
|
|
358
|
+
<div
|
|
359
|
+
className={colorPickerHueThumb}
|
|
360
|
+
style={{ left: `${hsva.a * 100}%` }}
|
|
361
|
+
/>
|
|
362
|
+
</div>
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
export interface ColorPickerHexProps
|
|
367
|
+
extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange"> {
|
|
368
|
+
/**
|
|
369
|
+
* input 좌측에 현재 색상 미리보기 swatch 표시 여부.
|
|
370
|
+
* @default true
|
|
371
|
+
*/
|
|
372
|
+
showSwatch?: boolean;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/** Hex 직접 입력 + 좌측 swatch. blur·Enter 시 검증·커밋되며 잘못된 값은 이전 값으로 되돌린다. */
|
|
376
|
+
export function ColorPickerHex({
|
|
377
|
+
className,
|
|
378
|
+
showSwatch = true,
|
|
379
|
+
...rest
|
|
380
|
+
}: ColorPickerHexProps) {
|
|
381
|
+
const { hex, commitHex } = useColorPicker();
|
|
382
|
+
const [draft, setDraft] = React.useState(hex);
|
|
383
|
+
|
|
384
|
+
// 외부 hex 변경 시 draft 동기화 (단, 포커스 중이 아닐 때만)
|
|
385
|
+
const inputRef = React.useRef<HTMLInputElement>(null);
|
|
386
|
+
React.useEffect(() => {
|
|
387
|
+
if (document.activeElement !== inputRef.current) setDraft(hex);
|
|
388
|
+
}, [hex]);
|
|
389
|
+
|
|
390
|
+
const onCommit = () => {
|
|
391
|
+
if (!commitHex(draft)) setDraft(hex);
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
return (
|
|
395
|
+
<div
|
|
396
|
+
className={cn(colorPickerRow, className)}
|
|
397
|
+
{...rest}
|
|
398
|
+
>
|
|
399
|
+
{showSwatch && (
|
|
400
|
+
<div
|
|
401
|
+
className={colorPickerSwatch}
|
|
402
|
+
style={{ background: hex }}
|
|
403
|
+
aria-hidden
|
|
404
|
+
/>
|
|
405
|
+
)}
|
|
406
|
+
<input
|
|
407
|
+
ref={inputRef}
|
|
408
|
+
type="text"
|
|
409
|
+
className={colorPickerHex}
|
|
410
|
+
value={draft}
|
|
411
|
+
onChange={(e) => setDraft(e.target.value)}
|
|
412
|
+
onBlur={onCommit}
|
|
413
|
+
onKeyDown={(e) => {
|
|
414
|
+
if (e.key === "Enter") {
|
|
415
|
+
e.preventDefault();
|
|
416
|
+
(e.target as HTMLInputElement).blur();
|
|
417
|
+
}
|
|
418
|
+
}}
|
|
419
|
+
spellCheck={false}
|
|
420
|
+
aria-label="Hex"
|
|
421
|
+
/>
|
|
422
|
+
</div>
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export interface ColorPickerSwatchesProps
|
|
427
|
+
extends React.HTMLAttributes<HTMLDivElement> {
|
|
428
|
+
/**
|
|
429
|
+
* 표시할 hex 색상 목록. 항목 클릭 시 해당 색상으로 즉시 commit된다.
|
|
430
|
+
* 형식은 `"#RRGGBB"` 권장 (입력 시 대소문자는 자동 정규화).
|
|
431
|
+
*/
|
|
432
|
+
colors: string[];
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/** 미리 정의된 색상 팔레트 그리드. 각 항목 클릭 시 그 색상으로 즉시 커밋한다. */
|
|
436
|
+
export function ColorPickerSwatches({
|
|
437
|
+
className,
|
|
438
|
+
colors,
|
|
439
|
+
...rest
|
|
440
|
+
}: ColorPickerSwatchesProps) {
|
|
441
|
+
const { hex, commitHex } = useColorPicker();
|
|
442
|
+
return (
|
|
443
|
+
<div
|
|
444
|
+
role="group"
|
|
445
|
+
aria-label="미리 준비된 색상"
|
|
446
|
+
className={cn(colorPickerSwatches, className)}
|
|
447
|
+
{...rest}
|
|
448
|
+
>
|
|
449
|
+
{colors.map((c) => {
|
|
450
|
+
const normalized = c.toUpperCase();
|
|
451
|
+
const selected = normalized === hex.toUpperCase();
|
|
452
|
+
return (
|
|
453
|
+
<button
|
|
454
|
+
key={c}
|
|
455
|
+
type="button"
|
|
456
|
+
className={colorPickerSwatchBtn}
|
|
457
|
+
aria-label={c}
|
|
458
|
+
aria-pressed={selected}
|
|
459
|
+
data-selected={selected || undefined}
|
|
460
|
+
style={{ background: c }}
|
|
461
|
+
onClick={() => commitHex(c)}
|
|
462
|
+
/>
|
|
463
|
+
);
|
|
464
|
+
})}
|
|
465
|
+
</div>
|
|
466
|
+
);
|
|
467
|
+
}
|