Tab tab page

introduce

Tab component for switching between different content areas.

Code Demo

Basic usage

Bind the index value corresponding to the currently active tag through v-model:active. The first tag is enabled by default.

<z-tabs v-model:active="active">
   <z-tab title="Tab 1">Content 1</z-tab>
   <z-tab title="Tab 2">Content 2</z-tab>
   <z-tab title="Tab 3">Content 3</z-tab>
   <z-tab title="Tab 4">Content 4</z-tab>
</z-tabs>
import { ref } from 'vue';
const active = ref(0);

Match by name

When the label specifies the name attribute, the value of v-model:active is the name of the current label (the label cannot be matched by the index value at this time).

<z-tabs v-model:active="activeName">
   <z-tab title="Tab 1" name="a">Content 1</z-tab>
   <z-tab title="Tab 2" name="b">Content 2</z-tab>
   <z-tab title="Tab 3" name="c">Content 3</z-tab>
</z-tabs>
import { ref } from 'vue';
const activeName = ref('b');

Tab bar scrolling

When the number of tabs exceeds 5, the tab bar can be scrolled horizontally, and the current tab will be automatically centered when switching.

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

Disable tags

A label can be disabled by setting the disabled attribute.

<z-tabs v-model:active="active">
   <z-tab title="Tab 1">Content 1</z-tab>
   <z-tab title="Tab 2" disabled>Content 2</z-tab>
   <z-tab title="Tab 3">Content 3</z-tab>
</z-tabs>

Style style

Tab supports two styles: line and card. The default is line style. You can switch the style through the type attribute.

<z-tabs v-model:active="active" type="card">
   <z-tab title="Tab 1">Content 1</z-tab>
   <z-tab title="Tab 2">Content 2</z-tab>
   <z-tab title="Tab 3">Content 3</z-tab>
</z-tabs>

Click event

When a tab is clicked, the click-tab event is triggered.

<z-tabs v-model:active="active" @click-tab="onClickTab">
   <z-tab title="Tab 1">Content 1</z-tab>
   <z-tab title="Tab 2">Content 2</z-tab>
</z-tabs>
import { ref } from 'vue';
import { useToast } from '../../uni_modules/zebra-ui'
const toast = useToast()
const onClickTab = ({ title }: any) => toast.showToast(title)

Sticky layout

Sticky layout can be turned on through the sticky attribute. Under sticky layout, the tab will automatically stick to the top when it is scrolled to the top.

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

Tips: If there is other content at the top of the page, you can set the distance from the top through the offset-top property. A custom navigation bar needs to declare the custom-navbar attribute.

Shrink layout

Shrink layout can be turned on through the shrink attribute. When turned on, all labels will be shrunk and aligned to the left.

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

Custom labels

Label content can be customized through the title slot.

<z-tabs v-model:active="active">
   <z-tab v-for="index in 2">
     <template #title> <z-icon name="star" />Options </template>
     Content {{ index }}
   </z-tab>
</z-tabs>

Switch animation

The transition animation when switching tag content can be turned on through the animated attribute.

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

Slide switch

Swipe switching tabs can be enabled through the swipeable attribute.

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

Scroll Navigation

The scroll navigation mode can be enabled through the scrollspy attribute. In this mode, the content will be displayed tiles.

<z-tabs v-model:active="activeScrollspy" scrollspy>
   <view style="height: 1000rpx">
     <z-tab
       v-for="(item, index) in 18"
       :key="index"
       :title="`Tag ${item}`"
     >
       <view class="demo-tab-item"> Content{{ item }} </view>
     </z-tab>
   </view>
</z-tabs>

Asynchronous switching

The before-change attribute allows you to perform specific logic before switching tags.

<z-tabs v-model:active="activeBefore" :before-change="beforeChange">
   <z-tab
     v-for="(item, index) in 4"
     :key="index"
     :title="`Tag ${item}`"
   >
     <view class="demo-tab-item"> Content{{ item }} </view>
   </z-tab>
