DashboardSidebarPRO
用法
DashboardSidebar 组件用于显示侧边栏。它的状态(大小、折叠状态等)将根据您提供给 DashboardGroup 组件的 storage
和 storage-key
prop 进行保存。
在 DashboardGroup 组件的默认插槽中使用它
<template>
<UDashboardGroup>
<UDashboardSidebar />
<slot />
</UDashboardGroup>
</template>
使用 left
、default
和 right
插槽来自定义侧边栏,使用 body
或 content
插槽来自定义侧边栏菜单。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const items: NavigationMenuItem[][] = [[{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox',
badge: '4'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}, {
label: 'Settings',
icon: 'i-lucide-settings',
defaultOpen: true,
children: [{
label: 'General'
}, {
label: 'Members'
}, {
label: 'Notifications'
}]
}], [{
label: 'Feedback',
icon: 'i-lucide-message-circle',
to: 'https://github.com/nuxt-ui-pro/dashboard',
target: '_blank'
}, {
label: 'Help & Support',
icon: 'i-lucide-info',
to: 'https://github.com/nuxt/ui-pro',
target: '_blank'
}]]
</script>
<template>
<UDashboardSidebar collapsible resizable :ui="{ footer: 'border-t border-default' }">
<template #header="{ collapsed }">
<LogoPro :collapsed="collapsed" class="h-5 w-auto shrink-0" />
</template>
<template #default="{ collapsed }">
<UButton
:label="collapsed ? undefined : 'Search...'"
icon="i-lucide-search"
color="neutral"
variant="outline"
block
:square="collapsed"
>
<template v-if="!collapsed" #trailing>
<div class="flex items-center gap-0.5 ms-auto">
<UKbd value="meta" variant="subtle" />
<UKbd value="K" variant="subtle" />
</div>
</template>
</UButton>
<UNavigationMenu
:collapsed="collapsed"
:items="items[0]"
orientation="vertical"
/>
<UNavigationMenu
:collapsed="collapsed"
:items="items[1]"
orientation="vertical"
class="mt-auto"
/>
</template>
<template #footer="{ collapsed }">
<UButton
:avatar="{
src: 'https://github.com/benjamincanac.png'
}"
:label="collapsed ? undefined : 'Benjamin'"
color="neutral"
variant="ghost"
class="w-full"
:block="collapsed"
/>
</template>
</UDashboardSidebar>
</template>
可调整大小
使用 resizable
prop 使侧边栏可调整大小。
<template>
<UDashboardSidebar resizable>
<Placeholder class="h-96" />
</UDashboardSidebar>
</template>
Collapsible
使用 collapsible
prop 使侧边栏在拖动到屏幕边缘附近时可折叠。
DashboardSidebarCollapse
组件将无效。<template>
<UDashboardSidebar resizable collapsible>
<Placeholder class="h-96" />
</UDashboardSidebar>
</template>
大小
使用 min-size
、max-size
、default-size
和 collapsed-size
prop 自定义侧边栏的大小。
<template>
<UDashboardSidebar
resizable
collapsible
:min-size="22"
:default-size="35"
:max-size="40"
:collapsed-size="0"
>
<Placeholder class="h-96" />
</UDashboardSidebar>
</template>
collapsed-size
prop 默认设置为 0
,但侧边栏有一个 min-w-16
以确保其可见。侧边
使用 side
prop 改变侧边栏的位置。默认为 left
。
<template>
<UDashboardSidebar side="right" resizable collapsible>
<Placeholder class="h-96" />
</UDashboardSidebar>
</template>
模式
使用 mode
prop 改变侧边栏菜单的模式。默认为 slideover
。
使用 body
插槽填充菜单主体(在头部下方),或使用 content
插槽填充整个菜单。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
defineProps<{
mode: 'drawer' | 'slideover' | 'modal'
}>()
const items: NavigationMenuItem[] = [{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}]
</script>
<template>
<UDashboardGroup>
<UDashboardSidebar :mode="mode">
<template #header>
<LogoPro class="h-5 w-auto" />
</template>
<UNavigationMenu
:items="items"
orientation="vertical"
/>
</UDashboardSidebar>
<UDashboardPanel>
<template #header>
<UDashboardNavbar title="Dashboard" />
</template>
</UDashboardPanel>
</UDashboardGroup>
</template>
切换
使用 toggle
prop 自定义在移动设备上显示的 DashboardSidebarToggle 组件。
您可以传递 Button 组件的任何属性来自定义它。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const items: NavigationMenuItem[] = [{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}]
</script>
<template>
<UDashboardGroup>
<UDashboardSidebar
open
:toggle="{
color: 'primary',
variant: 'subtle',
class: 'rounded-full'
}"
>
<template #header>
<LogoPro class="h-5 w-auto" />
</template>
<UNavigationMenu
:items="items"
orientation="vertical"
/>
</UDashboardSidebar>
<UDashboardPanel>
<template #header>
<UDashboardNavbar title="Dashboard" />
</template>
</UDashboardPanel>
</UDashboardGroup>
</template>
切换侧边
使用 toggle-side
prop 改变切换按钮的位置。默认为 left
。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const items: NavigationMenuItem[] = [{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}]
</script>
<template>
<UDashboardGroup>
<UDashboardSidebar
open
toggle-side="right"
>
<template #header>
<LogoPro class="h-5 w-auto" />
</template>
<UNavigationMenu
:items="items"
orientation="vertical"
/>
</UDashboardSidebar>
<UDashboardPanel>
<template #header>
<UDashboardNavbar title="Dashboard" />
</template>
</UDashboardPanel>
</UDashboardGroup>
</template>
示例
控制打开状态
您可以使用 open
prop 或 v-model:open
指令控制打开状态。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const items: NavigationMenuItem[] = [{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}]
const open = ref(true)
defineShortcuts({
o: () => open.value = !open.value
})
</script>
<template>
<UDashboardSidebar v-model:open="open">
<template #header>
<LogoPro class="h-5 w-auto" />
</template>
<UNavigationMenu
:items="items"
orientation="vertical"
/>
</UDashboardSidebar>
</template>
defineShortcuts
,您可以通过按下 O 键切换 DashboardSidebar 的打开状态。控制折叠状态
您可以使用 collapsed
prop 或 v-model:collapsed
指令控制折叠状态。
<script setup lang="ts">
import type { NavigationMenuItem } from '@nuxt/ui'
const items: NavigationMenuItem[] = [{
label: 'Home',
icon: 'i-lucide-house',
active: true
}, {
label: 'Inbox',
icon: 'i-lucide-inbox'
}, {
label: 'Contacts',
icon: 'i-lucide-users'
}]
const collapsed = ref(false)
defineShortcuts({
c: () => collapsed.value = !collapsed.value
})
</script>
<template>
<UDashboardSidebar v-model:collapsed="collapsed" collapsible>
<template #header>
<LogoPro class="h-5 w-auto" :collapsed="collapsed" />
</template>
<UNavigationMenu
:collapsed="collapsed"
:items="items"
orientation="vertical"
/>
</UDashboardSidebar>
</template>
defineShortcuts
,您可以通过按下 C 键切换 DashboardSidebar 的折叠状态。API
属性
属性 | 默认值 | 类型 |
---|---|---|
open |
| |
collapsed |
| |
mode |
|
侧边栏菜单的模式。 |
menu |
侧边栏菜单组件的 prop。
| |
切换 |
|
自定义用于打开侧边栏的切换按钮。 |
切换侧边 |
|
渲染切换按钮的侧边。 |
collapsible |
|
是否允许用户折叠面板。 |
ID |
|
面板的 ID。 |
side |
|
渲染面板的侧边。 |
minSize |
|
面板的最小尺寸。 |
maxSize |
|
面板的最大尺寸。 |
defaultSize |
|
面板的默认尺寸。 |
resizable |
|
是否允许用户调整面板大小。 |
collapsedSize |
|
面板折叠时的尺寸。 |
ui |
|
插槽
插槽 | 类型 |
---|---|
头部 |
|
默认 |
|
底部 |
|
切换 |
|
内容 |
|
调整大小把手 |
|
主题
export default defineAppConfig({
uiPro: {
dashboardSidebar: {
slots: {
root: 'relative hidden lg:flex flex-col min-h-svh min-w-16 w-(--width) shrink-0',
header: 'h-(--ui-header-height) shrink-0 flex items-center gap-1.5 px-4',
body: 'flex flex-col gap-4 flex-1 overflow-y-auto px-4 py-2',
footer: 'shrink-0 flex items-center gap-1.5 px-4 py-2',
toggle: '',
handle: '',
content: 'lg:hidden',
overlay: 'lg:hidden'
},
variants: {
menu: {
true: {
header: 'sm:px-6',
body: 'sm:px-6',
footer: 'sm:px-6'
}
},
side: {
left: {
root: 'border-r border-default'
},
right: {
root: ''
}
},
toggleSide: {
left: {
toggle: ''
},
right: {
toggle: 'ms-auto'
}
}
}
}
}
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'
export default defineConfig({
plugins: [
vue(),
ui({
uiPro: {
dashboardSidebar: {
slots: {
root: 'relative hidden lg:flex flex-col min-h-svh min-w-16 w-(--width) shrink-0',
header: 'h-(--ui-header-height) shrink-0 flex items-center gap-1.5 px-4',
body: 'flex flex-col gap-4 flex-1 overflow-y-auto px-4 py-2',
footer: 'shrink-0 flex items-center gap-1.5 px-4 py-2',
toggle: '',
handle: '',
content: 'lg:hidden',
overlay: 'lg:hidden'
},
variants: {
menu: {
true: {
header: 'sm:px-6',
body: 'sm:px-6',
footer: 'sm:px-6'
}
},
side: {
left: {
root: 'border-r border-default'
},
right: {
root: ''
}
},
toggleSide: {
left: {
toggle: ''
},
right: {
toggle: 'ms-auto'
}
}
}
}
}
})
]
})
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import uiPro from '@nuxt/ui-pro/vite'
export default defineConfig({
plugins: [
vue(),
uiPro({
uiPro: {
dashboardSidebar: {
slots: {
root: 'relative hidden lg:flex flex-col min-h-svh min-w-16 w-(--width) shrink-0',
header: 'h-(--ui-header-height) shrink-0 flex items-center gap-1.5 px-4',
body: 'flex flex-col gap-4 flex-1 overflow-y-auto px-4 py-2',
footer: 'shrink-0 flex items-center gap-1.5 px-4 py-2',
toggle: '',
handle: '',
content: 'lg:hidden',
overlay: 'lg:hidden'
},
variants: {
menu: {
true: {
header: 'sm:px-6',
body: 'sm:px-6',
footer: 'sm:px-6'
}
},
side: {
left: {
root: 'border-r border-default'
},
right: {
root: ''
}
},
toggleSide: {
left: {
toggle: ''
},
right: {
toggle: 'ms-auto'
}
}
}
}
}
})
]
})