@designbasekorea/ui 0.2.36 → 0.2.38

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -5322,14 +5322,13 @@ Chip.displayName = 'Chip';
5322
5322
  /* ----------------------- 유틸 ----------------------- */
5323
5323
  const clamp = (n, min, max) => Math.max(min, Math.min(max, n));
5324
5324
  const normalizeHex = (s) => (s || '').trim().toUpperCase();
5325
- /** #RRGGBB {r,g,b} */
5325
+ const isHex = (s) => /^#?[0-9A-Fa-f]{6}$/.test(s || '');
5326
5326
  const hexToRgb = (hex) => {
5327
5327
  const m = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex.trim());
5328
5328
  if (!m)
5329
5329
  return null;
5330
5330
  return { r: parseInt(m[1], 16), g: parseInt(m[2], 16), b: parseInt(m[3], 16) };
5331
5331
  };
5332
- /** RGB → HEX */
5333
5332
  const toHex = (r, g, b) => {
5334
5333
  const h = (x) => clamp(Math.round(x), 0, 255).toString(16).padStart(2, '0');
5335
5334
  return `#${h(r)}${h(g)}${h(b)}`.toUpperCase();
@@ -5428,9 +5427,9 @@ const rgbToHsl = (r, g, b) => {
5428
5427
  return { h, s: s * 100, l: l * 100 };
5429
5428
  };
5430
5429
  /* ----------------------- 컴포넌트 ----------------------- */
5431
- const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left', value, defaultValue = '#006FFF', showInput = true, showAlpha = true, showFormatSelector = true, showCopyButton = true, disabled = false, readonly = false, onChangeFormat = 'hex', fireOnInit = false, changeDebounceMs = 0, onChange, onApply, onCancel, className, }) => {
5432
- /** 초기 HSV props에서 즉시 도출(StrictMode 안전) */
5433
- const initialHex = normalizeHex(value || defaultValue) || '#006FFF';
5430
+ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left', value, defaultValue = '#006FFF', showInput = true, showAlpha = true, showFormatSelector = true, showCopyButton = true, showEyedropper = true, disabled = false, readonly = false, onChangeFormat = 'hex', fireOnInit = false, changeDebounceMs = 0, emitDuringDrag = true, onChange, onApply, onCancel, className, }) => {
5431
+ /** 초기 HSV StrictMode에서도 안전하도록 lazy init */
5432
+ const initialHex = (isHex(value) ? normalizeHex(value) : normalizeHex(defaultValue)) || '#006FFF';
5434
5433
  const initialRgb = hexToRgb(initialHex) || { r: 0, g: 111, b: 255 };
5435
5434
  const initialHsv = rgbToHsv(initialRgb.r, initialRgb.g, initialRgb.b);
5436
5435
  const [h, setH] = useState(() => initialHsv.h);
@@ -5445,17 +5444,26 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5445
5444
  const [tempColor, setTempColor] = useState('');
5446
5445
  const pickerRef = useRef(null);
5447
5446
  const areaRef = useRef(null);
5447
+ /** 드래그 상태/자체 발사 억제 */
5448
5448
  const dragging = useRef(false);
5449
- /** onChange 보호 상태 */
5450
- const lastEmittedRef = useRef(normalizeHex(initialHex)); // ✅ 초기값 미리 기록
5451
- const didMountRef = useRef(false); // ✅ 첫 렌더 감지
5449
+ const didMountRef = useRef(false);
5450
+ const lastEmittedHexRef = useRef(initialHex);
5451
+ const suppressNextValueSyncRef = useRef(false);
5452
5452
  const emitErrorCountRef = useRef(0);
5453
5453
  const emitBlockedUntilRef = useRef(0);
5454
- /** 제어형(value) 변화 → HSV 반영 */
5454
+ const debounceTimerRef = useRef(null);
5455
+ /** 외부 value 변화 → 내부 HSV 반영(자기 발사 직후는 무시 가능) */
5455
5456
  useEffect(() => {
5456
5457
  if (value == null)
5457
5458
  return;
5458
5459
  const norm = normalizeHex(value);
5460
+ if (!isHex(norm))
5461
+ return;
5462
+ // 자기 자신이 방금 보낸 값이면 동기화 스킵(미세 진동 방지)
5463
+ if (suppressNextValueSyncRef.current && norm === lastEmittedHexRef.current) {
5464
+ suppressNextValueSyncRef.current = false;
5465
+ return;
5466
+ }
5459
5467
  const rgb = hexToRgb(norm);
5460
5468
  if (!rgb)
5461
5469
  return;
@@ -5469,7 +5477,7 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5469
5477
  const rgb = useMemo(() => hsvToRgb(h, s, v), [h, s, v]);
5470
5478
  const hex = useMemo(() => toHex(rgb.r, rgb.g, rgb.b), [rgb]);
5471
5479
  const hsl = useMemo(() => rgbToHsl(rgb.r, rgb.g, rgb.b), [rgb]);
5472
- /** UI 표시 문자열 */
5480
+ /** 표시 문자열(UI 포맷) */
5473
5481
  const uiFormatted = useMemo(() => {
5474
5482
  const alpha = a / 100;
5475
5483
  switch (format) {
@@ -5481,56 +5489,9 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5481
5489
  default: return hex;
5482
5490
  }
5483
5491
  }, [format, hex, rgb, hsl, a]);
5484
- /** 콜백으로 내보낼 문자열 */
5485
- const outward = useMemo(() => {
5486
- if (onChangeFormat === 'hex')
5487
- return hex;
5488
- return uiFormatted;
5489
- }, [hex, uiFormatted, onChangeFormat]);
5490
- /** 최초 렌더 스킵 + 동일값/레이트리밋 검사 후 onChange */
5491
- useEffect(() => {
5492
- if (!onChange)
5493
- return;
5494
- // 첫 렌더는 기본 차단 (옵션으로 허용)
5495
- if (!didMountRef.current) {
5496
- didMountRef.current = true;
5497
- if (!fireOnInit)
5498
- return; // 기본: 초기 발사 금지
5499
- }
5500
- const now = Date.now();
5501
- if (emitBlockedUntilRef.current > now)
5502
- return;
5503
- const currentHexNorm = normalizeHex(outward);
5504
- const last = lastEmittedRef.current;
5505
- // 제어형: 부모 value와 동일하면 발사 금지
5506
- if (value && normalizeHex(value) === currentHexNorm)
5507
- return;
5508
- // 직전 발사값과 동일하면 금지
5509
- if (last === currentHexNorm)
5510
- return;
5511
- const fire = () => {
5512
- try {
5513
- lastEmittedRef.current = currentHexNorm;
5514
- onChange(onChangeFormat === 'hex' ? currentHexNorm : outward);
5515
- emitErrorCountRef.current = 0;
5516
- }
5517
- catch (e) {
5518
- emitErrorCountRef.current += 1;
5519
- if (emitErrorCountRef.current >= 20) {
5520
- emitBlockedUntilRef.current = Date.now() + 3000;
5521
- // eslint-disable-next-line no-console
5522
- console.warn('[ColorPicker] onChange errors too frequent; temporarily muted.');
5523
- }
5524
- }
5525
- };
5526
- if (changeDebounceMs > 0) {
5527
- const t = setTimeout(fire, changeDebounceMs);
5528
- return () => clearTimeout(t);
5529
- }
5530
- fire();
5531
- // eslint-disable-next-line react-hooks/exhaustive-deps
5532
- }, [outward]); // 의도적으로 onChange/value 제외
5533
- /** colorInput 동기화(표시 전용) */
5492
+ /** 바깥으로 내보낼 문자열 */
5493
+ const outward = useMemo(() => (onChangeFormat === 'hex' ? hex : uiFormatted), [hex, uiFormatted, onChangeFormat]);
5494
+ /** colorInput(표시 전용) 동기화 — 순수 표시만, 포커스 중이면 건드리지 않는 게 안전하지만 간단히 비교 후 반영 */
5534
5495
  useEffect(() => {
5535
5496
  const next = format === 'hex' ? normalizeHex(uiFormatted) : uiFormatted;
5536
5497
  if (colorInput !== next)
@@ -5547,7 +5508,67 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5547
5508
  document.addEventListener('mousedown', handler);
5548
5509
  return () => document.removeEventListener('mousedown', handler);
5549
5510
  }, [isOpen, type]);
5550
- /** S/V 영역 업데이트 */
5511
+ /** ===== onChange 발사 컨트롤러 ===== */
5512
+ const actuallyEmit = useCallback(() => {
5513
+ if (!onChange)
5514
+ return;
5515
+ const now = Date.now();
5516
+ if (emitBlockedUntilRef.current > now)
5517
+ return;
5518
+ const currentHex = normalizeHex(hex);
5519
+ const parentHex = isHex(value) ? normalizeHex(value) : null;
5520
+ // 부모값과 동치면 발사 금지
5521
+ if (parentHex && parentHex === currentHex)
5522
+ return;
5523
+ // 직전 발사와 동일하면 발사 금지
5524
+ if (lastEmittedHexRef.current === currentHex)
5525
+ return;
5526
+ try {
5527
+ lastEmittedHexRef.current = currentHex;
5528
+ suppressNextValueSyncRef.current = true; // 다음 value 동기화 스킵(왕복 진동 방지)
5529
+ onChange(onChangeFormat === 'hex' ? currentHex : outward);
5530
+ emitErrorCountRef.current = 0;
5531
+ }
5532
+ catch {
5533
+ emitErrorCountRef.current += 1;
5534
+ if (emitErrorCountRef.current >= 20) {
5535
+ emitBlockedUntilRef.current = Date.now() + 3000;
5536
+ console.warn('[ColorPicker] onChange errors too frequent; temporarily muted.');
5537
+ }
5538
+ }
5539
+ }, [hex, outward, onChange, onChangeFormat, value]);
5540
+ /** 변경 감지 → 발사(드래그 중 정책 + 디바운스 반영) */
5541
+ useEffect(() => {
5542
+ // 첫 렌더
5543
+ if (!didMountRef.current) {
5544
+ didMountRef.current = true;
5545
+ if (!fireOnInit)
5546
+ return;
5547
+ }
5548
+ // 드래그 중이면 정책 적용
5549
+ if (dragging.current && !emitDuringDrag) {
5550
+ // 드래그 끝에서 발사하므로 지금은 무시
5551
+ return;
5552
+ }
5553
+ if (!onChange)
5554
+ return;
5555
+ if (changeDebounceMs > 0) {
5556
+ if (debounceTimerRef.current)
5557
+ window.clearTimeout(debounceTimerRef.current);
5558
+ debounceTimerRef.current = window.setTimeout(() => {
5559
+ actuallyEmit();
5560
+ }, changeDebounceMs);
5561
+ return () => {
5562
+ if (debounceTimerRef.current)
5563
+ window.clearTimeout(debounceTimerRef.current);
5564
+ };
5565
+ }
5566
+ else {
5567
+ actuallyEmit();
5568
+ }
5569
+ // eslint-disable-next-line react-hooks/exhaustive-deps
5570
+ }, [hex, a, format]); // h/s/v → hex로 수렴됨. a/format도 외부로 나갈 수 있으니 포함.
5571
+ /** ===== SV 영역 ===== */
5551
5572
  const updateFromArea = (clientX, clientY) => {
5552
5573
  const el = areaRef.current;
5553
5574
  if (!el)
@@ -5557,26 +5578,69 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5557
5578
  const y = clamp(clientY - rect.top, 0, rect.height);
5558
5579
  const newS = (x / rect.width) * 100;
5559
5580
  const newV = 100 - (y / rect.height) * 100;
5581
+ // 소수점 그대로 보관 → 색 떨림 감소
5560
5582
  setS(newS);
5561
5583
  setV(newV);
5562
5584
  };
5563
- const onAreaMouseDown = (e) => { dragging.current = true; updateFromArea(e.clientX, e.clientY); };
5564
- const onAreaMouseMove = (e) => { if (dragging.current)
5565
- updateFromArea(e.clientX, e.clientY); };
5566
- const onAreaMouseUp = () => { dragging.current = false; };
5567
- const onAreaLeave = () => { dragging.current = false; };
5568
- const onAreaTouchStart = (e) => { dragging.current = true; const t = e.touches[0]; updateFromArea(t.clientX, t.clientY); };
5569
- const onAreaTouchMove = (e) => { if (!dragging.current)
5570
- return; const t = e.touches[0]; updateFromArea(t.clientX, t.clientY); };
5571
- const onAreaTouchEnd = () => { dragging.current = false; };
5585
+ const onAreaMouseDown = (e) => {
5586
+ if (disabled || readonly)
5587
+ return;
5588
+ dragging.current = true;
5589
+ updateFromArea(e.clientX, e.clientY);
5590
+ };
5591
+ const onAreaMouseMove = (e) => {
5592
+ if (!dragging.current || disabled || readonly)
5593
+ return;
5594
+ updateFromArea(e.clientX, e.clientY);
5595
+ };
5596
+ const finishDrag = () => {
5597
+ if (!dragging.current)
5598
+ return;
5599
+ dragging.current = false;
5600
+ // 드래그 종료 시 한 번만 발사
5601
+ if (!emitDuringDrag) {
5602
+ if (changeDebounceMs > 0) {
5603
+ if (debounceTimerRef.current)
5604
+ window.clearTimeout(debounceTimerRef.current);
5605
+ debounceTimerRef.current = window.setTimeout(() => {
5606
+ actuallyEmit();
5607
+ }, changeDebounceMs);
5608
+ }
5609
+ else {
5610
+ actuallyEmit();
5611
+ }
5612
+ }
5613
+ };
5614
+ const onAreaMouseUp = finishDrag;
5615
+ const onAreaLeave = finishDrag;
5616
+ const onAreaTouchStart = (e) => {
5617
+ if (disabled || readonly)
5618
+ return;
5619
+ dragging.current = true;
5620
+ const t = e.touches[0];
5621
+ updateFromArea(t.clientX, t.clientY);
5622
+ };
5623
+ const onAreaTouchMove = (e) => {
5624
+ if (!dragging.current || disabled || readonly)
5625
+ return;
5626
+ const t = e.touches[0];
5627
+ updateFromArea(t.clientX, t.clientY);
5628
+ };
5629
+ const onAreaTouchEnd = finishDrag;
5572
5630
  /** 슬라이더 */
5573
- const onHueChange = useCallback((e) => setH(parseFloat(e.target.value)), []);
5631
+ const onHueChange = useCallback((e) => {
5632
+ if (disabled || readonly)
5633
+ return;
5634
+ setH(parseFloat(e.target.value));
5635
+ }, [disabled, readonly]);
5574
5636
  const onAlphaChange = useCallback((e) => {
5637
+ if (disabled || readonly)
5638
+ return;
5575
5639
  const newAlpha = parseInt(e.target.value, 10);
5576
5640
  setA(newAlpha);
5577
5641
  setAlphaInput(String(newAlpha));
5578
- }, []);
5579
- /** 컬러 인풋 확정 */
5642
+ }, [disabled, readonly]);
5643
+ /** 입력 확정 */
5580
5644
  const commitColorInput = useCallback(() => {
5581
5645
  const str = colorInput.trim();
5582
5646
  if (/^#([0-9A-Fa-f]{6})$/.test(str)) {
@@ -5616,7 +5680,9 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5616
5680
  return;
5617
5681
  }
5618
5682
  // 인식 실패 → 표시값으로 원복
5619
- setColorInput(format === 'hex' ? normalizeHex(uiFormatted) : uiFormatted);
5683
+ const next = format === 'hex' ? normalizeHex(uiFormatted) : uiFormatted;
5684
+ if (colorInput !== next)
5685
+ setColorInput(next);
5620
5686
  }, [colorInput, uiFormatted, format]);
5621
5687
  const onColorKeyDown = (e) => { if (e.key === 'Enter')
5622
5688
  commitColorInput(); };
@@ -5625,7 +5691,7 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5625
5691
  if (colorInput !== next)
5626
5692
  setColorInput(next);
5627
5693
  };
