useSortable.ts 523 B

123456789101112131415161718192021
  1. import { nextTick, unref } from 'vue';
  2. import type { Ref } from 'vue';
  3. import type { Options } from 'sortablejs';
  4. export function useSortable(el: HTMLElement | Ref<HTMLElement>, options?: Options) {
  5. function initSortable() {
  6. nextTick(async () => {
  7. if (!el) return;
  8. const Sortable = (await import('sortablejs')).default;
  9. Sortable.create(unref(el), {
  10. animation: 500,
  11. delay: 400,
  12. delayOnTouchOnly: true,
  13. ...options,
  14. });
  15. });
  16. }
  17. return { initSortable };
  18. }