UNPKG

@mitcsutt/kiln-dnd

Version:

Kiln drag and drop: sortable lists, boards, drop zones and grid layouts with one keyboard model and one announcer, rendered through kiln-ui

103 lines (102 loc) • 2.37 kB
import { useIsomorphicLayoutEffect as e } from "./context.js"; import { useStore as t } from "./store.js"; import { useCallback as n, useEffect as r, useRef as i, useState as a } from "react"; //#region src/core/useDragSource.ts var o = (e) => e.engine; function s(s, c) { let { id: l, hasHandle: u, keyboard: d = !0 } = c, [f, p] = a(null), [m, h] = a(null), g = i(null), { label: _, data: v, disabled: y, kind: b, list: x = "", index: S = 0, custom: C = null } = c; e(() => { g.current?.id !== l && (g.current = { id: l }); let e = g.current; Object.assign(e, { label: _, data: v, disabled: y, kind: b, list: x, index: S, custom: C, element: f, handle: u ? m : f }), s.draggables.get(l) !== e && s.draggables.set(l, e); }), e(() => () => { s.draggables.get(l) === g.current && s.draggables.delete(l), s.operation?.id === l && s.cancel(); }, [s, l]); let w = t(s.store, o), T = u ? m : f, E = i(null); r(() => { if (!w || !f || !T) return; let e = w.draggable({ id: l, element: f, handle: u ? T : void 0, disabled: y, feedback: c.feedback ?? "clone" }); return E.current = e, () => { E.current = null, e.destroy(); }; }, [ w, f, T, l, u ]), r(() => { E.current?.update({ disabled: y }); }, [y]); let D = i(null), O = c.droppable ?? !1; r(() => { if (!w || !f || !O) return; let e = w.droppable({ id: l, element: f, disabled: !1, priority: 3, accepts: (e) => s.draggables.get(e)?.kind === "list" }); return D.current = e, () => { D.current = null, e.destroy(); }; }, [ w, f, O, l, s ]); let k = t(s.store, n((e) => e.focusId === l, [l])); e(() => { if (!k) return; let e = u ? m : f; e && (e.ownerDocument.activeElement !== e && e.focus(), s.store.set({ focusId: null })); }, [ k, m, f, u, s ]); let A = t(s.store, n((e) => e.operation?.id === l ? e.operation.input : null, [l])), j = s.instructionIds, M = { "data-kiln-dnd-handle": "", onKeyDown: n((e) => { !e.defaultPrevented && d && s.keyDown(l, e); }, [ s, l, d ]), ...d ? { tabIndex: 0, "aria-describedby": c.instructions === "grid" ? j.grid : j.list } : {} }; return { ref: p, handleRef: u ? h : p, handleProps: M, dragging: A }; } //#endregion export { s as useDragSource }; //# sourceMappingURL=useDragSource.js.map