Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(useDraggable): allowing calculations of bounds with fixed element #3335

Merged
merged 1 commit into from
Aug 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion packages/core/useDraggable/index.ts
Original file line number Diff line number Diff line change
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