-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathcollection.ts
152 lines (129 loc) · 5 KB
/
collection.ts
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import type { ComponentObjectPropsOptions, Ref } from 'vue'
import { computed, defineComponent, h, ref, toRefs, unref, watchEffect } from 'vue'
import { useComposedRefs, useForwardRef } from '@oku-ui/use-composable'
import { createProvideScope } from '@oku-ui/provide'
import { OkuSlot } from '@oku-ui/slot'
export const collectionProps = {
scope: { type: null, required: false },
}
export interface CollectionPropsType {
scope: any
}
export type CollectionElement = HTMLElement
// 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.
export function createCollection<ItemElement extends HTMLElement, T>(name: string, ItemData?: ComponentObjectPropsOptions) {
/* -----------------------------------------------------------------------------------------------
* CollectionProvider
* --------------------------------------------------------------------------------------------- */
const PROVIDER_NAME = `${name}CollectionProvider`
const [createCollectionProvide, createCollectionScope] = createProvideScope(PROVIDER_NAME)
type ContextValue = {
collectionRef: Ref<ItemElement | undefined>
itemMap: Ref<Map<Ref<ItemElement | null | undefined>, {
ref: ItemElement
} & T>>
}
const [CollectionProviderImpl, useCollectionInject] = createCollectionProvide<ContextValue>(
PROVIDER_NAME,
{ collectionRef: ref(undefined), itemMap: ref(new Map()) },
)
const CollectionProvider = defineComponent({
name: PROVIDER_NAME,
inheritAttrs: false,
props: {
...collectionProps,
},
setup(props, { slots }) {
const collectionRef = ref<ItemElement>()
const itemMap = ref(new Map())
CollectionProviderImpl({
collectionRef,
itemMap,
scope: props.scope,
})
return () => slots.default?.()
},
})
/* -----------------------------------------------------------------------------------------------
* CollectionSlot
* --------------------------------------------------------------------------------------------- */
const COLLECTION_SLOT_NAME = `${name}CollectionSlot`
const CollectionSlot = defineComponent({
name: COLLECTION_SLOT_NAME,
components: {
OkuSlot,
},
inheritAttrs: false,
props: {
...collectionProps,
...ItemData,
},
setup(props, { slots }) {
const inject = useCollectionInject(COLLECTION_SLOT_NAME, props.scope)
const forwaredRef = useForwardRef()
const composedRefs = useComposedRefs(forwaredRef, inject.collectionRef)
return () => h(OkuSlot, { ref: composedRefs }, slots)
},
})
/* -----------------------------------------------------------------------------------------------
* CollectionItem
* --------------------------------------------------------------------------------------------- */
const ITEM_SLOT_NAME = `${name}CollectionItemSlot`
const ITEM_DATA_ATTR = 'data-oku-collection-item'
const _CollectionItemSlot = defineComponent({
name: ITEM_SLOT_NAME,
components: {
OkuSlot,
},
inheritAttrs: false,
props: {
...collectionProps,
...ItemData,
},
setup(props, { attrs, slots }) {
const { scope, ...itemData } = toRefs(props)
const refValue = ref<ItemElement | null>()
const forwaredRef = useForwardRef()
const inject = useCollectionInject(ITEM_SLOT_NAME, scope.value)
const composedRefs = useComposedRefs(refValue, forwaredRef)
watchEffect((onClean) => {
inject.itemMap.value.set(refValue, { ref: refValue, ...unref(itemData as any), ...attrs })
onClean(() => {
inject.itemMap.value.delete(refValue)
})
})
return () => h(OkuSlot, { ref: composedRefs, ...{ [ITEM_DATA_ATTR]: '' } }, slots)
},
})
const CollectionItemSlot = _CollectionItemSlot as typeof _CollectionItemSlot & (new () => { $props: Partial<T> })
/* -----------------------------------------------------------------------------------------------
* useCollection
* --------------------------------------------------------------------------------------------- */
function useCollection(scope: any) {
const inject = useCollectionInject(`${name}CollectionConsumer`, scope)
const getItems = computed(() => {
const collectionNode = inject.collectionRef.value
if (!collectionNode)
return []
const orderedNodes = Array.from(collectionNode.querySelectorAll(`[${ITEM_DATA_ATTR}]`))
const items = Array.from(inject.itemMap.value.values())
const orderedItems = items.sort(
(a, b) => {
return orderedNodes.indexOf(a.ref) - orderedNodes.indexOf(b.ref)
},
)
return orderedItems
})
return getItems
}
return {
CollectionProvider,
CollectionSlot,
CollectionItemSlot,
useCollection,
createCollectionScope,
}
}