Spaces:
Running
Running
Download src/components/ResizableSplitter.tsx from zelk12/ShareGPT_Dialogue_Studio: direct link, hf CLI and curl.
- Browser
- Download file 5.16 kB
-
https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/components/ResizableSplitter.tsx
- Command line
-
hf download hf://spaces/zelk12/ShareGPT_Dialogue_Studio/src/components/ResizableSplitter.tsx
-
curl -L -o ResizableSplitter.tsx https://huggingface.co/spaces/zelk12/ShareGPT_Dialogue_Studio/resolve/main/src/components/ResizableSplitter.tsx
5.16 kB
| import React, { useEffect, useRef, useState } from 'react'; | |
| interface ResizableSplitterProps { | |
| orientation?: 'vertical' | 'horizontal'; | |
| value: number; | |
| min: number; | |
| max: number; | |
| defaultValue: number; | |
| /** Set true when dragging right/bottom handle that shrinks the panel as pointer moves right/down (e.g., right drawer) */ | |
| reverse?: boolean; | |
| onChange: (nextValue: number) => void; | |
| className?: string; | |
| label?: string; | |
| ariaLabel?: string; | |
| } | |
| export const ResizableSplitter: React.FC<ResizableSplitterProps> = ({ | |
| orientation = 'vertical', | |
| value, | |
| min, | |
| max, | |
| defaultValue, | |
| reverse = false, | |
| onChange, | |
| className = '', | |
| label = 'Изменить размер панели (двойной клик для сброса)', | |
| ariaLabel, | |
| }) => { | |
| const effectiveLabel = ariaLabel || label; | |
| const [isDragging, setIsDragging] = useState(false); | |
| const dragStartRef = useRef<{ | |
| pointerCoord: number; | |
| initialValue: number; | |
| } | null>(null); | |
| useEffect(() => { | |
| if (!isDragging) return; | |
| const handlePointerMove = (e: PointerEvent) => { | |
| if (!dragStartRef.current) return; | |
| const currentCoord = orientation === 'vertical' ? e.clientX : e.clientY; | |
| const delta = currentCoord - dragStartRef.current.pointerCoord; | |
| const signedDelta = reverse ? -delta : delta; | |
| const next = Math.min( | |
| max, | |
| Math.max(min, Math.round(dragStartRef.current.initialValue + signedDelta)) | |
| ); | |
| onChange(next); | |
| }; | |
| const stopDragging = () => { | |
| dragStartRef.current = null; | |
| setIsDragging(false); | |
| }; | |
| document.body.style.userSelect = 'none'; | |
| document.body.style.cursor = | |
| orientation === 'vertical' ? 'col-resize' : 'row-resize'; | |
| window.addEventListener('pointermove', handlePointerMove); | |
| window.addEventListener('pointerup', stopDragging); | |
| window.addEventListener('pointercancel', stopDragging); | |
| return () => { | |
| document.body.style.userSelect = ''; | |
| document.body.style.cursor = ''; | |
| window.removeEventListener('pointermove', handlePointerMove); | |
| window.removeEventListener('pointerup', stopDragging); | |
| window.removeEventListener('pointercancel', stopDragging); | |
| }; | |
| }, [isDragging, max, min, onChange, orientation, reverse]); | |
| const handlePointerDown = (e: React.PointerEvent<HTMLDivElement>) => { | |
| if (e.button !== 0 && e.pointerType === 'mouse') return; | |
| e.preventDefault(); | |
| dragStartRef.current = { | |
| pointerCoord: orientation === 'vertical' ? e.clientX : e.clientY, | |
| initialValue: value, | |
| }; | |
| setIsDragging(true); | |
| }; | |
| const handleKeyDown = (e: React.KeyboardEvent<HTMLDivElement>) => { | |
| const step = e.shiftKey ? 32 : 12; | |
| if (orientation === 'vertical') { | |
| if (e.key === 'ArrowLeft') { | |
| e.preventDefault(); | |
| const delta = reverse ? step : -step; | |
| onChange(Math.min(max, Math.max(min, value + delta))); | |
| } else if (e.key === 'ArrowRight') { | |
| e.preventDefault(); | |
| const delta = reverse ? -step : step; | |
| onChange(Math.min(max, Math.max(min, value + delta))); | |
| } else if (e.key === 'Home') { | |
| e.preventDefault(); | |
| onChange(defaultValue); | |
| } | |
| } else { | |
| if (e.key === 'ArrowUp') { | |
| e.preventDefault(); | |
| const delta = reverse ? step : -step; | |
| onChange(Math.min(max, Math.max(min, value + delta))); | |
| } else if (e.key === 'ArrowDown') { | |
| e.preventDefault(); | |
| const delta = reverse ? -step : step; | |
| onChange(Math.min(max, Math.max(min, value + delta))); | |
| } else if (e.key === 'Home') { | |
| e.preventDefault(); | |
| onChange(defaultValue); | |
| } | |
| } | |
| }; | |
| const isVertical = orientation === 'vertical'; | |
| return ( | |
| <div | |
| role="separator" | |
| tabIndex={0} | |
| aria-orientation={orientation} | |
| aria-valuenow={value} | |
| aria-valuemin={min} | |
| aria-valuemax={max} | |
| aria-label={effectiveLabel} | |
| title={effectiveLabel} | |
| onPointerDown={handlePointerDown} | |
| onDoubleClick={() => onChange(defaultValue)} | |
| onKeyDown={handleKeyDown} | |
| className={`group relative shrink-0 select-none touch-none focus:outline-hidden ${ | |
| isVertical | |
| ? 'w-1.5 -mx-0.5 cursor-col-resize flex items-center justify-center' | |
| : 'h-1.5 -my-0.5 cursor-row-resize flex items-center justify-center' | |
| } ${className}`} | |
| > | |
| {/* 1px hairline divider */} | |
| <div | |
| className={`transition-colors ${ | |
| isVertical ? 'w-px h-full' : 'h-px w-full' | |
| } ${ | |
| isDragging | |
| ? 'bg-amber-500 dark:bg-amber-400' | |
| : 'bg-stone-200 dark:bg-stone-800 group-hover:bg-amber-500/80 group-focus-visible:bg-amber-500' | |
| }`} | |
| /> | |
| {/* Subtle centered drag grip */} | |
| <div | |
| className={`absolute rounded-full transition-all pointer-events-none ${ | |
| isVertical ? 'w-1 h-8' : 'h-1 w-8' | |
| } ${ | |
| isDragging | |
| ? 'bg-amber-500 scale-110 opacity-100' | |
| : 'bg-stone-300 dark:bg-stone-700 opacity-0 group-hover:opacity-100 group-focus-visible:opacity-100' | |
| }`} | |
| /> | |
| </div> | |
| ); | |
| }; | |