import{o as a,a as t,y as n}from"./vue-libs.b44bc779.js";const e={class:"van-doc-markdown-body"},l=n(`

Tab

Intro

Used to switch between different content areas.

Install

Register component globally via app.use, refer to Component Registration for more registration ways.

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 active by default, you can set v-model:active to active specified tab.

<van-tabs v-model:active="active">
  <van-tab v-for="index in 4" :title="'tab' + index">
    content of tab {{ index }}
  </van-tab>
</van-tabs>
import { ref } from 'vue';

export default {
  setup() {
    const active = ref(0);
    return { active };
  },
};

Match By Name

<van-tabs v-model:active="activeName">
  <van-tab title="tab 1" name="a">content of tab 1</van-tab>
  <van-tab title="tab 2" name="b">content of tab 2</van-tab>
  <van-tab title="tab 3" name="c">content of tab 3</van-tab>
</van-tabs>
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.

<van-tabs v-model:active="active">
  <van-tab v-for="index in 8" :title="'tab' + index">
    content of tab {{ index }}
  </van-tab>
</van-tabs>

Disabled Tab

Use disabled prop to disable a tab.

<van-tabs v-model:active="active">
  <van-tab v-for="index in 3" :title="'tab' + index" :disabled="index === 2">
    content of tab {{ index }}
  </van-tab>
</van-tabs>

Card Style

Tabs styled as cards.

<van-tabs v-model:active="active" type="card">
  <van-tab v-for="index in 3" :title="'tab' + index">
    content of tab {{ index }}
  </van-tab>
</van-tabs>

Click Tab Event

<van-tabs v-model:active="active" @click-tab="onClickTab">
  <van-tab v-for="index in 2" :title="'tab' + index">
    content of tab {{ index }}
  </van-tab>
</van-tabs>
import { Toast } from 'vant';

export default {
  setup() {
    const onClickTab = ({ title }) => Toast(title);
    return {
      onClickTab,
    };
  },
};

Sticky

In sticky mode, the tab nav will be fixed to top when scroll to top.

<van-tabs v-model:active="active" sticky>
  <van-tab v-for="index in 4" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>

Shrink

In shrink mode, the tabs will be shrinked to the left.

<van-tabs v-model:active="active" shrink>
  <van-tab v-for="index in 4" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>

Custom title

Use title slot to custom tab title.

<van-tabs v-model:active="active">
  <van-tab v-for="index in 2">
    <template #title> <van-icon name="more-o" />tab </template>
    content {{ index }}
  </van-tab>
</van-tabs>

Switch Animation

Use animated props to change tabs with animation.

<van-tabs v-model:active="active" animated>
  <van-tab v-for="index in 4" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>

Swipeable

In swipeable mode, you can switch tabs with swipe gesture in the content.

<van-tabs v-model:active="active" swipeable>
  <van-tab v-for="index in 4" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>

Scrollspy

In scrollspy mode, the list of content will be tiled.

<van-tabs v-model:active="active" scrollspy sticky>
  <van-tab v-for="index in 8" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>

Before Change

<van-tabs v-model:active="active" :before-change="beforeChange">
  <van-tab v-for="index in 4" :title="'tab ' + index">
    content {{ index }}
  </van-tab>
</van-tabs>
import { ref } from 'vue';

export default {
  setup() {
    const active = ref(0);
    const beforeChange = (index) => {
      // prevent change
      if (index === 1) {
        return false;
      }

      // async
      return new Promise((resolve) => {
        resolve(index !== 3);
      });
    };

    return {
      active,
      beforeChange,
    };
  },
};

API

Tabs Props

AttributeDescriptionTypeDefault
v-model:activeIndex of active tabnumber | string0
typeCan be set to line cardstringline
colorTab colorstring#ee0a24
backgroundBackground colorstringwhite
durationToggle tab's animation timenumber | string0.3
line-widthWidth of tab linenumber | string40px
line-heightHeight of tab linenumber | string3px
animatedWhether to change tabs with animationbooleanfalse
borderWhether to show border when type="line"booleanfalse
ellipsisWhether to ellipsis too long titlebooleantrue
stickyWhether to use sticky modebooleanfalse
shrink v3.2.8Whether to shrink the the tabs to the leftbooleanfalse
swipeableWhether to enable gestures to slide left and rightbooleanfalse
lazy-renderWhether to enable tab content lazy renderbooleantrue
scrollspyWhether to use scrollspy modebooleanfalse
offset-topSticky offset top , supports px vw vh rem unit, default pxnumber | string0
swipe-thresholdSet swipe tabs thresholdnumber | string5
title-active-colorTitle active colorstring-
title-inactive-colorTitle inactive colorstring-
before-changeCallback function before changing tabs, return false to prevent change, support return Promise(name: number | string) => boolean | Promise<boolean>-

Tab Props

AttributeDescriptionTypeDefault
titleTitlestring-
disabledWhether to disable tabbooleanfalse
dotWhether to show red dot on the titlebooleanfalse
badgeContent of the badge on the titlenumber | string-
nameIdentifiernumber | stringIndex of tab
urlLinkstring-
toTarget route of the link, same as to of vue-routerstring | object-
replaceIf true, the navigation will not leave a history recordbooleanfalse
title-styleCustom title stylestring | Array | object-
title-classCustom title class namestring | Array | object-
show-zero-badge v3.2.2Whether to show badge when the value is zerobooleantrue

Tabs Events

EventDescriptionArguments
click-tab v3.1.4Emitted when a tab is clicked{ name: string | number, title: string, event: MouseEvent, disabled: boolean }
changeEmitted when active tab changedname: string | number, title: string
renderedEmitted when content first rendered in lazy-render modename: string | number, title: string
scrollEmitted when tab scrolling in sticky mode{ scrollTop: number, isFixed: boolean }

Tips: click and disabled event is deprecated, place use click-tab event instead.

Tabs Methods

Use ref to get Tabs instance and call instance methods.

NameDescriptionAttributeReturn value
resizeResize Tabs when container element resized or visibility changed--
scrollToGo to specified tab in scrollspy modename: string | number-

Types

The component exports the following type definitions:

import type { TabProps, TabsType, TabsProps, TabsInstance } from 'vant';

TabsInstance is the type of component instance:

import { ref } from 'vue';
import type { TabsInstance } from 'vant';

const tabsRef = ref<TabsInstance>();

tabsRef.value?.scrollTo(0);

Tabs Slots

NameDescription
nav-leftCustom nav left content
nav-rightCustom nav right content
nav-bottom v3.1.1Custom nav bottom content

Tab Slots

NameDescription
defaultContent of tab
titleCustom tab title

Theming

CSS Variables

The component provides the following CSS variables, which can be used to customize styles. Please refer to ConfigProvider component.

NameDefault ValueDescription
--van-tab-text-colorvar(--van-gray-7)-
--van-tab-active-text-colorvar(--van-text-color)-
--van-tab-disabled-text-colorvar(--van-text-color-3)-
--van-tab-font-sizevar(--van-font-size-md)-
--van-tab-line-heightvar(--van-line-height-md)-
--van-tabs-default-colorvar(--van-danger-color)-
--van-tabs-line-height44px-
--van-tabs-card-height30px-
--van-tabs-nav-background-colorvar(--van-background-color-light)-
--van-tabs-bottom-bar-width40px-
--van-tabs-bottom-bar-height3px-
--van-tabs-bottom-bar-colorvar(--van-danger-color)-
`,27),d=[l],h={__name:"README",setup(p,{expose:s}){return s({frontmatter:{}}),(o,r)=>(a(),t("div",e,d))}};export{h as default};