Initial commit
This commit is contained in:
33
src/hooks/ui/useSidebarResize.ts
Normal file
33
src/hooks/ui/useSidebarResize.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
|
||||
export const useSidebarResize = (initialWidth: number = 250) => {
|
||||
const [sidebarWidth, setSidebarWidth] = useState(initialWidth)
|
||||
const [isResizing, setIsResizing] = useState(false)
|
||||
|
||||
const startResizing = useCallback((e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
setIsResizing(true)
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
if (!isResizing) return
|
||||
|
||||
const stopResizing = () => setIsResizing(false)
|
||||
const resize = (e: MouseEvent) => {
|
||||
const newWidth = e.clientX
|
||||
if (newWidth > 150 && newWidth < 600) {
|
||||
setSidebarWidth(newWidth)
|
||||
}
|
||||
}
|
||||
|
||||
window.addEventListener('mousemove', resize)
|
||||
window.addEventListener('mouseup', stopResizing)
|
||||
return () => {
|
||||
window.removeEventListener('mousemove', resize)
|
||||
window.removeEventListener('mouseup', stopResizing)
|
||||
}
|
||||
}, [isResizing])
|
||||
|
||||
return { sidebarWidth, startResizing }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user