|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { Box } from '@devup-ui/react' |
| 4 | +import { useRef } from 'react' |
| 5 | +import { useCallback, useState } from 'react' |
| 6 | +import { ReactNode } from 'react' |
| 7 | + |
| 8 | +interface BenchBoxProps { |
| 9 | + children: ReactNode |
| 10 | +} |
| 11 | +export function BenchBox({ children }: BenchBoxProps) { |
| 12 | + const scrollRef = useRef<HTMLDivElement>(null) |
| 13 | + const [isDragging, setIsDragging] = useState(false) |
| 14 | + const [startX, setStartX] = useState(0) |
| 15 | + const [scrollLeft, setScrollLeft] = useState(0) |
| 16 | + |
| 17 | + const handleMouseDown = useCallback((e: React.MouseEvent) => { |
| 18 | + if (!scrollRef.current) return |
| 19 | + |
| 20 | + setIsDragging(true) |
| 21 | + setStartX(e.pageX - scrollRef.current.offsetLeft) |
| 22 | + setScrollLeft(scrollRef.current.scrollLeft) |
| 23 | + scrollRef.current.style.cursor = 'grabbing' |
| 24 | + scrollRef.current.style.userSelect = 'none' |
| 25 | + }, []) |
| 26 | + |
| 27 | + const handleMouseMove = useCallback( |
| 28 | + (e: React.MouseEvent) => { |
| 29 | + if (!isDragging || !scrollRef.current) return |
| 30 | + |
| 31 | + e.preventDefault() |
| 32 | + const x = e.pageX - scrollRef.current.offsetLeft |
| 33 | + const walk = (x - startX) * 2 // 스크롤 속도 조절 |
| 34 | + scrollRef.current.scrollLeft = scrollLeft - walk |
| 35 | + }, |
| 36 | + [isDragging, startX, scrollLeft], |
| 37 | + ) |
| 38 | + |
| 39 | + const handleMouseUp = useCallback(() => { |
| 40 | + if (!scrollRef.current) return |
| 41 | + |
| 42 | + setIsDragging(false) |
| 43 | + scrollRef.current.style.cursor = 'grab' |
| 44 | + scrollRef.current.style.userSelect = 'auto' |
| 45 | + }, []) |
| 46 | + |
| 47 | + const handleMouseLeave = useCallback(() => { |
| 48 | + if (!scrollRef.current) return |
| 49 | + |
| 50 | + setIsDragging(false) |
| 51 | + scrollRef.current.style.cursor = 'grab' |
| 52 | + scrollRef.current.style.userSelect = 'auto' |
| 53 | + }, []) |
| 54 | + |
| 55 | + return ( |
| 56 | + <Box |
| 57 | + ref={scrollRef} |
| 58 | + WebkitOverflowScrolling="touch" |
| 59 | + cursor={['grab', null, null, null, 'default']} |
| 60 | + onMouseDown={handleMouseDown} |
| 61 | + onMouseLeave={handleMouseLeave} |
| 62 | + onMouseMove={handleMouseMove} |
| 63 | + onMouseUp={handleMouseUp} |
| 64 | + overflow={['auto', null, null, null, 'visible']} |
| 65 | + scrollbarWidth="none" |
| 66 | + > |
| 67 | + {children} |
| 68 | + </Box> |
| 69 | + ) |
| 70 | +} |
0 commit comments