Tabs
Divides content into panels, displaying one at a time. Click a tab to switch. Each panel uses HuiTabPane.
Import
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
Internal State and Controlled Updates
Without modelValue, or with only an update listener, the component stores the active tab and automatically selects the first available item. A supplied prop makes it controlled; undefined means no active tab. Clicking, an approved async beforeLeave check and neighbor selection after closing only request a parent update. External model changes or switching control modes invalidate older pending results.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiInput, HuiButton, HuiSpace } from '@nscode/handy-ui'
const q1Redraw = ref(0)
const q1Last = ref('No request yet')
const q1Model = ref<string | number | undefined>('a')
</script>
<template>
<div class="tabs-q1-demo">
<HuiSpace
><HuiButton class="q1-redraw" @click="q1Redraw++">Parent redraw {{ q1Redraw }}</HuiButton
><HuiButton class="q1-clear" @click="q1Model = undefined"
>Clear the controlled active tab</HuiButton
></HuiSpace
><HuiSpace direction="vertical" fill>
<div>
<p>Prop omitted; internal state</p>
<div class="q1-local">
<HuiTabs type="browser"
><HuiTabPane name="a" label="Tab A"
><HuiInput placeholder="Draft within this tab" /></HuiTabPane
><HuiTabPane name="b" label="Tab B">Tab B content</HuiTabPane
><HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane></HuiTabs
>
</div>
</div>
<div>
<p>Update listener only; still internal state</p>
<div class="q1-listener">
<HuiTabs type="browser" @update:model-value="q1Last = String($event)"
><HuiTabPane name="a" label="Tab A"
><HuiInput placeholder="Draft within this tab" /></HuiTabPane
><HuiTabPane name="b" label="Tab B">Tab B content</HuiTabPane
><HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane></HuiTabs
>
</div>
</div>
<div>
<p>Explicit undefined; no active tab</p>
<div class="q1-empty">
<HuiTabs type="browser" :model-value="undefined"
><HuiTabPane name="a" label="Tab A"
><HuiInput placeholder="Draft within this tab" /></HuiTabPane
><HuiTabPane name="b" label="Tab B">Tab B content</HuiTabPane
><HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane></HuiTabs
>
</div>
</div>
<div>
<p>Fixed controlled value; parent rejects updates</p>
<div class="q1-fixed">
<HuiTabs type="browser" model-value="a" @update:model-value="q1Last = String($event)"
><HuiTabPane name="a" label="Tab A"
><HuiInput placeholder="Draft within this tab" /></HuiTabPane
><HuiTabPane name="b" label="Tab B">Tab B content</HuiTabPane
><HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane></HuiTabs
>
</div>
</div>
<div>
<p>Two-way binding</p>
<div class="q1-bound">
<HuiTabs type="browser" v-model="q1Model"
><HuiTabPane name="a" label="Tab A"
><HuiInput placeholder="Draft within this tab" /></HuiTabPane
><HuiTabPane name="b" label="Tab B">Tab B content</HuiTabPane
><HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane></HuiTabs
>
</div>
</div>
<p class="q1-notification">
Bound value: {{ q1Model }}; latest request: {{ q1Last }}
</p></HuiSpace
>
</div>
</template>
Basic Usage
Bind the active panel's name with v-model. HuiTabPane's label provides tab text, and its default slot provides panel content.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<HuiTabs v-model="activeName">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">Active tab: {{ activeName }}</p>
</template>
Types
Type accepts:
- Line (default): a thin divider below the tab bar and a primary-colored underline on the active tab.
- Card: bordered tabs, with the active tab visually connected to its content.
- Border-card: a border surrounds the tabs and content, with a pale gray tab bar.
- Button: joined buttons resembling HuiRadioGroup type=button. The active tab uses a primary fill and white text. There is no active underline or divider; the bar and content have a 12px gap.
- Browser: browser-style tabs, supporting top placement only. Natural content width is used first; overflowing tabs shrink before scroll arrows appear.
These five instances share one bound value. Switching any one updates the others:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<div>
<p class="row-label">line</p>
<HuiTabs v-model="activeName" type="line">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled
>Role management content</HuiTabPane
>
</HuiTabs>
</div>
<div>
<p class="row-label">card</p>
<HuiTabs v-model="activeName" type="card">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled
>Role management content</HuiTabPane
>
</HuiTabs>
</div>
<div>
<p class="row-label">border-card</p>
<HuiTabs v-model="activeName" type="border-card">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled
>Role management content</HuiTabPane
>
</HuiTabs>
</div>
<div>
<p class="row-label">button</p>
<HuiTabs v-model="activeName" type="button">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled
>Role management content</HuiTabPane
>
</HuiTabs>
</div>
<div>
<p class="row-label">browser</p>
<HuiTabs v-model="activeName" type="browser">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled
>Role management content</HuiTabPane
>
</HuiTabs>
</div>
</HuiSpace>
</template>
<style scoped>
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Button-style details:
- Height, padding and radius follow size; default is 32px high, aligned with default buttons and inputs. Only the two outer ends of the row are rounded.
- Disabled tabs are translucent.
- Left and right positions stack buttons vertically. Bottom places the bar below the content.
- Closable buttons place the close icon after the text. The add control is a bordered square of matching height.
- Stretch distributes width equally. Overflow scrolls as with other styles.
Browser Tabs
Type=browser suits multipage application navigation. Tabs follow the natural width of text and icons, with a maximum for long labels. Stretch distributes the full row equally. The bar is transparent by default. The selected tab uses a pale primary background and primary text and stands 2px taller. Selected and hovered tabs share rounded upper corners and reverse bottom curves with a fixed radius joining the baseline smoothly. Hover mixes 80% of the selected background with transparency. The selected tab covers adjacent overlapping curves; no vertical separators or raised shadow are drawn. Browser tabs support only tab-position=top, and editable adds both creation and closing. The close button or middle mouse button closes closable tabs. A label slot can include icons.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiIcon, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
const activeName = ref('home')
const tabs = ref([
{ name: 'home', label: 'Home', icon: 'home', closable: false },
{ name: 'user', label: 'User Management', icon: 'user', closable: true },
{ name: 'settings', label: 'System Settings', icon: 'setting', closable: true },
{ name: 'reports', label: 'Reports', icon: 'file', closable: true }
])
let seed = 4
const add = () => {
seed += 1
const name = `tab-${seed}`
tabs.value = [...tabs.value, { name, label: `New tab ${seed}`, icon: 'file', closable: true }]
activeName.value = name
}
const remove = (name: string | number) => {
tabs.value = tabs.value.filter((tab) => tab.name !== name)
}
</script>
<template>
<HuiTabs v-model="activeName" type="browser" editable @tab-add="add" @tab-remove="remove">
<HuiTabPane
v-for="tab in tabs"
:key="tab.name"
:name="tab.name"
:label="tab.label"
:closable="tab.closable"
>
<template #label>
<HuiIcon :name="tab.icon" size="sm" /><span style="margin-left: 4px">{{ tab.label }}</span>
</template>
{{ tab.label }} content
</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">Click + to add; click × or the middle mouse button to close.</p>
</template>
With many tabs, widths shrink to the minimum before left/right scroll arrows appear. Truncated names receive a native hover title, including icon-bearing label slots. Fully visible labels do not. After resizing the container, the active tab remains scrolled into view. Only the active narrow tab retains its close button:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabPane, HuiTabs } from '@nscode/handy-ui'
const activeName = ref('tab-1')
const tabs = ref(
Array.from({ length: 24 }, (_, index) => ({
name: `tab-${index + 1}`,
label: `Workspace tab ${index + 1}`
}))
)
let seed = 24
const add = () => {
seed += 1
const name = `tab-${seed}`
tabs.value.push({ name, label: `Workspace tab ${seed}` })
activeName.value = name
}
const remove = (name: string | number) => {
tabs.value = tabs.value.filter((tab) => tab.name !== name)
}
</script>
<template>
<HuiTabs v-model="activeName" type="browser" editable @tab-add="add" @tab-remove="remove">
<HuiTabPane v-for="tab in tabs" :key="tab.name" :name="tab.name" :label="tab.label">
{{ tab.label }} content
</HuiTabPane>
</HuiTabs>
</template>
Sizes
Size accepts large, default or small for all five types. Browser supports only top placement; other styles support all four sides. Omitted size follows HuiConfigProvider, then default. HuiTabs is not a form control and does not inherit HuiForm size.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<div>
<p class="row-label">large</p>
<HuiTabs v-model="activeName" size="large">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
</div>
<div>
<p class="row-label">default</p>
<HuiTabs v-model="activeName" size="default">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
</div>
<div>
<p class="row-label">small</p>
<HuiTabs v-model="activeName" size="small">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
</div>
</HuiSpace>
</template>
<style scoped>
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Change size below to compare other styles with close and add controls:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRadio, HuiRadioGroup, HuiSpace, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
import type { TabsSize } from '@nscode/handy-ui'
const size = ref<TabsSize>('small')
const activeName = ref<string | number>('user')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiRadioGroup v-model="size" type="button" size="small">
<HuiRadio value="large">large</HuiRadio>
<HuiRadio value="default">default</HuiRadio>
<HuiRadio value="small">small</HuiRadio>
</HuiRadioGroup>
<HuiTabs v-model="activeName" type="card" :size="size" editable>
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
<HuiTabs v-model="activeName" type="border-card" :size="size">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
<HuiTabs v-model="activeName" type="button" :size="size" closable>
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
Size values:
| Item | large | default | small |
|---|---|---|---|
| Font size | 16px | 14px | 12px |
| Padding for line, card and border-card | 10px vertical, 16px horizontal | 8px vertical, 12px horizontal | 6px vertical, 8px horizontal |
| Approximate tab height for line, card and border-card | 44px | 37px | 30px |
| Button height | 40px | 32px | 24px |
| Button horizontal padding | 20px | 16px | 12px |
| Outer radius for card, border-card and button | 8px | 6px | 4px |
| Close control / icon | 18px / 16px | 16px / 14px | 14px / 12px |
| Scroll arrows and add icon | 16px | 14px | 12px |
| Border-card content padding | 20px | 16px | 12px |
The button-style add control is square and matches button height. Small border-card bar padding is reduced to 4px. All sizes keep a 12px gap between bar and content.
Tab Position
Tab-position accepts top (default), bottom, left or right. Side positions stack tabs vertically.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<div class="grid">
<div>
<p class="row-label">top</p>
<HuiTabs v-model="activeName" tab-position="top">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</div>
<div>
<p class="row-label">bottom</p>
<HuiTabs v-model="activeName" tab-position="bottom">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</div>
<div>
<p class="row-label">left</p>
<HuiTabs v-model="activeName" tab-position="left" style="height: 160px">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</div>
<div>
<p class="row-label">right</p>
<HuiTabs v-model="activeName" tab-position="right" style="height: 160px">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</div>
</div>
</template>
<style scoped>
.grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
gap: 24px;
}
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
All four positions work with the four non-browser styles. Switch them below to compare:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiRadio, HuiRadioGroup, HuiSpace, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
import type { TabsPosition, TabsType } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
const position = ref<TabsPosition>('left')
const type = ref<TabsType>('card')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiSpace wrap>
<HuiRadioGroup v-model="position" type="button" size="small">
<HuiRadio value="top">top</HuiRadio>
<HuiRadio value="right">right</HuiRadio>
<HuiRadio value="bottom">bottom</HuiRadio>
<HuiRadio value="left">left</HuiRadio>
</HuiRadioGroup>
<HuiRadioGroup v-model="type" type="button" size="small">
<HuiRadio value="line">line</HuiRadio>
<HuiRadio value="card">card</HuiRadio>
<HuiRadio value="border-card">border-card</HuiRadio>
<HuiRadio value="button">button</HuiRadio>
</HuiRadioGroup>
</HuiSpace>
<HuiTabs v-model="activeName" :tab-position="position" :type="type" style="height: 200px">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
Disabled Tabs
Disabled HuiTabPane labels ignore clicks and are skipped by keyboard navigation. They also hide their close buttons when closing is enabled.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiTabs v-model="activeName">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration" disabled>Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
<HuiTabs v-model="activeName" type="card" closable>
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration (disabled)" disabled
>Configuration content</HuiTabPane
>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
Stretch
Stretch gives every tab an equal share of the tab bar width, across all styles.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiTabs v-model="activeName" stretch>
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
<HuiTabs v-model="activeName" type="button" stretch>
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management">Role management content</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
No Explicit Active Tab
With modelValue omitted, the first enabled tab activates automatically and its name is reported through update:modelValue. Listener-only usage also stores the selection internally. Explicit undefined, including an initially undefined v-model, is a controlled empty selection and is not filled automatically. Without a pane name, its position supplies '0', '1', '2' and so on.
<template>
<HuiTabs>
<HuiTabPane label="First tab">First panel</HuiTabPane>
<HuiTabPane label="Second tab">Second panel</HuiTabPane>
</HuiTabs>
</template>
The model prop is omitted below. Since the first tab is disabled, the second activates automatically and the listener receives '1':
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>()
</script>
<template>
<HuiTabs @update:model-value="activeName = $event">
<HuiTabPane label="Disabled tab" disabled>Disabled panel</HuiTabPane>
<HuiTabPane label="Second tab">Second panel</HuiTabPane>
<HuiTabPane label="Third tab">Third panel</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">Bound value: {{ activeName }}</p>
</template>
Closable Tabs
Closable adds a close button to each tab. Clicking it emits tab-remove; the component does not remove the panel itself. Remove it from your data in that handler. Set closable=false on an individual HuiTabPane to keep it open.
Closing the active tab selects the next available tab, or the previous if there is no next one. Disabled tabs are skipped.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>('home')
const tabs = ref([
{ name: 'home', label: 'Home', closable: false },
{ name: 'user', label: 'User Management', closable: true },
{ name: 'role', label: 'Role Management', closable: true }
])
const removed = ref<Array<string | number>>([])
const handleRemove = (name: string | number) => {
tabs.value = tabs.value.filter((tab) => tab.name !== name)
removed.value.push(name)
}
</script>
<template>
<HuiTabs v-model="activeName" type="card" closable @tab-remove="handleRemove">
<HuiTabPane
v-for="tab in tabs"
:key="tab.name"
:name="tab.name"
:label="tab.label"
:closable="tab.closable"
>
{{ tab.label }} content
</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">
Active tab: {{ activeName }}; closed: {{ removed.length ? removed.join(', ') : 'None' }}
</p>
</template>
Adding and Closing Tabs
Addable shows a plus button at the end of the bar and emits tab-add. Editable combines closable and addable. Add new tabs to your own data and activate them as needed.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
let seed = 2
const activeName = ref<string | number>('tab-1')
const tabs = ref([
{ name: 'tab-1', label: 'Tab 1' },
{ name: 'tab-2', label: 'Tab 2' }
])
const handleAdd = () => {
seed += 1
const name = `tab-${seed}`
tabs.value = [...tabs.value, { name, label: `Tab ${seed}` }]
activeName.value = name
}
const handleRemove = (name: string | number) => {
tabs.value = tabs.value.filter((tab) => tab.name !== name)
}
</script>
<template>
<HuiTabs
v-model="activeName"
type="card"
editable
@tab-add="handleAdd"
@tab-remove="handleRemove"
>
<HuiTabPane v-for="tab in tabs" :key="tab.name" :name="tab.name" :label="tab.label">
{{ tab.label }} content
</HuiTabPane>
</HuiTabs>
</template>
With only addable, users can add but not close tabs. In button style, the add control is a matching square:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>(1)
const count = ref(2)
const handleAdd = () => {
count.value += 1
activeName.value = count.value
}
</script>
<template>
<HuiTabs v-model="activeName" type="button" addable @tab-add="handleAdd">
<HuiTabPane v-for="index in count" :key="index" :name="index" :label="`Option ${index}`">
Option {{ index }} content
</HuiTabPane>
</HuiTabs>
</template>
Many Tabs
Overflow adds arrow controls at both ends for backward/forward scrolling. Horizontal bars also support mouse-wheel scrolling. An active tab outside the viewport scrolls into view automatically. For left/right tab bars, give HuiTabs a height to enable scrolling.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
const activeName = ref<string | number>(1)
</script>
<template>
<HuiSpace direction="vertical" fill size="large">
<HuiTabs v-model="activeName">
<HuiTabPane v-for="index in 30" :key="index" :name="index" :label="`Tab ${index}`">
Panel {{ index }} content
</HuiTabPane>
</HuiTabs>
<HuiTabs v-model="activeName" tab-position="left" style="height: 200px">
<HuiTabPane v-for="index in 30" :key="index" :name="index" :label="`Tab ${index}`">
Panel {{ index }} content
</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
Custom Labels
Use HuiTabPane's label slot for icons, badges and other content. The label prop also accepts a VNode directly.
<script setup lang="ts">
import { h, ref } from 'vue'
import { HuiTabs, HuiTabPane, HuiIcon, HuiBadge } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
const mailLabel = h('span', [h(HuiIcon, { name: 'mail' }), ' Inbox'])
</script>
<template>
<HuiTabs v-model="activeName">
<HuiTabPane name="user">
<template #label>
<HuiIcon name="user" />
<span style="margin-left: 4px">User Management</span>
</template>
User management content
</HuiTabPane>
<HuiTabPane name="setting">
<template #label>
<HuiIcon name="setting" />
<span style="margin-left: 4px">System Settings</span>
</template>
System settings content
</HuiTabPane>
<HuiTabPane name="todo">
<template #label>
<HuiBadge :value="5">
<span style="padding-right: 8px">Tasks</span>
</HuiBadge>
</template>
Task content
</HuiTabPane>
<HuiTabPane name="mail" :label="mailLabel">VNode label</HuiTabPane>
</HuiTabs>
</template>
Lazy Rendering
All panel content renders initially by default; inactive panels are only hidden. Lazy delays rendering until the first activation, suitable for heavy content or panels that request data on mount. Once rendered, panels are not destroyed when switching away, whether lazy or not, so their internal state is retained.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
const activeName = ref<string | number>('basic')
</script>
<template>
<HuiTabs v-model="activeName">
<HuiTabPane name="basic" label="Basic Information">Rendered initially</HuiTabPane>
<HuiTabPane name="log" label="Activity Log" lazy>Rendered on first activation</HuiTabPane>
</HuiTabs>
</template>
Checking Before Switching
Before-leave receives the destination name and current name before switching. Returning false, resolving false, throwing or rejecting blocks the switch; undefined allows it. Successive requests use the latest result. An external model change, clicking the current tab, closing the active tab or unmounting invalidates older pending results. A removed or disabled pending target cannot become active. Cancellation only invalidates results; it does not abort a business request. Switching caused by closing the active tab does not call before-leave. To block closing, decide whether to remove it inside tab-remove.
Enable Form has changes below to prevent leaving Edit Form. Switching to Publish runs a 1-second check and switches only after approval:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMessage, HuiSpace, HuiSwitch, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
const activeName = ref<string | number>('form')
const dirty = ref(true)
const beforeLeave = (newName: string | number, oldName: string | number | undefined) => {
if (oldName === 'form' && dirty.value) {
HuiMessage.warning('The form has unsaved changes; switching is blocked')
return false
}
if (newName === 'publish') {
return new Promise<boolean>((resolve) => {
HuiMessage.info('Validating…')
setTimeout(() => resolve(true), 1000)
})
}
return true
}
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiSwitch v-model="dirty" active-text="Form has changes" />
<HuiTabs v-model="activeName" :before-leave="beforeLeave">
<HuiTabPane name="form" label="Edit Form">Form content</HuiTabPane>
<HuiTabPane name="preview" label="Preview">Preview content</HuiTabPane>
<HuiTabPane name="publish" label="Publish">Publishing settings</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
Latest Async Switch Wins
Release requests manually to observe old results becoming invalid. External model updates also take precedence. The caller remains responsible for cancelling business requests.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
interface WaitingTab {
name: string | number
resolve: () => void
}
const guardLabels: Record<string, string> = {
a: 'Home',
b: 'Second tab',
c: 'Third tab',
d: 'Fourth tab'
}
const guardActive = ref<string | number>('a')
const guardPending = ref<WaitingTab[]>([])
const guardBeforeLeave = (name: string | number) =>
new Promise<void>((resolve) => {
guardPending.value.push({ name, resolve })
})
const guardRelease = (name: string | number) => {
const index = guardPending.value.findIndex((request) => request.name === name)
if (index < 0) return
const request = guardPending.value[index]
guardPending.value = guardPending.value.filter((_, at) => at !== index)
request.resolve()
}
</script>
<template>
<HuiSpace direction="vertical" fill class="tabs-guard-demo">
<HuiSpace wrap>
<HuiButton @click="guardActive = 'd'">Switch externally to the fourth tab</HuiButton>
<HuiButton
v-for="(request, index) in guardPending"
:key="index"
@click="guardRelease(request.name)"
>
Approve {{ guardLabels[String(request.name)] }}
</HuiButton>
</HuiSpace>
<HuiTabs v-model="guardActive" :before-leave="guardBeforeLeave">
<HuiTabPane
v-for="name in ['a', 'b', 'c', 'd']"
:key="name"
:name="name"
:label="guardLabels[name]"
>
{{ guardLabels[name] }} content
</HuiTabPane>
</HuiTabs>
<span class="tabs-guard-result">Current tab: {{ guardLabels[String(guardActive)] }}</span>
<span
>Request the second then third tab. Release the third before the second; the third stays
active. Approval returns undefined.</span
>
</HuiSpace>
</template>
Tab Clicks
Tab-click reports the clicked tab's information, including clicks on the active tab. Disabled tabs do not emit it.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiTabs, HuiTabPane } from '@nscode/handy-ui'
import type { TabPaneInstance } from '@nscode/handy-ui'
const activeName = ref<string | number>('user')
const clicked = ref('')
const handleTabClick = (tab: TabPaneInstance) => {
clicked.value = `${tab.label}(name:${tab.name})`
}
</script>
<template>
<HuiTabs v-model="activeName" @tab-click="handleTabClick">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled>Role management content</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">Last clicked: {{ clicked || 'None' }}</p>
</template>
Context Menu
Tab-contextmenu provides tab information and the original event, including disabled tabs. Right-clicking does not activate a tab. The menu below refreshes panel content. See ContextMenu for complete close, close-others, close-right and close-all examples.
<script setup lang="ts">
import { ref } from 'vue'
import {
HuiTabs,
HuiTabPane,
HuiContextMenu,
HuiDropdownItem,
HuiMessage
} from '@nscode/handy-ui'
import type { ContextMenuExpose, DropdownCommand, TabPaneInstance } from '@nscode/handy-ui'
const active = ref<string | number | undefined>('overview')
const menu = ref<ContextMenuExpose>()
const tabs = ref([
{ name: 'overview', label: 'Overview', refresh: 0 },
{ name: 'files', label: 'Files', refresh: 0 }
])
const pane = (data: unknown) => data as TabPaneInstance | undefined
const handleCommand = (command: DropdownCommand, data: unknown) => {
const target = tabs.value.find((tab) => tab.name === pane(data)?.name)
if (!target) return
if (command === 'refresh') target.refresh++
else HuiMessage.info(`Tab: ${target.label}, name=${target.name}`)
}
</script>
<template>
<HuiTabs v-model="active" @tab-contextmenu="(tab, event) => menu?.open(event, tab)">
<HuiTabPane v-for="tab in tabs" :key="tab.name" :name="tab.name" :label="tab.label">
{{ tab.label }}, refreshed {{ tab.refresh }} times
</HuiTabPane>
</HuiTabs>
<HuiContextMenu ref="menu" @command="handleCommand">
<HuiDropdownItem command="refresh" icon="refresh">Refresh</HuiDropdownItem>
<HuiDropdownItem command="view" icon="eye">View tab information</HuiDropdownItem>
</HuiContextMenu>
</template>
Reading Tab Information
The HuiTabs ref exposes activeName and panes, containing the active name and all tab information in display order.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
const tabsRef = ref<InstanceType<typeof HuiTabs>>()
const activeName = ref<string | number>('user')
const info = ref('')
const read = () => {
const panes = tabsRef.value?.panes ?? []
const names = panes.map((pane) => `${pane.name}${pane.disabled ? ' (disabled)' : ''}`)
info.value = `activeName:${tabsRef.value?.activeName};panes:${names.join(', ')}`
}
</script>
<template>
<HuiTabs ref="tabsRef" v-model="activeName">
<HuiTabPane name="user" label="User Management">User management content</HuiTabPane>
<HuiTabPane name="config" label="Configuration">Configuration content</HuiTabPane>
<HuiTabPane name="role" label="Role Management" disabled>Role management content</HuiTabPane>
</HuiTabs>
<p style="margin-top: 12px">
<HuiButton size="small" @click="read">Read</HuiButton>
<span style="margin-left: 8px">{{ info }}</span>
</p>
</template>
Keyboard Interaction
The tab bar has one Tab stop. Entry from outside uses the active tab; moving focus inside updates the stop, which returns to the active tab after leaving. Without an available active tab, the most recently focused available tab is preferred, then the first available tab.
| Key | Action |
|---|---|
| Left / Right | Cycle focus in top/bottom tab bars, skipping disabled tabs |
| Up / Down | Cycle focus in left/right tab bars, skipping disabled tabs |
| Home / End | Focus the first/last available tab |
| Enter / Space | Activate the focused tab through beforeLeave |
| Delete / Backspace | Request closing the focused closable tab |
Arrows and Home/End only move focus. Composition, key code 229, Alt/Ctrl/Meta combinations, and events already consumed by child controls are ignored. PageUp/PageDown and text prefixes are not navigation keys.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiSpace, HuiTabPane, HuiTabs } from '@nscode/handy-ui'
import type { TabsPosition } from '@nscode/handy-ui'
const active = ref<string | number | undefined>('a')
const position = ref<TabsPosition>('top')
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiButton @click="position = position === 'top' ? 'left' : 'top'"
>Position: {{ position }}</HuiButton
>
<HuiTabs v-model="active" :tab-position="position">
<HuiTabPane name="a" label="A">Content A</HuiTabPane>
<HuiTabPane name="b" label="B (disabled)" disabled>Content B</HuiTabPane>
<HuiTabPane name="c" label="C">Content C</HuiTabPane>
<HuiTabPane name="d" label="D">Content D</HuiTabPane>
</HuiTabs>
<span>Active: {{ active }}. Arrows move focus; Enter or Space activates.</span>
</HuiSpace>
</template>
Stable name values retain focus through reordering. Removing, filtering, or disabling a focused tab restores a nearby available tab, preferring the following item. That restoration does not activate a tab or alter a controlled value. Closing the focused tab prefers the newly active tab. Focus already outside the bar is not pulled back.
Ancestor Variable Overrides
Public variables may be set on the component or any ancestor. Root, size and browser classes set only private defaults. Nested tabs inherit public configuration while resetting their own size/type defaults. Browser geometry applies only to that instance, without reaching into its content.
| Variable | Default | Description |
|---|---|---|
--hui-tabs-font-size | 16/14/12px by size | Tab font size |
--hui-tabs-item-padding | By size | Padding for line/card and related tabs |
--hui-tabs-radius | 8/6/4px by size | Regular tab radius |
--hui-tabs-control-size | 40/32/24px by size | Button/browser control height |
--hui-tabs-button-padding | 20/16/12px by size | Button horizontal padding |
--hui-tabs-close-size, --hui-tabs-icon-size | 18/16/14px and 16/14/12px by size | Close area and icon sizes |
--hui-tabs-browser-tab-width | 200px, 160px for small | Maximum browser width; shorter titles follow content width |
--hui-tabs-browser-tab-min-width | 72px | Minimum browser tab width |
--hui-tabs-browser-radius | 8px, 6px for small | Upper and reverse-bottom curve radius |
--hui-tabs-browser-bar-bg | transparent | Browser bar background |
--hui-tabs-browser-bg-active | Theme pale primary color | Active browser background |
--hui-tabs-browser-bg-hover | 80% of actual active background | Inactive hover background |
The former --hui-tabs-browser-active-bg and --hui-tabs-browser-hover-bg are renamed to --hui-tabs-browser-bg-active and --hui-tabs-browser-bg-hover, placing the state last without fallback aliases. Unconfigured tabs retain a transparent bar, natural width and soft curves; only long titles are capped by the maximum width.
<script setup lang="ts">
import { HuiTabs, HuiTabPane, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace
class="tabs-variables-demo"
direction="vertical"
fill
style="--hui-tabs-font-size: 18px; --hui-tabs-control-size: 40px; --hui-tabs-browser-radius: 12px; --hui-tabs-browser-tab-width: 220px; --hui-tabs-browser-bg-active: var(--hui-color-success-light-9)"
>
<span
>Ancestor variables set font size, height and browser curves; small and nested tabs still
inherit public settings.</span
>
<HuiTabs class="tabs-variable-button" type="button" size="small">
<HuiTabPane name="a" label="Overview">Button overview</HuiTabPane>
<HuiTabPane name="b" label="Records">Button records</HuiTabPane>
</HuiTabs>
<HuiTabs class="tabs-variable-browser" type="browser" size="small">
<HuiTabPane name="a" label="Overview">
<HuiTabs class="tabs-variable-inner" type="card">
<HuiTabPane name="inner-a" label="Inner A">Nested A content</HuiTabPane>
<HuiTabPane name="inner-b" label="Inner B">Nested B content</HuiTabPane>
</HuiTabs>
</HuiTabPane>
<HuiTabPane name="b" label="Development Project Schedule and Acceptance Records"
>Browser records</HuiTabPane
>
<HuiTabPane name="c" label="Disabled" disabled>Disabled content</HuiTabPane>
</HuiTabs>
</HuiSpace>
</template>
HuiTabs Props
| Prop | Type | Default | Description |
|---|---|---|---|
| modelValue | string | number | - | Active panel name, supporting v-model. Omitted values automatically select the first enabled tab |
| type | 'line' | 'card' | 'border-card' | 'button' | 'browser' | 'line' | Tab appearance. Button uses joined buttons; browser uses browser-style tabs |
| tabPosition | 'top' | 'right' | 'bottom' | 'left' | 'top' | Tab bar position |
| stretch | boolean | false | Whether tabs share the bar width equally |
| size | 'large' | 'default' | 'small' | - | Follows HuiConfigProvider, then default |
| closable | boolean | false | Whether tabs show close buttons |
| addable | boolean | false | Whether to show an add button |
| editable | boolean | false | Enables both closable and addable |
| beforeLeave | (activeName: string | number, oldActiveName: string | number | undefined) => boolean | void | Promise<boolean | void> | - | Check before switching. False, throws or rejections block; undefined allows. Latest request wins; not called for closing-induced switching |
HuiTabPane Props
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | number | - | Panel identifier matching the bound value. Omitted names use positional strings '0', '1', and so on |
| label | string | VNode | - | Label text, overridden by the label slot |
| disabled | boolean | false | Prevents activation and closing |
| closable | boolean | true | Whether this tab may close, effective only when HuiTabs enables closable or editable |
| lazy | boolean | false | Whether to delay content rendering until first activation |
HuiTabs Events
| Event | Callback Arguments | Description |
|---|---|---|
| update:modelValue | (value: string | number | undefined) | Active tab changes, including user switching, automatic first selection and neighbor activation after closing. Undefined with no active tab |
| tab-click | (tab: TabPaneInstance) | Click, Enter or Space on a tab, including the active tab or a switch blocked by beforeLeave. Disabled tabs do not emit |
| tab-contextmenu | (tab: TabPaneInstance, event: MouseEvent) | Right-click on any tab, including disabled tabs. Does not activate it or prevent the native event |
| tab-remove | (name: string | number) | Close button, or Delete/Backspace on a tab, with its name |
| tab-add | - | Add button is clicked |
HuiTabs Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | HuiTabPane children. Other content appears directly in the content area without creating tabs |
HuiTabPane Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | Panel content |
| label | - | Tab label content |
Exposed HuiTabs Properties
Read through a component ref. HuiTabs exposes no callable methods, only two read-only states:
| Property | Type | Description |
|---|---|---|
| activeName | string | number | undefined | Active tab name |
| panes | TabPaneInstance[] | Information for all tabs in display order |
Types
import type {
TabsProps,
TabPaneProps,
TabsType,
TabsPosition,
TabsSize,
TabPaneInstance
} from '@nscode/handy-ui'
import type { Ref, VNode } from 'vue'
type TabsType = 'line' | 'card' | 'border-card' | 'button' | 'browser'
type TabsPosition = 'top' | 'right' | 'bottom' | 'left'
type TabsSize = 'large' | 'default' | 'small'
interface TabsProps {
modelValue?: string | number
type?: TabsType
closable?: boolean
addable?: boolean
editable?: boolean
tabPosition?: TabsPosition
stretch?: boolean
size?: TabsSize
beforeLeave?: (
activeName: string | number,
oldActiveName: string | number | undefined
) => boolean | void | Promise<boolean | void>
}
interface TabPaneProps {
label?: string | VNode
name?: string | number
disabled?: boolean
closable?: boolean
lazy?: boolean
}
/** Argument of tab-click and each item in panes on the ref */
interface TabPaneInstance {
name: string | number
label: string | VNode
disabled: boolean
closable: boolean
lazy: boolean
rendered: Ref<boolean>
}
TabsContext and TABS_INJECTION_KEY are also exported for custom child components integrating with HuiTabs.