Skip to content
Okaapiأوكابي
Framework for the code
Theme
Okaapi on GitHub

Chapter 07 Navigation

Tabs

Views of one thing side by side, such as an order's details and its history. Tab reaches the chosen tab, the arrow keys move between tabs and show each one, Home and End go to the first and last. The chosen tab gets a bar and full-strength text, not only a colour.

Use it in Angular
import {
  TabComponent,
  TabContentDirective, // content created only when its tab is shown

An order

Delivered to 12 Tahrir Street, Cairo. Paid

Angular
<ui-tabs label="Order 1042" [(selected)]="tab">
  <ui-tab label="Details">…</ui-tab>
  <ui-tab label="Invoice" disabled>…</ui-tab>
  <ui-tab label="History">…</ui-tab>
</ui-tabs>

Many tabs

Wider than the page, the tabs scroll. Choosing one, by click or arrow key, moves it to the middle, so the tabs on both sides of it stay in sight. Under reduced motion it moves without gliding.

Sales for January.

Angular
<ui-tabs label="Sales by month">
  @for (month of months; track month) {
    <ui-tab [label]="month">…</ui-tab>
  }
</ui-tabs>

Load when shown

Put content that calls an API in an ng-template with uiTabContent: it is created, and its call made, only when the tab is chosen. Without it, everything inside a tab is created with the page. The spinner tells screen readers the panel is loading.

Customer since 2021.

Angular
<ui-tabs label="Customer 518">
  <ui-tab label="Details">…</ui-tab>
  <ui-tab label="Invoices">
    <ng-template uiTabContent><app-invoices /></ng-template>
  </ui-tab>
</ui-tabs>

// app-invoices: the call starts when the tab is first chosen.
readonly invoices = httpResource<Invoice[]>(() => '/api/invoices');

@if (invoices.isLoading()) {
  <ui-spinner label="Loading invoices" showLabel />
} @else { … }

Keep or reload

By default, lazy content is created again each time its tab is chosen, so it loads fresh: watch the time change. Add keep to a tab to load it once and find it as it was when you come back, with what was typed and where it was scrolled.

3 invoices, loaded at 8:19:01 PM.

Angular
<ui-tabs label="Invoices">
  <!-- Created, and loaded, again on each visit -->
  <ui-tab label="Reloads">
    <ng-template uiTabContent><app-invoices /></ng-template>
  </ui-tab>
  <!-- Loaded once, then kept while hidden -->
  <ui-tab label="Kept" keep>
    <ng-template uiTabContent><app-invoices /></ng-template>
  </ui-tab>
</ui-tabs>

A page for each tab

When each tab has its own address, use ui-tab-nav: links styled as tabs, over a router-outlet. They are links, not a tablist, so Tab moves through them, the back button works and each one can open in a new browser tab. The current one is marked aria-current="page". Each visit makes the page again, so Billing loads again; Security is kept with KeepRouteStrategy: type a note, go to another tab and come back.

Hatem Mohamed, hatem@example.com.

Angular
<ui-tab-nav label="Account">
  <a class="ui-tab" routerLink="./" routerLinkActive ariaCurrentWhenActive="page"
     [routerLinkActiveOptions]="{ exact: true }">Profile</a>
  <a class="ui-tab" routerLink="billing" routerLinkActive ariaCurrentWhenActive="page">Billing</a>
  <a class="ui-tab" routerLink="security" routerLinkActive ariaCurrentWhenActive="page">Security</a>
</ui-tab-nav>
<router-outlet />

// Routes: each tab is a child of the page. data.keep keeps Security between visits.
{
  path: 'account',
  component: AccountPage,
  children: [
    { path: '', component: Profile },
    { path: 'billing', component: Billing },
    { path: 'security', component: Security, data: { keep: true } },
  ],
}

// app.config.ts
providers: [{ provide: RouteReuseStrategy, useClass: KeepRouteStrategy }]

In Arabic

The tabs run right to left and the arrow keys follow: the right arrow goes back.

اتسلّم في ١٢ شارع التحرير، القاهرة.

Angular
<ui-tabs label="الطلب ١٠٤٢">
  <ui-tab label="التفاصيل">…</ui-tab>
  <ui-tab label="السجل">…</ui-tab>
</ui-tabs>

When to use something else

If you needUse
Links to other pagesNavigation
Change how the same content is shownSegmented control
Sections the reader may open togetherAccordion

Inputs

NameTypeDefaultWhat it does
labelstring — Required, on ui-tabs. Names the tab list.
[(selected)]number0The index of the chosen tab.
testIdstring — data-testid prefix; each tab gets -<index>.
ui-tab labelstring — Required. The text on the tab.
ui-tab disabledbooleanfalseShown but not chosen; arrow keys skip it.
ui-tab keepbooleanfalseKeeps uiTabContent once shown, hidden while another tab is chosen. Off, it is made again on each visit.
ng-template uiTabContentdirective — Inside ui-tab. The tab's content, created only when the tab is chosen.
ui-tab-nav labelstring — Required. Names the navigation; put links with class="ui-tab" inside.
KeepRouteStrategyRouteReuseStrategy — Provide it as the RouteReuseStrategy; a route with data: { keep: true } keeps its page between tabs, until the reader leaves the section.

Keyboard and screen readers

KeysWhat they do
Tab Reach the chosen tab, and Tab again moves into its panel.
Left or Right Move to the previous or next tab and show it. Flipped in Arabic.
Home or End Go to the first or last tab.
  • Read as tabs, with how many there are and which is chosen.