UNPKG

@loke/ui

Version:
120 lines (117 loc) • 5.5 kB
var __create = Object.create; var { getPrototypeOf: __getProtoOf, defineProperty: __defProp, getOwnPropertyNames: __getOwnPropNames, getOwnPropertyDescriptor: __getOwnPropDesc } = Object, __hasOwnProp = Object.prototype.hasOwnProperty; function __accessProp(key) { return this[key]; } var __toESMCache_node, __toESMCache_esm, __toESM = (mod, isNodeMode, target) => { var canCache = mod != null && typeof mod === "object"; if (canCache) { var cache = isNodeMode ? __toESMCache_node ??= /* @__PURE__ */ new WeakMap : __toESMCache_esm ??= /* @__PURE__ */ new WeakMap, cached = cache.get(mod); if (cached) return cached; } target = mod != null ? __create(__getProtoOf(mod)) : {}; let to = isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", { value: mod, enumerable: !0 }) : target; if (mod && typeof mod === "object" || typeof mod === "function") { for (let key of __getOwnPropNames(mod)) if (!__hasOwnProp.call(to, key)) __defProp(to, key, { get: __accessProp.bind(mod, key), enumerable: !0 }); } if (canCache) cache.set(mod, to); return to; }, __toCommonJS = (from) => { var entry = (__moduleCache ??= /* @__PURE__ */ new WeakMap).get(from), desc; if (entry) return entry; if (entry = __defProp({}, "__esModule", { value: !0 }), from && typeof from === "object" || typeof from === "function") { for (var key of __getOwnPropNames(from)) if (!__hasOwnProp.call(entry, key)) __defProp(entry, key, { get: __accessProp.bind(from, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return __moduleCache.set(from, entry), entry; }, __moduleCache; var __returnValue = (v) => v; function __exportSetter(name, newValue) { this[name] = __returnValue.bind(null, newValue); } var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: !0, configurable: !0, set: __exportSetter.bind(all, name) }); }; // src/lib/collection/index.ts var exports_collection = {}; __export(exports_collection, { createCollection: () => createCollection }); module.exports = __toCommonJS(exports_collection); // src/lib/collection/collection.tsx var import_compose_refs = require("@loke/ui/compose-refs"), import_context = require("@loke/ui/context"), import_slot = require("@loke/ui/slot"), import_react = __toESM(require("react"), 1), jsx_runtime = require("react/jsx-runtime"); function createCollection(name) { let PROVIDER_NAME = `${name}CollectionProvider`, [createCollectionContext, createCollectionScope] = import_context.createContextScope(PROVIDER_NAME), [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, { collectionRef: { current: null }, itemMap: /* @__PURE__ */ new Map }), CollectionProvider = (props) => { let { scope, children } = props, ref = import_react.default.useRef(null), itemMap = import_react.default.useRef(/* @__PURE__ */ new Map).current; return /* @__PURE__ */ jsx_runtime.jsx(CollectionProviderImpl, { collectionRef: ref, itemMap, scope, children }); }; CollectionProvider.displayName = PROVIDER_NAME; let COLLECTION_SLOT_NAME = `${name}CollectionSlot`, CollectionSlotImpl = import_slot.createSlot(COLLECTION_SLOT_NAME), CollectionSlot = import_react.default.forwardRef((props, forwardedRef) => { let { scope, children } = props, context = useCollectionContext(COLLECTION_SLOT_NAME, scope), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, context.collectionRef); return /* @__PURE__ */ jsx_runtime.jsx(CollectionSlotImpl, { ref: composedRefs, children }); }); CollectionSlot.displayName = COLLECTION_SLOT_NAME; let ITEM_SLOT_NAME = `${name}CollectionItemSlot`, ITEM_DATA_ATTR = "data-squared-collection-item", CollectionItemSlotImpl = import_slot.createSlot(ITEM_SLOT_NAME), CollectionItemSlot = import_react.default.forwardRef((props, forwardedRef) => { let { scope, children, ...itemData } = props, ref = import_react.default.useRef(null), composedRefs = import_compose_refs.useComposedRefs(forwardedRef, ref), context = useCollectionContext(ITEM_SLOT_NAME, scope); return import_react.default.useEffect(() => (context.itemMap.set(ref, { ref, ...itemData }), () => { context.itemMap.delete(ref); })), /* @__PURE__ */ jsx_runtime.jsx(CollectionItemSlotImpl, { ...{ [ITEM_DATA_ATTR]: "" }, ref: composedRefs, children }); }); CollectionItemSlot.displayName = ITEM_SLOT_NAME; function useCollection(scope) { let context = useCollectionContext(`${name}CollectionConsumer`, scope); return import_react.default.useCallback(() => { let collectionNode = context.collectionRef.current; if (!collectionNode) return []; let orderedNodes = Array.from(collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)); return Array.from(context.itemMap.values()).sort((a, b) => { if (!(a.ref.current && b.ref.current)) return 0; return orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current); }); }, [context.collectionRef, context.itemMap]); } return [ { ItemSlot: CollectionItemSlot, Provider: CollectionProvider, Slot: CollectionSlot }, useCollection, createCollectionScope ]; }