Layout
Build a page frame with HuiLayout, HuiHeader, HuiSider, HuiContent and HuiFooter. Sider supports collapsing and optional pointer or keyboard width adjustment.
Import
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
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'
}
Examples
Basic layout
The header and footer have fixed heights, while content fills the remaining space. Give HuiLayout a definite height so it can calculate that space.
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiHeader>Header</HuiHeader>
<HuiContent>Content</HuiContent>
<HuiFooter>Footer</HuiFooter>
</HuiLayout>
</template>
With a sidebar
A Layout containing a Sider automatically arranges the sidebar and content horizontally. The sidebar fills the height of its Layout.
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiSider>Sidebar</HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
</template>
A header with a sidebar
Layouts can be nested. The outer Layout stacks its header and inner Layout vertically. The inner Layout contains a Sider and therefore arranges horizontally.
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 400px">
<HuiHeader>Header</HuiHeader>
<HuiLayout>
<HuiSider>Sidebar</HuiSider>
<HuiLayout>
<HuiContent>Content</HuiContent>
<HuiFooter>Footer</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</template>
The innermost Layout has no Sider, so its content and footer remain vertically arranged. The header can also sit beside the sidebar, allowing the sidebar to extend from the top to the bottom:
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiHeader, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 400px">
<HuiSider>Sidebar</HuiSider>
<HuiLayout>
<HuiHeader>Header</HuiHeader>
<HuiContent>Content</HuiContent>
<HuiFooter>Footer</HuiFooter>
</HuiLayout>
</HuiLayout>
</template>
Direction
direction accepts horizontal and vertical. Without an explicit value, Layout chooses according to whether it contains a Sider. An explicit value takes priority. These examples show both automatic cases and both explicit values.
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiContent, HuiSider, HuiFooter } from '@nscode/handy-ui'
</script>
<template>
<div class="layouts">
<div>
<p class="label">Automatic, without HuiSider: vertical</p>
<HuiLayout class="box">
<HuiHeader :height="40">Header</HuiHeader>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">Automatic, with HuiSider: horizontal</p>
<HuiLayout class="box">
<HuiSider :width="100">Sidebar</HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">direction="horizontal": horizontal without HuiSider</p>
<HuiLayout direction="horizontal" class="box">
<HuiContent>Content A</HuiContent>
<HuiContent class="divided">Content B</HuiContent>
</HuiLayout>
</div>
<div>
<p class="label">direction="vertical"</p>
<HuiLayout direction="vertical" class="box">
<HuiContent>Content</HuiContent>
<HuiFooter :height="40">Footer</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 sidebar
collapsible adds a trigger at the bottom of the sidebar. Clicking it switches between width and collapsedWidth. Bind v-model:collapsed to also control the state externally.
<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">Menu</span>
</HuiSider>
<HuiLayout>
<HuiHeader>
<HuiButton @click="collapsed = !collapsed">{{
collapsed ? 'Expand' : 'Collapse'
}}</HuiButton>
</HuiHeader>
<HuiContent>Content</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
Without a collapsed model binding, Sider stores the collapse state internally. Sidebar content scrolls independently; the trigger stays below the content without covering it.
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 300px">
<HuiSider collapsible :width="160">
<p v-for="i in 20" :key="i" style="margin: 0; padding: 8px 16px">Item {{ i }}</p>
</HuiSider>
<HuiContent
>The sidebar contains 20 items. Its content scrolls while the trigger stays at the
bottom.</HuiContent
>
</HuiLayout>
</template>
Collapsed width
width is the expanded width; collapsed-width is the width after collapsing. Both accept a number in pixels or a CSS length, with defaults of 200 and 64. Each sidebar below has its own trigger.
<script setup lang="ts">
import { HuiSpace, HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiLayout style="height: 120px">
<HuiSider collapsible>Default 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>
Hide completely when collapsed
Set collapsed-width to 0 to hide the sidebar completely. The trigger becomes a small button outside the sidebar, 64px below its top edge, so it remains available for expanding again.
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiSider collapsible :collapsed-width="0">Sidebar</HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
</template>
Control without the built-in trigger
collapsible=false is the default and omits the trigger. Control collapsed from another element, such as a button in the header.
<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 ? 'Expand sidebar' : 'Collapse sidebar'"
@click="collapsed = !collapsed"
/>
</HuiHeader>
<HuiLayout>
<HuiSider :collapsed="collapsed" :width="180" :collapsed-width="0">Sidebar</HuiSider>
<HuiContent>Use the header button to collapse or expand the sidebar.</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
Right sidebar
placement=right moves the border to the left edge and reverses the trigger arrow. Actual placement still follows template order: put the right Sider after Content.
<script setup lang="ts">
import { HuiLayout, HuiContent, HuiSider } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiContent>Content</HuiContent>
<HuiSider placement="right" collapsible :width="240">Right panel</HuiSider>
</HuiLayout>
</template>
Both placement values can be used together, giving the content an independently collapsible sidebar on each side:
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 300px">
<HuiSider placement="left" collapsible :width="160">Left sidebar</HuiSider>
<HuiContent>Content</HuiContent>
<HuiSider placement="right" collapsible :width="160">Right sidebar</HuiSider>
</HuiLayout>
</template>
Resize sidebar width
resizable adds a handle on the content side: the right edge of a left sidebar or the left edge of a right sidebar. v-model:width receives pixel numbers. Omitting width stores adjustments internally, including listener-only usage. A supplied width is controlled even without a listener; explicit undefined displays 200px.
The minimum defaults to collapsedWidth, except that a zero collapsedWidth uses 64px when minWidth is omitted. An explicit minWidth=0 remains valid. The maximum defaults to half the parent width. Limits accept numbers, px, %, vw and vh; percentages refer to the parent width. A maximum smaller than the minimum is raised to that minimum.
Tab focuses the handle. Arrow keys adjust by 10px, or 1px with Shift; Home and End select the minimum and maximum. Numeric and px widths use the current target width, so repeated key presses accumulate even during a width transition. Nonpixel widths are measured from the layout. Escape during a pointer drag requests the starting pixel width. A supplied CSS width stays controlled by the parent.
This example saves the width after resizeEnd. Both localStorage reads and writes use try/catch so resizing continues if storage is unavailable.
<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('Drag the sidebar or adjust it with the keyboard.')
const storageKey = 'hui-layout-demo-sider-width-en'
onMounted(() => {
try {
const saved = Number(localStorage.getItem(storageKey))
if (Number.isFinite(saved) && saved >= 120 && saved <= 360) width.value = saved
} catch {
/* Use the default width when local storage is unavailable. */
}
})
const saveWidth = (value: number) => {
result.value = `Final width: ${Math.round(value)}px`
try {
localStorage.setItem(storageKey, String(value))
} catch {
/* A storage failure does not affect resizing. */
}
}
</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 ? 'Menu' : 'Drag the right edge; Escape cancels this adjustment.' }}
</p>
</HuiSider>
<HuiContent
><p style="padding: 16px">
Collapsing hides the resize handle; expanding restores the retained width.
</p></HuiContent
>
</HuiLayout>
<span>Bound width: {{ Math.round(width) }}px; {{ result }}</span>
</HuiSpace>
</template>
Both resize edges and percentage limits
The two sidebars can be resized independently, with percentage limits relative to their parent.
<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">Left sidebar: drag the right edge</p></HuiSider
>
<HuiContent><p style="padding: 16px">Content</p></HuiContent>
<HuiSider v-model:width="right" placement="right" resizable min-width="15%" max-width="40%"
><p style="padding: 16px">Right sidebar: drag the left edge</p></HuiSider
>
</HuiLayout>
<span>Left: {{ Math.round(left) }}px; right: {{ Math.round(right) }}px</span>
</HuiSpace>
</template>
Enable resizing at runtime
Disabling resizable, collapsing, changing placement or limits, and externally changing width stop the previous drag, clean up page drag state and preserve the new configuration.
<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>Allow resizing</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">
The default minimum is 64px and the maximum is half the parent width.
</p></HuiSider
>
<HuiContent
><p style="padding: 16px">
Disabling resizing hides the handle and retains the adjusted width.
</p></HuiContent
>
</HuiLayout>
</HuiSpace>
</template>
Custom trigger content
The trigger slot replaces the content inside the trigger button. Its collapsed argument is the current state. The component keeps click and keyboard behavior on the outer button; do not add a second click handler inside the slot.
<script setup lang="ts">
import { HuiLayout, HuiSider, HuiContent } from '@nscode/handy-ui'
</script>
<template>
<HuiLayout style="height: 360px">
<HuiSider collapsible>
Sidebar
<template #trigger="{ collapsed }">
{{ collapsed ? 'Expand' : 'Collapse' }}
</template>
</HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
</template>
Combine with a menu
Put HuiMenu inside Sider and pass the same collapse state to the menu. The collapsed menu shows icons. The collapse event reports the new state after a trigger click.
<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 ? 'Sidebar collapsed' : 'Sidebar expanded'
}
</script>
<template>
<HuiLayout style="height: 400px">
<HuiHeader>Administration</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">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System administration" icon="setting">
<HuiMenuItem index="2-1" icon="user">Users</HuiMenuItem>
<HuiMenuItem index="2-2" icon="users">Roles</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3" icon="bell">Notifications</HuiMenuItem>
</HuiMenu>
</HuiSider>
<HuiContent>{{ log || 'Try the trigger at the bottom of the sidebar.' }}</HuiContent>
</HuiLayout>
</HuiLayout>
</template>
Custom heights and colors
Header and Footer height accepts a pixel number or any CSS length. Change colors through the CSS variables listed below.
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<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)">Header</HuiHeader>
<HuiLayout>
<HuiSider :width="180">Sidebar</HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
<HuiFooter height="40px">Footer</HuiFooter>
</HuiLayout>
</template>
Trigger, border and content colors can also be customized. Theme variables adapt the palette to dark mode:
<script setup lang="ts">
import { HuiLayout, HuiHeader, HuiSider, HuiContent, HuiFooter } from '@nscode/handy-ui'
</script>
<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">Header with a primary-color bottom border</HuiHeader>
<HuiLayout>
<HuiSider collapsible :width="160">Primary-surface sidebar</HuiSider>
<HuiLayout>
<HuiContent>Surface-colored content</HuiContent>
<HuiFooter :height="48">Subtle-fill footer</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</template>
Collapse state and parent updates
Omitting collapsed stores state starting from false, including listener-only usage. Supplying it, even as undefined, makes it controlled; undefined means expanded. If the parent rejects a request, width, trigger ARIA, slots, Layout registration and resize availability keep the actual state. Removing the prop restores internal state; mode changes warn in development. Width has its own control rules in the next example.
<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('No request')
const q1Model = ref(false)
</script>
<template>
<HuiSpace class="sider-q1-demo" direction="vertical" fill>
<HuiButton class="q1-redraw" @click="q1Redraw++">Parent render {{ q1Redraw }}</HuiButton>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
gap: 16px;
"
>
<div class="q1-local">
<p>Omitted prop</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible><p>Sidebar</p></HuiSider
><HuiContent>Content</HuiContent></HuiLayout
>
</div>
<div class="q1-listener">
<p>Update listener only</p>
<HuiLayout style="height: 120px"
><HuiSider
:width="160"
:collapsed-width="56"
collapsible
@update:collapsed="q1Last = String($event)"
><p>Sidebar</p></HuiSider
><HuiContent>Content</HuiContent></HuiLayout
>
</div>
<div class="q1-empty">
<p>Explicit undefined</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible :collapsed="undefined"
><p>Sidebar</p></HuiSider
><HuiContent>Content</HuiContent></HuiLayout
>
</div>
<div class="q1-fixed">
<p>Parent rejects updates</p>
<HuiLayout style="height: 120px"
><HuiSider
:width="160"
:collapsed-width="56"
collapsible
:collapsed="false"
@update:collapsed="q1Last = String($event)"
><p>Sidebar</p></HuiSider
><HuiContent>Content</HuiContent></HuiLayout
>
</div>
<div class="q1-bound">
<p>Two-way binding</p>
<HuiLayout style="height: 120px"
><HuiSider :width="160" :collapsed-width="56" collapsible v-model:collapsed="q1Model"
><p>Sidebar</p></HuiSider
><HuiContent>Content</HuiContent></HuiLayout
>
</div>
</div>
<p class="q1-notification">Bound value:{{ q1Model }};Latest request:{{ q1Last }}</p>
</HuiSpace>
</template>
Internal sidebar width and controlled updates
Omitted width, including listener-only usage, starts at 200px and stores adjustments internally. Supplying width makes it controlled even without a listener; explicit undefined displays 200px. The parent decides whether to accept, transform or reject pixel requests. Width, flex basis, ARIA and resizeEnd use the actual accepted state. Removing the prop restores internal width. Changing control mode warns in development and stops the old drag. Escape requests the starting pixel width; a controlled parent may accept it or keep its original CSS width.
<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('No request')
const widthEnd = ref('Not ended')
const widthModel = ref(200)
</script>
<template>
<HuiSpace class="sider-width-q1-demo" direction="vertical" fill>
<HuiButton class="width-redraw" @click="widthRedraw++"
>Parent render {{ widthRedraw }}</HuiButton
>
<div
style="
display: grid;
grid-template-columns: repeat(auto-fit, minmax(360px, 1fr));
gap: 16px;
"
>
<div class="width-local">
<p>Omitted width</p>
<HuiLayout style="height: 90px">
<HuiSider resizable :min-width="120" :max-width="320"><p>Left sidebar</p></HuiSider>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
<div class="width-listener">
<p>Right sidebar with listener only</p>
<HuiLayout style="height: 90px">
<HuiContent>Content</HuiContent>
<HuiSider
placement="right"
resizable
:min-width="120"
:max-width="320"
@update:width="widthLast = String($event)"
@resize-end="widthEnd = String($event)"
><p>Right sidebar</p></HuiSider
>
</HuiLayout>
</div>
<div class="width-empty">
<p>Explicit undefined stays controlled at 200px</p>
<HuiLayout style="height: 90px">
<HuiSider :width="undefined" resizable :min-width="120" :max-width="320"
><p>Empty controlled width</p></HuiSider
>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
<div class="width-fixed">
<p>Fixed 220px rejects updates</p>
<HuiLayout style="height: 90px">
<HuiSider :width="220" resizable :min-width="120" :max-width="320"
><p>Fixed controlled width</p></HuiSider
>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
<div class="width-bound">
<p>Two-way width binding</p>
<HuiLayout style="height: 90px">
<HuiSider v-model:width="widthModel" resizable :min-width="120" :max-width="320"
><p>{{ widthModel }}px</p></HuiSider
>
<HuiContent>Content</HuiContent>
</HuiLayout>
</div>
</div>
<p class="width-status">
Bound width: {{ widthModel }};Right request: {{ widthLast }};Actual end: {{ widthEnd }}
</p>
</HuiSpace>
</template>
Responsive sidebar
Set breakpoint to observe the viewport: sm=640, md=768, lg=1024, xl=1280 and 2xl=1536px. Widths below it request collapse; equality or larger widths request expansion. Initial mount, configuration changes and threshold crossings report breakpoint(broken). Only a different collapse target emits update:collapsed followed by collapse(value, { type: 'responsive' }); the trigger uses type=trigger. Events report requests, while controlled parents remain authoritative.
Manual expansion on a narrow screen remains until the next crossing. Removing the configuration stops observing without changing state. Collapse retains the expanded width; expansion restores its latest value. SSR does not read the viewport, and the first client synchronization has no width transition.
<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('Waiting')
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">Stop observing</HuiButton>
</HuiSpace>
<span>Configured: {{ breakpoint ?? 'Unset' }}; threshold: {{ broken }}; {{ status }}</span>
<HuiLayout direction="horizontal" style="height: 180px">
<HuiSider
v-model:collapsed="collapsed"
:breakpoint="breakpoint"
collapsible
@breakpoint="broken = $event"
@collapse="record"
><p>Responsive sidebar</p></HuiSider
>
<HuiContent
>Resize the browser or change the threshold above. Manual expansion on a narrow screen
remains until the next crossing.</HuiContent
>
</HuiLayout>
</HuiSpace>
</template>
Complete admin workspace
This standalone example shares one active page among Layout, Menu, Tabs and CommandPalette. Navigate using the menu, tabs or searchable commands, resize the sidebar, and cross the 1024px viewport threshold. State is local: no router installation or business endpoint is required. The sidebar changes width rather than becoming a mobile drawer.
<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 Admin',
title: 'Admin workspace',
search: 'Search pages',
waiting: 'Ready',
current: 'Current page',
responsive: 'Responsive request',
trigger: 'Trigger request',
collapsed: 'Collapsed',
expanded: 'Expanded',
width: 'Expanded width',
breakpoint: 'Breakpoint',
unchecked: 'Not measured',
narrow: 'Narrow',
wide: 'Wide',
hint: 'Menu, tabs and palette share the active page. Resize the browser to cross 1024px.',
pages: [
{
key: 'overview',
title: 'Overview',
icon: 'home',
description: 'Review today and pending work.'
},
{
key: 'orders',
title: 'Orders',
icon: 'file',
description: 'Place order tables and filters here.'
},
{
key: 'settings',
title: 'Settings',
icon: 'setting',
description: 'Place user preferences here.'
}
]
}
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>
Inherited Variables and Background Names
Background variables use bg, with hover state at the end. The three border-color variables keep their names. Theme defaults, regions, and interactions remain the same. Public variables are read with fallbacks and may be set on a component or any ancestor. Nested layouts inherit them; this example overrides the content background on a nearer ancestor. Set variables on an ancestor class in a parent <style scoped>; :deep() is unnecessary.
Migrate old names directly; no old-name fallback is kept:
| Old name | New name |
|---|---|
--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">Inherited header</HuiHeader>
<HuiLayout>
<HuiSider
v-model:collapsed="variableCollapsed"
collapsible
:width="140"
:collapsed-width="56"
@update:collapsed="variableChanges++"
>Sidebar</HuiSider
>
<HuiLayout>
<HuiContent>
<p class="layout-variable-result">
Collapsed: {{ variableCollapsed }};Updates: {{ variableChanges }}
</p>
<div
class="layout-variable-nested"
style="--hui-layout-content-bg:var(--hui-color-help-light-9)"
>
<HuiLayout style="height:90px"
><HuiContent>Local content background</HuiContent></HuiLayout
>
</div>
</HuiContent>
<HuiFooter :height="36">Inherited footer</HuiFooter>
</HuiLayout>
</HuiLayout>
</HuiLayout>
</div>
</template>
Props
HuiLayout
| Property | Type | Default | Description |
|---|---|---|---|
| direction | 'horizontal' | 'vertical' | Automatic | Horizontal when the nearest Layout contains a Sider; otherwise vertical |
HuiHeader
| Property | Type | Default | Description |
|---|---|---|---|
| height | string | number | 60 | Header height; numbers mean pixels, strings are CSS lengths |
HuiFooter
| Property | Type | Default | Description |
|---|---|---|---|
| height | string | number | 60 | Footer height; numbers mean pixels, strings are CSS lengths |
HuiSider
| Property | Type | Default | Description |
|---|---|---|---|
| width | string | number | 200 | Supplied props are controlled, including undefined as 200px; omitted props save width from 200px; supports v-model:width |
| collapsedWidth | string | number | 64 | Collapsed width; 0 hides the sidebar and floats the trigger outside |
| collapsed | boolean | undefined | Controlled whenever supplied, including undefined; omitted props store state from false; supports v-model:collapsed |
| breakpoint | 'sm' | 'md' | 'lg' | 'xl' | '2xl' | — | Request collapse below the threshold and expansion at or above it; omitted props do not observe the viewport |
| placement | 'left' | 'right' | 'left' | Determines border, trigger arrow and resize-edge orientation |
| collapsible | boolean | false | Show the built-in trigger; otherwise control collapsed externally |
| resizable | boolean | false | Show a width-resize handle on the content edge while expanded |
| minWidth | string | number | collapsedWidth; 64 if that is zero | Minimum expanded width; supports px, %, vw and vh |
| maxWidth | string | number | Half the parent width | Maximum expanded width; never smaller than the minimum |
HuiContent has no props.
Events
HuiSider
| Event | Arguments | Description |
|---|---|---|
| update:collapsed | (value: boolean) | Trigger or responsive request; the parent decides whether to accept |
| collapse | (value: boolean, context: SiderCollapseContext) | Follows the update request; type is trigger or responsive and does not imply parent acceptance |
| breakpoint | (broken: boolean) | Reports on initial mount, configuration changes and threshold crossings; true means below the threshold, before collapse requests |
| update:width | (value: number) | Drag, keyboard adjustment or Escape restoration request, in pixels |
| resizeEnd | (width: number) | Final expanded width after the parent accepts, transforms or rejects an update |
The public Sider completion event is declared and emitted as resizeEnd. Vue templates can still use @resize-end; render functions use onResizeEnd. Only the new declared event is emitted. Its single argument remains the actual expanded width in pixels after parent feedback; no cancelled argument is added.
Slots
| Component | Slot | Arguments | Description |
|---|---|---|---|
| HuiLayout | default | — | Header, Sider, Content and other layout containers |
| HuiHeader | default | — | Header content |
| HuiSider | default | — | Sidebar content |
| HuiSider | trigger | { collapsed: boolean } | Content inside the collapse trigger button |
| HuiContent | default | — | Main content |
| HuiFooter | default | — | Footer content |
Methods
No public instance methods. Control width and collapse state through props.
Types
Public types use component prefixes. SiderInfo remains a deprecated alias of LayoutSiderInfo and is scheduled for removal in 1.0.
import type {
LayoutProps,
HeaderProps,
FooterProps,
SiderProps,
ContentProps
} from '@nscode/handy-ui'
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 and LAYOUT_INJECTION_KEY are also exported. Sider uses this context to register with its nearest Layout; applications normally do not need it.
CSS variables
These variables are not predefined by the theme. Components supply the listed fallback values. Set them on HuiLayout or an ancestor to override them.
| Variable | Fallback | Description |
|---|---|---|
--hui-layout-bg | var(--hui-bg-color) | Layout background |
--hui-layout-header-bg | var(--hui-bg-color) | Header background |
--hui-layout-header-border-color | var(--hui-border-color-light) | Header bottom border |
--hui-layout-sider-bg | var(--hui-bg-color) | Sidebar background |
--hui-layout-sider-border-color | var(--hui-border-color-light) | Sidebar border and trigger top border |
--hui-layout-sider-trigger-bg | var(--hui-bg-color-hover) | Trigger background |
--hui-layout-sider-trigger-bg-hover | var(--hui-color-primary-light-9) | Trigger hover background |
--hui-layout-content-bg | var(--hui-bg-color-page) | Content background |
--hui-layout-footer-bg | var(--hui-bg-color) | Footer background |
--hui-layout-footer-border-color | var(--hui-border-color-light) | Footer top border |