-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
slot.ts
54 lines (46 loc) · 1.54 KB
/
slot.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
import { cloneVNode, createVNode, defineComponent, mergeProps } from 'vue'
import { useComposedRefs, useForwardRef } from '@oku-ui/use-composable'
import { isSlottable } from './utils'
const NAME = 'OkuSlot'
const OkuSlot = defineComponent({
name: NAME,
inheritAttrs: false,
setup(props, { attrs, slots }) {
const forwarded = useForwardRef()
const composedRefs = useComposedRefs(forwarded)
return () => {
const defaultSlot = slots.default?.()
const slottable = defaultSlot?.find(isSlottable)
if (slottable && defaultSlot?.length) {
// TODO: default TS type problem
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const newParentElement = slottable.children?.default?.()[0]
const newChildren = defaultSlot.map((child) => {
if (child === slottable)
return newParentElement.children
else return child
})
return createVNode(newParentElement.type, {
...mergeProps(attrs, props, newParentElement.props), ref: composedRefs,
}, {
default: () => newChildren,
})
}
else if (slots.default) {
return cloneVNode(slots.default?.()[0], { ...mergeProps(attrs, props), ref: composedRefs }, true)
}
else {
return null
}
}
},
})
const OkuSlottable = defineComponent({
name: 'OkuSlottable',
inheritAttrs: false,
setup(_, { slots }) {
return slots.default ? () => slots.default?.() : null
},
})
export { OkuSlot, OkuSlottable }