naive-ui/demo/pages/Layout.vue

92 lines
2.5 KiB
Vue
Raw Normal View History

2021-02-14 14:52:14 +08:00
<template>
2021-05-26 14:39:33 +08:00
<n-layout
id="doc-layout"
2021-05-26 14:39:33 +08:00
:has-sider="showSider"
:position="isMobile ? 'static' : 'absolute'"
:style="{
top: isMobile ? '' : 'var(--header-height)'
}"
2021-05-26 14:39:33 +08:00
>
<n-layout-sider
:native-scrollbar="false"
2021-04-06 01:31:09 +08:00
:collapsed-width="0"
collapse-mode="transform"
bordered
show-trigger="bar"
2021-06-02 16:17:13 +08:00
trigger-style="top: calc(50% - var(--header-height));"
v-if="showSider"
>
<n-menu
:value="menuValue"
:options="options"
:render-label="renderMenuLabel"
/>
</n-layout-sider>
2021-06-02 16:17:13 +08:00
<n-layout
ref="layoutInstRef"
:native-scrollbar="false"
:position="isMobile || showSider ? 'static' : 'absolute'"
2021-06-02 16:17:13 +08:00
content-style="min-height: calc(100vh - var(--header-height)); display: flex; flex-direction: column;"
>
<router-view />
2021-05-04 02:27:16 +08:00
<site-footer />
</n-layout>
</n-layout>
2021-02-14 14:52:14 +08:00
</template>
<script>
// Frame component for components & docs page
import { computed, watch, toRef, ref } from 'vue'
import { useRoute } from 'vue-router'
2021-04-05 17:59:04 +08:00
import { findMenuValue } from '../utils/route'
import { useIsMobile, useIsTablet } from '../utils/composables'
2021-05-04 02:27:16 +08:00
import SiteFooter from './home/Footer.vue'
2021-02-14 14:52:14 +08:00
import { useDocOptions, useComponentOptions } from '../store'
import { renderMenuLabel } from '../store/menu-options'
2021-04-24 01:04:18 +08:00
import { useMemo } from 'vooks'
2021-02-14 14:52:14 +08:00
export default {
2021-05-04 02:27:16 +08:00
components: {
SiteFooter
},
2021-02-14 14:52:14 +08:00
setup () {
const route = useRoute()
const layoutInstRef = ref(null)
const docOptionsRef = useDocOptions()
const componentOptionsRef = useComponentOptions()
const optionsRef = computed(() =>
route.path.includes('/docs/')
? docOptionsRef.value
: componentOptionsRef.value
)
2021-04-05 17:59:04 +08:00
const menuValueRef = computed(() => {
return findMenuValue(optionsRef.value, route.path)
})
2021-02-14 14:52:14 +08:00
watch(toRef(route, 'path'), (value, oldValue) => {
const langAndThemeReg = /\/(zh-CN|en-US)\/(light|dark|os-theme)/g
// only theme & lang change do not restore the scroll status
if (
value.replace(langAndThemeReg, '') !==
oldValue.replace(langAndThemeReg, '')
) {
2021-02-14 14:52:14 +08:00
layoutInstRef.value.scrollTo(0, 0)
}
})
const isMobileRef = useIsMobile()
const isTabletRef = useIsTablet()
2021-02-14 14:52:14 +08:00
return {
renderMenuLabel,
showSider: useMemo(() => {
return !isMobileRef.value && !isTabletRef.value
}),
2021-02-14 14:52:14 +08:00
layoutInstRef,
options: optionsRef,
menuValue: menuValueRef,
isMobile: isMobileRef
2021-02-14 14:52:14 +08:00
}
}
}
</script>