|
| 1 | +<script setup lang="ts"> |
| 2 | +// This starter template is using Vue 3 <script setup> SFCs |
| 3 | +// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup |
| 4 | +import HelloWorld from "./components/HelloWorld.vue"; |
| 5 | +</script> |
| 6 | + |
| 7 | +<template> |
| 8 | + <h1>SortableJS-vue3 demo</h1> |
| 9 | + <ol class="instructions"> |
| 10 | + <li>First run <code>yarn add sortablejs-vue3 sortablejs</code></li> |
| 11 | + <li> |
| 12 | + Then add |
| 13 | + <pre><code>import Sortable from "sortablejs-vue3"</code></pre> |
| 14 | + in your <code><script setup></code> |
| 15 | + </li> |
| 16 | + <li> |
| 17 | + Finally use the component: |
| 18 | + <pre> |
| 19 | +{{ `<Sortable |
| 20 | + :list="list" |
| 21 | + :itemKey="itemKey" |
| 22 | + options="options" |
| 23 | + @choose="(event) => console.log(event)" |
| 24 | + @end="(event) => console.log(event)"> |
| 25 | + <template #item="{element, index}"> |
| 26 | + <div class="draggable" :key="element.id"> |
| 27 | + {{ element.name }} |
| 28 | + </div> |
| 29 | + </template> |
| 30 | +</Sortable>` }} |
| 31 | + </pre> |
| 32 | + </li> |
| 33 | + </ol> |
| 34 | + <details> |
| 35 | + <summary>Props</summary> |
| 36 | + <ul> |
| 37 | + <li> |
| 38 | + <strong><code>options</code></strong |
| 39 | + >: An object supporting all SortedJS options. See Sortable on GitHub |
| 40 | + <a |
| 41 | + href="https://github.com/SortableJS/Sortable#options" |
| 42 | + rel="noreferrer" |
| 43 | + target="_blank" |
| 44 | + >for a full list</a |
| 45 | + >. |
| 46 | + </li> |
| 47 | + <li> |
| 48 | + <strong><code>list</code></strong |
| 49 | + >: An array-like object of elements to be made draggable. |
| 50 | + </li> |
| 51 | + <li> |
| 52 | + <strong><code>itemKey</code></strong |
| 53 | + >: A key to index the elements of the list. |
| 54 | + </li> |
| 55 | + </ul> |
| 56 | + </details> |
| 57 | + <details> |
| 58 | + <summary>Code for the below demo</summary> |
| 59 | + <pre> |
| 60 | +{{ `<script setup lang="ts"> |
| 61 | +import Sortable from "./Sortable.vue"; |
| 62 | +import { computed } from 'vue' |
| 63 | +import type { SortableOptions } from "sortablejs"; |
| 64 | +
|
| 65 | +const elements = computed(() => { |
| 66 | + return [ |
| 67 | + { |
| 68 | + id: '1', |
| 69 | + text: 'One', |
| 70 | + children: [ |
| 71 | + { |
| 72 | + id: '1-1', |
| 73 | + text: 'One-One', |
| 74 | + children: [ |
| 75 | + { |
| 76 | + id: '1-1-1', |
| 77 | + text: 'One-One-One', |
| 78 | + }, |
| 79 | + { |
| 80 | + id: '1-1-2', |
| 81 | + text: 'One-One-Two', |
| 82 | + }, |
| 83 | + ], |
| 84 | + }, |
| 85 | + { |
| 86 | + id: '1-2', |
| 87 | + text: 'One-Two', |
| 88 | + }, |
| 89 | + ], |
| 90 | + }, |
| 91 | + { |
| 92 | + id: '2', |
| 93 | + text: 'Two' |
| 94 | + }, |
| 95 | + { |
| 96 | + id: '3', |
| 97 | + text: 'Three' |
| 98 | + } |
| 99 | + ] |
| 100 | +}) |
| 101 | +
|
| 102 | +const logEvent = (evt: Event, evt2?: Event) => { |
| 103 | + if (evt2) { |
| 104 | + console.log(evt, evt2); |
| 105 | + } else { |
| 106 | + console.log(evt); |
| 107 | + } |
| 108 | +} |
| 109 | +
|
| 110 | +const options = computed(() => { |
| 111 | + return { |
| 112 | + draggable: '.draggable', |
| 113 | + animation: 150, |
| 114 | + ghostClass: 'ghost', |
| 115 | + dragClass: 'drag', |
| 116 | + } as SortableOptions |
| 117 | +}) |
| 118 | +</script> |
| 119 | + |
| 120 | +<style lang="css" scoped> |
| 121 | +main { |
| 122 | + max-width: 800px; |
| 123 | + margin: 0 auto; |
| 124 | +} |
| 125 | +
|
| 126 | +.draggable { |
| 127 | + background: #fff; |
| 128 | + padding: 10px; |
| 129 | + margin: 10px; |
| 130 | + border: 1px solid #ccc; |
| 131 | + cursor: move; |
| 132 | +} |
| 133 | +
|
| 134 | +.ghost { |
| 135 | + opacity: 0.5; |
| 136 | + background: #fff; |
| 137 | + border: 1px dashed #ccc; |
| 138 | +} |
| 139 | +
|
| 140 | +.drag { |
| 141 | + background: #f5f5f5; |
| 142 | +} |
| 143 | +</style> |
| 144 | + |
| 145 | +<template> |
| 146 | + <main> |
| 147 | + <Sortable |
| 148 | + :list="elements" |
| 149 | + item-key="id" |
| 150 | + :options="options" |
| 151 | + @change="logEvent" |
| 152 | + @choose="logEvent" |
| 153 | + @unchoose="logEvent" |
| 154 | + @start="logEvent" |
| 155 | + @end="logEvent" |
| 156 | + @add="logEvent" |
| 157 | + @update="logEvent" |
| 158 | + @sort="logEvent" |
| 159 | + @remove="logEvent" |
| 160 | + @filter="logEvent" |
| 161 | + @move="logEvent" |
| 162 | + @clone="logEvent" |
| 163 | + > |
| 164 | + <template #item="{element, index}"> |
| 165 | + <div class="draggable" :key="element.id"> |
| 166 | + {{ element.text }} |
| 167 | + <Sortable |
| 168 | + v-if="element.children" |
| 169 | + :list="element.children" |
| 170 | + item-key="id" |
| 171 | + :options="options" |
| 172 | + @change="logEvent" |
| 173 | + @choose="logEvent" |
| 174 | + @unchoose="logEvent" |
| 175 | + @start="logEvent" |
| 176 | + @end="logEvent" |
| 177 | + @add="logEvent" |
| 178 | + @update="logEvent" |
| 179 | + @sort="logEvent" |
| 180 | + @remove="logEvent" |
| 181 | + @filter="logEvent" |
| 182 | + @move="logEvent" |
| 183 | + @clone="logEvent" |
| 184 | + > |
| 185 | + <template #item="{element, index}"> |
| 186 | + <div class="draggable" :key="element.id"> |
| 187 | + {{ element.text }} |
| 188 | + </div> |
| 189 | + </template> |
| 190 | + </Sortable> |
| 191 | + </div> |
| 192 | + </template> |
| 193 | + </Sortable> |
| 194 | + </main> |
| 195 | +</template> |
| 196 | + |
| 197 | +<style scoped> |
| 198 | +
|
| 199 | +</style> |
| 200 | +`}} |
| 201 | + </pre> |
| 202 | + </details> |
| 203 | + <p> |
| 204 | + Open your console to view the events being logged while you interact with |
| 205 | + the list below. |
| 206 | + </p> |
| 207 | + |
| 208 | + <HelloWorld /> |
| 209 | +</template> |
| 210 | + |
| 211 | +<style> |
| 212 | +#app { |
| 213 | + font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, |
| 214 | + Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif; |
| 215 | + -webkit-font-smoothing: antialiased; |
| 216 | + -moz-osx-font-smoothing: grayscale; |
| 217 | + color: #2c3e50; |
| 218 | + margin: 0 auto; |
| 219 | + max-width: 800px; |
| 220 | +} |
| 221 | +
|
| 222 | +#app .instructions { |
| 223 | + font-size: 1.1rem; |
| 224 | +} |
| 225 | +
|
| 226 | +#app .instructions code { |
| 227 | + font-size: .9rem; |
| 228 | +} |
| 229 | +
|
| 230 | +#app .instructions li { |
| 231 | + margin-bottom: .5rem; |
| 232 | +} |
| 233 | +
|
| 234 | +#app details { |
| 235 | + margin: 20px 0; |
| 236 | + max-width: 800px; |
| 237 | + padding: 8px 10px; |
| 238 | + background: #f5f5f5; |
| 239 | + border: 1px solid #ccc; |
| 240 | + border-radius: 4px; |
| 241 | + cursor: pointer; |
| 242 | +} |
| 243 | +
|
| 244 | +#app pre { |
| 245 | + font-size: 14px; |
| 246 | + padding: 10px; |
| 247 | + margin: 0 auto; |
| 248 | + max-width: 800px; |
| 249 | + text-align: left; |
| 250 | + overflow-x: scroll; |
| 251 | + max-height: 500px; |
| 252 | +} |
| 253 | +
|
| 254 | +#app ul { |
| 255 | + list-style: none; |
| 256 | + padding: 0; |
| 257 | + text-align: left; |
| 258 | + max-width: 800px; |
| 259 | + margin: 8px auto; |
| 260 | +} |
| 261 | +
|
| 262 | +#app .draggable { |
| 263 | + border-radius: 4px; |
| 264 | +} |
| 265 | +
|
| 266 | +#app summary::after { |
| 267 | + content: " (click to expand)"; |
| 268 | +} |
| 269 | +</style> |
0 commit comments