# Tab ### Install Register component globally via `app.use`, refer to [Component Registration](#/en-US/advanced-usage#zu-jian-zhu-ce) for more registration ways。 ```js import { createApp } from 'vue'; import { Tab, Tabs } from 'vant'; const app = createApp(); app.use(Tab); app.use(Tabs); ``` ## Usage ### Basic Usage The first tab is actived by default, you can set `v-model:active` to active specified tab. ```html content of tab {{ index }} ``` ```js import { ref } from 'vue'; export default { setup() { const active = ref(2); return { active }; }, }; ``` ### Match By Name ```html content of tab 1 content of tab 2 content of tab 3 ``` ```js import { ref } from 'vue'; export default { setup() { const activeName = ref('a'); return { activeName }; }, }; ``` ### Swipe Tabs By default more than 5 tabs, you can scroll through the tabs. You can set `swipe-threshold` attribute to customize threshold number. ```html content of tab {{ index }} ``` ### Disabled Tab You can set `disabled` attribute on the corresponding `van-tab`. ```html content of tab {{ index }} ``` ```js import { Toast } from 'vant'; export default { setup() { const onClickDisabled = (name, title) => Toast(`${name} is disabled`); return { onClickDisabled, }; }, }; ``` ### Card Style Tabs styled as cards. ```html content of tab {{ index }} ``` ### Click Event ```html content of tab {{ index }} ``` ```js import { Toast } from 'vant'; export default { setup() { const onClick = (name, title) => Toast(title); return { onClick, }; }, }; ``` ### Sticky In sticky mode, the tab will be fixed to top when scroll to top. ```html content {{ index }} ``` ### Custom title Use title slot to custom tab title. ```html content {{ index }} ``` ### Switch Animation Use `animated` props to change tabs with animation. ```html content {{ index }} ``` ### Swipeable In swipeable mode, you can switch tabs with swipe gestrue in the content. ```html content {{ index }} ``` ### Scrollspy In scrollspy mode, the list of content will be tiled. ```html content {{ index }} ``` ### Before Change ```html content {{ index }} ``` ```js export default { setup() { const beforeChange = (index) => { // prevent change if (index === 1) { return false; } // async return new Promise((resolve) => { resolve(index !== 3); }); }; return { beforeChange, }; }, }; ``` ## API ### Tabs Props | Attribute | Description | Type | Default | | --- | --- | --- | --- | | v-model:active | Index of active tab | _number \| string_ | `0` | | type | Can be set to `line` `card` | _string_ | `line` | | color | Tab color | _string_ | `#ee0a24` | | background | Background color | _string_ | `white` | | duration | Toggle tab's animation time | _number \| string_ | `0.3` | - | | line-width | Width of tab line | _number \| string_ | `40px` | | line-height | Height of tab line | _number \| string_ | `3px` | | animated | Whether to change tabs with animation | _boolean_ | `false` | | border | Whether to show border when `type="line"` | _boolean_ | `false` | | ellipsis | Whether to ellipsis too long title | _boolean_ | `true` | | sticky | Whether to use sticky mode | _boolean_ | `false` | | swipeable | Whether to enable gestures to slide left and right | _boolean_ | `false` | | lazy-render | Whether to enable tab content lazy render | _boolean_ | `true` | | scrollspy | Whether to use scrollspy mode | _boolean_ | `false` | | offset-top | Sticky offset top , supports `px` `vw` `vh` `rem` unit, default `px` | _number \| string_ | `0` | | swipe-threshold | Set swipe tabs threshold | _number \| string_ | `5` | - | | title-active-color | Title active color | _string_ | - | | title-inactive-color | Title inactive color | _string_ | - | | before-change | Callback function before changing tabs,return `false` to prevent change,support return Promise | _(name) => boolean \| Promise_ | - | ### Tab Props | Attribute | Description | Type | Default | | --- | --- | --- | --- | | title | Title | _string_ | - | | disabled | Whether to disable tab | _boolean_ | `false` | | dot | Whether to show red dot on the title | _boolean_ | `false` | | badge | Content of the badge on the title | _number \| string_ | - | | name | Identifier | _number \| string_ | Index of tab | | url | Link | _string_ | - | | to | Target route of the link, same as to of vue-router | _string \| object_ | - | | replace | If true, the navigation will not leave a history record | _boolean_ | `false` | | title-style | Custom title style | _string \| Array \| object_ | - | | title-class | Custom title class name | _string \| Array \| object_ | - | ### Tabs Events | Event | Description | Arguments | | --- | --- | --- | | click | Emitted when a tab is clicked | _name: string \| number, title: string_ | | change | Emitted when active tab changed | _name: string \| number, title: string_ | | disabled | Emitted when a disabled tab is clicked | _name: string \| number, title: string_ | | rendered | Emitted when content first rendered in lazy-render mode | _name: string \| number, title: string_ | | scroll | Emitted when tab scrolling in sticky mode | _{ scrollTop: number, isFixed: boolean }_ | ### Tabs Methods Use [ref](https://v3.vuejs.org/guide/component-template-refs.html) to get Tabs instance and call instance methods. | Name | Description | Attribute | Return value | | --- | --- | --- | --- | | resize | Resize Tabs when container element resized or visibility changed | - | - | | scrollTo | Go to specified tab in scrollspy mode | _name: string \| number_ | - | ### Tabs Slots | Name | Description | | --------- | ------------------------ | | nav-left | Custom nav left content | | nav-right | Custom nav right content | ### Tab Slots | Name | Description | | ------- | ---------------- | | default | Content of tab | | title | Custom tab title | ### Less Variables How to use: [Custom Theme](#/en-US/theme). | Name | Default Value | Description | | -------------------------- | --------------------- | ----------- | | @tab-text-color | `@gray-7` | - | | @tab-active-text-color | `@text-color` | - | | @tab-disabled-text-color | `@gray-5` | - | | @tab-font-size | `@font-size-md` | - | | @tab-line-height | `@line-height-md` | - | | @tabs-default-color | `@red` | - | | @tabs-line-height | `44px` | - | | @tabs-card-height | `30px` | - | | @tabs-nav-background-color | `@white` | - | | @tabs-bottom-bar-width | `40px` | - | | @tabs-bottom-bar-height | `3px` | - | | @tabs-bottom-bar-color | `@tabs-default-color` | - |