enes

TabView

TabView lays a set of tabs out as a strip, in the shape a browser gives its document tabs. Every tab can carry an icon and a close button, and a new tab button can sit at the end of the strip.

The document you are working on.

The strip owns the selection and nothing else. What a tab opens is rendered by you, wherever it belongs on the page, so a tab can open a panel underneath it, swap a region somewhere else, or drive a route.

To create a tab strip, you will need to use the following components:

  • TabView: The root component that creates the context every tab reads: which tab is selected, whether the tabs close, and how they take their width. It renders the tablist and, optionally, the new tab button.
  • TabViewTab: A single tab of the strip. It reports its own selection and can carry an icon and a close button.

Usage

A tab strip expects a value on every tab and an activeTab to match it against. Bind activeTab to follow the selection, and render whatever the selected tab opens yourself.

<script>
	import { TabView, TabViewTab } from 'fluentui-svelte';

	let activeTab = $state('document');
</script>

<TabView bind:activeTab listAttributes={{ 'aria-label': 'Workspaces' }}>
	<TabViewTab id="tab-document" value="document">Document</TabViewTab>
	<TabViewTab id="tab-mail" value="mail">Mail</TabViewTab>
</TabView>

{#if activeTab === 'document'}
	<div role="tabpanel" aria-labelledby="tab-document">The document you are working on.</div>
{:else}
	<div role="tabpanel" aria-labelledby="tab-mail">Everything that landed in your inbox.</div>
{/if}

Closing Tabs

closable gives every tab a close button, and a single tab can opt in or out on its own. The strip never removes a tab: the tabs are your markup, so onTabClose hands the value back and leaves the removal — and which tab takes over — to you.

New Tab Button

showNewTabButton adds a button at the end of the strip, outside the tablist so it never reads as a tab. onNewTab fires when it is activated.

Tab Width

tabWidthMode decides how the tabs take their width: equal shares the strip evenly, size-to-content sizes every tab to its own label, and compact shrinks every resting tab to its icon and spells out only the selected one.

Disabled

A disabled tab cannot be selected, and the arrow keys pass over it rather than landing on it.

Component Props (TabView)

NameTypeDefaultDescription
ref bindableHTMLDivElement | nullThe DOM reference of the element wrapping the strip.
listRef bindableHTMLDivElement | nullThe DOM reference of the tab list.
listAttributesHTMLAttributesThe Attributes to spread on the tab list element, where the tablist role and its label belong.
activeTab bindablestringThe value of the selected tab.
onTabChange(event: Event, value: string) => voidFired when the selected tab changes.
closablebooleanfalseEvery tab shows a close button, unless the tab opts out of it.
onTabClose(event: Event, value: string) => voidFired when a tab asks to be closed. Removing the tab is left to the consumer.
showNewTabButtonbooleanfalseShows the button that adds a new tab at the end of the strip.
onNewTab(event: MouseEvent) => voidFired when the new tab button is activated.
newTabButtonAttributesHTMLButtonAttributesThe props to spread on the new tab button.
tabWidthMode'equal' | 'size-to-content' | 'compact''equal'How the tabs take their width: sharing the strip evenly, sizing to their own content, or shrinking to the icon of every tab but the selected one.
disableTabspotbooleanfalseOpts the strip out of the tabspot focus management, to wire your own.
Element Attributes (div)

Component Props (TabViewTab)

NameTypeDefaultDescription
ref bindableHTMLDivElement | nullThe DOM reference of the tab element.
valuestringThe value that identifies the tab. Defaults to a generated id.
iconSnippet | ComponentThe icon to display before the label.
closablebooleanShows the close button of the tab. Falls back to the closable of the strip.
disabledbooleanDisables the user interaction.
onClose(event: Event, value: string) => voidFired when the tab asks to be closed, either from its close button or the Delete key.
Element Attributes (div)

Keyboard Navigation

The strip is a single tab stop. ArrowLeft and ArrowRight walk the tabs, Home and End jump to the ends, Enter and Space select the focused tab, and Delete closes it when it is closable. disableTabspot hands the focus management back to you.

Accessibility

The strip renders a tablist and every tab a tab, so give the list a name through the aria-label you pass in listProps. Since you render what a tab opens, give that element a tabpanel role and point its aria-labelledby at the id of its tab.

Edit this Doc View as Markdown