svelte-sortablejsUnofficial Svelte package for the SortableJS library.
See a live demo on Netlify.
Install with npm, pnpm, or yarn.
npm i -D @jhubbardsf/svelte-sortablejs
Import SortableList from package.
import { SortableList } from '@jhubbardsf/svelte-sortablejs';
Use the SortableList component in your svelte file.
<SortableList>
<div> List Item 1 </div>
<div> List Item 2 </div>
<div> List Item 3 </div>
</SortableList>
Look in src/routes/index.svelte to see Svelte implementations of the SortableJS examples.
Below is a list explaining the SortableList component parameters. All parameters are optional. Check out the official SortableJS github for more in depth information on the parameters.
multiDragClass optionType: string
Default Value: null
Add a multiDrag class. Automatically mounts the multiDrag Plugin, sets it to true and sets fallbackTolerance to 3.
swapClass optionType: string
Default Value: null
Add a swap class. Automatically mounts the swap Plugin, sets it to true.
group optionType: string | GroupOptions
Default Value:
"name" OR { name: "...", pull: [true, false, 'clone', array], put: [true, false, array] }
sort optionType: boolean
Default Value: true
Sorting inside list
disabled optionType: boolean
Default Value: false
Disables the sortable if set to true.
store optionType: undefined
Default Value:
handle optionType: string
Default Value:
Drag handle selector within list items
swapThreshold optionType: number
Default Value: 1
Threshold of the swap zone. Must be between 0 and 1.
invertSwap optionType: boolean
Default Value: false
Will always use inverted swap zone if set to true.
invertedSwapThreshold optionType: number
Default Value:
Threshold of the inverted swap zone (will be set to swapThreshold value by default).
removeCloneOnHide optionType: boolean
Default Value: true
Remove the clone element when it is not showing, rather than just hiding it
ghostClass optionType: string
Default Value: "sortable-ghost"
Class name for the drop placeholder.
chosenClass optionType: string
Default Value: "sortable-chosen"
Class name for the chosen item.
dragClass optionType: string
Default Value: "sortable-drag"
Class name for the dragging item.
ignore optionType: string
Default Value: "a; img"
Elements to ignore
filter optionType: string | Function
Default Value:
Selectors that do not lead to dragging (String or Function)
preventOnFilter optionType: boolean
Default Value: true
animation optionType: number
Default Value: 0
easing optionType: string | function
Default Value:
Easing for animation. Defaults to null. See https://easings.net/ for examples. For other possible values, see https://www.w3schools.com/cssref/css3_pr_animation-timing-function.asp
dataIdAttr optionType: string
Default Value: "data-id"
HTML attribute that is used by the toArray() method.
delay optionType: number
Default Value: 0
Time in milliseconds to define when the sorting should start
delayOnTouchOnly optionType: boolean
Default Value: false
Whether or not the delay should be applied only if the user is using touch (eg. on a mobile device). No delay will be applied in any other case.
forceFallback optionType: boolean
Default Value: false
Ignore the HTML5 DnD behaviour and force the fallback to kick in
fallbackClass optionType: string
Default Value: "sortable-fallback"
Class name for the cloned DOM Element when using forceFallback.
fallbackOnBody optionType: boolean
Default Value: false
Appends the cloned DOM Element into the Document's Body.
fallbackTolerance optionType: number
Default Value: 0
Specify in pixels how far the mouse should move before it's considered as a drag.
fallbackOffset optionType: { x: 0, y: 0 }
Default Value: { x: 0, y: 0 }
emptyInsertThreshold optionType: number
Default Value: 5
Pixel distance mouse must be from empty sortable to insert drag element into it
direction optionType: undefined
Default Value:
Direction that the Sortable should sort in. Can be set to 'vertical', 'horizontal', or a function, which will be called whenever a target is dragged over. Must return 'vertical' or 'horizontal'. Leave blank to auto detect.
touchStartThreshold optionType: undefined
Default Value:
How many pixels the point should move before cancelling a delayed drag event
setData optionType: undefined
Default Value:
draggable optionType: undefined
Default Value: null
Specifies which items inside the element should be draggable.
onChoose optionType: fn(event: SortableEvent) => void
Default Value:
Element is chosen
onUnchoose optionType: fn(event: SortableEvent) => void
Default Value:
Element is unchosen
onStart optionType: fn(event: SortableEvent) => void
Default Value:
Element dragging started
onEnd optionType: fn(event: SortableEvent) => void
Default Value:
Element dragging ended
onAdd optionType: fn(event: SortableEvent) => void
Default Value:
Element is dropped into the list from another list
onUpdate optionType: fn(event: SortableEvent) => void
Default Value:
Changed sorting within list
onRemove optionType: fn(event: SortableEvent) => void
Default Value:
Element is removed from the list into another list
onFilter optionType: fn(event: SortableEvent) => void
Default Value:
Attempt to drag a filtered element
onSort optionType: fn(event: SortableEvent) => void
Default Value:
Called by any change to the list (add / update / remove)
onClone optionType: fn(event: SortableEvent) => void
Default Value:
Called when creating a clone of element
onMove optionType: fn(event: SortableEvent) => void
Default Value:
Event when you move an item in the list or between lists
onChange optionType: fn(event: SortableEvent) => void
Default Value:
Called when dragging element changes position