For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /dolan/tab-bar.md.

Tab Bar

Switch between your app's top-level screens from a row of icon-and-label tabs along the bottom of the screen, as most mobile apps do.

Installation

npx @blotch/dolan add tab-bar
import { UiTabBar, UiTabBarItem } from '../components/ui/tab-bar';

This also adds Icon, whose icons and theme colors the tabs draw with.

Usage

Give each tab a route, an icon and a label, and put the bar under a <lynx-tab-outlet>, which keeps each tab as a native tab bar does:

src/app/app.ts
import { Component } from '@angular/core';
import { LYNX_ELEMENTS, LynxTabOutlet } from '@blotch/angular-lynx';
import { UiTabBar, UiTabBarItem } from '../components/ui/tab-bar';

@Component({
  selector: 'app-root',
  imports: [LYNX_ELEMENTS, LynxTabOutlet, UiTabBar, UiTabBarItem],
  template: `
    <view class="flex h-full flex-col bg-background">
      <lynx-tab-outlet />
      <ui-tab-bar>
        <ui-tab-bar-item route="/home" icon="home" label="Home" />
        <ui-tab-bar-item route="/search" icon="search" label="Search" />
        <ui-tab-bar-item route="/profile" icon="user" label="Profile" />
      </ui-tab-bar>
    </view>
  `,
})
export class App {}
  • A tap navigates to the tab's route, with Angular's own RouterLink. Set up the router with provideRouter() from @blotch/angular-lynx.
  • The tab of the current route is selected: its icon and label take the theme's primary color, and the others its muted-foreground.
  • A tab stays selected on the routes under it. The Inbox tab at /inbox is still selected at /inbox/42.
  • A tab shows selected as soon as it's tapped, before the screen it shows has been built, so a tab that takes a moment to build doesn't look like a missed tap. If the navigation ends somewhere else (a guard says no or redirects, or it fails), the tab of the route the app is on is selected again.
  • Going back to a tab finds it as you left it, with <lynx-tab-outlet>: its scroll positions, the screens pushed in it, and its native views. A tap on the selected tab pops its stack to its first screen. With <router-outlet>, each tab is built again every time.
  • The tabs share the bar's width equally, and the bar pads itself by the bottom safe area, so the tabs sit above the iPhone's home indicator.
  • The icon is any Icon name, or your own mark.

Give each routed screen's host flex-1 so it fills the space above the bar, or its <scroll-view> grows to fit its content instead of scrolling:

@Component({
  selector: 'app-home',
  host: { class: 'flex flex-1 flex-col' },
  template: `
    <scroll-view scroll-orientation="vertical" class="h-full w-full">
      <text>Home</text>
    </scroll-view>
  `,
})
export class Home {}

Away on a pushed screen

Give a tab a <lynx-stack-outlet>, and a screen it pushes over the tab's first screen takes the bar away, as iOS's hidesBottomBarWhenPushed and Material's navigation bar do. The example's Inbox is one: open a message.

  • While it's away, the bar lies over the bottom of the screen rather than under the outlet, so the pushed screen runs to the bottom edge.
  • Where screens move as iOS's do, the screen underneath carries the bar: it moves a little left with that screen, and the pushed screen slides over it, rounded corner and shadow and all.
  • Where they move as Android's do, it slides down off the screen.
  • Back, Android's back button or a swipe brings it back, following the finger while it drags. While it's away, screen readers skip it and it takes no taps.

A route keeps the bar with data: { bottomBar: 'visible' }. See LynxStackChrome for what the stack says, and LynxStackBottomBar for how a screen carries the bar.

Badges

[badge] shows a count or a short text over the tab's icon, such as unread messages:

<ui-tab-bar-item
  route="/inbox"
  icon="inbox"
  label="Inbox"
  [badge]="unreadCount()"
/>

A count of 0, an empty string, null or undefined shows no badge, so bind the count as it is.

Your own mark

For a mark the icon set doesn't have, such as your app's logo on the Home tab, pass its markup to svg instead of icon:

