naive-ui/demo/pages/Layout.vue

79 lines
2.1 KiB
Vue
Raw Normal View History

2021-02-14 14:52:14 +08:00
<template>
2021-04-06 01:31:09 +08:00
<n-layout has-sider :position="isXs ? undefined : 'absolute'">
<n-layout-sider
:native-scrollbar="false"
2021-04-06 01:31:09 +08:00
:collapsed-width="0"
collapse-mode="transform"
bordered
show-trigger="bar"
v-if="showSider"
>
<n-menu
:value="menuValue"
:options="options"
@update:value="handleMenuUpdateValue"
/>
</n-layout-sider>
<n-layout ref="layoutInstRef" :native-scrollbar="false">
<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, useRouter } from 'vue-router'
2021-04-05 17:59:04 +08:00
import { findMenuValue } from '../utils/route'
2021-04-06 01:31:09 +08:00
import { useIsXs } 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'
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 router = useRouter()
2021-02-14 14:52:14 +08:00
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)
}
})
2021-04-24 01:04:18 +08:00
const isXsRef = useIsXs()
2021-02-14 14:52:14 +08:00
return {
showSider: useMemo(() => {
2021-04-24 01:04:18 +08:00
return !isXsRef.value
}),
2021-02-14 14:52:14 +08:00
layoutInstRef,
options: optionsRef,
menuValue: menuValueRef,
2021-04-24 01:04:18 +08:00
isXs: isXsRef,
handleMenuUpdateValue: (_, option) => {
router.push(option.path)
2021-02-14 14:52:14 +08:00
}
}
}
}
</script>