fix: 修复侧边栏拖动条位置偏移问题
This commit is contained in:
@@ -3,13 +3,16 @@ import { useCallback, useEffect, useRef, useState } from 'react'
|
||||
export const useSidebarResize = (initialWidth: number = 250) => {
|
||||
const [sidebarWidth, setSidebarWidth] = useState(initialWidth)
|
||||
const [isResizing, setIsResizing] = useState(false)
|
||||
const offsetX = useRef(0)
|
||||
const sidebarLeft = useRef(0)
|
||||
|
||||
const startResizing = useCallback((e: React.MouseEvent) => {
|
||||
e.preventDefault()
|
||||
const rect = (e.target as HTMLElement).getBoundingClientRect()
|
||||
offsetX.current = e.clientX - rect.left
|
||||
setIsResizing(true)
|
||||
const sidebar = (e.target as HTMLElement).closest('.sidebar-container') as HTMLElement
|
||||
if (sidebar) {
|
||||
const rect = sidebar.getBoundingClientRect()
|
||||
sidebarLeft.current = rect.left
|
||||
setIsResizing(true)
|
||||
}
|
||||
}, [])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -17,7 +20,7 @@ export const useSidebarResize = (initialWidth: number = 250) => {
|
||||
|
||||
const stopResizing = () => setIsResizing(false)
|
||||
const resize = (e: MouseEvent) => {
|
||||
const newWidth = e.clientX - offsetX.current
|
||||
const newWidth = e.clientX - sidebarLeft.current
|
||||
if (newWidth > 150 && newWidth < 600) {
|
||||
setSidebarWidth(newWidth)
|
||||
}
|
||||
@@ -33,4 +36,3 @@ export const useSidebarResize = (initialWidth: number = 250) => {
|
||||
|
||||
return { sidebarWidth, startResizing }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user