├── .gitignore ├── package.json ├── README.md └── DragDropList.svelte /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /public/build/ 3 | 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "svelte-dragdroplist", 3 | "main": "DragDropList.svelte", 4 | "svelte": "DragDropList.svelte", 5 | "version": "1.1.1", 6 | "peerDependencies": { 7 | "svelte": "^3.0.0" 8 | }, 9 | "devDependencies": { 10 | "svelte": "^3.0.0" 11 | }, 12 | "license": "ISC", 13 | "respository": { 14 | "type": "git", 15 | "url": "git+https://github.com/jwlarocque/svelte-dragdroplist.git" 16 | }, 17 | "bugs": { 18 | "url": "https://github.com/jwlarocque/svelte-dragdroplist/issues" 19 | }, 20 | "description": "Sortable lists with Svelte 3. Animated, touch-friendly, and accessible.", 21 | "author": "John LaRocque ", 22 | "homepage": "https://github.com/jwlarocque/svelte-dragdroplist", 23 | "keywords": [ 24 | "svelte", 25 | "drag", 26 | "drop", 27 | "list", 28 | "sortable", 29 | "component", 30 | "dnd" 31 | ] 32 | } 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Svelte-DragDropList 2 | 3 | Sortable lists [made with Svelte](https://madewithsvelte.com/svelte-dragdroplist). [Available from NPM!](https://www.npmjs.com/package/svelte-dragdroplist) 4 | 5 | ### Why this component? 6 | 7 | * Bidirectional binding - data order updates as soon as the user drags a list item into a new position, even before it is dropped 8 | * Full touch support - doesn't use the HTML5 drag and drop API 9 | * Accessible - includes buttons to move elements without dragging 10 | * Easier than writing a new one, probably. 11 | 12 | ### Usage 13 | 14 | [Basic REPL](https://svelte.dev/repl/6fb61b9868734493aec65eb53dc1a4bd?version=3.22.2) 15 | [REPL with every feature!](https://svelte.dev/repl/915db3b3ed704fddb7ddfb64bcbc2624?version=3.22.2) 16 | 17 | The simplest way to use the component is to pass it an array of unique strings. If you `bind:data`, the source array will be updated as the user rearranges its items. 18 | ```js 19 | 24 | 25 | 26 | ``` 27 | 28 | ##### Unique IDs 29 | 30 | If you aren't sure that your strings will be unique, you should instead pass an array of objects, each with a unique ID: 31 | 32 | ```js 33 | let data = [{"id": 0, "text": "Boston"}, 34 | {"id": 1, "text": "Boston"}, 35 | {"id": 2, "text": "Chicago"}, 36 | {"id": 3, "text": "Denver"}]; 37 | ``` 38 | 39 | ##### HTML 40 | 41 | You can also include an "html" attribute instead of "text". It's up to you to make sure the html is clean. 42 | If you want, you can even use both in one list. 43 | ```js 44 | let data = [{"id": 0, "text": "Adams"}, 45 | {"id": 1, "text": "Boston"}, 46 | {"id": 2, "html": "

Chicago

"}, 47 | {"id": 3, "html": "

Denver

"}]; 48 | ``` 49 | 50 | ##### Removable Items 51 | 52 | A delete button can be added to each item with the `removesItems` prop: 53 | ```js 54 | 55 | ``` 56 | Note: _adding_ items is as simple as adding them to the data array. 57 | 58 | ### Styling 59 | 60 | To style the list and its elements from a parent component or global stylesheet, prefix your selectors with `.dragdroplist`. You may need to increase the specificity of your selectors or even use the `!important` rule in order to override the classes applied by Svelte. For example: 61 | 62 | ```css 63 | :global(.dragdroplist) {} /* entire component */ 64 | :global(.dragdroplist > .list > div.item) {} /* list item */ 65 | :global(.dragdroplist div.buttons > button.down) {} /* move down button */ 66 | :global(.dragdroplist div.content) {} /* text/html contents of item */ 67 | ``` 68 | 69 | If you only need to style the contents of an item, you can also use an object with an `html` property as described above. 70 | 71 | ### In Progress 72 | 73 | * Additional animations on drop -------------------------------------------------------------------------------- /DragDropList.svelte: -------------------------------------------------------------------------------- 1 | 72 | 73 | 157 | 158 | 168 |
169 |

174 |
180 | {#each data as datum, i (datum.id ? datum.id : JSON.stringify(datum))} 181 |
192 |
193 | 199 | 205 |
206 | 207 |
208 | {#if datum.html} 209 | {@html datum.html} 210 | {:else if datum.text} 211 |

{datum.text}

212 | {:else} 213 |

{datum}

214 | {/if} 215 |
216 | 217 |
218 | {#if removesItems} 219 | 223 | {/if} 224 |
225 |
226 | {/each} 227 |
228 |
--------------------------------------------------------------------------------