Tabs organize related content at the same level, one view at a time. M3 has two variants: primary tabs sit at the top of the content pane, under an app bar, and secondary tabs subdivide one of those views. Tabs are for related content, not sequential steps.
Usage
Start one tab with state: 'active'; the component does not pick one. change fires with the
tab's value whenever the selection changes.
import { createTabs } from 'material';
const tabs = createTabs({
tabs: [
{ text: 'Flights', value: 'flights', state: 'active' },
{ text: 'Trips', value: 'trips' },
{ text: 'Explore', value: 'explore' },
],
});
tabs.on('change', ({ value }) => showPanel(value));
document.body.append(tabs.element);
<m-tabs value="flights">
<m-tab value="flights">Flights</m-tab>
<m-tab value="trips">Trips</m-tab>
<m-tab value="explore">Explore</m-tab>
</m-tabs>
<script type="module">
const tabs = document.querySelector('m-tabs');
tabs.addEventListener('change', (event) => showPanel(event.detail.value));
</script>
import { useState } from 'react';
import { Tabs, Tab } from 'material/react';
import { showPanel } from './app';
export function Example() {
const [value, setValue] = useState("flights");
return (
<Tabs value={value} onChange={(event) => { setValue(event.detail.value); showPanel(event.detail.value); }}>
<Tab value="flights">Flights</Tab>
<Tab value="trips">Trips</Tab>
<Tab value="explore">Explore</Tab>
</Tabs>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MTabs, MTab } from 'material/vue';
import { showPanel } from './app';
const value = ref("flights");
</script>
<template>
<MTabs v-model="value" @change="showPanel($event.detail.value)">
<MTab value="flights">Flights</MTab>
<MTab value="trips">Trips</MTab>
<MTab value="explore">Explore</MTab>
</MTabs>
</template>
<script lang="ts">
import { Tabs, Tab } from 'material/svelte';
import { showPanel } from './app';
let value = $state("flights");
</script>
<Tabs bind:value onchange={(event) => showPanel(event.detail.value)}>
<Tab value="flights">Flights</Tab>
<Tab value="trips">Trips</Tab>
<Tab value="explore">Explore</Tab>
</Tabs>
import { createSignal } from 'solid-js';
import { Tabs, Tab } from 'material/solid';
import { showPanel } from './app';
export function Example() {
const [value, setValue] = createSignal("flights");
return (
<Tabs value={value()} onChange={(event) => { setValue(event.detail.value); showPanel(event.detail.value); }}>
<Tab value="flights">Flights</Tab>
<Tab value="trips">Trips</Tab>
<Tab value="explore">Explore</Tab>
</Tabs>
);
}
Examples
Icons and a badge
With a label, the icon sits above it and the row is 64dp tall. A tab with an icon and no
label needs ariaLabel; without it the tab's name falls back to its value. A badge is a
count or a short text, four characters at most.
import { createTabs } from 'material';
const tabs = createTabs({
tabs: [
{ text: 'Video', value: 'video', icon: videoIcon, state: 'active' },
{ text: 'Photos', value: 'photos', icon: photoIcon, badge: 3 },
{ text: 'Audio', value: 'audio', icon: audioIcon },
],
});
document.body.append(tabs.element);
<m-tabs value="video">
<m-tab value="video">Video</m-tab>
<m-tab value="photos" badge="3">Photos</m-tab>
<m-tab value="audio">Audio</m-tab>
</m-tabs>
<script type="module">
const tabs = document.querySelector('m-tabs');
tabs.querySelector('m-tab[value="video"]').setAttribute('icon', videoIcon);
tabs.querySelector('m-tab[value="photos"]').setAttribute('icon', photoIcon);
tabs.querySelector('m-tab[value="audio"]').setAttribute('icon', audioIcon);
</script>
import { useState } from 'react';
import { Tabs, Tab } from 'material/react';
import { videoIcon, photoIcon, audioIcon } from './app';
export function Example() {
const [value, setValue] = useState("video");
return (
<Tabs value={value} onChange={(event) => setValue(event.detail.value)}>
<Tab value="video" icon={videoIcon}>Video</Tab>
<Tab value="photos" icon={photoIcon} badge="3">Photos</Tab>
<Tab value="audio" icon={audioIcon}>Audio</Tab>
</Tabs>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MTabs, MTab } from 'material/vue';
import { videoIcon, photoIcon, audioIcon } from './app';
const value = ref("video");
</script>
<template>
<MTabs v-model="value">
<MTab value="video" :icon="videoIcon">Video</MTab>
<MTab value="photos" :icon="photoIcon" badge="3">Photos</MTab>
<MTab value="audio" :icon="audioIcon">Audio</MTab>
</MTabs>
</template>
<script lang="ts">
import { Tabs, Tab } from 'material/svelte';
import { videoIcon, photoIcon, audioIcon } from './app';
let value = $state("video");
</script>
<Tabs bind:value>
<Tab value="video" icon={videoIcon}>Video</Tab>
<Tab value="photos" icon={photoIcon} badge="3">Photos</Tab>
<Tab value="audio" icon={audioIcon}>Audio</Tab>
</Tabs>
import { createSignal } from 'solid-js';
import { Tabs, Tab } from 'material/solid';
import { videoIcon, photoIcon, audioIcon } from './app';
export function Example() {
const [value, setValue] = createSignal("video");
return (
<Tabs value={value()} onChange={(event) => setValue(event.detail.value)}>
<Tab value="video" icon={videoIcon}>Video</Tab>
<Tab value="photos" icon={photoIcon} badge="3">Photos</Tab>
<Tab value="audio" icon={audioIcon}>Audio</Tab>
</Tabs>
);
}
Secondary
import { createTabs } from 'material';
const tabs = createTabs({
variant: 'secondary',
tabs: [
{ text: 'Overview', value: 'overview', state: 'active' },
{ text: 'Specifications', value: 'specs' },
],
});
document.body.append(tabs.element);
<m-tabs value="overview" variant="secondary">
<m-tab value="overview">Overview</m-tab>
<m-tab value="specs">Specifications</m-tab>
</m-tabs>
import { useState } from 'react';
import { Tabs, Tab } from 'material/react';
export function Example() {
const [value, setValue] = useState("overview");
return (
<Tabs value={value} onChange={(event) => setValue(event.detail.value)} variant="secondary">
<Tab value="overview">Overview</Tab>
<Tab value="specs">Specifications</Tab>
</Tabs>
);
}
<script setup lang="ts">
import { ref } from 'vue';
import { MTabs, MTab } from 'material/vue';
const value = ref("overview");
</script>
<template>
<MTabs v-model="value" variant="secondary">
<MTab value="overview">Overview</MTab>
<MTab value="specs">Specifications</MTab>
</MTabs>
</template>
<script lang="ts">
import { Tabs, Tab } from 'material/svelte';
let value = $state("overview");
</script>
<Tabs bind:value variant="secondary">
<Tab value="overview">Overview</Tab>
<Tab value="specs">Specifications</Tab>
</Tabs>
import { createSignal } from 'solid-js';
import { Tabs, Tab } from 'material/solid';
export function Example() {
const [value, setValue] = createSignal("overview");
return (
<Tabs value={value()} onChange={(event) => setValue(event.detail.value)} variant="secondary">
<Tab value="overview">Overview</Tab>
<Tab value="specs">Specifications</Tab>
</Tabs>
);
}
Panels
Give each panel role="tabpanel", the id tabpanel-<groupId>-<value> and
aria-labelledby="tab-<groupId>-<value>", when value is only [A-Za-z0-9_-].
Any other character gets a derived id (tabPanelIdFor) instead of that pattern.
The tabs point at their panels with
aria-controls, and show and hide them as the selection changes. Pin groupId in the
factory's options, or the id is allocated. Keep a pinned group id to [A-Za-z0-9_].
<div role="tabpanel" id="tabpanel-travel-flights" aria-labelledby="tab-travel-flights">…</div>
<div role="tabpanel" id="tabpanel-travel-trips" aria-labelledby="tab-travel-trips" hidden>…</div>
The factory's row scrolls by default; scrollable: false divides it evenly between the tabs
(fixed tabs). The web component takes the tabs, variant and the selection.
Set the responsive option on createTabs to switch a tab group's layout on narrow screens.
The behaviour is a plugin: import tabsResponsive from material/components/tabs/responsive
and pass the result as the option.
import { createTabs } from 'material/components/tabs';
import { tabsResponsive } from 'material/components/tabs/responsive';
const responsiveTabs = createTabs({
tabs: [
{ text: 'Video', value: 'video', icon: videoIcon, state: 'active' },
{ text: 'Photos', value: 'photos', icon: photoIcon },
],
responsive: tabsResponsive({
smallScreen: { layout: 'icon-only' },
breakpoints: { small: 600 },
}),
});
Below the small breakpoint (600px by default), tabs built with an icon and a label take
smallScreen.layout ('icon-only' by default), and the group carries the
mtrl-tabs--responsive-small class while it does. The layout applies while the group is created, so a
narrow page never paints the built layout first. setupResponsiveBehavior(tabs, config) still
works and is deprecated in favour of the option; it is removed in 4.0.
API
Options
tabsThe tabs to build, in order
TabConfig[][]variantPrimary or secondary tabs
'primary' | 'secondary''primary'scrollableA horizontally scrolling row, 52dp in from both edges. false divides the row evenly between the tabs (fixed tabs)
booleantrueshowDividerThe 1dp divider along the bottom of the row
booleantrueautoActivateWhether an arrow key also selects the tab it moves to. By default it only moves focus, and Space or Enter selects
booleanfalseresponsiveResponsive layout plugin from tabsResponsive(config) in material/components/tabs/responsive
(tabs: TabsComponent) => voidundefinedindicatorThe active indicator (below)
IndicatorConfig{}groupIdThe id every tab's id is built from, tab-<groupId>-<value>. Pin it when ids must survive a re-render
stringonEvent handlers registered at creation, as with on()
{ change }undefinedclassAdditional CSS classes on the row
stringundefinedprefixPrefix for CSS class names
string'mtrl'Tab options
Each entry of tabs, and the argument to addTab().
textThe label
stringundefinediconIcon as SVG markup. With a label, the icon sits above it and the row is 64dp tall
stringundefinedariaLabelThe accessible name of a tab with an icon and no label; without it the name falls back to the tab's text, then its value, with any badge count joined after
stringundefinedvalueIdentifies the tab to change, setActiveTab() and its panel
stringundefinedstateWhether the tab starts active
'active' | 'inactive''inactive'disabledWhether the tab starts disabled
booleanfalsebadgeBadge content; keep it to four characters, including a "+"
string | numberundefinedbadgeConfigOptions for the badge: variant, color, size, position, max
objectundefinedrippleWhether pressing the tab shows the ripple
booleantrueclassAdditional CSS classes on the tab
stringundefinedIndicator options
widthStrategyauto: the label's width inset 2dp on each side (24dp at least) for primary tabs, the whole tab for secondary ones
'auto' | 'content' | 'dynamic' | 'fixed''auto'heightHeight in pixels
numbercolorThe indicator's color
stringprimaryvisibleWhether it starts shown; getIndicator().hide() and show() change it later
booleantruefixedWidthWidth for the fixed strategy
number40animationDurationReplaces Material's default spatial spring with a fixed duration in milliseconds
numberanimationTimingAn easing to use with animationDuration
stringMethods
Tabs
addTab(config)Builds a tab and appends it
TabComponentadd(tab)Appends a tab built with createTab
TabsComponentremoveTab(tabOrValue)Removes and destroys a tab
TabsComponentgetTabs()The tabs, in order
TabComponent[]getActiveTab()The active tab
TabComponent | nullsetActiveTab(tabOrValue)Selects a tab, updates its panels and emits change. An unknown value clears the selection
TabsComponentgetIndicator()The indicator: show(), hide(), setColor(), update()
TabIndicatoron(event, handler) / off(event, handler)Events: change
TabsComponentdestroy()Destroys every tab and the row
voidTab
getValue() / setValue(value)The tab's value
string / TabComponentisActive()Whether the tab is selected
booleansetText(text) / getText()The label
TabComponent / stringsetIcon(icon) / getIcon()The icon
TabComponent / stringsetBadge(content) / getBadge() / showBadge() / hideBadge()The badge
TabComponent / stringenable() / disable()Disabled state
TabComponenton(event, handler) / off(event, handler)Events: click, focus, blur
TabComponentdestroy()Removes the tab
voidEvents
changeThe selected tab changed, by pointer, keyboard or setActiveTab(). tab and value are null when an unknown value cleared the selection
{ tab, value }The web component's change carries { value }.
Styling
.mtrl-tabs { } /* the row */
.mtrl-tabs--primary, .mtrl-tabs--secondary, .mtrl-tabs--scrollable { }
.mtrl-tabs__scroll { } /* the scrolling container */
.mtrl-tabs__indicator, .mtrl-tabs__divider { }
.mtrl-tabs--responsive-small { } /* below the small breakpoint */
.mtrl-tab { } /* one tab, a button */
.mtrl-tab--active { }
.mtrl-tab--text-only, .mtrl-tab--icon-only, .mtrl-tab--icon-and-text { }
.mtrl-tab-panel { }
Measurements
From the M3 tabs specs, then Compose's PrimaryNavigationTabTokens and
SecondaryNavigationTabTokens:
| Attribute | Value |
|---|---|
| Height | 48dp with a label or an icon, 64dp with both |
| Label | Title Small; primary (primary) or on-surface (secondary) when active, on-surface-variant when inactive |
| Icon | 24dp |
| Tab padding | 16dp on each side |
| Primary indicator | 3dp, primary, top corners 3dp, the label's width inset 2dp on each side, 24dp at least |
| Secondary indicator | 2dp, primary, the tab's full width |
| Divider | 1dp outline-variant, inside the row's height |
| Scrollable edge | 52dp before the first tab and after the last |
| States | An inactive tab turns on-surface on hover, focus and press, over an on-surface layer; a press on a primary tab is primary, drawn by the ripple |
| Motion | The indicator moves on the default spatial spring |