Skip to content

Commit 8809d2d

Browse files
committed
feat: built-in ClientOnly component
1 parent 9bd79e8 commit 8809d2d

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { ref, onMounted, defineComponent } from 'vue'
2+
3+
export const ClientOnly = defineComponent({
4+
setup(_, { slots }) {
5+
const show = ref(false)
6+
onMounted(() => {
7+
show.value = true
8+
})
9+
return () => (show.value && slots.default ? slots.default() : null)
10+
}
11+
})

src/client/app/components/Content.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { h } from 'vue'
1+
import { defineComponent, h } from 'vue'
22
import { useRoute } from '../router'
33

4-
export const Content = {
4+
export const Content = defineComponent({
55
name: 'VitePressContent',
66
setup() {
77
const route = useRoute()
88
return () => (route.component ? h(route.component) : null)
99
}
10-
}
10+
})

src/client/app/mixin.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SiteDataRef } from './composables/siteData'
44
import { PageDataRef } from './composables/pageData'
55
import { Content } from './components/Content'
66
import Debug from './components/Debug.vue'
7+
import { ClientOnly } from './components/ClientOnly'
78

89
export function mixinGlobalComputed(
910
app: App,
@@ -68,6 +69,6 @@ export function mixinGlobalComponents(app: App) {
6869
const isProd = process.env.NODE_ENV === 'production'
6970

7071
app.component('Content', Content)
71-
72+
app.component('ClientOnly', ClientOnly)
7273
app.component('Debug', isProd ? () => null : Debug)
7374
}

0 commit comments

Comments
 (0)