5628
- /** 알파 인풋 확정 */
5694
+ /** 알파 입력 확정 */
5629
5695
  const commitAlphaInput = useCallback(() => {
5630
5696
  const n = Number(alphaInput.trim());
5631
5697
  if (!Number.isNaN(n) && n >= 0 && n <= 100) {
@@ -5670,6 +5736,7 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5670
5736
  console.error(e);
5671
5737
  }
5672
5738
  }, []);
5739
+ const isEyedropperAvailable = typeof window !== 'undefined' && 'EyeDropper' in window;
5673
5740
  /** 모달 */
5674
5741
  const handleModalOpen = useCallback(() => {
5675
5742
  if (type === 'modal')
@@ -5734,7 +5801,7 @@ const ColorPicker = ({ size = 'm', type = 'dropdown', position = 'bottom-left',
5734
5801
  const Trigger = (jsxs("div", { className: "designbase-color-picker__trigger", onClick: () => !disabled && !readonly && (type === 'modal' ? handleModalOpen() : setIsOpen(v => !v)), children: [jsx("div", { className: "designbase-color-picker__color-display", children: jsx("div", { className: "designbase-color-picker__color-box", style: {
5735
5802
  backgroundColor: showAlpha ? `rgba(${rgb.r}, ${rgb.g}, ${rgb.b}, ${a / 100})` : hex
5736
5803
  } }) }), showInput && (jsx("input", { type: "text", value: colorInput, onChange: (e) => setColorInput(e.target.value), onKeyDown: onColorKeyDown, onBlur: onColorBlur, onClick: (e) => e.stopPropagation(), disabled: disabled, readOnly: readonly, className: "designbase-color-picker__input", placeholder: "#000000" })), showInput && showCopyButton && (jsx("button", { type: "button", className: "designbase-color-picker__copy-button-inline", onClick: (e) => { e.stopPropagation(); onCopy(); }, disabled: disabled, "aria-label": "Copy color value", children: isCopied ? jsx(DoneIcon$1, { size: 14 }) : jsx(CopyIcon, { size: 14 }) })), jsx("button", { type: "button", className: "designbase-color-picker__toggle", disabled: disabled, "aria-label": "Toggle color picker", children: jsx(ChevronDownIcon, { size: 16 }) })] }));
5737
- const Selector = (jsxs("div", { className: "designbase-color-picker__selector", children: [jsx("div", { className: "designbase-color-picker__color-area", children: jsx("div", { ref: areaRef, className: "designbase-color-picker__color-field", style: areaBackground, onMouseDown: onAreaMouseDown, onMouseMove: onAreaMouseMove, onMouseUp: onAreaMouseUp, onMouseLeave: onAreaLeave, onTouchStart: onAreaTouchStart, onTouchMove: onAreaTouchMove, onTouchEnd: onAreaTouchEnd, children: jsx("div", { className: "designbase-color-picker__color-pointer", style: { left: `${s}%`, top: `${100 - v}%`, backgroundColor: `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})` } }) }) }), jsxs("div", { className: "designbase-color-picker__controls", children: [jsx(Button, { variant: "tertiary", size: "m", iconOnly: true, onClick: onEyedrop, "aria-label": "Eyedropper tool", children: jsx(EyedropperIcon, {}) }), jsxs("div", { className: "designbase-color-picker__slider-container", children: [jsx("div", { className: "designbase-color-picker__hue-slider", children: jsx("input", { type: "range", min: 0, max: 360, value: h, onChange: onHueChange, className: "designbase-color-picker__slider designbase-color-picker__slider--hue", style: hueTrackStyle }) }), showAlpha && (jsx("div", { className: "designbase-color-picker__alpha-slider", children: jsx("input", { type: "range", min: 0, max: 100, value: a, onChange: onAlphaChange, className: "designbase-color-picker__slider designbase-color-picker__slider--alpha", style: alphaTrackStyle }) }))] })] }), jsxs("div", { className: "designbase-color-picker__value-display", children: [showFormatSelector && (jsx(Select, { value: format, onChange: (v) => setFormat(v), showClearButton: false, options: [
5804
+ const Selector = (jsxs("div", { className: "designbase-color-picker__selector", children: [jsx("div", { className: "designbase-color-picker__color-area", children: jsx("div", { ref: areaRef, className: "designbase-color-picker__color-field", style: areaBackground, onMouseDown: onAreaMouseDown, onMouseMove: onAreaMouseMove, onMouseUp: onAreaMouseUp, onMouseLeave: onAreaLeave, onTouchStart: onAreaTouchStart, onTouchMove: onAreaTouchMove, onTouchEnd: onAreaTouchEnd, children: jsx("div", { className: "designbase-color-picker__color-pointer", style: { left: `${s}%`, top: `${100 - v}%`, backgroundColor: `rgb(${rgb.r}, ${rgb.g}, ${rgb.b})` } }) }) }), jsxs("div", { className: "designbase-color-picker__controls", children: [showEyedropper && isEyedropperAvailable && (jsx(Button, { variant: "tertiary", size: "m", iconOnly: true, onClick: onEyedrop, "aria-label": "Eyedropper tool", children: jsx(EyedropperIcon, {}) })), jsxs("div", { className: "designbase-color-picker__slider-container", children: [jsx("div", { className: "designbase-color-picker__hue-slider", children: jsx("input", { type: "range", min: 0, max: 360, value: h, onChange: onHueChange, className: "designbase-color-picker__slider designbase-color-picker__slider--hue", style: hueTrackStyle }) }), showAlpha && (jsx("div", { className: "designbase-color-picker__alpha-slider", children: jsx("input", { type: "range", min: 0, max: 100, value: a, onChange: onAlphaChange, className: "designbase-color-picker__slider designbase-color-picker__slider--alpha", style: alphaTrackStyle }) }))] })] }), jsxs("div", { className: "designbase-color-picker__value-display", children: [showFormatSelector && (jsx(Select, { value: format, onChange: (v) => setFormat(v), showClearButton: false, options: [
5738
5805
  { label: 'HEX', value: 'hex' },
5739
5806
  { label: 'RGB', value: 'rgb' },
5740
5807
  { label: 'RGBA', value: 'rgba' },