UNPKG

@konstructio/ui

Version:

A set of reusable and customizable React components built for konstruct.io

48 lines (47 loc) 1.7 kB
import n from "react"; import { c as O } from "./index-Cva-e5M4.js"; import { u as I, S as x } from "./index-B_6_jmOO.js"; import { jsx as m } from "react/jsx-runtime"; function L(s) { const f = s + "CollectionProvider", [A, N] = O(f), [_, a] = A( f, { collectionRef: { current: null }, itemMap: /* @__PURE__ */ new Map() } ), u = (c) => { const { scope: e, children: l } = c, o = n.useRef(null), t = n.useRef(/* @__PURE__ */ new Map()).current; return /* @__PURE__ */ m(_, { scope: e, itemMap: t, collectionRef: o, children: l }); }; u.displayName = f; const d = s + "CollectionSlot", p = n.forwardRef( (c, e) => { const { scope: l, children: o } = c, t = a(d, l), r = I(e, t.collectionRef); return /* @__PURE__ */ m(x, { ref: r, children: o }); } ); p.displayName = d; const C = s + "CollectionItemSlot", R = "data-radix-collection-item", M = n.forwardRef( (c, e) => { const { scope: l, children: o, ...t } = c, r = n.useRef(null), S = I(e, r), i = a(C, l); return n.useEffect(() => (i.itemMap.set(r, { ref: r, ...t }), () => void i.itemMap.delete(r))), /* @__PURE__ */ m(x, { [R]: "", ref: S, children: o }); } ); M.displayName = C; function E(c) { const e = a(s + "CollectionConsumer", c); return n.useCallback(() => { const o = e.collectionRef.current; if (!o) return []; const t = Array.from(o.querySelectorAll(`[${R}]`)); return Array.from(e.itemMap.values()).sort( (i, T) => t.indexOf(i.ref.current) - t.indexOf(T.ref.current) ); }, [e.collectionRef, e.itemMap]); } return [ { Provider: u, Slot: p, ItemSlot: M }, E, N ]; } export { L as c };