Skip to content

Commit

Permalink
feat(useDraggable): allowing calculations of bounds with fixed element (
Browse files Browse the repository at this point in the history
  • Loading branch information
linspw committed Aug 25, 2023
1 parent 8acdb47 commit c08e5e0
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/core/useDraggable/index.ts
Expand Up @@ -41,6 +41,13 @@ export interface UseDraggableOptions {
*/
draggingElement?: MaybeRefOrGetter<HTMLElement | SVGElement | Window | Document | null | undefined>

/**
* Element for calculating bounds (If not set, it will use the event's target).
*
* @default undefined
*/
containerElement?: MaybeRefOrGetter<HTMLElement | SVGElement | null | undefined>

/**
* Handle that triggers the drag event
*
Expand Down Expand Up @@ -107,6 +114,7 @@ export function useDraggable(
initialValue,
axis = 'both',
draggingElement = defaultWindow,
containerElement,
handle: draggingHandle = target,
} = options

Expand Down Expand Up @@ -134,7 +142,8 @@ export function useDraggable(
return
if (toValue(exact) && e.target !== toValue(target))
return
const rect = toValue(target)!.getBoundingClientRect()
const container = toValue(containerElement) ?? toValue(target)
const rect = container!.getBoundingClientRect()
const pos = {
x: e.clientX - rect.left,
y: e.clientY - rect.top,
Expand Down

0 comments on commit c08e5e0

Please sign in to comment.