@Component({
  imports: [UiTabBar, UiTabBarItem],
  template: `
    <ui-tab-bar>
      <ui-tab-bar-item route="/home" [svg]="logo" label="Home" />
      <ui-tab-bar-item route="/search" icon="search" label="Search" />
    </ui-tab-bar>
  `,
})
export class App {
  // The app's logo (its path shortened here), filled with currentColor.
  readonly logo = `<svg viewBox="0 0 24 24"><path fill="currentColor" d="…"/></svg>`;
}
  • currentColor takes the tab's color, as the set's icons do: primary while it's selected and muted-foreground otherwise. A one-color mark written in currentColor follows the selection and your theme.
  • Colors written out stay, so a multi-color mark keeps them.
  • xmlns is optional, and lines must be <path>s, as for an icon's own SVG.

A tab for the root route

Every route starts with /, so a tab for / would stay selected everywhere. Match it exactly:

<ui-tab-bar-item
  route="/"
  icon="home"
  label="Home"
  [routerLinkActiveOptions]="{ exact: true }"
/>

Router options

The item takes RouterLink's inputs, which work as on the web: queryParams, queryParamsHandling, fragment, preserveFragment, state, info, relativeTo, replaceUrl and skipLocationChange.

<ui-tab-bar-item
  route="/profile"
  icon="user"
  label="Profile"
  [queryParams]="{ tab: 'posts' }"
  [replaceUrl]="true"
/>
  • Each tap adds a history entry, as any RouterLink does, so Android's back button walks back through the tabs. Set replaceUrl to replace the entry instead.
  • A relative route resolves from the component the bar is in. A bar inside a routed screen can switch its child routes with route="feed".

React to a tap

(pressed) emits on every tap, including one on the tab that's already selected, where the route doesn't change. Native apps scroll the screen back to the top then:

<ui-tab-bar-item
  route="/home"
  icon="home"
  label="Home"
  (pressed)="onHomeTap()"
/>

Dark mode

The icons draw with the light theme's colors until you say otherwise, as every icon does. If your app switches to dark mode, set the icons' color scheme with provideIconTheme().

API

UiTabBar

InputTypeDefaultDescription
classstring''Extra Tailwind classes for the bar.

UiTabBarItem

InputTypeDefaultDescription
routestring | any[] | UrlTree | null—Where a tap navigates. RouterLink's routerLink.
iconIconName | undefinedundefinedThe Icon drawn above the label. Give this or svg.
svgstring | undefinedundefinedSVG markup to draw instead, such as your logo, with currentColor in the tab's color. Give this or icon.
labelstring—Required. The tab's name, shown and read by screen readers.
badgestring | number | null | undefinedundefinedA count or short text over the icon. Hidden for 0, '', null and undefined.
routerLinkActiveOptions{ exact: boolean } | IsActiveMatchOptionssubsetHow the current route is matched. RouterLinkActive's input.
queryParams, …See RouterLink—queryParams, queryParamsHandling, fragment, preserveFragment, state, info, relativeTo, replaceUrl, skipLocationChange.
classstring''Extra Tailwind classes for the tab's content.
OutputTypeDescription
pressedvoidEmits on every tap, including a tap on the selected tab.

Accessibility

  • Each tab is one button, named by its label and then its badge: "Inbox, 3".
  • The selected tab has the selected trait, from the tap, as it shows, so iOS VoiceOver announces it as selected. Android has no selected trait, so TalkBack doesn't say which tab is selected.
  • On iOS, the bar has the tabbar trait, as a native tab bar does, which tells VoiceOver that its buttons are tabs.
  • Away on a pushed screen, the bar is hidden from screen readers and takes no taps.
  • Routing and navigation: set up the router and its routes
  • Safe area: the inset the bar pads for
  • Dock: the same navigation as a floating Liquid Glass bar
  • Tabs: switch panels within one screen instead of routed screens
  • Nav Drawer: a side menu, for more destinations than a tab bar fits
  • Icon: the icons a tab can show, and how to write your own mark