@tamagui/collection 1.2.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Radix
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var Collection_exports = {};
30
+ __export(Collection_exports, {
31
+ createCollection: () => createCollection
32
+ });
33
+ module.exports = __toCommonJS(Collection_exports);
34
+ var import_jsx_runtime = require("react/jsx-runtime");
35
+ var import_compose_refs = require("@tamagui/compose-refs");
36
+ var import_core = require("@tamagui/core");
37
+ var import_create_context = require("@tamagui/create-context");
38
+ var import_react = __toESM(require("react"));
39
+ function createCollection(name) {
40
+ const PROVIDER_NAME = name + "CollectionProvider";
41
+ const [createCollectionContext, createCollectionScope] = (0, import_create_context.createContextScope)(PROVIDER_NAME);
42
+ const [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, {
43
+ collectionRef: { current: null },
44
+ itemMap: /* @__PURE__ */ new Map()
45
+ });
46
+ const CollectionProvider = (props) => {
47
+ const { scope, children } = props;
48
+ const ref = import_react.default.useRef(null);
49
+ const itemMap = import_react.default.useRef(/* @__PURE__ */ new Map()).current;
50
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(CollectionProviderImpl, { scope, itemMap, collectionRef: ref, children });
51
+ };
52
+ CollectionProvider.displayName = PROVIDER_NAME;
53
+ const COLLECTION_SLOT_NAME = name + "CollectionSlot";
54
+ const CollectionSlot = import_react.default.forwardRef(
55
+ (props, forwardedRef) => {
56
+ const { scope, children } = props;
57
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
58
+ const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, context.collectionRef);
59
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Slot, { ref: composedRefs, children });
60
+ }
61
+ );
62
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME;
63
+ const ITEM_SLOT_NAME = name + "CollectionItemSlot";
64
+ const ITEM_DATA_ATTR = "data-collection-item";
65
+ const CollectionItemSlot = import_react.default.forwardRef(
66
+ (props, forwardedRef) => {
67
+ const { scope, children, ...itemData } = props;
68
+ const ref = import_react.default.useRef(null);
69
+ const composedRefs = (0, import_compose_refs.useComposedRefs)(forwardedRef, ref);
70
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope);
71
+ import_react.default.useEffect(() => {
72
+ context.itemMap.set(ref, { ref, ...itemData });
73
+ return () => void context.itemMap.delete(ref);
74
+ });
75
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_core.Slot, { ...{ [ITEM_DATA_ATTR]: "" }, ref: composedRefs, children });
76
+ }
77
+ );
78
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME;
79
+ function useCollection(scope) {
80
+ const context = useCollectionContext(name + "CollectionConsumer", scope);
81
+ const getItems = import_react.default.useCallback(() => {
82
+ const collectionNode = context.collectionRef.current;
83
+ if (!collectionNode)
84
+ return [];
85
+ const orderedNodes = Array.from(
86
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
87
+ );
88
+ const items = Array.from(context.itemMap.values());
89
+ const orderedItems = items.sort(
90
+ (a, b) => orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current)
91
+ );
92
+ return orderedItems;
93
+ }, [context.collectionRef, context.itemMap]);
94
+ return getItems;
95
+ }
96
+ return [
97
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
98
+ useCollection,
99
+ createCollectionScope
100
+ ];
101
+ }
102
+ // Annotate the CommonJS export names for ESM import in node:
103
+ 0 && (module.exports = {
104
+ createCollection
105
+ });
106
+ //# sourceMappingURL=Collection.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Collection.tsx"],
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { Slot } from '@tamagui/core'\nimport { createContextScope } from '@tamagui/create-context'\nimport React from 'react'\n\ntype SlotProps = React.ComponentPropsWithoutRef<typeof Slot>\ntype CollectionElement = HTMLElement\ninterface CollectionProps extends SlotProps {\n scope: any\n}\n\n// We have resorted to returning slots directly rather than exposing primitives that can then\n// be slotted like `<CollectionItem as={Slot}>\u2026</CollectionItem>`.\n// This is because we encountered issues with generic types that cannot be statically analysed\n// due to creating them dynamically via createCollection.\n\nfunction createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {\n /* -----------------------------------------------------------------------------------------------\n * CollectionProvider\n * ---------------------------------------------------------------------------------------------*/\n\n const PROVIDER_NAME = name + 'CollectionProvider'\n const [createCollectionContext, createCollectionScope] =\n createContextScope(PROVIDER_NAME)\n\n type ContextValue = {\n collectionRef: React.RefObject<CollectionElement>\n itemMap: Map<\n React.RefObject<ItemElement>,\n { ref: React.RefObject<ItemElement> } & ItemData\n >\n }\n\n const [CollectionProviderImpl, useCollectionContext] =\n createCollectionContext<ContextValue>(PROVIDER_NAME, {\n collectionRef: { current: null },\n itemMap: new Map(),\n })\n\n const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (\n props\n ) => {\n const { scope, children } = props\n const ref = React.useRef<CollectionElement>(null)\n const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current\n return (\n <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>\n {children}\n </CollectionProviderImpl>\n )\n }\n\n CollectionProvider.displayName = PROVIDER_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionSlot\n * ---------------------------------------------------------------------------------------------*/\n\n const COLLECTION_SLOT_NAME = name + 'CollectionSlot'\n\n const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(\n (props, forwardedRef) => {\n const { scope, children } = props\n const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)\n const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)\n return <Slot ref={composedRefs}>{children}</Slot>\n }\n )\n\n CollectionSlot.displayName = COLLECTION_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionItem\n * ---------------------------------------------------------------------------------------------*/\n\n const ITEM_SLOT_NAME = name + 'CollectionItemSlot'\n const ITEM_DATA_ATTR = 'data-collection-item'\n\n type CollectionItemSlotProps = ItemData & {\n children: React.ReactNode\n scope: any\n }\n\n const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(\n (props, forwardedRef) => {\n const { scope, children, ...itemData } = props\n const ref = React.useRef<ItemElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n const context = useCollectionContext(ITEM_SLOT_NAME, scope)\n\n React.useEffect(() => {\n context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })\n return () => void context.itemMap.delete(ref)\n })\n\n return (\n <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>\n {children}\n </Slot>\n )\n }\n )\n\n CollectionItemSlot.displayName = ITEM_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * useCollection\n * ---------------------------------------------------------------------------------------------*/\n\n function useCollection(scope: any) {\n const context = useCollectionContext(name + 'CollectionConsumer', scope)\n\n const getItems = React.useCallback(() => {\n const collectionNode = context.collectionRef.current\n if (!collectionNode) return []\n const orderedNodes = Array.from(\n collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)\n )\n const items = Array.from(context.itemMap.values())\n const orderedItems = items.sort(\n (a, b) =>\n orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)\n )\n return orderedItems\n }, [context.collectionRef, context.itemMap])\n\n return getItems\n }\n\n return [\n { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },\n useCollection,\n createCollectionScope,\n ] as const\n}\n\nexport { createCollection }\nexport type { CollectionProps }\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA8CM;AA9CN,0BAAgC;AAChC,kBAAqB;AACrB,4BAAmC;AACnC,mBAAkB;AAalB,SAAS,iBAAiE,MAAc;AAKtF,QAAM,gBAAgB,OAAO;AAC7B,QAAM,CAAC,yBAAyB,qBAAqB,QACnD,0CAAmB,aAAa;AAUlC,QAAM,CAAC,wBAAwB,oBAAoB,IACjD,wBAAsC,eAAe;AAAA,IACnD,eAAe,EAAE,SAAS,KAAK;AAAA,IAC/B,SAAS,oBAAI,IAAI;AAAA,EACnB,CAAC;AAEH,QAAM,qBAA2E,CAC/E,UACG;AACH,UAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,UAAM,MAAM,aAAAA,QAAM,OAA0B,IAAI;AAChD,UAAM,UAAU,aAAAA,QAAM,OAAgC,oBAAI,IAAI,CAAC,EAAE;AACjE,WACE,4CAAC,0BAAuB,OAAc,SAAkB,eAAe,KACpE,UACH;AAAA,EAEJ;AAEA,qBAAmB,cAAc;AAMjC,QAAM,uBAAuB,OAAO;AAEpC,QAAM,iBAAiB,aAAAA,QAAM;AAAA,IAC3B,CAAC,OAAO,iBAAiB;AACvB,YAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,YAAM,UAAU,qBAAqB,sBAAsB,KAAK;AAChE,YAAM,mBAAe,qCAAgB,cAAc,QAAQ,aAAa;AACxE,aAAO,4CAAC,oBAAK,KAAK,cAAe,UAAS;AAAA,IAC5C;AAAA,EACF;AAEA,iBAAe,cAAc;AAM7B,QAAM,iBAAiB,OAAO;AAC9B,QAAM,iBAAiB;AAOvB,QAAM,qBAAqB,aAAAA,QAAM;AAAA,IAC/B,CAAC,OAAO,iBAAiB;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Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
15
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
16
+ var src_exports = {};
17
+ module.exports = __toCommonJS(src_exports);
18
+ __reExport(src_exports, require("./Collection"), module.exports);
19
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './Collection'\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,yBAAd;",
6
+ "names": []
7
+ }
@@ -0,0 +1,72 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useComposedRefs } from "@tamagui/compose-refs";
3
+ import { Slot } from "@tamagui/core";
4
+ import { createContextScope } from "@tamagui/create-context";
5
+ import React from "react";
6
+ function createCollection(name) {
7
+ const PROVIDER_NAME = name + "CollectionProvider";
8
+ const [createCollectionContext, createCollectionScope] = createContextScope(PROVIDER_NAME);
9
+ const [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, {
10
+ collectionRef: { current: null },
11
+ itemMap: /* @__PURE__ */ new Map()
12
+ });
13
+ const CollectionProvider = (props) => {
14
+ const { scope, children } = props;
15
+ const ref = React.useRef(null);
16
+ const itemMap = React.useRef(/* @__PURE__ */ new Map()).current;
17
+ return /* @__PURE__ */ jsx(CollectionProviderImpl, { scope, itemMap, collectionRef: ref, children });
18
+ };
19
+ CollectionProvider.displayName = PROVIDER_NAME;
20
+ const COLLECTION_SLOT_NAME = name + "CollectionSlot";
21
+ const CollectionSlot = React.forwardRef(
22
+ (props, forwardedRef) => {
23
+ const { scope, children } = props;
24
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
25
+ const composedRefs = useComposedRefs(forwardedRef, context.collectionRef);
26
+ return /* @__PURE__ */ jsx(Slot, { ref: composedRefs, children });
27
+ }
28
+ );
29
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME;
30
+ const ITEM_SLOT_NAME = name + "CollectionItemSlot";
31
+ const ITEM_DATA_ATTR = "data-collection-item";
32
+ const CollectionItemSlot = React.forwardRef(
33
+ (props, forwardedRef) => {
34
+ const { scope, children, ...itemData } = props;
35
+ const ref = React.useRef(null);
36
+ const composedRefs = useComposedRefs(forwardedRef, ref);
37
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope);
38
+ React.useEffect(() => {
39
+ context.itemMap.set(ref, { ref, ...itemData });
40
+ return () => void context.itemMap.delete(ref);
41
+ });
42
+ return /* @__PURE__ */ jsx(Slot, { ...{ [ITEM_DATA_ATTR]: "" }, ref: composedRefs, children });
43
+ }
44
+ );
45
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME;
46
+ function useCollection(scope) {
47
+ const context = useCollectionContext(name + "CollectionConsumer", scope);
48
+ const getItems = React.useCallback(() => {
49
+ const collectionNode = context.collectionRef.current;
50
+ if (!collectionNode)
51
+ return [];
52
+ const orderedNodes = Array.from(
53
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
54
+ );
55
+ const items = Array.from(context.itemMap.values());
56
+ const orderedItems = items.sort(
57
+ (a, b) => orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current)
58
+ );
59
+ return orderedItems;
60
+ }, [context.collectionRef, context.itemMap]);
61
+ return getItems;
62
+ }
63
+ return [
64
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
65
+ useCollection,
66
+ createCollectionScope
67
+ ];
68
+ }
69
+ export {
70
+ createCollection
71
+ };
72
+ //# sourceMappingURL=Collection.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Collection.tsx"],
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { Slot } from '@tamagui/core'\nimport { createContextScope } from '@tamagui/create-context'\nimport React from 'react'\n\ntype SlotProps = React.ComponentPropsWithoutRef<typeof Slot>\ntype CollectionElement = HTMLElement\ninterface CollectionProps extends SlotProps {\n scope: any\n}\n\n// We have resorted to returning slots directly rather than exposing primitives that can then\n// be slotted like `<CollectionItem as={Slot}>\u2026</CollectionItem>`.\n// This is because we encountered issues with generic types that cannot be statically analysed\n// due to creating them dynamically via createCollection.\n\nfunction createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {\n /* -----------------------------------------------------------------------------------------------\n * CollectionProvider\n * ---------------------------------------------------------------------------------------------*/\n\n const PROVIDER_NAME = name + 'CollectionProvider'\n const [createCollectionContext, createCollectionScope] =\n createContextScope(PROVIDER_NAME)\n\n type ContextValue = {\n collectionRef: React.RefObject<CollectionElement>\n itemMap: Map<\n React.RefObject<ItemElement>,\n { ref: React.RefObject<ItemElement> } & ItemData\n >\n }\n\n const [CollectionProviderImpl, useCollectionContext] =\n createCollectionContext<ContextValue>(PROVIDER_NAME, {\n collectionRef: { current: null },\n itemMap: new Map(),\n })\n\n const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (\n props\n ) => {\n const { scope, children } = props\n const ref = React.useRef<CollectionElement>(null)\n const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current\n return (\n <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>\n {children}\n </CollectionProviderImpl>\n )\n }\n\n CollectionProvider.displayName = PROVIDER_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionSlot\n * ---------------------------------------------------------------------------------------------*/\n\n const COLLECTION_SLOT_NAME = name + 'CollectionSlot'\n\n const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(\n (props, forwardedRef) => {\n const { scope, children } = props\n const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)\n const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)\n return <Slot ref={composedRefs}>{children}</Slot>\n }\n )\n\n CollectionSlot.displayName = COLLECTION_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionItem\n * ---------------------------------------------------------------------------------------------*/\n\n const ITEM_SLOT_NAME = name + 'CollectionItemSlot'\n const ITEM_DATA_ATTR = 'data-collection-item'\n\n type CollectionItemSlotProps = ItemData & {\n children: React.ReactNode\n scope: any\n }\n\n const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(\n (props, forwardedRef) => {\n const { scope, children, ...itemData } = props\n const ref = React.useRef<ItemElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n const context = useCollectionContext(ITEM_SLOT_NAME, scope)\n\n React.useEffect(() => {\n context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })\n return () => void context.itemMap.delete(ref)\n })\n\n return (\n <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>\n {children}\n </Slot>\n )\n }\n )\n\n CollectionItemSlot.displayName = ITEM_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * useCollection\n * ---------------------------------------------------------------------------------------------*/\n\n function useCollection(scope: any) {\n const context = useCollectionContext(name + 'CollectionConsumer', scope)\n\n const getItems = React.useCallback(() => {\n const collectionNode = context.collectionRef.current\n if (!collectionNode) return []\n const orderedNodes = Array.from(\n collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)\n )\n const items = Array.from(context.itemMap.values())\n const orderedItems = items.sort(\n (a, b) =>\n orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)\n )\n return orderedItems\n }, [context.collectionRef, context.itemMap])\n\n return getItems\n }\n\n return [\n { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },\n useCollection,\n createCollectionScope,\n ] as const\n}\n\nexport { createCollection }\nexport type { CollectionProps }\n"],
5
+ "mappings": "AA8CM;AA9CN,SAAS,uBAAuB;AAChC,SAAS,YAAY;AACrB,SAAS,0BAA0B;AACnC,OAAO,WAAW;AAalB,SAAS,iBAAiE,MAAc;AAKtF,QAAM,gBAAgB,OAAO;AAC7B,QAAM,CAAC,yBAAyB,qBAAqB,IACnD,mBAAmB,aAAa;AAUlC,QAAM,CAAC,wBAAwB,oBAAoB,IACjD,wBAAsC,eAAe;AAAA,IACnD,eAAe,EAAE,SAAS,KAAK;AAAA,IAC/B,SAAS,oBAAI,IAAI;AAAA,EACnB,CAAC;AAEH,QAAM,qBAA2E,CAC/E,UACG;AACH,UAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,UAAM,MAAM,MAAM,OAA0B,IAAI;AAChD,UAAM,UAAU,MAAM,OAAgC,oBAAI,IAAI,CAAC,EAAE;AACjE,WACE,oBAAC,0BAAuB,OAAc,SAAkB,eAAe,KACpE,UACH;AAAA,EAEJ;AAEA,qBAAmB,cAAc;AAMjC,QAAM,uBAAuB,OAAO;AAEpC,QAAM,iBAAiB,MAAM;AAAA,IAC3B,CAAC,OAAO,iBAAiB;AACvB,YAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,YAAM,UAAU,qBAAqB,sBAAsB,KAAK;AAChE,YAAM,eAAe,gBAAgB,cAAc,QAAQ,aAAa;AACxE,aAAO,oBAAC,QAAK,KAAK,cAAe,UAAS;AAAA,IAC5C;AAAA,EACF;AAEA,iBAAe,cAAc;AAM7B,QAAM,iBAAiB,OAAO;AAC9B,QAAM,iBAAiB;AAOvB,QAAM,qBAAqB,MAAM;AAAA,IAC/B,CAAC,OAAO,iBAAiB;AACvB,YAAM,EAAE,OAAO,UAAU,GAAG,SAAS,IAAI;AACzC,YAAM,MAAM,MAAM,OAAoB,IAAI;AAC1C,YAAM,eAAe,gBAAgB,cAAc,GAAG;AACtD,YAAM,UAAU,qBAAqB,gBAAgB,KAAK;AAE1D,YAAM,UAAU,MAAM;AACpB,gBAAQ,QAAQ,IAAI,KAAK,EAAE,KAAK,GAAI,SAAiC,CAAC;AACtE,eAAO,MAAM,KAAK,QAAQ,QAAQ,OAAO,GAAG;AAAA,MAC9C,CAAC;AAED,aACE,oBAAC,QAAM,GAAG,EAAE,CAAC,cAAc,GAAG,GAAG,GAAG,KAAK,cACtC,UACH;AAAA,IAEJ;AAAA,EACF;AAEA,qBAAmB,cAAc;AAMjC,WAAS,cAAc,OAAY;AACjC,UAAM,UAAU,qBAAqB,OAAO,sBAAsB,KAAK;AAEvE,UAAM,WAAW,MAAM,YAAY,MAAM;AACvC,YAAM,iBAAiB,QAAQ,cAAc;AAC7C,UAAI,CAAC;AAAgB,eAAO,CAAC;AAC7B,YAAM,eAAe,MAAM;AAAA,QACzB,eAAe,iBAAiB,IAAI,iBAAiB;AAAA,MACvD;AACA,YAAM,QAAQ,MAAM,KAAK,QAAQ,QAAQ,OAAO,CAAC;AACjD,YAAM,eAAe,MAAM;AAAA,QACzB,CAAC,GAAG,MACF,aAAa,QAAQ,EAAE,IAAI,OAAQ,IAAI,aAAa,QAAQ,EAAE,IAAI,OAAQ;AAAA,MAC9E;AACA,aAAO;AAAA,IACT,GAAG,CAAC,QAAQ,eAAe,QAAQ,OAAO,CAAC;AAE3C,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,EAAE,UAAU,oBAAoB,MAAM,gBAAgB,UAAU,mBAAmB;AAAA,IACnF;AAAA,IACA;AAAA,EACF;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,72 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { useComposedRefs } from "@tamagui/compose-refs";
3
+ import { Slot } from "@tamagui/core";
4
+ import { createContextScope } from "@tamagui/create-context";
5
+ import React from "react";
6
+ function createCollection(name) {
7
+ const PROVIDER_NAME = name + "CollectionProvider";
8
+ const [createCollectionContext, createCollectionScope] = createContextScope(PROVIDER_NAME);
9
+ const [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, {
10
+ collectionRef: { current: null },
11
+ itemMap: /* @__PURE__ */ new Map()
12
+ });
13
+ const CollectionProvider = (props) => {
14
+ const { scope, children } = props;
15
+ const ref = React.useRef(null);
16
+ const itemMap = React.useRef(/* @__PURE__ */ new Map()).current;
17
+ return /* @__PURE__ */ jsx(CollectionProviderImpl, { scope, itemMap, collectionRef: ref, children });
18
+ };
19
+ CollectionProvider.displayName = PROVIDER_NAME;
20
+ const COLLECTION_SLOT_NAME = name + "CollectionSlot";
21
+ const CollectionSlot = React.forwardRef(
22
+ (props, forwardedRef) => {
23
+ const { scope, children } = props;
24
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
25
+ const composedRefs = useComposedRefs(forwardedRef, context.collectionRef);
26
+ return /* @__PURE__ */ jsx(Slot, { ref: composedRefs, children });
27
+ }
28
+ );
29
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME;
30
+ const ITEM_SLOT_NAME = name + "CollectionItemSlot";
31
+ const ITEM_DATA_ATTR = "data-collection-item";
32
+ const CollectionItemSlot = React.forwardRef(
33
+ (props, forwardedRef) => {
34
+ const { scope, children, ...itemData } = props;
35
+ const ref = React.useRef(null);
36
+ const composedRefs = useComposedRefs(forwardedRef, ref);
37
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope);
38
+ React.useEffect(() => {
39
+ context.itemMap.set(ref, { ref, ...itemData });
40
+ return () => void context.itemMap.delete(ref);
41
+ });
42
+ return /* @__PURE__ */ jsx(Slot, { ...{ [ITEM_DATA_ATTR]: "" }, ref: composedRefs, children });
43
+ }
44
+ );
45
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME;
46
+ function useCollection(scope) {
47
+ const context = useCollectionContext(name + "CollectionConsumer", scope);
48
+ const getItems = React.useCallback(() => {
49
+ const collectionNode = context.collectionRef.current;
50
+ if (!collectionNode)
51
+ return [];
52
+ const orderedNodes = Array.from(
53
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
54
+ );
55
+ const items = Array.from(context.itemMap.values());
56
+ const orderedItems = items.sort(
57
+ (a, b) => orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current)
58
+ );
59
+ return orderedItems;
60
+ }, [context.collectionRef, context.itemMap]);
61
+ return getItems;
62
+ }
63
+ return [
64
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
65
+ useCollection,
66
+ createCollectionScope
67
+ ];
68
+ }
69
+ export {
70
+ createCollection
71
+ };
72
+ //# sourceMappingURL=Collection.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Collection.tsx"],
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { Slot } from '@tamagui/core'\nimport { createContextScope } from '@tamagui/create-context'\nimport React from 'react'\n\ntype SlotProps = React.ComponentPropsWithoutRef<typeof Slot>\ntype CollectionElement = HTMLElement\ninterface CollectionProps extends SlotProps {\n scope: any\n}\n\n// We have resorted to returning slots directly rather than exposing primitives that can then\n// be slotted like `<CollectionItem as={Slot}>\u2026</CollectionItem>`.\n// This is because we encountered issues with generic types that cannot be statically analysed\n// due to creating them dynamically via createCollection.\n\nfunction createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {\n /* -----------------------------------------------------------------------------------------------\n * CollectionProvider\n * ---------------------------------------------------------------------------------------------*/\n\n const PROVIDER_NAME = name + 'CollectionProvider'\n const [createCollectionContext, createCollectionScope] =\n createContextScope(PROVIDER_NAME)\n\n type ContextValue = {\n collectionRef: React.RefObject<CollectionElement>\n itemMap: Map<\n React.RefObject<ItemElement>,\n { ref: React.RefObject<ItemElement> } & ItemData\n >\n }\n\n const [CollectionProviderImpl, useCollectionContext] =\n createCollectionContext<ContextValue>(PROVIDER_NAME, {\n collectionRef: { current: null },\n itemMap: new Map(),\n })\n\n const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (\n props\n ) => {\n const { scope, children } = props\n const ref = React.useRef<CollectionElement>(null)\n const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current\n return (\n <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>\n {children}\n </CollectionProviderImpl>\n )\n }\n\n CollectionProvider.displayName = PROVIDER_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionSlot\n * ---------------------------------------------------------------------------------------------*/\n\n const COLLECTION_SLOT_NAME = name + 'CollectionSlot'\n\n const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(\n (props, forwardedRef) => {\n const { scope, children } = props\n const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)\n const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)\n return <Slot ref={composedRefs}>{children}</Slot>\n }\n )\n\n CollectionSlot.displayName = COLLECTION_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionItem\n * ---------------------------------------------------------------------------------------------*/\n\n const ITEM_SLOT_NAME = name + 'CollectionItemSlot'\n const ITEM_DATA_ATTR = 'data-collection-item'\n\n type CollectionItemSlotProps = ItemData & {\n children: React.ReactNode\n scope: any\n }\n\n const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(\n (props, forwardedRef) => {\n const { scope, children, ...itemData } = props\n const ref = React.useRef<ItemElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n const context = useCollectionContext(ITEM_SLOT_NAME, scope)\n\n React.useEffect(() => {\n context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })\n return () => void context.itemMap.delete(ref)\n })\n\n return (\n <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>\n {children}\n </Slot>\n )\n }\n )\n\n CollectionItemSlot.displayName = ITEM_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * useCollection\n * ---------------------------------------------------------------------------------------------*/\n\n function useCollection(scope: any) {\n const context = useCollectionContext(name + 'CollectionConsumer', scope)\n\n const getItems = React.useCallback(() => {\n const collectionNode = context.collectionRef.current\n if (!collectionNode) return []\n const orderedNodes = Array.from(\n collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)\n )\n const items = Array.from(context.itemMap.values())\n const orderedItems = items.sort(\n (a, b) =>\n orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)\n )\n return orderedItems\n }, [context.collectionRef, context.itemMap])\n\n return getItems\n }\n\n return [\n { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },\n useCollection,\n createCollectionScope,\n ] as const\n}\n\nexport { createCollection }\nexport type { CollectionProps }\n"],
5
+ "mappings": "AA8CM;AA9CN,SAAS,uBAAuB;AAChC,SAAS,YAAY;AACrB,SAAS,0BAA0B;AACnC,OAAO,WAAW;AAalB,SAAS,iBAAiE,MAAc;AAKtF,QAAM,gBAAgB,OAAO;AAC7B,QAAM,CAAC,yBAAyB,qBAAqB,IACnD,mBAAmB,aAAa;AAUlC,QAAM,CAAC,wBAAwB,oBAAoB,IACjD,wBAAsC,eAAe;AAAA,IACnD,eAAe,EAAE,SAAS,KAAK;AAAA,IAC/B,SAAS,oBAAI,IAAI;AAAA,EACnB,CAAC;AAEH,QAAM,qBAA2E,CAC/E,UACG;AACH,UAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,UAAM,MAAM,MAAM,OAA0B,IAAI;AAChD,UAAM,UAAU,MAAM,OAAgC,oBAAI,IAAI,CAAC,EAAE;AACjE,WACE,oBAAC,0BAAuB,OAAc,SAAkB,eAAe,KACpE,UACH;AAAA,EAEJ;AAEA,qBAAmB,cAAc;AAMjC,QAAM,uBAAuB,OAAO;AAEpC,QAAM,iBAAiB,MAAM;AAAA,IAC3B,CAAC,OAAO,iBAAiB;AACvB,YAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,YAAM,UAAU,qBAAqB,s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3C,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,EAAE,UAAU,oBAAoB,MAAM,gBAAgB,UAAU,mBAAmB;AAAA,IACnF;AAAA,IACA;AAAA,EACF;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./Collection";
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './Collection'\n"],
5
+ "mappings": "AAAA,cAAc;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./Collection";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './Collection'\n"],
5
+ "mappings": "AAAA,cAAc;",
6
+ "names": []
7
+ }
@@ -0,0 +1,71 @@
1
+ import { useComposedRefs } from "@tamagui/compose-refs";
2
+ import { Slot } from "@tamagui/core";
3
+ import { createContextScope } from "@tamagui/create-context";
4
+ import React from "react";
5
+ function createCollection(name) {
6
+ const PROVIDER_NAME = name + "CollectionProvider";
7
+ const [createCollectionContext, createCollectionScope] = createContextScope(PROVIDER_NAME);
8
+ const [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, {
9
+ collectionRef: { current: null },
10
+ itemMap: /* @__PURE__ */ new Map()
11
+ });
12
+ const CollectionProvider = (props) => {
13
+ const { scope, children } = props;
14
+ const ref = React.useRef(null);
15
+ const itemMap = React.useRef(/* @__PURE__ */ new Map()).current;
16
+ return <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>{children}</CollectionProviderImpl>;
17
+ };
18
+ CollectionProvider.displayName = PROVIDER_NAME;
19
+ const COLLECTION_SLOT_NAME = name + "CollectionSlot";
20
+ const CollectionSlot = React.forwardRef(
21
+ (props, forwardedRef) => {
22
+ const { scope, children } = props;
23
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
24
+ const composedRefs = useComposedRefs(forwardedRef, context.collectionRef);
25
+ return <Slot ref={composedRefs}>{children}</Slot>;
26
+ }
27
+ );
28
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME;
29
+ const ITEM_SLOT_NAME = name + "CollectionItemSlot";
30
+ const ITEM_DATA_ATTR = "data-collection-item";
31
+ const CollectionItemSlot = React.forwardRef(
32
+ (props, forwardedRef) => {
33
+ const { scope, children, ...itemData } = props;
34
+ const ref = React.useRef(null);
35
+ const composedRefs = useComposedRefs(forwardedRef, ref);
36
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope);
37
+ React.useEffect(() => {
38
+ context.itemMap.set(ref, { ref, ...itemData });
39
+ return () => void context.itemMap.delete(ref);
40
+ });
41
+ return <Slot {...{ [ITEM_DATA_ATTR]: "" }} ref={composedRefs}>{children}</Slot>;
42
+ }
43
+ );
44
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME;
45
+ function useCollection(scope) {
46
+ const context = useCollectionContext(name + "CollectionConsumer", scope);
47
+ const getItems = React.useCallback(() => {
48
+ const collectionNode = context.collectionRef.current;
49
+ if (!collectionNode)
50
+ return [];
51
+ const orderedNodes = Array.from(
52
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
53
+ );
54
+ const items = Array.from(context.itemMap.values());
55
+ const orderedItems = items.sort(
56
+ (a, b) => orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current)
57
+ );
58
+ return orderedItems;
59
+ }, [context.collectionRef, context.itemMap]);
60
+ return getItems;
61
+ }
62
+ return [
63
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
64
+ useCollection,
65
+ createCollectionScope
66
+ ];
67
+ }
68
+ export {
69
+ createCollection
70
+ };
71
+ //# sourceMappingURL=Collection.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Collection.tsx"],
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { Slot } from '@tamagui/core'\nimport { createContextScope } from '@tamagui/create-context'\nimport React from 'react'\n\ntype SlotProps = React.ComponentPropsWithoutRef<typeof Slot>\ntype CollectionElement = HTMLElement\ninterface CollectionProps extends SlotProps {\n scope: any\n}\n\n// We have resorted to returning slots directly rather than exposing primitives that can then\n// be slotted like `<CollectionItem as={Slot}>\u2026</CollectionItem>`.\n// This is because we encountered issues with generic types that cannot be statically analysed\n// due to creating them dynamically via createCollection.\n\nfunction createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {\n /* -----------------------------------------------------------------------------------------------\n * CollectionProvider\n * ---------------------------------------------------------------------------------------------*/\n\n const PROVIDER_NAME = name + 'CollectionProvider'\n const [createCollectionContext, createCollectionScope] =\n createContextScope(PROVIDER_NAME)\n\n type ContextValue = {\n collectionRef: React.RefObject<CollectionElement>\n itemMap: Map<\n React.RefObject<ItemElement>,\n { ref: React.RefObject<ItemElement> } & ItemData\n >\n }\n\n const [CollectionProviderImpl, useCollectionContext] =\n createCollectionContext<ContextValue>(PROVIDER_NAME, {\n collectionRef: { current: null },\n itemMap: new Map(),\n })\n\n const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (\n props\n ) => {\n const { scope, children } = props\n const ref = React.useRef<CollectionElement>(null)\n const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current\n return (\n <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>\n {children}\n </CollectionProviderImpl>\n )\n }\n\n CollectionProvider.displayName = PROVIDER_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionSlot\n * ---------------------------------------------------------------------------------------------*/\n\n const COLLECTION_SLOT_NAME = name + 'CollectionSlot'\n\n const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(\n (props, forwardedRef) => {\n const { scope, children } = props\n const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)\n const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)\n return <Slot ref={composedRefs}>{children}</Slot>\n }\n )\n\n CollectionSlot.displayName = COLLECTION_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionItem\n * ---------------------------------------------------------------------------------------------*/\n\n const ITEM_SLOT_NAME = name + 'CollectionItemSlot'\n const ITEM_DATA_ATTR = 'data-collection-item'\n\n type CollectionItemSlotProps = ItemData & {\n children: React.ReactNode\n scope: any\n }\n\n const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(\n (props, forwardedRef) => {\n const { scope, children, ...itemData } = props\n const ref = React.useRef<ItemElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n const context = useCollectionContext(ITEM_SLOT_NAME, scope)\n\n React.useEffect(() => {\n context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })\n return () => void context.itemMap.delete(ref)\n })\n\n return (\n <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>\n {children}\n </Slot>\n )\n }\n )\n\n CollectionItemSlot.displayName = ITEM_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * useCollection\n * ---------------------------------------------------------------------------------------------*/\n\n function useCollection(scope: any) {\n const context = useCollectionContext(name + 'CollectionConsumer', scope)\n\n const getItems = React.useCallback(() => {\n const collectionNode = context.collectionRef.current\n if (!collectionNode) return []\n const orderedNodes = Array.from(\n collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)\n )\n const items = Array.from(context.itemMap.values())\n const orderedItems = items.sort(\n (a, b) =>\n orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)\n )\n return orderedItems\n }, [context.collectionRef, context.itemMap])\n\n return getItems\n }\n\n return [\n { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },\n useCollection,\n createCollectionScope,\n ] as const\n}\n\nexport { createCollection }\nexport type { CollectionProps }\n"],
5
+ "mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,YAAY;AACrB,SAAS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AAM,KAAK,QAAQ,QAAQ,OAAO,GAAG;AAAA,MAC9C,CAAC;AAED,aACE,CAAC,SAAS,EAAE,CAAC,cAAc,GAAG,GAAG,GAAG,KAAK,eACtC,SACH,EAFC;AAAA,IAIL;AAAA,EACF;AAEA,qBAAmB,cAAc;AAMjC,WAAS,cAAc,OAAY;AACjC,UAAM,UAAU,qBAAqB,OAAO,sBAAsB,KAAK;AAEvE,UAAM,WAAW,MAAM,YAAY,MAAM;AACvC,YAAM,iBAAiB,QAAQ,cAAc;AAC7C,UAAI,CAAC;AAAgB,eAAO,CAAC;AAC7B,YAAM,eAAe,MAAM;AAAA,QACzB,eAAe,iBAAiB,IAAI,iBAAiB;AAAA,MACvD;AACA,YAAM,QAAQ,MAAM,KAAK,QAAQ,QAAQ,OAAO,CAAC;AACjD,YAAM,eAAe,MAAM;AAAA,QACzB,CAAC,GAAG,MACF,aAAa,QAAQ,EAAE,IAAI,OAAQ,IAAI,aAAa,QAAQ,EAAE,IAAI,OAAQ;AAAA,MAC9E;AACA,aAAO;AAAA,IACT,GAAG,CAAC,QAAQ,eAAe,QAAQ,OAAO,CAAC;AAE3C,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,EAAE,UAAU,oBAAoB,MAAM,gBAAgB,UAAU,mBAAmB;AAAA,IACnF;AAAA,IACA;AAAA,EACF;AACF;",
6
+ "names": []
7
+ }
@@ -0,0 +1,71 @@
1
+ import { useComposedRefs } from "@tamagui/compose-refs";
2
+ import { Slot } from "@tamagui/core";
3
+ import { createContextScope } from "@tamagui/create-context";
4
+ import React from "react";
5
+ function createCollection(name) {
6
+ const PROVIDER_NAME = name + "CollectionProvider";
7
+ const [createCollectionContext, createCollectionScope] = createContextScope(PROVIDER_NAME);
8
+ const [CollectionProviderImpl, useCollectionContext] = createCollectionContext(PROVIDER_NAME, {
9
+ collectionRef: { current: null },
10
+ itemMap: /* @__PURE__ */ new Map()
11
+ });
12
+ const CollectionProvider = (props) => {
13
+ const { scope, children } = props;
14
+ const ref = React.useRef(null);
15
+ const itemMap = React.useRef(/* @__PURE__ */ new Map()).current;
16
+ return <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>{children}</CollectionProviderImpl>;
17
+ };
18
+ CollectionProvider.displayName = PROVIDER_NAME;
19
+ const COLLECTION_SLOT_NAME = name + "CollectionSlot";
20
+ const CollectionSlot = React.forwardRef(
21
+ (props, forwardedRef) => {
22
+ const { scope, children } = props;
23
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
24
+ const composedRefs = useComposedRefs(forwardedRef, context.collectionRef);
25
+ return <Slot ref={composedRefs}>{children}</Slot>;
26
+ }
27
+ );
28
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME;
29
+ const ITEM_SLOT_NAME = name + "CollectionItemSlot";
30
+ const ITEM_DATA_ATTR = "data-collection-item";
31
+ const CollectionItemSlot = React.forwardRef(
32
+ (props, forwardedRef) => {
33
+ const { scope, children, ...itemData } = props;
34
+ const ref = React.useRef(null);
35
+ const composedRefs = useComposedRefs(forwardedRef, ref);
36
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope);
37
+ React.useEffect(() => {
38
+ context.itemMap.set(ref, { ref, ...itemData });
39
+ return () => void context.itemMap.delete(ref);
40
+ });
41
+ return <Slot {...{ [ITEM_DATA_ATTR]: "" }} ref={composedRefs}>{children}</Slot>;
42
+ }
43
+ );
44
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME;
45
+ function useCollection(scope) {
46
+ const context = useCollectionContext(name + "CollectionConsumer", scope);
47
+ const getItems = React.useCallback(() => {
48
+ const collectionNode = context.collectionRef.current;
49
+ if (!collectionNode)
50
+ return [];
51
+ const orderedNodes = Array.from(
52
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
53
+ );
54
+ const items = Array.from(context.itemMap.values());
55
+ const orderedItems = items.sort(
56
+ (a, b) => orderedNodes.indexOf(a.ref.current) - orderedNodes.indexOf(b.ref.current)
57
+ );
58
+ return orderedItems;
59
+ }, [context.collectionRef, context.itemMap]);
60
+ return getItems;
61
+ }
62
+ return [
63
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
64
+ useCollection,
65
+ createCollectionScope
66
+ ];
67
+ }
68
+ export {
69
+ createCollection
70
+ };
71
+ //# sourceMappingURL=Collection.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Collection.tsx"],
4
+ "sourcesContent": ["import { useComposedRefs } from '@tamagui/compose-refs'\nimport { Slot } from '@tamagui/core'\nimport { createContextScope } from '@tamagui/create-context'\nimport React from 'react'\n\ntype SlotProps = React.ComponentPropsWithoutRef<typeof Slot>\ntype CollectionElement = HTMLElement\ninterface CollectionProps extends SlotProps {\n scope: any\n}\n\n// We have resorted to returning slots directly rather than exposing primitives that can then\n// be slotted like `<CollectionItem as={Slot}>\u2026</CollectionItem>`.\n// This is because we encountered issues with generic types that cannot be statically analysed\n// due to creating them dynamically via createCollection.\n\nfunction createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {\n /* -----------------------------------------------------------------------------------------------\n * CollectionProvider\n * ---------------------------------------------------------------------------------------------*/\n\n const PROVIDER_NAME = name + 'CollectionProvider'\n const [createCollectionContext, createCollectionScope] =\n createContextScope(PROVIDER_NAME)\n\n type ContextValue = {\n collectionRef: React.RefObject<CollectionElement>\n itemMap: Map<\n React.RefObject<ItemElement>,\n { ref: React.RefObject<ItemElement> } & ItemData\n >\n }\n\n const [CollectionProviderImpl, useCollectionContext] =\n createCollectionContext<ContextValue>(PROVIDER_NAME, {\n collectionRef: { current: null },\n itemMap: new Map(),\n })\n\n const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (\n props\n ) => {\n const { scope, children } = props\n const ref = React.useRef<CollectionElement>(null)\n const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current\n return (\n <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>\n {children}\n </CollectionProviderImpl>\n )\n }\n\n CollectionProvider.displayName = PROVIDER_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionSlot\n * ---------------------------------------------------------------------------------------------*/\n\n const COLLECTION_SLOT_NAME = name + 'CollectionSlot'\n\n const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(\n (props, forwardedRef) => {\n const { scope, children } = props\n const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)\n const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)\n return <Slot ref={composedRefs}>{children}</Slot>\n }\n )\n\n CollectionSlot.displayName = COLLECTION_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * CollectionItem\n * ---------------------------------------------------------------------------------------------*/\n\n const ITEM_SLOT_NAME = name + 'CollectionItemSlot'\n const ITEM_DATA_ATTR = 'data-collection-item'\n\n type CollectionItemSlotProps = ItemData & {\n children: React.ReactNode\n scope: any\n }\n\n const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(\n (props, forwardedRef) => {\n const { scope, children, ...itemData } = props\n const ref = React.useRef<ItemElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref)\n const context = useCollectionContext(ITEM_SLOT_NAME, scope)\n\n React.useEffect(() => {\n context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })\n return () => void context.itemMap.delete(ref)\n })\n\n return (\n <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>\n {children}\n </Slot>\n )\n }\n )\n\n CollectionItemSlot.displayName = ITEM_SLOT_NAME\n\n /* -----------------------------------------------------------------------------------------------\n * useCollection\n * ---------------------------------------------------------------------------------------------*/\n\n function useCollection(scope: any) {\n const context = useCollectionContext(name + 'CollectionConsumer', scope)\n\n const getItems = React.useCallback(() => {\n const collectionNode = context.collectionRef.current\n if (!collectionNode) return []\n const orderedNodes = Array.from(\n collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)\n )\n const items = Array.from(context.itemMap.values())\n const orderedItems = items.sort(\n (a, b) =>\n orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)\n )\n return orderedItems\n }, [context.collectionRef, context.itemMap])\n\n return getItems\n }\n\n return [\n { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },\n useCollection,\n createCollectionScope,\n ] as const\n}\n\nexport { createCollection }\nexport type { CollectionProps }\n"],
5
+ "mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,YAAY;AACrB,SAAS,0BAA0B;AACnC,OAAO,WAAW;AAalB,SAAS,iBAAiE,MAAc;AAKtF,QAAM,gBAAgB,OAAO;AAC7B,QAAM,CAAC,yBAAyB,qBAAqB,IACnD,mBAAmB,aAAa;AAUlC,QAAM,CAAC,wBAAwB,oBAAoB,IACjD,wBAAsC,eAAe;AAAA,IACnD,eAAe,EAAE,SAAS,KAAK;AAAA,IAC/B,SAAS,oBAAI,IAAI;AAAA,EACnB,CAAC;AAEH,QAAM,qBAA2E,CAC/E,UACG;AACH,UAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,UAAM,MAAM,MAAM,OAA0B,IAAI;AAChD,UAAM,UAAU,MAAM,OAAgC,oBAAI,IAAI,CAAC,EAAE;AACjE,WACE,CAAC,uBAAuB,OAAO,OAAO,SAAS,SAAS,eAAe,MACpE,SACH,EAFC;AAAA,EAIL;AAEA,qBAAmB,cAAc;AAMjC,QAAM,uBAAuB,OAAO;AAEpC,QAAM,iBAAiB,MAAM;AAAA,IAC3B,CAAC,OAAO,iBAAiB;AACvB,YAAM,EAAE,OAAO,SAAS,IAAI;AAC5B,YAAM,UAAU,qBAAqB,sBAAsB,KAAK;AAChE,YAAM,eAAe,gBAAgB,cAAc,QAAQ,aAAa;AACxE,aAAO,CAAC,KAAK,KAAK,eAAe,SAAS,EAAlC;AAAA,IACV;AAAA,EACF;AAEA,iBAAe,cAAc;AAM7B,QAAM,iBAAiB,OAAO;AAC9B,QAAM,iBAAiB;AAOvB,QAAM,qBAAqB,MAAM;AAAA,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
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './Collection'\n"],
5
+ "mappings": "AAAA,cAAc;",
6
+ "names": []
7
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./Collection";
2
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './Collection'\n"],
5
+ "mappings": "AAAA,cAAc;",
6
+ "names": []
7
+ }
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@tamagui/collection",
3
+ "version": "1.2.15",
4
+ "sideEffects": [
5
+ "*.css"
6
+ ],
7
+ "source": "src/index.ts",
8
+ "types": "./types/index.d.ts",
9
+ "main": "dist/cjs",
10
+ "module": "dist/esm",
11
+ "module:jsx": "dist/jsx",
12
+ "files": [
13
+ "src",
14
+ "types",
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tamagui-build",
19
+ "watch": "tamagui-build --watch"
20
+ },
21
+ "dependencies": {
22
+ "@tamagui/compose-refs": "^1.2.15",
23
+ "@tamagui/core": "^1.2.15",
24
+ "@tamagui/create-context": "^1.2.15",
25
+ "@tamagui/polyfill-dev": "^1.2.15",
26
+ "@tamagui/stacks": "^1.2.15",
27
+ "@tamagui/use-controllable-state": "^1.2.15"
28
+ },
29
+ "peerDependencies": {
30
+ "react": "*",
31
+ "react-dom": "*"
32
+ },
33
+ "devDependencies": {
34
+ "@tamagui/build": "^1.2.15",
35
+ "react": "^18.2.0",
36
+ "react-dom": "^18.2.0"
37
+ },
38
+ "publishConfig": {
39
+ "access": "public"
40
+ },
41
+ "gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14"
42
+ }
@@ -0,0 +1,138 @@
1
+ import { useComposedRefs } from '@tamagui/compose-refs'
2
+ import { Slot } from '@tamagui/core'
3
+ import { createContextScope } from '@tamagui/create-context'
4
+ import React from 'react'
5
+
6
+ type SlotProps = React.ComponentPropsWithoutRef<typeof Slot>
7
+ type CollectionElement = HTMLElement
8
+ interface CollectionProps extends SlotProps {
9
+ scope: any
10
+ }
11
+
12
+ // We have resorted to returning slots directly rather than exposing primitives that can then
13
+ // be slotted like `<CollectionItem as={Slot}>…</CollectionItem>`.
14
+ // This is because we encountered issues with generic types that cannot be statically analysed
15
+ // due to creating them dynamically via createCollection.
16
+
17
+ function createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {
18
+ /* -----------------------------------------------------------------------------------------------
19
+ * CollectionProvider
20
+ * ---------------------------------------------------------------------------------------------*/
21
+
22
+ const PROVIDER_NAME = name + 'CollectionProvider'
23
+ const [createCollectionContext, createCollectionScope] =
24
+ createContextScope(PROVIDER_NAME)
25
+
26
+ type ContextValue = {
27
+ collectionRef: React.RefObject<CollectionElement>
28
+ itemMap: Map<
29
+ React.RefObject<ItemElement>,
30
+ { ref: React.RefObject<ItemElement> } & ItemData
31
+ >
32
+ }
33
+
34
+ const [CollectionProviderImpl, useCollectionContext] =
35
+ createCollectionContext<ContextValue>(PROVIDER_NAME, {
36
+ collectionRef: { current: null },
37
+ itemMap: new Map(),
38
+ })
39
+
40
+ const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (
41
+ props
42
+ ) => {
43
+ const { scope, children } = props
44
+ const ref = React.useRef<CollectionElement>(null)
45
+ const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current
46
+ return (
47
+ <CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>
48
+ {children}
49
+ </CollectionProviderImpl>
50
+ )
51
+ }
52
+
53
+ CollectionProvider.displayName = PROVIDER_NAME
54
+
55
+ /* -----------------------------------------------------------------------------------------------
56
+ * CollectionSlot
57
+ * ---------------------------------------------------------------------------------------------*/
58
+
59
+ const COLLECTION_SLOT_NAME = name + 'CollectionSlot'
60
+
61
+ const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(
62
+ (props, forwardedRef) => {
63
+ const { scope, children } = props
64
+ const context = useCollectionContext(COLLECTION_SLOT_NAME, scope)
65
+ const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)
66
+ return <Slot ref={composedRefs}>{children}</Slot>
67
+ }
68
+ )
69
+
70
+ CollectionSlot.displayName = COLLECTION_SLOT_NAME
71
+
72
+ /* -----------------------------------------------------------------------------------------------
73
+ * CollectionItem
74
+ * ---------------------------------------------------------------------------------------------*/
75
+
76
+ const ITEM_SLOT_NAME = name + 'CollectionItemSlot'
77
+ const ITEM_DATA_ATTR = 'data-collection-item'
78
+
79
+ type CollectionItemSlotProps = ItemData & {
80
+ children: React.ReactNode
81
+ scope: any
82
+ }
83
+
84
+ const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(
85
+ (props, forwardedRef) => {
86
+ const { scope, children, ...itemData } = props
87
+ const ref = React.useRef<ItemElement>(null)
88
+ const composedRefs = useComposedRefs(forwardedRef, ref)
89
+ const context = useCollectionContext(ITEM_SLOT_NAME, scope)
90
+
91
+ React.useEffect(() => {
92
+ context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) })
93
+ return () => void context.itemMap.delete(ref)
94
+ })
95
+
96
+ return (
97
+ <Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>
98
+ {children}
99
+ </Slot>
100
+ )
101
+ }
102
+ )
103
+
104
+ CollectionItemSlot.displayName = ITEM_SLOT_NAME
105
+
106
+ /* -----------------------------------------------------------------------------------------------
107
+ * useCollection
108
+ * ---------------------------------------------------------------------------------------------*/
109
+
110
+ function useCollection(scope: any) {
111
+ const context = useCollectionContext(name + 'CollectionConsumer', scope)
112
+
113
+ const getItems = React.useCallback(() => {
114
+ const collectionNode = context.collectionRef.current
115
+ if (!collectionNode) return []
116
+ const orderedNodes = Array.from(
117
+ collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`)
118
+ )
119
+ const items = Array.from(context.itemMap.values())
120
+ const orderedItems = items.sort(
121
+ (a, b) =>
122
+ orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)
123
+ )
124
+ return orderedItems
125
+ }, [context.collectionRef, context.itemMap])
126
+
127
+ return getItems
128
+ }
129
+
130
+ return [
131
+ { Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
132
+ useCollection,
133
+ createCollectionScope,
134
+ ] as const
135
+ }
136
+
137
+ export { createCollection }
138
+ export type { CollectionProps }
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './Collection'
@@ -0,0 +1,22 @@
1
+ import { Slot } from '@tamagui/core';
2
+ import React from 'react';
3
+ type SlotProps = React.ComponentPropsWithoutRef<typeof Slot>;
4
+ interface CollectionProps extends SlotProps {
5
+ scope: any;
6
+ }
7
+ declare function createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string): readonly [{
8
+ readonly Provider: React.FC<{
9
+ children?: React.ReactNode;
10
+ scope: any;
11
+ }>;
12
+ readonly Slot: React.ForwardRefExoticComponent<CollectionProps & React.RefAttributes<HTMLElement>>;
13
+ readonly ItemSlot: React.ForwardRefExoticComponent<React.PropsWithoutRef<ItemData & {
14
+ children: React.ReactNode;
15
+ scope: any;
16
+ }> & React.RefAttributes<ItemElement>>;
17
+ }, (scope: any) => () => ({
18
+ ref: React.RefObject<ItemElement>;
19
+ } & ItemData)[], import("@tamagui/create-context").CreateScope];
20
+ export { createCollection };
21
+ export type { CollectionProps };
22
+ //# sourceMappingURL=Collection.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Collection.d.ts","sourceRoot":"","sources":["../src/Collection.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AAEpC,OAAO,KAAK,MAAM,OAAO,CAAA;AAEzB,KAAK,SAAS,GAAG,KAAK,CAAC,wBAAwB,CAAC,OAAO,IAAI,CAAC,CAAA;AAE5D,UAAU,eAAgB,SAAQ,SAAS;IACzC,KAAK,EAAE,GAAG,CAAA;CACX;AAOD,iBAAS,gBAAgB,CAAC,WAAW,SAAS,WAAW,EAAE,QAAQ,GAAG,EAAE,EAAE,IAAI,EAAE,MAAM;;mBAuBpC,MAAM,SAAS;eAAS,GAAG;;;;kBAwC/D,MAAM,SAAS;eAClB,GAAG;;WA6BkB,GAAG;SAhFtB,MAAM,SAAS,CAAC,WAAW,CAAC;gEAyGxC;AAED,OAAO,EAAE,gBAAgB,EAAE,CAAA;AAC3B,YAAY,EAAE,eAAe,EAAE,CAAA"}
@@ -0,0 +1,2 @@
1
+ export * from './Collection';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAA"}