@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
54 lines (53 loc) • 1.51 kB
JavaScript
import { useDndController as e, useIsomorphicLayoutEffect as t } from "../core/context.js";
import { useStore as n } from "../core/store.js";
import { useCallback as r, useEffect as i, useRef as a, useState as o } from "react";
//#region src/hooks/useDroppable.ts
function s(s) {
let l = e("useDroppable"), { id: u } = s, [d, f] = o(null), p = a(null), { label: m, data: h, accept: g, list: _, onDrop: v } = s, y = s.disabled ?? !1, b = s.orientation ?? "vertical";
t(() => {
p.current?.id !== u && (p.current = { id: u });
let e = p.current;
Object.assign(e, {
label: m,
data: h,
disabled: y,
accept: g,
list: _,
orientation: b,
element: d,
onDrop: v
}), l.droppables.get(u) !== e && l.droppables.set(u, e);
}), t(() => () => {
l.droppables.get(u) === p.current && l.droppables.delete(u);
}, [l, u]);
let x = n(l.store, c), S = a(null), C = s.list !== void 0;
return i(() => {
if (!x || !d) return;
let e = x.droppable({
id: u,
element: d,
disabled: y,
priority: C ? 1 : 2,
accepts: (e) => l.accepts(u, e)
});
return S.current = e, () => {
S.current = null, e.destroy();
};
}, [
x,
d,
u,
C,
l
]), i(() => {
S.current?.update({ disabled: y });
}, [y]), {
ref: f,
isOver: n(l.store, r((e) => l.isOver(e, u), [l, u])),
isActive: n(l.store, r((e) => e.operation !== null && l.accepts(u, e.operation.id), [l, u]))
};
}
var c = (e) => e.engine;
//#endregion
export { s as useDroppable };
//# sourceMappingURL=useDroppable.js.map