Files
zyt/admin/src/layout/default/components/sidebar/dual-sidebar.vue
T

226 lines
8.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="dual-sidebar" :class="{ 'dual-sidebar--compact': app.isCollapsed }"
@keydown.space="activateLinkWithSpace">
<nav class="dual-primary" :aria-label="app.isCollapsed ? '折叠菜单' : '一级菜单'">
<app-link v-if="settings.showLogo" class="logo dual-brand" to="/"
:aria-label="brandName ? brandName + ',返回首页' : '返回首页'" :title="brandName || '首页'">
<image-contain class="logo-mark" :height="34" :width="34" fit="contain"
:src="app.config.web_logo" alt="" />
</app-link>
<div v-else class="admin-nav-heading dual-brand" aria-hidden="true">
<svg viewBox="0 0 24 24" width="24" height="24" fill="none">
<path d="M4 6.5h10M4 12h16M4 17.5h10" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
<circle cx="19" cy="6.5" r="2" fill="currentColor"/>
</svg>
</div>
<side-menu v-if="app.isCollapsed"
:nodes="roots" :active-key="selection.leaf?.key"
:is-collapsed="true" :classic="false" :theme="settings.sideTheme"
:unique-opened="settings.isUniqueOpened" />
<ul v-else ref="rootList" class="dual-roots">
<li v-for="root in roots" :key="root.key">
<component
:is="root.children.length ? 'button' : AppLink"
:type="root.children.length ? 'button' : undefined"
:to="root.children.length ? undefined : root.target"
:rel="root.external ? 'noopener noreferrer' : undefined"
class="dual-root"
:class="{ 'is-active': activeRoot?.key === root.key }"
:aria-expanded="root.children.length ? activeRoot?.key === root.key : undefined"
:aria-current="!root.children.length && selection.leaf?.key === root.key ? 'page' : undefined"
:aria-label="root.title"
:title="root.title"
@click="chooseRoot(root)"
>
<span class="dual-root-icon" aria-hidden="true">
<icon v-if="root.icon" :name="root.icon" :size="20" />
<template v-else>{{ Array.from(root.title.trim())[0] }}</template>
</span>
<span class="dual-root-label">{{ root.title }}</span>
</component>
</li>
</ul>
</nav>
<nav v-if="!app.isCollapsed && activeRoot?.children.length" class="dual-secondary"
aria-label="二级菜单" :style="{ width: settings.sideWidth + 'px' }">
<div class="dual-heading" :title="panelHeading">{{ panelHeading }}</div>
<div ref="branchList" class="dual-scroll" :key="activeRoot.key">
<dual-branch
:nodes="activeRoot.children"
:leaf-key="selection.leaf?.key"
:ancestor-keys="ancestorKeys"
:route-key="route.fullPath"
:unique="settings.isUniqueOpened"
/>
</div>
</nav>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, ref, watch } from 'vue'
import { useRoute, useRouter, type RouteRecordRaw } from 'vue-router'
import AppLink from '@/components/app-link/index.vue'
import useAppStore from '@/stores/modules/app'
import useSettingStore from '@/stores/modules/setting'
import DualBranch from './dual-branch.vue'
import SideMenu from './menu.vue'
import { buildDualMenu, findDualSelection, firstDualLeaf, type DualNode } from './dual-model'
const props = defineProps<{ routes: RouteRecordRaw[] }>()
const app = useAppStore()
const settings = useSettingStore()
const router = useRouter()
const route = useRoute()
const roots = computed(() => buildDualMenu(props.routes))
const selection = computed(() => findDualSelection(roots.value, route))
const ancestorKeys = computed(() => selection.value.ancestors.map(node => node.key))
// Only an external-only directory needs a local selection: it has no internal
// destination. Never open an external site while selecting a group or syncing a route.
const externalGroup = ref<string>()
watch([() => route.fullPath, roots], () => { externalGroup.value = undefined })
const activeRoot = computed(() => externalGroup.value
? roots.value.find(node => node.key === externalGroup.value)
: selection.value.root)
const brandName = computed(() => String(app.config.web_name || ''))
const panelHeading = computed(() =>
(settings.showLogo && brandName.value) || activeRoot.value?.title || ''
)
const rootList = ref<HTMLElement>()
const branchList = ref<HTMLElement>()
function revealActiveItems() {
for (const list of [rootList.value, branchList.value]) {
const item = list?.querySelector('.is-active')
if (!list || !item) continue
const row = item.getBoundingClientRect()
const box = list.getBoundingClientRect()
if (row.top < box.top) list.scrollTop += row.top - box.top
else if (row.bottom > box.bottom) list.scrollTop += row.bottom - box.bottom
}
}
onMounted(revealActiveItems)
watch([activeRoot, selection, () => app.isCollapsed], revealActiveItems, { flush: 'post' })
function chooseRoot(node: DualNode) {
if (!node.children.length) {
if (!node.external) externalGroup.value = undefined
return // The AppLink handles normal navigation and modifier-clicks.
}
const leaf = firstDualLeaf(node)
externalGroup.value = leaf ? undefined : node.key
if (leaf) void router.push(leaf.target)
}
function activateLinkWithSpace(event: KeyboardEvent) {
// Buttons already handle both keys natively. Extend anchors with Space
// without intercepting their Enter or modified pointer navigation.
if (!(event.target instanceof HTMLAnchorElement)) return
event.preventDefault()
if (!event.repeat) event.target.click()
}
</script>
<style scoped>
.dual-sidebar {
--rail-width: 80px;
display: flex;
flex: 1 1 0;
min-height: 0;
background: var(--zyt-nav-bg);
color: var(--zyt-nav-text);
}
.dual-sidebar--compact { --rail-width: 64px; }
.dual-primary, .dual-secondary {
display: flex;
flex: none;
flex-direction: column;
min-height: 0;
}
.dual-primary { width: var(--rail-width); }
.dual-sidebar .dual-primary > .dual-brand {
flex: none;
justify-content: center;
width: 100%;
height: var(--navbar-height);
padding: 0;
}
.dual-roots, .dual-scroll {
flex: 1;
min-height: 0;
overflow-x: hidden;
overflow-y: auto;
overscroll-behavior: contain;
scrollbar-width: thin;
}
.dual-roots {
display: flex;
flex-direction: column;
gap: 6px;
margin: 0;
padding: 4px 8px 12px;
list-style: none;
scrollbar-width: none; /* Keep icon centers aligned with the logo on scroll. */
}
.dual-roots::-webkit-scrollbar { display: none; }
.dual-root {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 5px;
width: 100%;
height: 60px;
padding: 0 2px;
border: 0;
border-radius: 5px;
background: transparent;
color: var(--zyt-nav-text);
text-align: center;
cursor: pointer;
}
.dual-root:hover { background: var(--zyt-nav-hover); }
.dual-root.is-active {
background: var(--zyt-nav-active);
color: var(--zyt-nav-selected);
}
.dual-root-icon {
display: inline-flex;
align-items: center;
justify-content: center;
width: 20px;
height: 20px;
color: var(--zyt-nav-icon);
font-size: 16px;
line-height: 1;
}
.dual-root.is-active .dual-root-icon { color: inherit; }
.dual-root-label, .dual-heading {
display: block;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.dual-root-label { width: 100%; font-size: 12px; line-height: 16px; }
.dual-secondary {
box-sizing: border-box;
border-inline-start: 1px solid var(--zyt-card-border);
}
.dual-heading {
flex: none;
padding-inline: 20px;
color: var(--zyt-nav-title);
font-size: 17px;
font-weight: 600;
line-height: var(--navbar-height);
}
.dual-scroll { padding: 4px 8px 12px; }
.dual-root:focus-visible, .dual-brand:focus-visible {
outline: 2px solid var(--zyt-nav-selected);
outline-offset: -2px;
}
</style>