A lightweight Svelte Action to make your elements draggable.
Inspired from the amazing react-draggable library, and implements the same API.
pnpm add svelte-drag
# npm
npm install svelte-drag
# yarn
yarn add svelte-drag
Basic usage
<script>
import { draggable } from 'svelte-drag';
</script>
Hello
With options
<script>
import { draggable } from 'svelte-drag';
</script>
Hello
Defining options elsewhere with typescript
<script lang="ts">
import { draggable } from 'svelte-drag';
import type { DragOptions } from 'svelte-drag';
let options: DragOptions = {
axis: 'y',
bounds: 'parent',
};
</script>
Hello
There are tons of options available for this package. All of them are already documented within the code itself, so you'll never have to leave the code editor.
type: 'both' | 'x' | 'y' | 'none'
Default Value: 'both'
Axis on which the element can be dragged on. Valid values: both, x, y, none.
both - Element can move in any directionx - Only horizontal movement possibley - Only vertical movement possiblenone - No movement at allExamples:
Text
Dynamically change axis using radio buttons.
<script>
let axis;
</script>
Axis:
<label>
<input type="radio" bind:group={axis} value="both" />
Both
</label>
<label>
<input type="radio" bind:group={axis} value="x" />
x
</label>
<label>
<input type="radio" bind:group={axis} value="y" />
y
</label>
<label>
<input type="radio" bind:group={axis} value="none" />
none
</label>
Text
type: HTMLElement | 'parent' | string | { top?: number; right?: number; bottom?: number; left?: number }
Default Value: undefined
Optionally limit the drag area
parent: Limit to parent
Or, you can specify any selector and it will be bound to that.
Note: This library doesn't check whether the selector is bigger than the node element. You yourself will have to make sure of that, or it may lead to unexpected behavior.
Or, finally, you can pass an object of type { top: number; right: number; bottom: number; left: number }.
These mimic the css top, right, bottom and left, in the sense that bottom starts from the bottom of the window, and right from right of window.
If any of these properties are unspecified, they are assumed to be 0.
Examples:
Bound to any element
Hello
Bound to parent
Hello
Bound to body
Hello
Bound to an ancestor selector somewhere in page
Hello
Manually through coordinates. Empty object means bound to the window.
NOTE: It isn't strictly empty object. If you omit any property from this object, it will be assumed as 0.
Hello
Bound only to top and bottom, and unbounded horizontally in practice by setting bounds way beyond the screen.
Hello
type: boolean
Default value: true
If true, uses translate3d instead of translate to move the element around, and the hardware acceleration kicks in.
true by default, but can be set to false if blurry text issue occurs.
Example 👇
Hello
type: boolean
Default value: true
Applies user-select: none on <body /> element when dragging, to prevent the irritating effect where dragging doesn't happen and the text is selected. Applied when dragging starts and removed when it stops.
type: boolean
Default value: false
Ignores touch events with more than 1 touch. This helps when you have multiple elements on a canvas where you want to implement pinch-to-zoom behaviour.
Text
type: boolean
Default Value: undefined
Disables dragging.
type: [number, number]
Default value: undefined
Applies a grid on the page to which the element snaps to when dragging, rather than the default continuous grid.
Note: If you're programmatically creating the grid, do not set it to [0, 0] ever, that will stop drag at all. Set it to undefined to make it continuous once again.
type: { x: number; y: number }
Default Value: undefined
Controls the position of the element programmatically. Fully reactive.
Read more below in the Controlled vs Uncontrolled section.
type: string | HTMLElement
Default value: undefined
CSS Selector of an element inside the parent node(on which use:draggable is applied). If it is provided, Trying to drag inside the cancel selector will prevent dragging.
This will drag!
You shall not drag!!🧙♂️
type: string | HTMLElement
Default Value: undefined
CSS Selector of an element inside the parent node(on which use:draggable is applied). If it is provided, Only clicking and dragging on this element will allow the parent to drag, anywhere else on the parent won't work.
You shall not drag!!🧙♂️
This will drag 😁
You shall not drag!!🧙♂️
This will drag 😁
type: string
Default Value: 'svelte-draggable'
Class to apply on the element on which use:draggable is applied.
Note that if handle is provided, it will still apply class on the parent element, NOT the handle
type: string
Default Value: 'svelte-draggable-dragging'
Class to apply on the parent element when it is dragging
type: string
Default Value: 'svelte-draggable-dragged'
Class to apply on the parent element if it has been dragged at least once.
type: { x: number; y: number }
Default Value: { x: 0, y: 0 }
Offsets your element to the position you specify in the very beginning. x and y should be in pixels
type: (data: { offsetX: number; offsetY: number }) => void
Default Value: undefined
Fires when dragging start.
type: (data: { offsetX: number; offsetY: number }) => void
Default Value: undefined
Fires when dragging is going on.
type: (data: { offsetX: number; offsetY: number }) => void
Default Value: undefined
Fires when dragging ends.
svelte-drag emits 3 events, on:svelte-drag, on:svelte-drag:start & on:svelte-drag:end. These are all custom events, and can be listened on the node the use:draggable is applied to
Example:
console.log('Dragging started', e)}
on:svelte-drag={(e) => console.log(e.detail)}
on:svelte-drag:end={(e) => console.log('Dragging stopped', e)}
>
Hello
Event signatures:
on:svelte-drag:start: (e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>) => void. Provides the initial offset when dragging starts, on the e.detail object.
on:svelte-drag:: (e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>. Provides how far the element has been dragged from it's original position in x and y coordinates on the event.detail object
on:svelte-drag:end: (e: CustomEvent<{ offsetX: number; offsetY: number; domRect: DOMRect }>) => void. No internal state provided to event.detail. Provides the final offset when dragging ends, on the e.detail object.
If you scroll up, you'll see 3 options, onDragStart, onDrag and onDragEnd. These are basically event handlers that you specify as methods of the options object.
Why have two ways to listen to events? Because at the time of writing, the Svelte extension for VSCode doesn't work fully well with custom events when using TypeScript, even after they're explicitly typed by the user in TypeScript.
I take TypeScript very seriously, hence I am going an extra step to provide duplicate implementations for event handling.
How to use events-as-options? The syntax is similar to the custom events one 👇
{
// Do something
},
onDrag: ({ offsetX, offsetY, domRect }) => {
// Do something
},
onDragEnd: ({ offsetX, offsetY, domRect }) => {
// Do something
},
}}
class="box"
/>
Ultimately, this gives everyone a choice. non-TypeScript users will prefer the on:svelte-drag:* method because it is more idiomatic, and TypeScript users can go with events-as-options way to get better TS experience
Note: Do not use same event in two different ways. I.E., having
on:svelte-drag:startandonDragStartat once will have both fire at the time when dragging starts. Use only one at a time.
If you're a TypeScript user, read on below 👇
This library ships with proper TypeScript typings, for the best Developer Experience, whether authoring JS or TS.
This package exports these types you can use:
import type { DragAxis, DragBounds, DragBoundsCoords, DragOptions } from 'svelte-drag';
DragOptions is the documented list of all options provided by the component.
DragAxis is the type of axis option, and is equal to 'both' | 'x' | 'y' | 'none'.
DragBounds is 'parent' | string | Partial<DragBoundsCoords>, the complete type of bounds option.
DragBoundsCoords is when you're specifying the bounds field using an object, this is the type needed for that.
export type DragBoundsCoords = {
/** Number of pixels from left of the window */
left: number;
/** Number of pixels from top of the window */
top: number;
/** Number of pixels from the right side of window */
right: number;
/** Number of pixels from the bottom of the window */
bottom: number;
};
This is taken straight from React's philosophy(After all, this package is inspired from react-draggable).
Uncontrolled means your app doesn't control the dragging of the app. Meaning, the user drags the element, it changes position, and you do something with that action. You yourself don't change position of the element or anything. This is the default behavior of this library.
Controlled means your app, using state variables, changes the position of the element, or in simple terms, programmatically drag the element. You basically set the position property to { x: 10, y: 50 }(or any other numbers), and voila! yur now controlling the position of the element programmatically 🥳🥳
OFC, this library doesn't go fully Controlled. The user can still drag it around even when position is set.
So, when you change position, the element position changes. However, when the element is dragged by user interaction, position is not changed. This is done intentionally, as two-way data binding here isn't possible and also will lead to unexpected behavior. To keep the position variable up to date, use the on:svelte-drag event to keep your state up to date to the draggable's internal state.
To have it be strictly Controlled, meaning it can only be moved programmatically, add the disabled option to your draggable element's config
Here are a bunch of examples showing controlled behavior 👇
In case you're wondering why this library is an action, and not a component, the answer is simple: Actions usage is much much simpler and elegan
$ claude mcp add neodrag \
-- python -m otcore.mcp_server <graph>