Sortable
Wrapper for SortableJS
โก๏ธ Prerequisites
- Install the SortableJS package:
๐ฌ Usage
<script>
import { sortable } from "@sveu/extend/sortable"
import type { Options } from "sortablejs"
let list = [
{ id: 1, name: "a" },
{ id: 2, name: "b" },
{ id: 3, name: "c" },
]
const options: Options = {
animation: 500,
}
</script>
<ul
use:sortable="{options}">
</ul>
๐ฉโ๐ปAPI
๐ Options
Read the SortableJS docs
๐งช Playground
Source Code ๐
Source Code
import Sortable from "sortablejs"
import type { Options } from "sortablejs"
/**
* Move an element from one index to another in the same list.
*
* @param list - The list to move the element in.
*
* @param from - The index to move the element from.
*
* @param to - The index to move the element to.
*
*/
export function move_list<T>(list: T[], from: number, to: number) {
const _list = list
const element = _list[from]
_list.splice(from, 1)
_list.splice(to, 0, element)
return _list
}
/**
* Wrapper for the SortableJS.
*
* @param target - The element to make sortable.
*
* @param options - The options to pass to SortableJS. [See SortableJS docs](https://github.com/SortableJS/Sortable#options)
*
*/
export function sortable(target: HTMLElement, options: Options = {}) {
const _sortable = new Sortable(target, { ...options })
return {
destroy() {
_sortable.destroy()
},
}
}
Last update: 2023-03-18
Authors: Mohamed-Kaizen