Tabs
Tab component for switching between different content areas.
Component Types
Basic Usage
v-model accepts a numeric index or a string or numeric name.
<wd-tabs v-model="tab1" @change="handleChange">
<block v-for="item in 4" :key="item">
<wd-tab :title="`Tab${item}`">
<view class="content">Content{{ tab1 + 1 }}</view>
</wd-tab>
</block>
</wd-tabs>const tab1 = ref(0)
function handleChange(event) {
console.log(event)
}Name Matching
Set name on wd-tab to select a tab by its string or numeric name. Names use strict equality: the number 1 and the string '1' are different names.
<wd-tabs v-model="tab">
<wd-tab v-for="item in tabs" :key="item" :title="item" :name="item">
<view class="content">Content{{ tab }}</view>
</wd-tab>
</wd-tabs>const tabs = ref(['this', 'is', 'a', 'individual', 'example'])
const tab = ref('a')Numeric names can be negative, zero, or nonconsecutive. Bind them to wd-tab with :name:
<wd-tabs v-model="numericTab" swipeable>
<wd-tab v-for="name in numericTabs" :key="name" :name="name" :title="`Tab${name}`" :disabled="name === 0">
<view class="content">Content{{ name }}</view>
</wd-tab>
</wd-tabs>const numericTabs = [-1, 0, 1, 3, 2]
const numericTab = ref(-1)Tabs without a name use their zero-based index as their identifier. Explicitly set names take precedence; if no name matches, a valid numeric index can still select a tab. Unmatched names and invalid indices fall back to the first tab, while disabled tabs cannot be activated. Switching tabs updates the bound value to the selected tab's name, or its index if no name is set.
If an explicit numeric name equals an unnamed tab's index, the explicit name takes precedence. For example, if the first tab has no name and the second has :name="0", binding 0 selects the second tab. However, both tabs then have the identifier 0, so the component logs a warning. A single bound value cannot distinguish these tabs, and reliable two-way binding is not supported with duplicate identifiers. All identifiers (explicit names or default indices) must be unique. When mixing named and unnamed tabs, consider assigning a unique name to every tab.
Use Badge
Add badges to tabs through badge-props.
<wd-tabs v-model="tabWithBadge">
<wd-tab v-for="(item, index) in tabsWithBadge" :key="index" :title="item.title" :badge-props="item.badgeProps">
<view class="content">{{ item.title }} Badge</view>
</wd-tab>
</wd-tabs>Component States
Sticky Layout
Setting sticky enables sticky layout, can be combined with offset-top to control sticky offset.
<wd-tabs v-model="tab2" sticky>
<wd-tab v-for="item in 4" :key="item" :title="`Tab${item}`">
<view class="content">Content{{ tab2 + 1 }}</view>
</wd-tab>
</wd-tabs>Disable Tab
Disable individual tabs through the disabled property of wd-tab.
<wd-tabs v-model="tab3">
<wd-tab v-for="item in 4" :key="item" :title="`Tab${item}`" :disabled="item === 1">
<view class="content">Content{{ tab3 + 1 }}</view>
</wd-tab>
</wd-tabs>Component Styles
Bottom Bar Style
Adjust bottom bar appearance through line-theme, supports normal, text, underline, dot.
<wd-tabs v-model="tabLineTheme.normal" line-theme="normal">
<wd-tab v-for="item in 4" :key="item" :title="`normal ${item}`">
<view class="content">Content{{ item }}</view>
</wd-tab>
</wd-tabs>Special Styles
Click Event
Listen to click to get information about the currently clicked tab.
<wd-tabs v-model="tab4" @click="handleClick" @change="handleChange">
<wd-tab v-for="item in 4" :key="item" :title="`Tab${item}`">
<view class="content">Content{{ tab4 + 1 }}</view>
</wd-tab>
</wd-tabs>function handleClick({ index, name }) {
console.log(index, name)
}Switch Animation
Setting animated enables content switch animation.
<wd-tabs v-model="tab8" animated>
<wd-tab v-for="item in 4" :key="item" :title="`Tab${item}`">
<view class="content">Content{{ tab8 + 1 }}</view>
</wd-tab>
</wd-tabs>Gesture Swipe
Setting swipeable enables gesture swipe, often used in combination with animated.
<wd-tabs v-model="tab5" swipeable animated>
<wd-tab v-for="item in 4" :key="item" :title="`Tab${item}`">
<view class="content">Content{{ tab5 + 1 }}</view>
</wd-tab>
</wd-tabs>Overflow Scroll and Navigation Map
After the number of tabs exceeds slidable-num, scrolling is enabled. After exceeding map-num, a navigation map is displayed. Setting slidable to always enables always left-aligned scrolling.
<wd-tabs v-model="tab6">
<wd-tab v-for="item in 7" :key="item" :title="`Tab${item}`">
<view class="content">Content{{ tab6 + 1 }}</view>
</wd-tab>
</wd-tabs>
<wd-tabs v-model="tab9" slidable="always">
<wd-tab v-for="item in 5" :key="item" :title="`Large Tab${item}`">
<view class="content">Content{{ tab9 + 1 }}</view>
</wd-tab>
</wd-tabs>Use in Popup
In WeChat Mini Program and other scenarios, after the popup opens, you can call updateLineStyle to update the active item style.
<wd-button @click="handleOpenClick">Open Popup</wd-button>
<wd-popup v-model="showPopup" position="bottom" @after-enter="handlePopupShow" closable>
<wd-tabs v-model="tab10" ref="tabsRef">
<wd-tab v-for="item in tabs" :key="item" :title="item" :name="item">
<view class="content">Content{{ tab10 }}</view>
</wd-tab>
</wd-tabs>
</wd-popup>import type { TabsInstance } from '@/uni_modules/wot-ui/components/wd-tabs/types'
const showPopup = ref(false)
const tabsRef = ref<TabsInstance>()
function handleOpenClick() {
showPopup.value = true
}
function handlePopupShow() {
tabsRef.value?.updateLineStyle(false)
}Tabs Attributes
| Parameter | Description | Type | Default Value |
|---|---|---|---|
| v-model | Active item; explicit names take precedence, falling back to a valid numeric index when no name matches | number | string | 0 |
| slidable-num | Threshold for automatically enabling scrollable tabs | number | 6 |
| map-num | Threshold for showing navigation map | number | 10 |
| map-title | Navigation map title | string | - |
| sticky | Whether to enable sticky layout | boolean | false |
| offset-top | Sticky offset | number | 0 |
| swipeable | Whether to enable gesture swipe | boolean | false |
| line-theme | Bottom bar style, optional values are normal, text, underline, dot | TabsLineTheme | normal |
| line-width | Bottom bar width | `number | string` |
| line-height | Bottom bar height | `number | string` |
| color | Active item text color | string | '' |
| inactive-color | Inactive item text color | string | '' |
| animated | Whether to enable switch animation | boolean | false |
| duration | Animation duration, in milliseconds | number | 300 |
| slidable | Whether to enable scroll navigation, optional values are auto, always | TabsSlidable | auto |
| show-scrollbar | Whether to show scrollbar when scrolling | boolean | false |
| custom-class | Root node custom class name | string | '' |
| custom-style | Root node custom style | string | '' |
Tabs Events
| Event Name | Description | Parameters |
|---|---|---|
| change | Triggered when active item changes | { index, name } |
| click | Triggered when clicking tab title | { index, name } |
| disabled | Triggered when clicking disabled tab | { index, name } |
| update:modelValue | Triggered when active item changes | `number |
Tabs Methods
| Method Name | Description | Parameters | Return Value |
|---|---|---|---|
| setActive | Set active item | (value: number | string, init: boolean, setScroll: boolean) | - |
| scrollIntoView | Scroll selected item into view | - | - |
| updateLineStyle | Update active item bottom bar style | (animation?: boolean) | - |
Tabs Slots
| Name | Description |
|---|---|
| default | Tab content |
Tab Attributes
| Parameter | Description | Type | Default Value |
|---|---|---|---|
| name | Tab unique identifier, defaults to index | `number | string` |
| title | Tab title | string | - |
| disabled | Whether disabled | boolean | false |
| lazy | Whether to lazy load content | boolean | true |
| badge-props | Badge properties, passed to Badge component | Partial<BadgeProps> | - |
| custom-class | Root node custom class name | string | '' |
| custom-style | Root node custom style | string | '' |
Tab Slots
| Name | Description |
|---|---|
| default | Tab content |
主题定制
CSS 变量
组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置。
| 名称 | 默认值 | 描述 |
|---|---|---|
| --wot-tabs-nav-bg | $filled-oppo | 导航背景色 |
| --wot-tabs-nav-sticky-width | 100vw | 导航吸顶宽度 |
| --wot-tabs-nav-color | $text-secondary | 导航文字颜色 |
| --wot-tabs-nav-color-active | $primary-6 | 导航激活文字颜色 |
| --wot-tabs-nav-font-weight-active | $font-weight-medium | 导航激活文字字重 |
| --wot-tabs-nav-item-font-size | $typography-body-size-extra-large | 导航项字号 |
| --wot-tabs-nav-item-line-height | $typography-body-line-height-size-extra-large | 导航项行高 |
| --wot-tabs-nav-item-padding | $padding-main $padding-extra-loose | 导航项内边距 |
| --wot-tabs-nav-color-disabled | $text-disabled | 导航禁用文字颜色 |
| --wot-tabs-nav-line-bg | $primary-6 | 导航线背景色 |
| --wot-tabs-nav-line-height | $n-4 | 导航线高度 |
| --wot-tabs-nav-line-width | $n-24 | 导航线宽度 |
| --wot-tabs-nav-line-bottom | $n-2 | 导航线底部偏移 |
| --wot-tabs-duration | 0.3s | 过渡时长 |
| --wot-tabs-map-modal-bg | $opac-7_55 | 地图模式遮罩背景 |
| --wot-tabs-map-arrow-size | $n-16 | 地图模式箭头尺寸 |
| --wot-tabs-map-arrow-color | $icon-secondary | 地图模式箭头颜色 |
| --wot-tabs-map-btn-width | $n-44 | 地图模式按钮宽度 |
| --wot-tabs-map-btn-height | $n-44 | 地图模式按钮高度 |
| --wot-tabs-map-btn-before-bg | linear-gradient(270deg, $filled-oppo 0%, $base-transparent 100%) | 地图模式按钮前景渐变背景 |
| --wot-tabs-map-color | $text-main | 地图模式通用文字颜色 |
| --wot-tabs-map-header-font-size | $typography-body-size-extra-large | 地图模式头部字号 |
| --wot-tabs-map-header-line-height | $typography-body-line-height-size-extra-large | 地图模式头部行高 |
| --wot-tabs-map-header-padding | $padding-main $padding-extra-loose | 地图模式头部内边距 |
| --wot-tabs-map-nav-btn-gap | $spacing-main | 地图模式导航按钮间距 |
| --wot-tabs-map-nav-btn-disabled-opacity | $opacity-disabled | 地图模式导航按钮禁用透明度 |
| --wot-tabs-map-nav-btn-bg | $filled-bottom | 地图模式导航按钮背景 |
| --wot-tabs-map-nav-btn-color | $text-main | 地图模式导航按钮文字颜色 |
| --wot-tabs-map-nav-btn-radius | $radius-main | 地图模式导航按钮圆角 |
| --wot-tabs-map-nav-btn-font-size | $typography-body-size-ultra-large | 地图模式导航按钮字号 |
| --wot-tabs-map-nav-btn-line-height | $typography-body-line-height-size-ultra-large | 地图模式导航按钮行高 |
| --wot-tabs-map-nav-btn-padding | $padding-tight $padding-extra-loose | 地图模式导航按钮内边距 |
| --wot-tabs-map-body-padding | $padding-loose $padding-extra-loose | 地图模式内容内边距 |
| --wot-tabs-map-body-bg | $filled-oppo | 地图模式内容背景色 |