@anoki/fse-marche-ui
Version:
FSE UI components library
50 lines (49 loc) • 1.83 kB
JavaScript
import n from "react";
import { createContextScope as O } from "./index.es237.js";
import { useComposedRefs as I } from "./index.es236.js";
import { Slot as S } from "./index.es257.js";
import { j as u } from "./index.es137.js";
function L(s) {
const f = s + "CollectionProvider", [A, E] = O(f), [N, m] = A(
f,
{ collectionRef: { current: null }, itemMap: /* @__PURE__ */ new Map() }
), a = (c) => {
const { scope: e, children: l } = c, o = n.useRef(null), t = n.useRef(/* @__PURE__ */ new Map()).current;
return /* @__PURE__ */ u.jsx(N, { scope: e, itemMap: t, collectionRef: o, children: l });
};
a.displayName = f;
const d = s + "CollectionSlot", p = n.forwardRef(
(c, e) => {
const { scope: l, children: o } = c, t = m(d, l), r = I(e, t.collectionRef);
return /* @__PURE__ */ u.jsx(S, { ref: r, children: o });
}
);
p.displayName = d;
const C = s + "CollectionItemSlot", R = "data-radix-collection-item", x = n.forwardRef(
(c, e) => {
const { scope: l, children: o, ...t } = c, r = n.useRef(null), M = I(e, r), i = m(C, l);
return n.useEffect(() => (i.itemMap.set(r, { ref: r, ...t }), () => void i.itemMap.delete(r))), /* @__PURE__ */ u.jsx(S, { [R]: "", ref: M, children: o });
}
);
x.displayName = C;
function _(c) {
const e = m(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: a, Slot: p, ItemSlot: x },
_,
E
];
}
export {
L as createCollection
};
//# sourceMappingURL=index.es272.js.map