naive-ui/demo/SiteHeader.vue

434 lines
11 KiB
Vue
Raw Normal View History

2019-09-17 19:28:28 +08:00
<template>
2021-04-05 17:59:04 +08:00
<n-layout-header bordered class="nav" :style="style">
<n-text tag="div" class="ui-logo" :depth="1" @click="handleLogoClick">
<img src="./assets/images/naivelogo.svg" />
<span v-if="!isMobile">Naive UI</span>
2021-04-05 17:59:04 +08:00
</n-text>
<div :style="!isMobile ? 'display: flex; align-items: center;' : ''">
<div class="nav-menu" v-if="!(isMobile || isTablet)">
2021-04-05 17:59:04 +08:00
<n-menu
mode="horizontal"
:value="menuValue"
:options="menuOptions"
:render-label="renderMenuLabel"
2020-03-03 19:25:25 +08:00
/>
</div>
2021-04-05 17:59:04 +08:00
<n-auto-complete
v-model:value="searchPattern"
:style="!isMobile ? 'width: 216px; margin-left: 24px' : undefined"
2021-04-05 17:59:04 +08:00
:placeholder="t('searchPlaceholder')"
:options="searchOptions"
clear-after-select
blur-after-select
@select="handleSearch"
/>
</div>
2021-04-06 01:31:09 +08:00
<n-popover
v-if="isMobile || isTablet"
2021-04-06 01:31:09 +08:00
style="padding: 0; width: 288px"
placement="bottom-end"
display-directive="show"
trigger="click"
ref="mobilePopoverRef"
2021-04-06 01:31:09 +08:00
>
<template #trigger>
<n-icon size="20" style="margin-left: 12px"><menu-outline /></n-icon>
</template>
2021-04-06 18:06:04 +08:00
<div style="overflow: auto; max-height: 79vh">
2021-04-06 01:31:09 +08:00
<n-menu
:value="mobileMenuValue"
:options="mobileMenuOptions"
2021-04-06 01:31:09 +08:00
:indent="18"
@update:value="handleUpdateMobileMenu"
:render-label="renderMenuLabel"
2021-04-06 01:31:09 +08:00
/>
</div>
</n-popover>
2021-04-24 01:04:18 +08:00
<div class="nav-end" v-else>
<n-button text class="nav-picker" @click="handleLocaleUpdate">
{{ localeLabelMap[locale] }}
</n-button>
<n-button text class="nav-picker" @click="handleThemeUpdate">
{{ themeLabelMap[theme] }}
</n-button>
2021-06-05 04:20:31 +08:00
<n-button tag="a" text class="nav-picker" :href="repoUrl" target="_blank">
GitHub
2021-06-05 04:20:31 +08:00
</n-button>
2021-04-24 01:04:18 +08:00
<n-text class="nav-picker">
{{ version }}
</n-text>
<n-button
v-if="dev"
text
class="nav-picker"
@click="handleDisplayModeUpdate"
2021-04-05 17:59:04 +08:00
>
2021-04-24 01:04:18 +08:00
{{ displayModeLabelMap[displayMode] }}
</n-button>
<n-button
2021-04-05 17:59:04 +08:00
v-if="tusimple || dev"
2021-04-24 01:04:18 +08:00
text
class="nav-picker"
@click="handleConfigProviderUpdate"
2021-04-05 17:59:04 +08:00
>
2021-04-24 01:04:18 +08:00
{{ cfgProviderLabelMap[configProviderName] }}
2021-04-05 17:59:04 +08:00
</n-button>
</div>
2020-03-03 19:25:25 +08:00
</n-layout-header>
2019-09-17 19:28:28 +08:00
</template>
<script>
import { computed, ref } from 'vue'
2021-04-05 17:59:04 +08:00
import { useRouter, useRoute } from 'vue-router'
import { useMessage, version } from 'naive-ui'
2021-04-05 17:59:04 +08:00
import { MenuOutline } from '@vicons/ionicons5'
2021-06-05 04:20:31 +08:00
import { repoUrl } from './utils/github-url'
import { i18n, useIsMobile, useIsTablet } from './utils/composables'
2021-04-05 17:59:04 +08:00
import { findMenuValue } from './utils/route'
2020-12-12 13:51:22 +08:00
import {
2021-01-13 12:01:02 +08:00
useThemeName,
useLocaleName,
useDisplayMode,
2021-02-17 23:31:56 +08:00
useFlattenedDocOptions,
2021-04-05 17:59:04 +08:00
useConfigProviderName,
useDocOptions,
useComponentOptions
2021-01-13 12:01:02 +08:00
} from './store'
import { renderMenuLabel } from './store/menu-options'
2019-09-17 19:28:28 +08:00
2021-04-05 17:59:04 +08:00
// match substr
2020-02-26 17:21:03 +08:00
function match (pattern, string) {
if (!pattern.length) return true
if (!string.length) return false
if (pattern[0] === string[0]) return match(pattern.slice(1), string.slice(1))
return match(pattern, string.slice(1))
}
2021-04-05 17:59:04 +08:00
const locales = {
'zh-CN': {
dark: '深色',
light: '浅色',
searchPlaceholder: '搜索',
home: '首页',
doc: '文档',
component: '组件',
common: '常规',
debug: '调试',
alreadyHome: '别点了,你已经在首页了',
tusimpleTheme: '图森主题',
defaultTheme: '默认主题'
},
'en-US': {
dark: 'Dark',
light: 'Light',
searchPlaceholder: 'Search',
home: 'Home',
doc: 'Docs',
component: 'Components',
common: 'Common',
debug: 'Debug',
alreadyHome: 'You are already in home page. No clicking anymore.',
2021-06-09 02:21:58 +08:00
tusimpleTheme: 'TuSimple Theme',
2021-04-05 17:59:04 +08:00
defaultTheme: 'Default Theme'
}
}
2019-09-17 19:28:28 +08:00
export default {
2020-09-27 22:27:25 +08:00
name: 'SiteHeader',
2021-04-05 17:59:04 +08:00
components: {
MenuOutline
},
2020-09-27 22:27:25 +08:00
setup () {
2021-03-26 01:09:16 +08:00
const message = useMessage()
2021-04-05 17:59:04 +08:00
const route = useRoute()
const router = useRouter()
const mobilePopoverRef = ref(null)
const themeAndLocaleReg = /^(\/[^/]+){2}/
2021-04-05 17:59:04 +08:00
// i18n
const { t } = i18n(locales)
// menu
const menuOptionsRef = computed(() => {
2020-10-07 20:45:51 +08:00
return [
{
2021-02-14 14:52:14 +08:00
key: 'home',
label: t('home'),
path: themeAndLocaleReg.exec(route.path)[0]
2020-10-07 20:45:51 +08:00
},
{
2021-02-14 14:52:14 +08:00
key: 'doc',
label: t('doc'),
path: themeAndLocaleReg.exec(route.path)[0] + '/docs/introduction'
2021-02-14 14:52:14 +08:00
},
{
key: 'component',
label: t('component'),
path: themeAndLocaleReg.exec(route.path)[0] + '/components/button'
2020-10-07 20:45:51 +08:00
}
]
})
2021-04-05 17:59:04 +08:00
const menuValueRef = computed(() => {
if (/\/docs\//.test(route.path)) return 'doc'
if (/\/components\//.test(route.path)) return 'component'
else if (route.name === 'home') return 'home'
return null
})
// mobile options
2021-04-05 17:59:04 +08:00
const docOptionsRef = useDocOptions()
const componentOptionsRef = useComponentOptions()
const mobileMenuOptionsRef = computed(() => {
2021-04-05 17:59:04 +08:00
return [
{
key: 'theme',
label: themeLabelMapRef.value[themeNameRef.value]
2021-04-05 17:59:04 +08:00
},
{
key: 'locale',
label: localeNameRef.value === 'zh-CN' ? 'English' : '中文'
2021-04-05 17:59:04 +08:00
},
{
key: 'home',
label: t('home'),
path: themeAndLocaleReg.exec(route.path)[0]
2021-04-05 17:59:04 +08:00
},
{
key: 'doc',
label: t('doc'),
children: docOptionsRef.value,
path: themeAndLocaleReg.exec(route.path)[0] + '/docs/introduction'
2021-04-05 17:59:04 +08:00
},
{
key: 'component',
label: t('component'),
path: themeAndLocaleReg.exec(route.path)[0] + '/components/button',
2021-04-05 17:59:04 +08:00
children: componentOptionsRef.value
2021-06-05 04:20:31 +08:00
},
{
key: 'github',
label: 'GitHub'
2021-04-05 17:59:04 +08:00
}
]
})
const mobileMenuValueRef = computed(() => {
2021-04-05 20:06:50 +08:00
if (route.name === 'home') return 'home'
return findMenuValue(mobileMenuOptionsRef.value, route.path)
2021-04-05 17:59:04 +08:00
})
function handleUpdateMobileMenu (value, { path }) {
2021-04-05 17:59:04 +08:00
if (value === 'theme') {
handleThemeUpdate()
} else if (value === 'locale') {
if (localeNameRef.value === 'zh-CN') {
localeNameRef.value = 'en-US'
} else {
localeNameRef.value = 'zh-CN'
}
} else if (path) {
router.push(path)
} else {
window.open(repoUrl, '_blank')
2021-04-05 17:59:04 +08:00
}
mobilePopoverRef.value.setShow(false)
2021-04-05 17:59:04 +08:00
}
// theme
const themeNameRef = useThemeName()
const themeLabelMapRef = computed(() => ({
dark: t('light'),
light: t('dark')
2020-10-07 20:45:51 +08:00
}))
2021-04-05 17:59:04 +08:00
function handleThemeUpdate () {
if (themeNameRef.value === 'dark') {
themeNameRef.value = 'light'
} else {
themeNameRef.value = 'dark'
}
}
// locale
const localeNameRef = useLocaleName()
const localeLabelMap = {
'zh-CN': 'English',
'en-US': '中文'
2021-04-05 17:59:04 +08:00
}
2021-04-24 01:04:18 +08:00
function handleLocaleUpdate () {
if (localeNameRef.value === 'zh-CN') {
localeNameRef.value = 'en-US'
} else {
localeNameRef.value = 'zh-CN'
2021-04-05 17:59:04 +08:00
}
2021-04-24 01:04:18 +08:00
}
2021-04-05 17:59:04 +08:00
// display mode
const displayModeRef = useDisplayMode()
const displayModeLabelMap = {
2021-04-24 01:04:18 +08:00
common: 'Debug',
debug: 'Prod'
2021-04-05 17:59:04 +08:00
}
2021-04-24 01:04:18 +08:00
function handleDisplayModeUpdate () {
if (displayModeRef.value === 'common') {
displayModeRef.value = 'debug'
} else {
displayModeRef.value = 'common'
2021-04-05 17:59:04 +08:00
}
2021-04-24 01:04:18 +08:00
}
2021-04-05 17:59:04 +08:00
// config provider
const configProviderNameRef = useConfigProviderName()
const cfgProviderLabelMapRef = computed(() => ({
2021-04-24 01:04:18 +08:00
tusimple: t('defaultTheme'),
default: t('tusimpleTheme')
2021-03-03 17:26:54 +08:00
}))
2021-04-24 01:04:18 +08:00
function handleConfigProviderUpdate () {
if (configProviderNameRef.value === 'tusimple') {
configProviderNameRef.value = 'default'
} else {
configProviderNameRef.value = 'tusimple'
2021-03-03 17:26:54 +08:00
}
2021-04-24 01:04:18 +08:00
}
2021-04-05 17:59:04 +08:00
// search
const searchableOptionsRef = useFlattenedDocOptions()
const searchPatternRef = ref('')
const searchOptionsRef = computed(() => {
2020-02-11 18:10:49 +08:00
function getLabel (item) {
2021-02-14 15:19:32 +08:00
if (item.label) {
return item.label + (item.extra ? ' ' + item.extra : '')
2020-12-12 15:33:41 +08:00
}
2021-02-14 15:19:32 +08:00
return item.key
2020-02-11 18:10:49 +08:00
}
2021-04-05 17:59:04 +08:00
if (!searchPatternRef.value) return []
2020-02-11 18:10:49 +08:00
const replaceRegex = / |-/g
2021-04-05 17:59:04 +08:00
return searchableOptionsRef.value
2020-12-12 13:51:22 +08:00
.filter((item) => {
2021-04-05 17:59:04 +08:00
const pattern = searchPatternRef.value
2020-12-12 13:51:22 +08:00
.toLowerCase()
.replace(replaceRegex, '')
.slice(0, 20)
const label = getLabel(item).toLowerCase().replace(replaceRegex, '')
return match(pattern, label)
})
.map((item) => ({
label: getLabel(item),
value: item.path
}))
2021-04-05 17:59:04 +08:00
})
function handleSearch (value) {
router.push(value)
2019-09-23 11:32:50 +08:00
}
2021-04-05 17:59:04 +08:00
// common
const isMobileRef = useIsMobile()
const isTabletRef = useIsTablet()
2021-04-05 17:59:04 +08:00
function handleLogoClick () {
if (/^(\/[^/]+){2}$/.test(route.path)) {
message.info(t('alreadyHome'))
2020-09-27 22:27:25 +08:00
return
}
2021-04-05 17:59:04 +08:00
router.push(/^(\/[^/]+){2}/.exec(route.path)[0])
}
return {
renderMenuLabel,
mobilePopoverRef,
2021-04-05 17:59:04 +08:00
tusimple: process.env.TUSIMPLE,
dev: __DEV__,
message,
t,
version,
isMobile: isMobileRef,
isTablet: isTabletRef,
2021-06-05 04:20:31 +08:00
repoUrl,
2021-04-05 17:59:04 +08:00
// theme
theme: themeNameRef,
handleThemeUpdate,
themeLabelMap: themeLabelMapRef,
// displayMode
displayMode: displayModeRef,
displayModeLabelMap,
2021-04-24 01:04:18 +08:00
handleDisplayModeUpdate,
2021-04-05 17:59:04 +08:00
// locale
locale: localeNameRef,
localeLabelMap,
2021-04-24 01:04:18 +08:00
handleLocaleUpdate,
2021-04-05 17:59:04 +08:00
// configProvider
configProviderName: configProviderNameRef,
cfgProviderLabelMap: cfgProviderLabelMapRef,
2021-04-24 01:04:18 +08:00
handleConfigProviderUpdate,
2021-04-05 17:59:04 +08:00
// search
searchPattern: searchPatternRef,
searchOptions: searchOptionsRef,
handleSearch,
// menu
menuOptions: menuOptionsRef,
menuValue: menuValueRef,
// mobile & tablet menu
mobileMenuOptions: mobileMenuOptionsRef,
handleUpdateMobileMenu,
mobileMenuValue: mobileMenuValueRef,
2021-04-05 17:59:04 +08:00
// common
handleLogoClick,
style: computed(() => {
return isMobileRef.value
2021-04-05 17:59:04 +08:00
? {
'--side-padding': '16px',
'grid-template-columns': 'auto 1fr auto'
}
: {
'--side-padding': '32px',
'grid-template-columns':
'calc(272px - var(--side-padding)) 1fr auto'
}
})
2019-09-17 19:28:28 +08:00
}
}
}
</script>
2020-11-01 19:35:00 +08:00
<style scoped>
2019-09-17 19:28:28 +08:00
.nav {
display: grid;
2021-06-02 16:17:13 +08:00
grid-template-rows: calc(var(--header-height) - 1px);
2019-09-17 19:28:28 +08:00
align-items: center;
2021-04-05 17:59:04 +08:00
padding: 0 var(--side-padding);
2019-09-17 19:28:28 +08:00
}
2019-09-17 19:28:28 +08:00
.ui-logo {
2020-02-27 23:03:15 +08:00
cursor: pointer;
2019-10-20 00:18:55 +08:00
display: flex;
align-items: center;
font-size: 18px;
}
2019-10-20 00:18:55 +08:00
.ui-logo > img {
margin-right: 12px;
height: 32px;
width: 32px;
2019-09-17 19:28:28 +08:00
}
2020-02-27 18:09:23 +08:00
.nav-menu {
2020-11-28 13:34:45 +08:00
padding-left: 36px;
2020-02-27 18:09:23 +08:00
}
2020-09-17 10:43:29 +08:00
2020-03-04 14:34:14 +08:00
.nav-picker {
2021-04-24 01:04:18 +08:00
margin-right: 24px;
2020-11-01 19:35:00 +08:00
}
.nav-picker:last-child {
margin-right: 0;
2019-09-17 19:28:28 +08:00
}
2021-04-24 01:04:18 +08:00
.nav-end {
display: flex;
align-items: center;
}
2019-09-17 19:28:28 +08:00
</style>
2020-11-14 20:38:30 +08:00
<style>
.nav-menu .n-menu-item {
2021-06-02 16:17:13 +08:00
height: calc(var(--header-height) - 1px) !important;
2020-11-14 20:38:30 +08:00
}
</style>