-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Portal.ts
63 lines (54 loc) · 1.52 KB
/
Portal.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
import { Primitive, primitiveProps } from '@oku-ui/primitive'
import type {
ElementType,
PrimitiveProps,
} from '@oku-ui/primitive'
import { useForwardRef } from '@oku-ui/use-composable'
import type { PropType } from 'vue'
import { Teleport, defineComponent, h, toRefs } from 'vue'
const PORTAL_NAME = 'OkuPortal'
export type PortalElementIntrinsicElement = ElementType<'div'>
export type PortalElement = HTMLDivElement
export interface PortalProps extends PrimitiveProps {
/**
* An optional container where the portaled content should be appended.
*/
container?: HTMLElement | null | string | undefined
}
export const portalProps = {
props: {
...primitiveProps,
container: {
type: [Object, String] as PropType<HTMLElement | string | null | undefined>,
default: () => globalThis?.document?.body,
},
},
emits: {},
}
const portal = defineComponent({
name: PORTAL_NAME,
inheritAttrs: false,
props: {
...portalProps.props,
...primitiveProps,
},
setup(props, { slots, attrs }) {
const { asChild, container } = toRefs(props)
const forwardedRef = useForwardRef()
return () => container.value
? h(Teleport, { to: container.value, disabled: false }, h(Primitive.div,
{
...attrs,
ref: forwardedRef,
asChild: asChild.value,
}, {
default: () => slots.default?.(),
}),
)
: null
},
})
export const OkuPortal = portal as typeof portal &
(new () => {
$props: Partial<PortalElementIntrinsicElement>
})