v-ons-tabbar

タブバーをページ下部に表示するためのコンポーネントです。ons-tabと組み合わせて使うことで、ページを管理できます。

実例

Tabs

The VOnsTabbar component is used to add tab navigation to an app. It is a very common navigation pattern in mobile apps.

The tabs prop includes all the necessary information to render VOnsTab components and their linked VOnsPage components. This prop must be an array of object containing at least one of the next keys: icon, label or page. It can include other optional keys such as activeIcon, badge, active, etc. A special props key can also be provided in order to specify props for the corresponding page.

<v-ons-tabbar
  :tabs="[
    { label: 't1', page: p1 },
    { label: 't2', page: p2, props: { aPageProp: 'hello' } }
  ]"
>
</v-ons-tabbar>

The mentioned tabs prop is the preferred way to provide tabs information to VOnsTabbar, although it is not the only one. It is also possible to provide slot="pages" and VOnsTab components directly as children instead:

<v-ons-tabbar>
  <template slot="pages">
    <home-page></home-page>
    <news-page></news-page>
    <settings-page></settings-page>
  </template>

  <v-ons-tab v-for="(tab, i) in tabs"
    :icon="tabs[i].icon"
    :label="tabs[i].label"
    :badge="tabs[i].badge"
    ></v-ons-tab>
</v-ons-tabbar>

This is a longer version that offers higher control. It can be combined with tabs prop by, for example, providing only VOnsTab-related information (everything except page key) in tabs prop and also slot="pages" at the same time (the VOnsTab will match page’s DOM index).

Synchronizing :index prop

The VOnsTabbar component implements an optional index prop which is used to specify the page that is currently visible. This component fires update:index events whenever the user taps on a VOnsTab. This is useful to synchronize the value of index prop and can be directly handled by using Vue’s sync modifier:

<v-ons-tabbar :index.sync="tabbarIndex"></v-ons-tabbar>

index prop is not completely necessary. If you don’t need to set an active tab in any other way than tapping, then it would be enough to provide active attribute (or key in tabs prop) to the desired VOnsTab in order to set the initial active tab.

The VOnsTab component

VOnsTab components have the following attributes/props:

  • icon: specifies the displayed icon.
  • label: specifies the displayed text label.
  • badge: shows a small badge on top of the tab.
  • activeIcon: allows to change the icon when the tab becomes active.
  • active: Whether the tab should be displayed as active or not. This is not necessary when using index prop.

Every tab has, by default, the same width. 50% with two tabs, 25% with four tabs and so on. To allow tabs grow depending on their content (i.e. shorter/ longer labels), use the autogrow modifier in v-ons-tabbar component. Optionally, max-width CSS property can be specified to set the width of the tab (for each v-ons-tab).

Swipes and Animations

By default, the tab bar will slide from one page to another on tab click. Use animation="none" attribute to have an instant change.

swipeable attribute can be used to enable this functionality. It can be toggled to allow or prevent swipes at different moments of the app.

These attributes can be combined to have a tab bar with instant changes that can also be swiped:

<v-ons-tabbar swipeable animation="none">...</v-ons-tabbar>

For iOS, tab-border attribute can be included to show a tab border that updates position during swipe (this is always default on Android).

Advanced usage

VOnsTab behavior can be overridden by running event.preventDefault on click event handler.

For example, this can provide fine control to support some of Vue’s cool features:

<v-ons-tabbar>
  <template slot="pages">
    <transition>
      <keep-alive>
        <component :is="currentPage"></component>
      </keep-alive>
    </transition>
  </template>

  <v-ons-tab
    @click.prevent="currentPage = 'home'"
    :active="currentPage === 'home'"
  ></v-ons-tab>
  <v-ons-tab
    @click.prevent="currentPage = 'settings'"
    :active="currentPage === 'settings'"
  ></v-ons-tab>
</v-ons-tabbar>

