├── .gitignore ├── README.md ├── SortableList.svelte ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | yarn.lock 4 | .nvmrc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 🔃 SortableList 2 | 3 | This component renders a list of items which can be reordered by draggin and dropping and implements FLIP animation for adding/removing/reordering items in the list. 4 | 5 | To make the component work you need two thing at least: setting the `list` prop and responding to `on:reorder` event. 6 | 7 | ### Basic Example ( [Open in REPL](https://svelte.dev/repl/413e33b7f34049f08e443a31d51f5367?version=3.6.4) ) 8 | 9 | ```jsx 10 | 16 | 17 | 21 | ``` 22 | 23 | ## ⤵️ Props and Slot 24 | 25 | | name | type | required | default | 26 | | ------ | --------- | -------- | --------------------------------- | 27 | | `list` | Array | ✔️ | ✖️ | 28 | | `key` | String | ❌ | ✖️ | 29 | | `slot` | Component | ❌ | `

{key ? item[key] : item}

` | 30 | 31 | The way this works is that you are required to pass a `list` prop to the component, which could be an array with anything inside, but if the array contains objects or arrays you must pass the `key` prop to specify what property is going to be used as key (needs to be unique to each object in the array). 32 | 33 | You can customize what element is used as the list item passing any element as the child. If you do this you can access the item data by setting `let:item` on `` and `{item}` on your element ( you can also access the index in `let:index`). 34 | 35 | ## ⤴️ Events 36 | 37 | The component handles all the internal functionality but since you are passing the list as a prop, it can't actually change the data you pass to it, so you need to respond to an event that gets triggered any time you sort items. 38 | This is done using the `on:sort` event handler, which gets passed an `event` object that contains the list inside the `details` property ( this is the default way of handling event data in svelte). 39 | 40 | 41 | ### Complete Example ( [Open in REPL](https://svelte.dev/repl/0b4fd50a87a94efd81b533229b43d941?version=3.6.4) ) 42 | 43 | ```jsx 44 | 55 | 56 | 62 | 63 | 64 | ``` -------------------------------------------------------------------------------- /SortableList.svelte: -------------------------------------------------------------------------------- 1 | 69 | 70 | 83 | 84 | {#if list && list.length} 85 | 105 | {/if} 106 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-sortable-list", 3 | "version": "1.1.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "svelte": { 8 | "version": "3.6.4", 9 | "resolved": "https://registry.npmjs.org/svelte/-/svelte-3.6.4.tgz", 10 | "integrity": "sha512-oI2FAZK4iLUKwwf/ltn9Y9185+03LX3uMzKlVnLrkW5P1xazJnCOHBX5mI5FeyhSIPIyueTQtqJ9cQaEUMMXgA==", 11 | "dev": true 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-sortable-list", 3 | "main": "SortableList.svelte", 4 | "svelte": "SortableList.svelte", 5 | "devDependencies": { 6 | "svelte": "^3.6.4" 7 | }, 8 | "keywords": [ 9 | "svelte", 10 | "component", 11 | "drag-n-drop", 12 | "sortable", 13 | "list" 14 | ], 15 | "description": "A svelte 3 component that implements a list with animated drag-n-drop functionality.", 16 | "version": "1.1.0", 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/brunomolteni/svelte-sortable-list.git" 20 | }, 21 | "author": "Bruno Molteni ", 22 | "license": "ISC", 23 | "bugs": { 24 | "url": "https://github.com/brunomolteni/svelte-sortable-list/issues" 25 | }, 26 | "homepage": "https://github.com/brunomolteni/svelte-sortable-list#readme", 27 | "peerDependencies": { 28 | "svelte": "^3.4.1" 29 | } 30 | } 31 | --------------------------------------------------------------------------------