Layout 布局
搭页面整体框架用的容器:HuiLayout 是外框,里面放顶栏 HuiHeader、侧边栏 HuiSider、内容区 HuiContent、底栏 HuiFooter。侧边栏可以折叠。
引入
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
基础用法
上中下三段:顶栏和底栏高度固定,内容区占满剩下的高度。HuiLayout 要有确定的高度,内容区才知道剩下多少。
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiHeader>顶栏</HuiHeader>
<HuiContent>内容区</HuiContent>
<HuiFooter>底栏</HuiFooter>
</HuiLayout>
</template>
带侧边栏
HuiLayout 里放了 HuiSider 时,不用写 direction,自动横着排:侧边栏和内容区左右并列,侧边栏的高度和 HuiLayout 一样。
<template>
<HuiLayout style="height: 360px">
<HuiSider>侧边栏</HuiSider>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</template>
顶栏加侧边栏
HuiLayout 可以嵌套。外层竖着排顶栏和内层 HuiLayout,内层放侧边栏和内容区,自动横排。
<template>
<HuiLayout style="height: 400px">
<HuiHeader>顶栏</HuiHeader>
<HuiLayout>
<HuiSider>侧边栏</HuiSider>
<HuiLayout>
<HuiContent>内容区</HuiContent>
<HuiFooter>底栏</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</template>
最里面那个 HuiLayout 没有 HuiSider,所以还是竖着排内容区和底栏。
顶栏也可以放在内容区那一侧,侧边栏从上到下占满整个高度:
<template>
<HuiLayout style="height: 400px">
<HuiSider>侧边栏</HuiSider>
<HuiLayout>
<HuiHeader>顶栏</HuiHeader>
<HuiContent>内容区</HuiContent>
<HuiFooter>底栏</HuiFooter>
</HuiLayout>
</HuiLayout>
</template>
排列方向
direction 可选 horizontal(横排)、vertical(竖排)。不写时按里面有没有 HuiSider 自动决定;写了就以写的为准。下面四个布局分别是:
<template>
<div class="layouts">
<div>
<p class="label">不写,没有 HuiSider:竖排</p>
<HuiLayout class="box">
<HuiHeader :height="40">顶栏</HuiHeader>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">不写,有 HuiSider:横排</p>
<HuiLayout class="box">
<HuiSider :width="100">侧边栏</HuiSider>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">direction="horizontal":没有 HuiSider 也横排</p>
<HuiLayout direction="horizontal" class="box">
<HuiContent>内容区一</HuiContent>
<HuiContent class="divided">内容区二</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">direction="vertical"</p>
<HuiLayout direction="vertical" class="box">
<HuiContent>内容区</HuiContent>
<HuiFooter :height="40">底栏</HuiFooter>
</HuiLayout>
</div>
</div>
</template>
<style scoped>
.layouts {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
gap: var(--hui-spacing-4);
}
.label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
.box {
height: 160px;
border: 1px solid var(--hui-border-color-light);
}
.divided {
border-left: 1px solid var(--hui-border-color-light);
}
</style>
可折叠侧边栏
collapsible 在侧边栏底部加一个折叠触发条,点了在 width 和 collapsedWidth 之间切换。用 v-model:collapsed 绑定折叠状态,在外面也能控制折叠。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiButton } from '@nscode/handy-ui'
const collapsed = ref(false)
</script>
<template>
<HuiLayout style="height: 400px">
<HuiSider v-model:collapsed="collapsed" collapsible :width="220" :collapsed-width="64">
<span v-if="!collapsed">菜单</span>
</HuiSider>
<HuiLayout>
<HuiHeader>
<HuiButton @click="collapsed = !collapsed">{{ collapsed ? '展开' : '收起' }}</HuiButton>
</HuiHeader>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
不绑定 collapsed 也能折叠,状态由组件自己记着。侧边栏的内容区可以滚动,触发条固定在最下面,不会盖住内容。
<template>
<HuiLayout style="height: 300px">
<HuiSider collapsible :width="160">
<p v-for="i in 20" :key="i" style="margin: 0; padding: 8px 16px">第 {{ i }} 项</p>
</HuiSider>
<HuiContent>侧边栏里有 20 项,超出的部分在侧边栏里滚动,触发条一直在最下面。</HuiContent>
</HuiLayout>
</template>
折叠后的宽度
width 是展开时的宽度,collapsed-width 是折叠后的宽度,都可以是数字(px)或任意 CSS 长度,默认 200 和 64。下面三个侧边栏都可以点底部的触发条折叠:
<template>
<HuiSpace direction="vertical" fill>
<HuiLayout style="height: 120px">
<HuiSider collapsible>默认 200 / 64</HuiSider>
<HuiContent>width 200,collapsed-width 64</HuiContent>
</HuiLayout>
<HuiLayout style="height: 120px">
<HuiSider collapsible width="30%" collapsed-width="10%">30% / 10%</HuiSider>
<HuiContent>width "30%",collapsed-width "10%"</HuiContent>
</HuiLayout>
<HuiLayout style="height: 120px">
<HuiSider collapsible :width="160" :collapsed-width="120">160 / 120</HuiSider>
<HuiContent>width 160,collapsed-width 120</HuiContent>
</HuiLayout>
</HuiSpace>
</template>
折叠后完全隐藏
collapsed-width 设成 0 时,折叠后侧边栏完全隐藏,触发条变成一个小按钮,浮在侧边栏外侧(距顶部 64px),点它可以再展开。
<template>
<HuiLayout style="height: 360px">
<HuiSider collapsible :collapsed-width="0">侧边栏</HuiSider>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</template>
不显示触发条
collapsible 为 false(默认)时没有触发条,只能通过 collapsed 折叠,适合用顶栏上的按钮控制。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiContent, HuiHeader, HuiLayout, HuiSider } from '@nscode/handy-ui'
const collapsed = ref(false)
</script>
<template>
<HuiLayout style="height: 300px">
<HuiHeader>
<HuiButton
:icon="collapsed ? 'double-right' : 'double-left'"
:aria-label="collapsed ? '展开侧边栏' : '收起侧边栏'"
@click="collapsed = !collapsed"
/>
</HuiHeader>
<HuiLayout>
<HuiSider :collapsed="collapsed" :width="180" :collapsed-width="0">侧边栏</HuiSider>
<HuiContent>点顶栏上的按钮折叠、展开侧边栏</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
右侧侧边栏
placement="right" 时侧边栏的边框在左边,触发条的箭头方向也跟着反过来。侧边栏实际显示在左边还是右边,取决于它在模板里的位置,要把它写在 HuiContent 后面。
<template>
<HuiLayout style="height: 360px">
<HuiContent>内容区</HuiContent>
<HuiSider placement="right" collapsible :width="240">右侧面板</HuiSider>
</HuiLayout>
</template>
placement 的两个值可以同时用,内容区两侧各一个侧边栏,各自折叠:
<template>
<HuiLayout style="height: 300px">
<HuiSider placement="left" collapsible :width="160">左侧(left)</HuiSider>
<HuiContent>内容区</HuiContent>
<HuiSider placement="right" collapsible :width="160">右侧(right)</HuiSider>
</HuiLayout>
</template>
拖动改侧栏宽度
resizable 开启靠内容区一侧的拖动条:左侧栏拖右边,右侧栏拖左边。v-model:width 收到 px 数字;未传width时组件自己保存拖过的宽度(仅监听更新也一样),折叠后再展开仍保留;传入width即受控,显式undefined按200px显示。
min-width 默认是 collapsed-width,max-width 默认是父元素宽度的一半;可传数字、px、%、vw、vh,百分比相对父元素。最大值小于最小值时按最小值。把手可 Tab 聚焦,方向键每次 10px,Shift+方向键每次 1px,Home/End 到最小/最大;拖动中 Esc 恢复本次按下时的宽度。
下面在 resizeEnd 后记住宽度。读取和写入 localStorage 都包了 try/catch,禁用本地存储的浏览器也能正常使用。
<script setup lang="ts">
import { onMounted, ref } from 'vue'
import { HuiContent, HuiLayout, HuiSider, HuiSpace } from '@nscode/handy-ui'
const width = ref(220)
const collapsed = ref(false)
const result = ref('拖动侧栏,或用键盘调整。')
const storageKey = 'hui-layout-demo-sider-width'
onMounted(() => {
try {
const saved = Number(localStorage.getItem(storageKey))
if (Number.isFinite(saved) && saved >= 120 && saved <= 360) width.value = saved
} catch {
/* 本地存储不可用时使用默认宽度。 */
}
})
const saveWidth = (value: number) => {
result.value = `结束宽度:${Math.round(value)}px`
try {
localStorage.setItem(storageKey, String(value))
} catch {
/* 存储失败不影响调整。 */
}
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiLayout style="height: 240px; border: 1px solid var(--hui-border-color)">
<HuiSider
v-model:width="width"
v-model:collapsed="collapsed"
resizable
collapsible
:min-width="120"
:max-width="360"
@resize-end="saveWidth"
>
<p style="padding: 16px">{{ collapsed ? '菜单' : '拖动右边缘;Esc 可取消本次调整。' }}</p>
</HuiSider>
<HuiContent><p style="padding: 16px">折叠时没有拖动条,再展开恢复展开宽度。</p></HuiContent>
</HuiLayout>
<span>绑定宽度:{{ Math.round(width) }}px;{{ result }}</span>
</HuiSpace>
</template>
左右侧栏和百分比范围
<script setup lang="ts">
import { ref } from 'vue'
import { HuiContent, HuiLayout, HuiSider, HuiSpace } from '@nscode/handy-ui'
const left = ref(180)
const right = ref(180)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiLayout style="height: 220px; border: 1px solid var(--hui-border-color)">
<HuiSider v-model:width="left" resizable :min-width="100" max-width="35%"
><p style="padding: 16px">左侧拖右边</p></HuiSider
>
<HuiContent><p style="padding: 16px">内容区</p></HuiContent>
<HuiSider v-model:width="right" placement="right" resizable min-width="15%" max-width="40%"
><p style="padding: 16px">右侧拖左边</p></HuiSider
>
</HuiLayout>
<span>左:{{ Math.round(left) }}px;右:{{ Math.round(right) }}px</span>
</HuiSpace>
</template>
运行时开关
关闭 resizable、折叠、切换位置、更改限制或外部改宽度都会结束旧拖动,清理页面拖动状态,并保留新配置。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiContent, HuiLayout, HuiSider, HuiSpace, HuiSwitch } from '@nscode/handy-ui'
const resizable = ref(true)
const width = ref(220)
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace><span>允许调整</span><HuiSwitch v-model="resizable" /></HuiSpace>
<HuiLayout style="height: 200px; border: 1px solid var(--hui-border-color)">
<HuiSider :resizable="resizable" v-model:width="width"
><p style="padding: 16px">默认最小64px,最大为父容器一半。</p></HuiSider
>
<HuiContent><p style="padding: 16px">关闭后不显示拖动条,已调整的宽度仍保留。</p></HuiContent>
</HuiLayout>
</HuiSpace>
</template>
自定义触发条
trigger 插槽替换触发条里的内容,作用域参数 collapsed 是当前是否折叠。整个触发条仍是一个按钮,点击和键盘操作都由组件处理,插槽里不用再绑点击事件。
<template>
<HuiLayout style="height: 360px">
<HuiSider collapsible>
侧边栏
<template #trigger="{ collapsed }">
{{ collapsed ? '展开' : '收起' }}
</template>
</HuiSider>
<HuiContent>内容区</HuiContent>
</HuiLayout>
</template>
配合菜单
侧边栏里放 HuiMenu,把折叠状态同时传给菜单的 collapse,折叠后菜单只显示图标。collapse 事件在点触发条切换后触发,参数是切换后是否折叠。
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiContent,
HuiHeader,
HuiLayout,
HuiMenu,
HuiMenuItem,
HuiSider,
HuiSubMenu
} from '@nscode/handy-ui'
const collapsed = ref(false)
const log = ref('')
const handleCollapse = (value: boolean) => {
log.value = value ? '侧边栏收起了' : '侧边栏展开了'
}
</script>
<template>
<HuiLayout style="height: 400px">
<HuiHeader>后台管理系统</HuiHeader>
<HuiLayout>
<HuiSider
v-model:collapsed="collapsed"
collapsible
:width="200"
:collapsed-width="64"
@collapse="handleCollapse"
>
<HuiMenu :collapse="collapsed" default-active="1">
<HuiMenuItem index="1" icon="home">工作台</HuiMenuItem>
<HuiSubMenu index="2" title="系统管理" icon="setting">
<HuiMenuItem index="2-1" icon="user">用户管理</HuiMenuItem>
<HuiMenuItem index="2-2" icon="users">角色管理</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3" icon="bell">消息中心</HuiMenuItem>
</HuiMenu>
</HuiSider>
<HuiContent>{{ log || '点侧边栏底部的触发条试试' }}</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
自定义高度和颜色
HuiHeader、HuiFooter 的 height 可以是数字(px)或任意 CSS 长度。颜色通过 CSS 变量改,见下面的“CSS 变量”。
<template>
<HuiLayout
style="
height: 360px;
--hui-layout-header-bg: var(--hui-color-primary);
--hui-layout-sider-bg: var(--hui-fill-color-light);
"
>
<HuiHeader :height="48" style="color: var(--hui-color-white)">顶栏</HuiHeader>
<HuiLayout>
<HuiSider :width="180">侧边栏</HuiSider>
<HuiContent>内容区</HuiContent>
</HuiLayout>
<HuiFooter height="40px">底栏</HuiFooter>
</HuiLayout>
</template>
触发条、边框、内容区的颜色也都能改。用主题变量配色时,暗色主题下颜色也会跟着变:
<template>
<HuiLayout
style="
height: 300px;
--hui-layout-header-border-color: var(--hui-color-primary);
--hui-layout-sider-bg: var(--hui-color-primary-bg);
--hui-layout-sider-border-color: var(--hui-color-primary-bg-hover);
--hui-layout-sider-trigger-bg: var(--hui-color-primary-bg-hover);
--hui-layout-sider-trigger-bg-hover: var(--hui-color-primary-lighter);
--hui-layout-content-bg: var(--hui-bg-color);
--hui-layout-footer-bg: var(--hui-fill-color-light);
--hui-layout-footer-border-color: var(--hui-border-color);
"
>
<HuiHeader :height="48">主色下边框的顶栏</HuiHeader>
<HuiLayout>
<HuiSider collapsible :width="160">浅蓝侧边栏</HuiSider>
<HuiLayout>
<HuiContent>白色内容区</HuiContent>
<HuiFooter :height="48">浅灰底栏</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</template>
折叠状态与父回填
未传collapsed或仅监听更新时,从false保存折叠状态;传入属性(含undefined,按false显示)即受控。父拒绝时宽度、触发条ARIA、插槽、Layout登记和伸缩可用状态均保父值。动态移除属性恢复原内部状态,切换模式在开发环境警告。width的独立受控规则见下一例。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiLayout, HuiSider, HuiContent, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('尚无请求')
const q1Model = ref(false)
</script>
<template>
<HuiSpace class="sider-q1-demo" direction="vertical" fill>
<HuiButton class="q1-redraw" @click="q1Redraw++">父重绘 {{ q1Redraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
"
>
<div class="q1-local">
<p>未传属性,自行保存</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible><p>侧栏</p></HuiSider
><HuiContent>内容</HuiContent></HuiLayout
>
</div>
<div class="q1-listener">
<p>仅监听更新,仍自行保存</p>
<HuiLayout style="height: 120px"
><HuiSider
:width="160"
:collapsed-width="56"
collapsible
@update:collapsed="q1Last = String($event)"
><p>侧栏</p></HuiSider
><HuiContent>内容</HuiContent></HuiLayout
>
</div>
<div class="q1-empty">
<p>显式undefined,保持展开</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible :collapsed="undefined"
><p>侧栏</p></HuiSider
><HuiContent>内容</HuiContent></HuiLayout
>
</div>
<div class="q1-fixed">
<p>固定受控,父拒绝更新</p>
<HuiLayout style="height: 120px"
><HuiSider
:width="160"
:collapsed-width="56"
collapsible
:collapsed="false"
@update:collapsed="q1Last = String($event)"
><p>侧栏</p></HuiSider
><HuiContent>内容</HuiContent></HuiLayout
>
</div>
<div class="q1-bound">
<p>双向绑定</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible v-model:collapsed="q1Model"
><p>侧栏</p></HuiSider
><HuiContent>内容</HuiContent></HuiLayout
>
</div>
</div>
<p class="q1-notification">绑定值:{{ q1Model }};最近请求:{{ q1Last }}</p>
</HuiSpace>
</template>
侧栏宽度自持与受控回填
未传width或仅监听更新时,从200px自行保存展开宽度。传入width即受控,固定值不监听也不会自行改变,显式undefined按200px显示。像素更新请求由父组件接受、改写或拒绝;宽度、flex、ARIA和resizeEnd都使用实际状态。移除属性恢复原内部宽度,切换受控模式在开发环境警告并结束旧拖动。Esc请求恢复按下时的像素宽度;受控父组件可接受请求或保留原CSS写法。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiContent, HuiLayout, HuiSider, HuiSpace } from '@nscode/handy-ui'
const widthRedraw = ref(0)
const widthLast = ref('尚无请求')
const widthEnd = ref('尚未结束')
const widthModel = ref(200)
</script>
<template>
<HuiSpace class="sider-width-q1-demo" direction="vertical" fill>
<HuiButton class="width-redraw" @click="widthRedraw++">父重绘 {{ widthRedraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
gap: 16px;
"
>
<div class="width-local">
<p>未传宽度,自行保存</p>
<HuiLayout style="height: 90px">
<HuiSider resizable :min-width="120" :max-width="320"><p>左侧栏</p></HuiSider>
<HuiContent>内容</HuiContent>
</HuiLayout>
</div>
<div class="width-listener">
<p>仅监听,右侧栏自行保存</p>
<HuiLayout style="height: 90px">
<HuiContent>内容</HuiContent>
<HuiSider
placement="right"
resizable
:min-width="120"
:max-width="320"
@update:width="widthLast = String($event)"
@resize-end="widthEnd = String($event)"
><p>右侧栏</p></HuiSider
>
</HuiLayout>
</div>
<div class="width-empty">
<p>显式undefined,受控为200px</p>
<HuiLayout style="height: 90px">
<HuiSider :width="undefined" resizable :min-width="120" :max-width="320"
><p>空值受控</p></HuiSider
>
<HuiContent>内容</HuiContent>
</HuiLayout>
</div>
<div class="width-fixed">
<p>固定220px,父拒绝更新</p>
<HuiLayout style="height: 90px">
<HuiSider :width="220" resizable :min-width="120" :max-width="320"
><p>固定受控</p></HuiSider
>
<HuiContent>内容</HuiContent>
</HuiLayout>
</div>
<div class="width-bound">
<p>双向绑定宽度</p>
<HuiLayout style="height: 90px">
<HuiSider v-model:width="widthModel" resizable :min-width="120" :max-width="320"
><p>{{ widthModel }}px</p></HuiSider
>
<HuiContent>内容</HuiContent>
</HuiLayout>
</div>
</div>
<p class="width-status">
绑定宽度:{{ widthModel }};右侧请求:{{ widthLast }};实际结束:{{ widthEnd }}
</p>
</HuiSpace>
</template>
响应式侧栏
配置 breakpoint 按视口自动请求折叠:sm=640、md=768、lg=1024、xl=1280、2xl=1536px,小于阈值收起,等于或大于阈值展开。首次挂载、切换配置和跨过边界时报告 breakpoint(broken);只有折叠目标不同于实际值时才依次发出 update:collapsed、collapse(value, { type: 'responsive' })。触发条使用 { type: 'trigger' }。父组件拒绝回填时保留实际值,事件只报告请求。
小屏手动展开后,同侧视口变化不覆盖手动操作;再次跨界才同步。移除配置只停止监听并保留当前状态。折叠保留展开宽度,恢复时使用最新展开宽度;SSR不读视口,挂载后同步且首次不播放收缩过渡。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
import type { SiderBreakpoint, SiderCollapseContext } from '@nscode/handy-ui'
const breakpoint = ref<SiderBreakpoint | undefined>('lg')
const collapsed = ref(false)
const status = ref('等待检测')
const broken = ref<boolean>()
const record = (value: boolean, context: SiderCollapseContext) => {
status.value = context.type + ': ' + String(value)
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSpace wrap>
<HuiButton
v-for="name in ['sm', 'md', 'lg', 'xl', '2xl'] as const"
:key="name"
@click="breakpoint = name"
>{{ name }}</HuiButton
>
<HuiButton @click="breakpoint = undefined">停止监听</HuiButton>
</HuiSpace>
<span>配置:{{ breakpoint ?? '未配置' }};断点状态:{{ broken }};{{ status }}</span>
<HuiLayout direction="horizontal" style="height: 180px">
<HuiSider
v-model:collapsed="collapsed"
:breakpoint="breakpoint"
collapsible
@breakpoint="broken = $event"
@collapse="record"
><p>响应式侧栏</p></HuiSider
>
<HuiContent>调整浏览器宽度,或切换上方断点。窄屏手动展开后保持,直到再次跨界。</HuiContent>
</HuiLayout>
</HuiSpace>
</template>
完整后台布局
以下自包含示例把 Layout、Menu、Tabs、CommandPalette 放在同一页面状态中:菜单和标签页可切换,命令面板可搜索并跳转,侧栏支持1024px响应式折叠和拖宽。使用本地页面状态,无需安装路由或连接业务接口。侧栏是宽度折叠,不是移动抽屉。
<script setup lang="ts">
import { computed, ref } from 'vue'
import {
HuiLayout,
HuiHeader,
HuiSider,
HuiContent,
HuiFooter,
HuiMenu,
HuiMenuItem,
HuiTabs,
HuiTabPane,
HuiCommandPalette,
HuiButton
} from '@nscode/handy-ui'
import type {
CommandPaletteExpose,
CommandPaletteItem,
SiderCollapseContext
} from '@nscode/handy-ui'
const text = {
brand: 'HandyUI 管理台',
title: '后台布局',
search: '搜索页面',
waiting: '等待操作',
current: '当前页面',
responsive: '响应式请求',
trigger: '触发条请求',
collapsed: '已收起',
expanded: '已展开',
width: '展开宽度',
breakpoint: '断点',
unchecked: '尚未检测',
narrow: '窄屏',
wide: '宽屏',
hint: '菜单、标签页和命令面板共用当前页面;调整浏览器宽度可观察1024px断点。',
pages: [
{ key: 'overview', title: '工作总览', icon: 'home', description: '查看今日概况与待办事项。' },
{ key: 'orders', title: '订单列表', icon: 'file', description: '这里放订单表格和查询表单。' },
{
key: 'settings',
title: '系统设置',
icon: 'setting',
description: '这里放用户偏好与界面配置。'
}
]
}
const collapsed = ref(false)
const width = ref(220)
const active = ref('overview')
const broken = ref<boolean>()
const status = ref(text.waiting)
const palette = ref<CommandPaletteExpose>()
const pages = computed(() => text.pages)
const navigate = (key: string) => {
if (!pages.value.some((page) => page.key === key)) return
active.value = key
status.value = `${text.current}: ${pages.value.find((page) => page.key === key)!.title}`
}
const commands = computed<CommandPaletteItem[]>(() =>
pages.value.map((page) => ({
id: page.key,
label: page.title,
description: page.description,
icon: page.icon,
keywords: [page.key],
group: text.title,
action: () => navigate(page.key)
}))
)
const onCollapse = (value: boolean, context: SiderCollapseContext) => {
status.value = `${context.type === 'responsive' ? text.responsive : text.trigger}: ${value ? text.collapsed : text.expanded}`
}
</script>
<template>
<HuiLayout class="hui-backend-layout-demo" direction="horizontal">
<HuiSider
v-model:collapsed="collapsed"
v-model:width="width"
breakpoint="lg"
:collapsed-width="64"
collapsible
resizable
:min-width="160"
:max-width="320"
@breakpoint="broken = $event"
@collapse="onCollapse"
>
<div class="hui-backend-layout-demo__brand">{{ collapsed ? 'N' : text.brand }}</div>
<HuiMenu :default-active="active" :collapse="collapsed" @select="navigate">
<HuiMenuItem v-for="page in pages" :key="page.key" :index="page.key" :icon="page.icon">{{
page.title
}}</HuiMenuItem>
</HuiMenu>
</HuiSider>
<HuiLayout direction="vertical" class="hui-backend-layout-demo__main">
<HuiHeader :height="56" class="hui-backend-layout-demo__header">
<strong>{{ text.title }}</strong>
<HuiButton @click="palette?.open()">{{ text.search }}</HuiButton>
</HuiHeader>
<HuiContent>
<HuiTabs v-model="active" type="card" @update:model-value="navigate(String($event))">
<HuiTabPane v-for="page in pages" :key="page.key" :name="page.key" :label="page.title">
<section class="hui-backend-layout-demo__page">
<h3>{{ page.title }}</h3>
<p>{{ page.description }}</p>
<div class="hui-backend-layout-demo__cards">
<article>
<span>{{ text.current }}</span
><strong>{{ page.title }}</strong>
</article>
<article>
<span>{{ text.title }}</span
><strong>{{ collapsed ? text.collapsed : text.expanded }}</strong>
</article>
<article>
<span>{{ text.width }}</span
><strong>{{ width }}px</strong>
</article>
</div>
<p>{{ text.hint }}</p>
</section>
</HuiTabPane>
</HuiTabs>
</HuiContent>
<HuiFooter :height="48" class="hui-backend-layout-demo__footer">
<span>{{ status }}</span
><span
>{{ text.breakpoint }}:
{{ broken === undefined ? text.unchecked : broken ? text.narrow : text.wide }}</span
>
</HuiFooter>
</HuiLayout>
<HuiCommandPalette ref="palette" :items="commands" :width="560" />
</HuiLayout>
</template>
<style scoped>
.hui-backend-layout-demo {
height: 480px;
width: 100%;
overflow: hidden;
border: var(--hui-border-width) solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
color: var(--hui-text-color);
}
.hui-backend-layout-demo__main {
min-width: 0;
}
.hui-backend-layout-demo__brand {
height: 56px;
display: flex;
align-items: center;
justify-content: center;
flex: none;
font-weight: var(--hui-font-weight-semibold);
border-bottom: var(--hui-border-width) solid var(--hui-border-color-light);
}
.hui-backend-layout-demo :deep(.hui-menu) {
width: 100%;
border-right: 0;
}
.hui-backend-layout-demo__header,
.hui-backend-layout-demo__footer {
justify-content: space-between;
gap: var(--hui-spacing-3);
}
.hui-backend-layout-demo__footer {
font-size: var(--hui-font-size-sm);
flex-wrap: wrap;
}
.hui-backend-layout-demo__page h3 {
margin: var(--hui-spacing-3) 0;
}
.hui-backend-layout-demo__page p {
color: var(--hui-text-color-secondary);
}
.hui-backend-layout-demo__cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 140px), 1fr));
gap: var(--hui-spacing-3);
}
.hui-backend-layout-demo__cards article {
display: flex;
flex-direction: column;
gap: var(--hui-spacing-2);
padding: var(--hui-spacing-4);
background: var(--hui-bg-color);
border: var(--hui-border-width) solid var(--hui-border-color-light);
border-radius: var(--hui-border-radius);
}
</style>
祖先变量与背景命名
背景变量统一使用 bg,悬停状态放在属性后面。三个 border-color 变量保持原名;默认主题值、部位和交互保持。公开变量只读取并带兜底,可设在组件自身或任意祖先;嵌套布局继承公开配置,下例在更近的祖先局部覆盖内容背景。父组件的 <style scoped> 可给祖先类设置变量,不需要 :deep()。
旧名直接迁为新名,不留旧名读取兜底:
| 旧名 | 新名 |
|---|---|
--hui-layout-sider-trigger-bg-color-hover | --hui-layout-sider-trigger-bg-hover |
--hui-layout-sider-trigger-bg-color | --hui-layout-sider-trigger-bg |
--hui-layout-header-bg-color | --hui-layout-header-bg |
--hui-layout-footer-bg-color | --hui-layout-footer-bg |
--hui-layout-content-bg-color | --hui-layout-content-bg |
--hui-layout-sider-bg-color | --hui-layout-sider-bg |
--hui-layout-bg-color | --hui-layout-bg |
<script setup lang="ts">
import { ref } from 'vue'
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
const variableCollapsed = ref(false)
const variableChanges = ref(0)
</script>
<template>
<div
class="layout-variables-demo"
style="--hui-layout-bg:var(--hui-fill-color-light);--hui-layout-header-bg:var(--hui-color-primary-light-9);--hui-layout-header-border-color:var(--hui-color-primary);--hui-layout-sider-bg:var(--hui-color-success-light-9);--hui-layout-sider-border-color:var(--hui-color-success);--hui-layout-sider-trigger-bg:var(--hui-color-success-light-8);--hui-layout-sider-trigger-bg-hover:var(--hui-color-success-light-7);--hui-layout-content-bg:var(--hui-bg-color-page);--hui-layout-footer-bg:var(--hui-color-warning-light-9);--hui-layout-footer-border-color:var(--hui-color-warning);max-width:720px"
>
<HuiLayout style="height:300px">
<HuiHeader :height="40">祖先配置的顶栏</HuiHeader>
<HuiLayout>
<HuiSider
v-model:collapsed="variableCollapsed"
collapsible
:width="140"
:collapsed-width="56"
@update:collapsed="variableChanges++"
>侧栏</HuiSider
>
<HuiLayout>
<HuiContent>
<p class="layout-variable-result">
折叠:{{ variableCollapsed }};更新:{{ variableChanges }}
</p>
<div
class="layout-variable-nested"
style="--hui-layout-content-bg:var(--hui-color-help-light-9)"
>
<HuiLayout style="height:90px"><HuiContent>局部内容背景覆盖</HuiContent></HuiLayout>
</div>
</HuiContent>
<HuiFooter :height="36">祖先配置的底栏</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</div>
</template>
HuiLayout Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| direction | 'horizontal' | 'vertical' | - | 子元素的排列方向。不写时,里面有 HuiSider 就是 'horizontal',没有就是 'vertical' |
HuiHeader Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| height | string | number | 60 | 高度。数字单位是 px,字符串原样用 |
HuiFooter Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| height | string | number | 60 | 高度。数字单位是 px,字符串原样用 |
HuiSider Props
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| width | string | number | 200 | 传入即受控(含undefined,按200显示);未传或仅监听时从200保存。数字为px,字符串原样用 |
| collapsedWidth | string | number | 64 | 折叠后的宽度。为 0 时折叠后完全隐藏,触发条浮在侧边栏外侧 |
| collapsed | boolean | undefined | 传入即受控(含undefined,按false显示);未传从false保存,支持v-model:collapsed |
| breakpoint | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | — | 小于断点请求收起,回到阈值及以上请求展开;不传时不监听视口 |
| placement | 'left' | 'right' | 'left' | 侧边栏在哪一侧,决定边框和触发条箭头的方向 |
| collapsible | boolean | false | 是否显示折叠触发条;不影响collapsed外部控制及breakpoint响应式折叠 |
| resizable | boolean | false | 是否在靠内容区的一侧显示宽度拖动条,折叠时隐藏 |
| minWidth | string | number | collapsedWidth(为0时取64px) | 最小展开宽度;支持 px、%、vw、vh,百分比相对父元素,显式0仍有效 |
| maxWidth | string | number | 父宽度50% | 最大展开宽度;小于最小值时按最小值 |
HuiContent 没有属性。
HuiSider Events
| 事件 | 回调参数 | 说明 |
|---|---|---|
| update:collapsed | (value: boolean) | 触发条或响应式折叠请求,父组件决定是否回填 |
| collapse | (value: boolean, context: SiderCollapseContext) | 更新请求后通知,context.type为trigger或responsive,不代表父组件已接受 |
| breakpoint | (broken: boolean) | 首次挂载、切配置和跨界时报告;true表示窄于断点,先于折叠更新请求 |
| update:width | (value: number) | 拖动、键盘调整或 Esc 恢复时触发,参数为 px 数字 |
| resizeEnd | (width: number) | 本次调整结束后的展开宽度;父级接受或改写绑定后触发 |
Sider公开结束事件声明与实际发射统一为 resizeEnd。Vue模板仍可写 @resize-end,渲染函数使用 onResizeEnd,只发新声明事件。参数仍只有父组件处理后的实际展开宽度(像素),不增加取消标记。
Slots
| 组件 | 插槽 | 作用域参数 | 说明 |
|---|---|---|---|
| HuiLayout | default | - | 放 Header、Sider、Content 等 |
| HuiHeader | default | - | 顶栏的内容 |
| HuiSider | default | - | 侧边栏的内容 |
| HuiSider | trigger | { collapsed: boolean } | 折叠触发条里的内容 |
| HuiContent | default | - | 内容区的内容 |
| HuiFooter | default | - | 底栏的内容 |
类型
公开类型使用组件前缀。兼容映射:SiderInfo → LayoutSiderInfo。旧名仍可从 @nscode/handy-ui 导入,已标记 @deprecated,将在 1.0 删除。
import type {
LayoutProps,
HeaderProps,
FooterProps,
SiderProps,
ContentProps,
SiderBreakpoint,
SiderCollapseContext
} from '@nscode/handy-ui'
type SiderBreakpoint = 'sm' | 'md' | 'lg' | 'xl' | '2xl'
interface SiderCollapseContext {
type: 'trigger' | 'responsive'
}
interface LayoutProps {
direction?: 'horizontal' | 'vertical'
}
interface HeaderProps {
height?: string | number
}
interface FooterProps {
height?: string | number
}
interface SiderProps {
width?: string | number
collapsedWidth?: string | number
collapsed?: boolean
breakpoint?: SiderBreakpoint
placement?: 'left' | 'right'
collapsible?: boolean
resizable?: boolean
minWidth?: string | number
maxWidth?: string | number
}
interface ContentProps {}
另外还导出了 LayoutSiderInfo、LayoutContext 和注入用的 LAYOUT_INJECTION_KEY,HuiSider 通过它向所在的 HuiLayout 登记自己,一般用不到。
CSS 变量
这些变量主题里没有预先定义,组件里写了默认值。在 HuiLayout 或更外层的元素上设置即可覆盖。
| 变量 | 默认值 | 说明 |
|---|---|---|
--hui-layout-bg | var(--hui-bg-color) | HuiLayout 的背景色 |
--hui-layout-header-bg | var(--hui-bg-color) | 顶栏背景色 |
--hui-layout-header-border-color | var(--hui-border-color-light) | 顶栏下边框颜色 |
--hui-layout-sider-bg | var(--hui-bg-color) | 侧边栏背景色 |
--hui-layout-sider-border-color | var(--hui-border-color-light) | 侧边栏边框、触发条上边框 |
--hui-layout-sider-trigger-bg | var(--hui-bg-color-hover) | 触发条背景色 |
--hui-layout-sider-trigger-bg-hover | var(--hui-color-primary-light-9) | 鼠标悬停时触发条背景色 |
--hui-layout-content-bg | var(--hui-bg-color-page) | 内容区背景色 |
--hui-layout-footer-bg | var(--hui-bg-color) | 底栏背景色 |
--hui-layout-footer-border-color | var(--hui-border-color-light) | 底栏上边框颜色 |