Notice that preventing the default behavior means that VOnsTabbar events (prechange, postchange, reactive…) are not fired. Also, it won’t be swipeable if only 1 page is provided at a time (when using :is="component", for example).

関連情報

Prop一覧

名前 型 / デフォルト値 概要
hide-tabs Boolean タブを非表示にする場合に指定します。 Optional.
ignore-edge-width Number Distance in pixels from both edges. Swiping on these areas will prioritize parent components such as ons-splitter or ons-navigator. (翻訳中) Optional.
index Number If exists, specifies the current active index. It is also used as the initial index. Must be modified on update:index event. (翻訳中) Optional.
modifier String タブバーの表現を指定します。 Optional.
on-swipe Function Hook called whenever the user slides the tabbar. It gets a decimal index and an animationOptions object as arguments. (翻訳中) Optional.
options.animation String If this attribute is set to "none" the transitions will not be animated. (翻訳中) Optional.
options.animationOptions Expression アニメーション時のduration, timing, delayをオブジェクトリテラルで指定します。e.g. {duration: 0.2, delay: 1, timing: ‘ease-in’} Optional.
position String タブバーの位置を指定します。”bottom”もしくは”top”を選択できます。デフォルトは”bottom”です。 Optional.
swipeable Boolean この属性がある時、タブバーをスワイプやドラッグで移動できるようになります。 Optional.
tab-border Boolean If this attribute is set the tabs show a dynamic bottom border. Only works for iOS flat design since the border is always visible in Material Design. (翻訳中) Optional.
tabbar-style Boolean Optional style for the actual tabbar component. Accepts any Vue valid style. (翻訳中) Optional.
tabs Array Contains as many objects as desired tabs in the tabbar. Every object describes a VOnsTab component. Every object must include at least one of the next properties: page, icon or label (see VOnsTab reference for more options). It is also possible to pass props to the pages through a props object for each tab. Example: tabs: [ { label: 'p1', page: p1 }, { label: 'p2', page: p2, props: { myPage2Prop: 'something' } } ]. This can be omitted if using slot="pages" and slot="tabs" components. (翻訳中) Optional.

プリセットのmodifier

Name 概要
material A tabbar in Material Design. (翻訳中)
autogrow Tabs automatically grow depending on their content instead of having a fixed width. (翻訳中)
top-border Shows a static border-bottom in tabs for iOS top tabbars. (翻訳中)

イベント

名前 概要
prechange アクティブなタブが変わる前に発火します。
postchange アクティブなタブが変わった後に発火します。
reactive すでにアクティブになっているタブがもう一度タップやクリックされた場合に発火します。
update:index Fired right after user interaction. Useful to update index prop. (翻訳中)
prechange

アクティブなタブが変わる前に発火します。

パラメーター
名前 概要
event Object イベントオブジェクト。
event.index Number 現在アクティブになっているons-tabのインデックスを返します。
event.tabItem Object tabItemオブジェクト。
event.cancel Function この関数を呼び出すと、アクティブなタブの変更がキャンセルされます。
postchange

アクティブなタブが変わった後に発火します。

パラメーター
名前 概要
event Object イベントオブジェクト。
event.index Number 現在アクティブになっているons-tabのインデックスを返します。
event.tabItem Object tabItemオブジェクト。
reactive

すでにアクティブになっているタブがもう一度タップやクリックされた場合に発火します。

パラメーター
名前 概要
event Object イベントオブジェクト。
event.index Number 現在アクティブになっているons-tabのインデックスを返します。
event.tabItem Object tabItemオブジェクト。
update:index

Fired right after user interaction. Useful to update index prop. (翻訳中)

パラメーター
名前 概要
event Number New value for index prop. (翻訳中)

議論

お困りですか?

Onsen UIに関する質問は、Stack Overflowにてonsen-uiタグを付与してください。Onsen UIチームはあなたの問題解決をお手伝いします。

バグ報告や機能要望については、GitHub Issuesに記載をお願いいたします。

あわせて、下記の情報も参考にしてください。