Skip to content

Sidebar 侧边栏 ​

使用侧边栏组织分组导航。

基础用法 ​

每项使用唯一的 index;通过 children 添加子菜单,监听 select 处理导航。

查看代码收起代码
vue
<script setup lang="ts">
import type { SidebarItem } from '@codex-proxy/ui/sidebar'
import { ZIconButton } from '@codex-proxy/ui/icon-button'
import { ZSidebar } from '@codex-proxy/ui/sidebar'
import { Box, ChartNoAxesColumn, FileText, LayoutDashboard, PanelLeftClose, PanelLeftOpen, Puzzle, Settings } from '@lucide/vue'
import { computed, shallowRef } from 'vue'

const collapse = shallowRef(false)
const activeIndex = shallowRef('overview')
const lastEvent = shallowRef('')
const items: SidebarItem[] = [
  { index: 'overview', label: '概览', icon: LayoutDashboard },
  { index: 'usage', label: '使用统计', icon: ChartNoAxesColumn },
  {
    index: 'plugins',
    label: '插件',
    icon: Puzzle,
    children: [
      { index: 'installed', label: '已安装', icon: Box },
      { index: 'extensions', label: '扩展页', icon: FileText },
    ],
  },
  { index: 'settings', label: '系统设置', icon: Settings, disabled: true },
]
const activeLabel = computed(() => items.flatMap(item => [item, ...item.children ?? []])
  .find(item => item.index === activeIndex.value)
  ?.label ?? '')

function handleSelect(index: string, indexPath: string[]) {
  activeIndex.value = index
  lastEvent.value = `select: ${indexPath.join(' / ')}`
}
</script>

<template>
  <div class="flex h-130 min-w-0 overflow-hidden rounded-z-lg bg-z-bg-layout">
    <ZSidebar
      :items="items"
      :collapse="collapse"
      :default-active="activeIndex"
      :default-openeds="['plugins']"
      navigation-label="示例导航"
      @select="handleSelect"
      @open="index => lastEvent = `open: ${index}`"
      @close="index => lastEvent = `close: ${index}`"
    >
      <template #brand-icon>
        <span class="grid size-11 place-items-center rounded-z bg-z-primary-container text-z-primary-on-container"><Box class="size-6" /></span>
      </template>
      <template #brand>
        <strong class="text-z-lg font-heavy">工作台</strong>
      </template>
      <template #footer>
        <div class="flex justify-end rounded-z bg-z-fill-quaternary p-1">
          <ZIconButton :aria-label="collapse ? '展开侧边栏' : '收起侧边栏'" @click="collapse = !collapse">
            <PanelLeftOpen v-if="collapse" />
            <PanelLeftClose v-else />
          </ZIconButton>
        </div>
      </template>
    </ZSidebar>
    <div class="grid min-w-0 flex-1 content-start gap-3 p-5">
      <strong class="text-z-lg">{{ activeLabel }}</strong>
      <output aria-label="菜单事件" class="break-words font-mono text-z-xs text-z-text-secondary">{{ lastEvent }}</output>
    </div>
  </div>
</template>

收起时点击分组会触发该分组的 select;展开时点击分组可显示子菜单。

Sidebar API ​

属性 ​

属性说明类型默认值
items两级菜单数据,顶层为 SidebarItem,子项为 SidebarChildItemSidebarItem[]必填
collapse是否收起侧边栏booleanfalse
width展开宽度,支持 CSS 长度string'251px'
collapse-width收起宽度,支持 CSS 长度string'88px'
default-active当前选中项的 indexstring''
default-openeds展开的分组 indexstring[][]
navigation-label内部 nav 的可访问名称string—

事件 ​

事件说明
select用户选择菜单项,参数为 index: string、indexPath: string[]
open分组展开,参数为 index: string、indexPath: string[]
close分组收起,参数为 index: string、indexPath: string[]

插槽 ​

插槽说明
brand-icon收起后保留的品牌图标
brand展开时显示的品牌文字
footer固定底部操作区

各插槽接收 { collapse: boolean },底部按钮的业务状态与动作由调用方提供

实例 ​

方法说明
open(index: string)展开指定分组
close(index: string)收起指定分组
updateActiveIndex(index: string)更新选中项,不触发 select