Skip to content

Commit

Permalink
fix(useDraggable): avoid moving out of container (#3768)
Browse files Browse the repository at this point in the history
Co-authored-by: wangliangxin3 <wangliangxin3@jd.com>
  • Loading branch information
wangliangxin and wangliangxin3 committed Feb 20, 2024
1 parent c2cfd34 commit 55b943a
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/core/useDraggable/index.ts
Expand Up @@ -162,18 +162,17 @@ export function useDraggable(
return

const container = toValue(containerElement)
const containerRect = container?.getBoundingClientRect?.()
const targetRect = toValue(target)!.getBoundingClientRect()
let { x, y } = position.value
if (axis === 'x' || axis === 'both') {
x = e.clientX - pressedDelta.value.x
if (container)
x = Math.min(Math.max(0, x), containerRect!.width + container.scrollLeft - targetRect!.width)
x = Math.min(Math.max(0, x), container.scrollWidth - targetRect!.width)
}
if (axis === 'y' || axis === 'both') {
y = e.clientY - pressedDelta.value.y
if (container)
y = Math.min(Math.max(0, y), containerRect!.height + container.scrollTop - targetRect!.height)
y = Math.min(Math.max(0, y), container.scrollHeight - targetRect!.height)
}
position.value = {
x,
Expand Down

0 comments on commit 55b943a

Please sign in to comment.