</z-tabs>
const beforeChange = (index: any) => {
   if (index === 1) {
     return false
   }
   return new Promise((resolve) => {
     resolve(index !== 3)
   })
}

Tips: Swiping through gestures will not trigger the before-change attribute.

Hide title bar

The title bar of Tabs can not be rendered by setting the showHeader property to false. In this case, you can control the active property of Tabs through some custom components.

<z-tabs v-model:active="active" :show-header="false">
   <z-tab v-for="index in 4"> Content {{ index }} </z-tab>
</z-tabs>

API

Tabs Props

ParametersDescriptionTypeDefault value
v-model:activeBind the identifier of the currently selected tagnumber | string0
typeStyle type, optional value is cardstringline
colorTag theme colorstring#1989fa
backgroundtab bar background colorstringwhite
durationAnimation time in seconds, set to 0 to disable animationnumber | string0.3
line-widthBottom bar width, default unit pxnumber | string80rpx
line-heightBottom bar height, default unit pxnumber | string6rpx
animatedWhether to enable transition animation when switching tab contentbooleanfalse
borderWhether to display the outer border of the label bar, only valid when type="line"booleanfalse
ellipsisWhether to omit too long title textbooleantrue
stickyWhether to use sticky layoutbooleanfalse
shrinkWhether to enable left shrink layoutbooleanfalse
swipeableWhether to enable left and right sliding switching with gesturesbooleanfalse
scrollspyWhether to enable scroll navigationbooleanfalse
show-headerWhether to display the title barbooleantrue
offset-topThe distance from the top when ceiling is mounted in sticky layout, supports rpx unit, default is pxnumber | string0
swipe-thresholdScroll threshold, horizontal scrolling starts when the number of tabs exceeds the threshold and the total width exceeds the width of the tab barnumber | string5
title-active-colortitle active colorstring-
title-inactive-colorTitle default colorstring-
before-changeswitch The callback function before the label, returning false can prevent switching, and supports returning Promise(name: number | string) => boolean | Promise<boolean>-

Tab Props

ParametersDescriptionTypeDefault value
titletitlestring-
disabledWhether to disable the labelbooleanfalse
dotWhether to display a small red dot in the upper right corner of the titlebooleanfalse
badgeThe content of the logo in the upper right corner of the iconnumber | string-
nametag name as matching identifiernumber | stringindex value of the tag
title-styleCustom title stylestring | Array | object-
title-classCustom title class namestring | Array | object-
show-zero-badgeWhen badge is the number 0, whether to display the logobooleantrue

Tabs Events

Event nameDescriptionCallback parameters
click-tabTriggered when a tab is clicked{ name: string | number, title: string, event: MouseEvent, disabled: boolean }
changeTriggered when the currently active label changesname: string | number, title: string
scrollTriggered when scrolling, only effective in sticky mode{ scrollTop: number, isFixed: boolean }

Tabs method

Through ref, you can get the Tabs instance and call the instance method.

Method nameDescriptionParametersReturn value
resizeThis method can be called to trigger redrawing when the size of the outer element or the display state of the component changes--
scrollToScroll to the specified tab, available in scroll navigation modename: string | number-

Tabs Slots

NameDescription
nav-leftContent on the left side of the tab bar
nav-rightContent on the right side of the tab bar
nav-bottomContent below the tab bar

Tab Slots

NameDescription
defaulttab content
titlecustom title

Theme customization

Style variables

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

NameDefaultDescription
--z-tab-text-colorvar(--z-gray-7)-
--z-tab-active-text-colorvar(--z-text-color)-
--z-tab-disabled-text-colorvar(--z-text-color-3)-
--z-tab-font-sizevar(--z-font-size-md)-
--z-tab-line-heightvar(--z-line-height-md)-
--z-tabs-default-colorvar(--z-primary-color)-
--z-tabs-line-height88rpx-
--z-tabs-card-height60rpx-
--z-tabs-nav-backgroundvar(--z-background-2)-
--z-tabs-bottom-bar-width80rpx-
--z-tabs-bottom-bar-height6rpx-
--z-tabs-bottom-bar-colorvar(--z-primary-color)-