1
1
import { createElementBlock , createElementVNode , defineComponent , h , mergeProps , onMounted , ref } from 'vue'
2
+ import type { ComponentOptions } from 'vue'
2
3
3
4
export default defineComponent ( {
4
5
name : 'ClientOnly' ,
@@ -8,7 +9,7 @@ export default defineComponent({
8
9
setup ( _ , { slots, attrs } ) {
9
10
const mounted = ref ( false )
10
11
onMounted ( ( ) => { mounted . value = true } )
11
- return ( props ) => {
12
+ return ( props : any ) => {
12
13
if ( mounted . value ) { return slots . default ?.( ) }
13
14
const slot = slots . fallback || slots . placeholder
14
15
if ( slot ) { return slot ( ) }
@@ -21,7 +22,7 @@ export default defineComponent({
21
22
22
23
const cache = new WeakMap ( )
23
24
24
- export function createClientOnly ( component ) {
25
+ export function createClientOnly < T extends ComponentOptions > ( component : T ) {
25
26
if ( cache . has ( component ) ) {
26
27
return cache . get ( component )
27
28
}
@@ -30,9 +31,9 @@ export function createClientOnly (component) {
30
31
31
32
if ( clone . render ) {
32
33
// override the component render (non script setup component)
33
- clone . render = ( ctx , ...args ) => {
34
+ clone . render = ( ctx : any , ...args : any [ ] ) => {
34
35
if ( ctx . mounted$ ) {
35
- const res = component . render ( ctx , ...args )
36
+ const res = component . render ! ( ctx , ...args )
36
37
return ( res . children === null || typeof res . children === 'string' )
37
38
? createElementVNode ( res . type , res . props , res . children , res . patchFlag , res . dynamicProps , res . shapeFlag )
38
39
: h ( res )
@@ -56,7 +57,7 @@ export function createClientOnly (component) {
56
57
. then ( ( setupState ) => {
57
58
return typeof setupState !== 'function'
58
59
? { ...setupState , mounted$ }
59
- : ( ...args ) => {
60
+ : ( ...args : any [ ] ) => {
60
61
if ( mounted$ . value ) {
61
62
const res = setupState ( ...args )
62
63
return ( res . children === null || typeof res . children === 'string' )
0 commit comments