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 開始自行保存收合狀態。傳入屬性即受控,包含按 false 顯示的 undefined。父元件拒絕回填時,寬度、觸發條 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 顯示。父元件可接受、改寫或拒絕 px 請求,寬度、flex、ARIA 及 resizeEnd 皆使用實際狀態。移除屬性恢復原內部寬度;切換模式會警告並結束舊拖曳。Esc 請求恢復按下時的 px 寬度,父元件亦可選擇保留原 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>
祖先變數與背景命名
背景變數統一使用 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>
響應式側欄
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 broken = ref<boolean>()
const status = ref('等待偵測')
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"
>響應式側欄</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 collapsed = ref(false)
const width = ref(220)
const active = ref<string | number | undefined>('overview')
const broken = ref<boolean>()
const status = ref('等待操作')
const palette = ref<CommandPaletteExpose>()
const pages = [
{ key: 'overview', title: '工作總覽', icon: 'home', description: '查看今日概況與待辦事項。' },
{ key: 'orders', title: '訂單清單', icon: 'file', description: '此處放置表格與查詢表單。' },
{
key: 'settings',
title: '系統設定',
icon: 'setting',
description: '此處放置個人偏好與介面設定。'
}
]
const navigate = (key: string) => {
const page = pages.find((item) => item.key === key)
if (!page) return
active.value = key
status.value = '目前頁面:' + page.title
}
const commands = computed<CommandPaletteItem[]>(() =>
pages.map((page) => ({
id: page.key,
label: page.title,
description: page.description,
icon: page.icon,
keywords: [page.key],
group: '頁面',
action: () => navigate(page.key)
}))
)
const onCollapse = (value: boolean, context: SiderCollapseContext) => {
status.value = `${context.type === 'responsive' ? '響應式請求' : '觸發條請求'}:${value ? '已收合' : '已展開'}`
}
</script>
<template>
<HuiLayout class="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="brand">{{ collapsed ? 'N' : 'HandyUI 管理台' }}</div>
<HuiMenu :default-active="String(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" style="min-width: 0">
<HuiHeader :height="56"><HuiButton @click="palette?.open()">搜尋頁面</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"
><h3>{{ page.title }}</h3>
<p>{{ page.description }}</p>
<p>展開寬度:{{ width }}px</p></HuiTabPane
>
</HuiTabs>
</HuiContent>
<HuiFooter :height="48"
>{{ status }};斷點:{{
broken === undefined ? '尚未偵測' : broken ? '窄螢幕' : '寬螢幕'
}}</HuiFooter
>
</HuiLayout>
<HuiCommandPalette ref="palette" :items="commands" :width="560" />
</HuiLayout>
</template>
<style scoped>
.backend-layout-demo {
height: 420px;
width: 100%;
overflow: hidden;
border: var(--hui-border-width) solid var(--hui-border-color);
border-radius: var(--hui-border-radius);
}
.brand {
padding: var(--hui-spacing-4);
font-weight: var(--hui-font-weight-semibold);
}
</style>
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 |
| placement | 'left' | 'right' | 'left' | 側邊欄在哪一側,決定邊框和觸發條箭頭的方向 |
| collapsible | boolean | false | 是否顯示收合觸發條;為 false 時沒有觸發條,仍可透過 collapsed 控制收合 |
| resizable | boolean | false | 是否在靠內容區的一側顯示寬度拖曳條,收合時隱藏 |
| minWidth | string | number | collapsedWidth(為0時取64px) | 最小展開寬度;支援 px、%、vw、vh,百分比相對父元素,明確0仍有效 |
| maxWidth | string | number | 父寬度50% | 最大展開寬度;小於最小值時按最小值 |
| breakpoint | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | - | 小於門檻請求收合,等於或大於請求展開;未設定時不監聽視口 |
HuiContent 沒有屬性。
HuiSider Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| update:collapsed | (value: boolean) | 點擊觸發條請求切換收合時觸發,供 v-model:collapsed 使用 |
| collapse | (value: boolean, context: SiderCollapseContext) | 更新請求後通知,context.type為trigger或responsive,不表示父元件已接受 |
| update:width | (value: number) | 拖曳、鍵盤調整或 Esc 恢復時觸發,參數為 px 數字 |
| resizeEnd | (width: number) | 本次調整結束後的展開寬度;父級接受或改寫綁定後觸發 |
| breakpoint | (broken: boolean) | 首次掛載、切換設定與跨界時通知,true為窄於斷點;先於收合更新請求 |
Sider 的公開完成事件統一宣告並發送為 resizeEnd。Vue 範本可使用 @resize-end,渲染函式使用 onResizeEnd,不另發送舊名稱。參數仍只有經父元件處理的實際展開寬度(px),不增加取消標記。
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'
interface LayoutProps {
direction?: 'horizontal' | 'vertical'
}
interface HeaderProps {
height?: string | number
}
interface FooterProps {
height?: string | number
}
type SiderBreakpoint = 'sm' | 'md' | 'lg' | 'xl' | '2xl'
interface SiderCollapseContext {
type: 'trigger' | 'responsive'
}
interface SiderProps {
breakpoint?: SiderBreakpoint
width?: string | number
collapsedWidth?: string | number
collapsed?: boolean
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) | 底欄上邊框顏色 |