MCPcopy Create free account
hub / github.com/editablejs/editable / createCollection

Function createCollection

packages/ui/src/collection.tsx:14–126  ·  view source on GitHub ↗
(name: string)

Source from the content-addressed store, hash-verified

12// due to creating them dynamically via createCollection.
13
14function createCollection<ItemElement extends HTMLElement, ItemData = {}>(name: string) {
15 /* -----------------------------------------------------------------------------------------------
16 * CollectionProvider
17 * ---------------------------------------------------------------------------------------------*/
18
19 const PROVIDER_NAME = name + 'CollectionProvider'
20
21 type ContextValue = {
22 collectionRef: React.RefObject<CollectionElement>
23 itemMap: Map<React.RefObject<ItemElement>, { ref: React.RefObject<ItemElement> } & ItemData>
24 }
25
26 const CollectionContext = React.createContext<ContextValue>({
27 collectionRef: { current: null },
28 itemMap: new Map(),
29 })
30
31 const useCollectionContext = () => React.useContext(CollectionContext)
32
33 const CollectionProvider: React.FC<{ children?: React.ReactNode }> = props => {
34 const { children } = props
35 const ref = React.useRef<CollectionElement>(null)
36 const itemMap = React.useRef<ContextValue['itemMap']>(new Map()).current
37 return (
38 <CollectionContext.Provider
39 value={{
40 itemMap,
41 collectionRef: ref,
42 }}
43 >
44 {children}
45 </CollectionContext.Provider>
46 )
47 }
48
49 CollectionProvider.displayName = PROVIDER_NAME
50
51 /* -----------------------------------------------------------------------------------------------
52 * CollectionSlot
53 * ---------------------------------------------------------------------------------------------*/
54
55 const COLLECTION_SLOT_NAME = name + 'CollectionSlot'
56
57 const CollectionSlot = React.forwardRef<CollectionElement, CollectionProps>(
58 (props, forwardedRef) => {
59 const { children } = props
60 const context = useCollectionContext()
61 const composedRefs = useComposedRefs(forwardedRef, context.collectionRef)
62 return <Slot ref={composedRefs}>{children}</Slot>
63 },
64 )
65
66 CollectionSlot.displayName = COLLECTION_SLOT_NAME
67
68 /* -----------------------------------------------------------------------------------------------
69 * CollectionItem
70 * ---------------------------------------------------------------------------------------------*/
71

Callers 1

menu.tsxFile · 0.90

Calls 2

useComposedRefsFunction · 0.90
useCollectionContextFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…