@designbasekorea/ui 0.2.9 → 0.2.12

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
@@ -10853,8 +10853,11 @@ Table.displayName = 'Table';
10853
10853
  const Tabs = ({ items, defaultSelectedId, selectedId, orientation = 'horizontal', size = 'm', fullWidth = false, variant = 'default', className, onTabChange, ...props }) => {
10854
10854
  const [activeTabId, setActiveTabId] = useState(selectedId ?? defaultSelectedId ?? items[0]?.id ?? '');
10855
10855
  const [focusedTabId, setFocusedTabId] = useState('');
10856
+ const [isDragging, setIsDragging] = useState(false);
10856
10857
  const tabListRef = useRef(null);
10857
10858
  const tabRefs = useRef(new Map());
10859
+ const dragStartRef = useRef(null);
10860
+ const clickStartRef = useRef(null);
10858
10861
  // 제어 컴포넌트 처리
10859
10862
  useEffect(() => {
10860
10863
  if (selectedId !== undefined) {
@@ -10926,18 +10929,126 @@ const Tabs = ({ items, defaultSelectedId, selectedId, orientation = 'horizontal'
10926
10929
  }
10927
10930
  }, [items, orientation]);
10928
10931
  const handleTabSelect = useCallback((tabId) => {
10932
+ // 드래그 중이면 탭 선택 무시
10933
+ if (isDragging)
10934
+ return;
10929
10935
  const tab = items.find(item => item.id === tabId);
10930
10936
  if (tab && !tab.disabled) {
10931
10937
  setActiveTabId(tabId);
10932
10938
  onTabChange?.(tabId);
10933
10939
  }
10934
- }, [items, onTabChange]);
10940
+ }, [items, onTabChange, isDragging]);
10935
10941
  const handleTabFocus = useCallback((tabId) => {
10936
10942
  setFocusedTabId(tabId);
10937
10943
  }, []);
10938
10944
  const handleTabBlur = useCallback(() => {
10939
10945
  setFocusedTabId('');
10940
10946
  }, []);
10947
+ // 드래그 스크롤 핸들러들
10948
+ const handleMouseDown = useCallback((e) => {
10949
+ if (orientation !== 'horizontal' || !tabListRef.current)
10950
+ return;
10951
+ // 클릭 시작 위치와 시간 기록
10952
+ clickStartRef.current = {
10953
+ x: e.pageX,
10954
+ y: e.pageY,
10955
+ time: Date.now()
10956
+ };
10957
+ dragStartRef.current = {
10958
+ x: e.pageX - tabListRef.current.offsetLeft,
10959
+ scrollLeft: tabListRef.current.scrollLeft
10960
+ };
10961
+ }, [orientation]);
10962
+ const handleMouseMove = useCallback((e) => {
10963
+ if (!dragStartRef.current || !tabListRef.current)
10964
+ return;
10965
+ // 아직 드래그가 시작되지 않았다면 드래그 시작 여부 판단
10966
+ if (!isDragging && clickStartRef.current) {
10967
+ const deltaX = Math.abs(e.pageX - clickStartRef.current.x);
10968
+ const deltaY = Math.abs(e.pageY - clickStartRef.current.y);
10969
+ const deltaTime = Date.now() - clickStartRef.current.time;
10970
+ // 5px 이상 이동하거나 200ms 이상 지났으면 드래그 시작
10971
+ if (deltaX > 5 || deltaY > 5 || deltaTime > 200) {
10972
+ setIsDragging(true);
10973
+ }
10974
+ }
10975
+ if (!isDragging || !dragStartRef.current)
10976
+ return;
10977
+ e.preventDefault();
10978
+ const x = e.pageX - tabListRef.current.offsetLeft;
10979
+ const walk = (x - dragStartRef.current.x) * 2; // 스크롤 속도 조절
10980
+ tabListRef.current.scrollLeft = dragStartRef.current.scrollLeft - walk;
10981
+ }, [isDragging]);
10982
+ const handleMouseUp = useCallback(() => {
10983
+ setIsDragging(false);
10984
+ dragStartRef.current = null;
10985
+ clickStartRef.current = null;
10986
+ }, []);
10987
+ const handleMouseLeave = useCallback(() => {
10988
+ setIsDragging(false);
10989
+ dragStartRef.current = null;
10990
+ clickStartRef.current = null;
10991
+ }, []);
10992
+ // 터치 이벤트 핸들러들
10993
+ const handleTouchStart = useCallback((e) => {
10994
+ if (orientation !== 'horizontal' || !tabListRef.current)
10995
+ return;
10996
+ const touch = e.touches[0];
10997
+ // 클릭 시작 위치와 시간 기록
10998
+ clickStartRef.current = {
10999
+ x: touch.pageX,
11000
+ y: touch.pageY,
11001
+ time: Date.now()
11002
+ };
11003
+ dragStartRef.current = {
11004
+ x: touch.pageX - tabListRef.current.offsetLeft,
11005
+ scrollLeft: tabListRef.current.scrollLeft
11006
+ };
11007
+ }, [orientation]);
11008
+ const handleTouchMove = useCallback((e) => {
11009
+ if (!dragStartRef.current || !tabListRef.current)
11010
+ return;
11011
+ // 아직 드래그가 시작되지 않았다면 드래그 시작 여부 판단
11012
+ if (!isDragging && clickStartRef.current) {
11013
+ const touch = e.touches[0];
11014
+ const deltaX = Math.abs(touch.pageX - clickStartRef.current.x);
11015
+ const deltaY = Math.abs(touch.pageY - clickStartRef.current.y);
11016
+ const deltaTime = Date.now() - clickStartRef.current.time;
11017
+ // 5px 이상 이동하거나 200ms 이상 지났으면 드래그 시작
11018
+ if (deltaX > 5 || deltaY > 5 || deltaTime > 200) {
11019
+ setIsDragging(true);
11020
+ }
11021
+ }
11022
+ if (!isDragging || !dragStartRef.current)
11023
+ return;
11024
+ e.preventDefault();
11025
+ const touch = e.touches[0];
11026
+ const x = touch.pageX - tabListRef.current.offsetLeft;
11027
+ const walk = (x - dragStartRef.current.x) * 2; // 스크롤 속도 조절
11028
+ tabListRef.current.scrollLeft = dragStartRef.current.scrollLeft - walk;
11029
+ }, [isDragging]);
11030
+ const handleTouchEnd = useCallback(() => {
11031
+ setIsDragging(false);
11032
+ dragStartRef.current = null;
11033
+ clickStartRef.current = null;
11034
+ }, []);
11035
+ // 드래그 이벤트 리스너 등록/해제
11036
+ useEffect(() => {
11037
+ if (isDragging) {
11038
+ document.addEventListener('mousemove', handleMouseMove);
11039
+ document.addEventListener('mouseup', handleMouseUp);
11040
+ document.addEventListener('mouseleave', handleMouseLeave);
11041
+ document.addEventListener('touchmove', handleTouchMove, { passive: false });
11042
+ document.addEventListener('touchend', handleTouchEnd);
11043
+ }
11044
+ return () => {
11045
+ document.removeEventListener('mousemove', handleMouseMove);
11046
+ document.removeEventListener('mouseup', handleMouseUp);
11047
+ document.removeEventListener('mouseleave', handleMouseLeave);
11048
+ document.removeEventListener('touchmove', handleTouchMove);
11049
+ document.removeEventListener('touchend', handleTouchEnd);
11050
+ };
11051
+ }, [isDragging, handleMouseMove, handleMouseUp, handleMouseLeave, handleTouchMove, handleTouchEnd]);
10941
11052
  const activeTab = items.find(item => item.id === activeTabId);
10942
11053
  items.findIndex(item => item.id === activeTabId);
10943
11054
  const classes = clsx('designbase-tabs', `designbase-tabs--${orientation}`, `designbase-tabs--${size}`, `designbase-tabs--${variant}`, {
@@ -10945,8 +11056,9 @@ const Tabs = ({ items, defaultSelectedId, selectedId, orientation = 'horizontal'
10945
11056
  }, className);
10946
11057
  const tabListClasses = clsx('designbase-tabs__list', `designbase-tabs__list--${orientation}`, {
10947
11058
  'designbase-tabs__list--full-width': fullWidth,
11059
+ 'designbase-tabs__list--dragging': isDragging,
10948
11060
  });
10949
- return (jsxs("div", { className: classes, ...props, children: [jsx("div", { ref: tabListRef, className: tabListClasses, role: "tablist", "aria-orientation": orientation, "aria-label": "\uD0ED \uBAA9\uB85D", children: items.map((item, index) => {
11061
+ return (jsxs("div", { className: classes, ...props, children: [jsx("div", { ref: tabListRef, className: tabListClasses, role: "tablist", "aria-orientation": orientation, "aria-label": "\uD0ED \uBAA9\uB85D", onMouseDown: handleMouseDown, onTouchStart: handleTouchStart, children: items.map((item, index) => {
10950
11062
  const isSelected = item.id === activeTabId;
10951
11063
  const isFocused = item.id === focusedTabId;
10952
11064
  const isDisabled = item.disabled;