-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayout.vue
137 lines (118 loc) · 3.53 KB
/
Layout.vue
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
<template>
<div class="theme" :class="pageClasses">
<NavBar
v-if="showNavbar"
:show-sidebar="showSidebar"
:class="isHome ? '!border-transparent !bg-opacity-50 !md:bg-transparent <md:(backdrop-filter backdrop-blur)' : ''"
@toggle="toggleSidebar"
>
<template #search>
<slot name="navbar-search">
<AlgoliaSearchBox v-if="theme.algolia" :options="theme.algolia" />
</slot>
</template>
</NavBar>
<SideBar :open="openSideBar">
<template #sidebar-top>
<slot name="sidebar-top" />
</template>
<template #sidebar-bottom>
<slot name="sidebar-bottom" />
</template>
</SideBar>
<!-- TODO: make this button accessible -->
<div class="sidebar-mask" @click="toggleSidebar(false)" />
<Content v-if="isCustomLayout" />
<Home v-else-if="enableHome">
<template #hero>
<slot name="home-hero" />
</template>
<template #features>
<slot name="home-features" />
</template>
<template #footer>
<slot name="home-footer" />
</template>
</Home>
<Page v-else>
<template #top>
<slot name="page-top" />
</template>
<template #bottom>
<slot name="page-bottom" />
</template>
</Page>
</div>
<!-- <Debug /> -->
<!-- <ClientOnly>
<WorkingInProgress />
</ClientOnly> -->
</template>
<script setup lang="ts">
import { ref, computed, watch, defineAsyncComponent } from 'vue'
import {
useRoute,
useData,
} from 'vitepress'
// components
import NavBar from './components/NavBar.vue'
import SideBar from './components/SideBar.vue'
import Page from './components/Page.vue'
const Home = defineAsyncComponent(() => import('./components/Home.vue'))
// generic state
const route = useRoute()
const {site: siteData} = useData()
const theme = computed(() => siteData.value.themeConfig)
const AlgoliaSearchBox = defineAsyncComponent(
() => import('./components/AlgoliaSearchBox.vue'),
)
// custom layout
const isCustomLayout = computed(() => !!route.data.frontmatter.customLayout)
// home
const enableHome = computed(() => !!route.data.frontmatter.home)
// navbar
const showNavbar = computed(() => {
const { themeConfig } = siteData.value
const { frontmatter } = route.data
if (frontmatter.navbar === false || themeConfig.navbar === false)
return false
return (
siteData.value.title
|| themeConfig.logo
|| themeConfig.repo
|| themeConfig.nav
)
})
const isHome = computed(() => route.path === '/' || route.path === '/index.html')
// sidebar
const openSideBar = ref(false)
const showSidebar = computed(() => {
const { frontmatter } = route.data
const { themeConfig } = siteData.value
return (
!frontmatter.home
&& frontmatter.sidebar !== false
&& ((typeof themeConfig.sidebar === 'object'
&& Object.keys(themeConfig.sidebar).length !== 0)
|| (Array.isArray(themeConfig.sidebar) && themeConfig.sidebar.length !== 0))
)
})
const toggleSidebar = (to?: boolean) => {
openSideBar.value = typeof to === 'boolean' ? to : !openSideBar.value
}
const hideSidebar = toggleSidebar.bind(null, false)
// close the sidebar when navigating to a different location
watch(route, hideSidebar)
// TODO: route only changes when the pathname changes
// listening to hashchange does nothing because it's prevented in router
// page classes
const pageClasses = computed(() => {
return [
{
'no-navbar': !showNavbar.value,
'sidebar-open': openSideBar.value,
'no-sidebar': !showSidebar.value,
},
]
})
</script>