Sidebar
Data-driven sidebar navigation; only the latest asynchronous guard result can select an item.
Import
Use <hui-sidebar> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-sidebar
v-model="selected"
:items="items"
:height="240"
:before-change="beforeChange"
@change="onChange"
@click="onClick"
>
<template #default="{ active }"
><text>当前内容 / Current content: {{ active }}</text></template
> </hui-sidebar
><text>{{ selected }} / {{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { SidebarName, SidebarItem } from '@nscode/handy-uni'
const lastEvent = ref('尚无事件 / No event')
const selected = ref<string | number>('all')
const onChange = (name: SidebarName, context: { item: SidebarItem; oldName?: SidebarName }) => {
lastEvent.value = JSON.stringify({ name, context })
}
const onClick = (item: SidebarItem, context: { index: number }) => {
lastEvent.value = JSON.stringify({ item, context })
}
const items = [
{ name: 'all', label: '全部 / All' },
{ name: 'favorite', label: '收藏 / Favorites', badge: 3 },
{ name: 'disabled', label: '禁用 / Disabled', disabled: true }
]
const beforeChange = async () => true
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
modelValue | SidebarName | — | Controlled current value; accept update:modelValue to change it. |
defaultValue | SidebarName | — | Initial uncontrolled value. |
items | SidebarItem[] | [] | Data items rendered in source order. |
beforeChange | ( name: SidebarName, oldName: SidebarName | undefined ) => boolean | void | Promise<boolean | void> | — | Asynchronous state-change guard; false or rejection cancels. |
width | number | string | 88 | Width; numbers use px and strings preserve their unit. |
height | number | string | — | Height; numbers use px and strings preserve their unit. |
label | string | — | Accessible name or text label. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
update:modelValue | (name: SidebarName) |
change | (name: SidebarName, context: { item: SidebarItem; oldName?: SidebarName }) |
click | (item: SidebarItem, context: { index: number }) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
item | { item, active } |
default | { active } |
Methods
| Method | Signature | Description |
|---|---|---|
scrollToActive | (): Promise<void> | Scroll the active navigation entry into view. |
Types
SidebarName、SidebarItem、SidebarProps、SidebarEmits、SidebarExpose.
Platform notes
Supports H5, WeChat mini-program, App-vue and ordinary Vue 3 Web through shared platform components. Unit tests do not replace device acceptance; verify touch, screen readers, deactivation and native rendering on each target.
Device smoke checklist.