-
Notifications
You must be signed in to change notification settings - Fork 881
/
Copy pathCollection.tsx
130 lines (104 loc) · 4.9 KB
/
Collection.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import React from 'react';
import { createContextScope } from '@radix-ui/react-context';
import { useComposedRefs } from '@radix-ui/react-compose-refs';
import { Slot } from '@radix-ui/react-slot';
import type * as Radix from '@radix-ui/react-primitive';
type SlotProps = Radix.ComponentPropsWithoutRef<typeof Slot>;
type CollectionElement = HTMLElement;
interface CollectionProps extends SlotProps {
scope: any;
}
// We have resorted to returning slots directly rather than exposing primitives that can then
// be slotted like `<CollectionItem as={Slot}>…</CollectionItem>`.
// This is because we encountered issues with generic types that cannot be statically analysed
// due to creating them dynamically via createCollection.
function createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {
/* -----------------------------------------------------------------------------------------------
* CollectionProvider
* ---------------------------------------------------------------------------------------------*/
const PROVIDER_NAME = name + 'CollectionProvider';
const [createCollectionContext, createCollectionScope] = createContextScope(PROVIDER_NAME);
type ContextValue = {
collectionRef: React.RefObject<CollectionElement>;
itemMap: Map<React.RefObject<ItemElement>, { ref: React.RefObject<ItemElement> } & ItemData>;
};
const [CollectionProviderImpl, useCollectionContext] = createCollectionContext<ContextValue>(
PROVIDER_NAME,
{ collectionRef: { current: null }, itemMap: new Map() }
);
const CollectionProvider: React.FC<{ children?: React.ReactNode; scope: any }> = (props) => {
const { scope, children } = props;
const ref = React.useRef<CollectionElement>(null);
const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current;
return (
<CollectionProviderImpl scope={scope} itemMap={itemMap} collectionRef={ref}>
{children}
</CollectionProviderImpl>
);
};
CollectionProvider.displayName = PROVIDER_NAME;
/* -----------------------------------------------------------------------------------------------
* CollectionSlot
* ---------------------------------------------------------------------------------------------*/
const COLLECTION_SLOT_NAME = name + 'CollectionSlot';
const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(
(props, forwardedRef) => {
const { scope, children } = props;
const context = useCollectionContext(COLLECTION_SLOT_NAME, scope);
const composedRefs = useComposedRefs(forwardedRef, context.collectionRef);
return <Slot ref={composedRefs}>{children}</Slot>;
}
);
CollectionSlot.displayName = COLLECTION_SLOT_NAME;
/* -----------------------------------------------------------------------------------------------
* CollectionItem
* ---------------------------------------------------------------------------------------------*/
const ITEM_SLOT_NAME = name + 'CollectionItemSlot';
const ITEM_DATA_ATTR = 'data-radix-collection-item';
type CollectionItemSlotProps = ItemData & {
children: React.ReactNode;
scope: any;
};
const CollectionItemSlot = React.forwardRef<ItemElement, CollectionItemSlotProps>(
(props, forwardedRef) => {
const { scope, children, ...itemData } = props;
const ref = React.useRef<ItemElement>(null);
const composedRefs = useComposedRefs(forwardedRef, ref);
const context = useCollectionContext(ITEM_SLOT_NAME, scope);
React.useEffect(() => {
context.itemMap.set(ref, { ref, ...(itemData as unknown as ItemData) });
return () => void context.itemMap.delete(ref);
});
return (
<Slot {...{ [ITEM_DATA_ATTR]: '' }} ref={composedRefs}>
{children}
</Slot>
);
}
);
CollectionItemSlot.displayName = ITEM_SLOT_NAME;
/* -----------------------------------------------------------------------------------------------
* useCollection
* ---------------------------------------------------------------------------------------------*/
function useCollection(scope: any) {
const context = useCollectionContext(name + 'CollectionConsumer', scope);
const getItems = React.useCallback(() => {
const collectionNode = context.collectionRef.current;
if (!collectionNode) return [];
const orderedNodes = Array.from(collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`));
const items = Array.from(context.itemMap.values());
const orderedItems = items.sort(
(a, b) => orderedNodes.indexOf(a.ref.current!) - orderedNodes.indexOf(b.ref.current!)
);
return orderedItems;
}, [context.collectionRef, context.itemMap]);
return getItems;
}
return [
{ Provider: CollectionProvider, Slot: CollectionSlot, ItemSlot: CollectionItemSlot },
useCollection,
createCollectionScope,
] as const;
}
export { createCollection };
export type { CollectionProps };