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