# Tab bar

> Bottom navigation for the main areas of an app, with a sliding indicator and content that blurs between tabs.

Docs: https://plumbago.ochotzas.com/components/tab-bar

## Installation

```sh
npx plumbago add tab-bar
```

This copies `components/ui/tab-bar.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

```tsx
import { TabBarContent, TabBarItem, TabBarList, TabBarPanel, TabBarRoot } from "@/components/ui/tab-bar"

<TabBarRoot defaultValue="today" className="h-dvh">
  <TabBarContent>
    <TabBarPanel value="today">Today</TabBarPanel>
    <TabBarPanel value="upcoming">Upcoming</TabBarPanel>
  </TabBarContent>
  <TabBarList aria-label="Main">
    <TabBarItem value="today" icon={<House />}>Today</TabBarItem>
    <TabBarItem value="upcoming" icon={<CalendarDays />}>Upcoming</TabBarItem>
  </TabBarList>
</TabBarRoot>
```

## Examples

### Preview

```tsx
import { Island, ListRow, ListRowChevron, ListRowSubtitle, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"
import { ScrollFade } from "@/components/ui/scroll-fade"
import { TabBarContent, TabBarItem, TabBarList, TabBarPanel, TabBarRoot } from "@/components/ui/tab-bar"
import { Calendar, CalendarDays, House, Settings } from "lucide-react"
import type { ReactNode } from "react"

const today = [
  ["Order flour and yeast", "Before 10:00"],
  ["Update the lunch menu", "11:30"],
  ["Call the fish supplier", "13:00"],
  ["Count the cash drawer", "15:00"],
  ["Prep tomorrow's stock", "17:30"],
  ["Lock up and set the alarm", "22:00"],
]

const upcoming = [
  ["Staff meeting", "Monday"],
  ["Inventory count", "Friday"],
  ["Renew the food licence", "28 Nov"],
  ["Service the espresso machine", "2 Dec"],
]

const settings = [
  ["Notifications", "On"],
  ["Appearance", "Automatic"],
  ["Account", "hello@casaverde.example"],
]

function Screen({ title, children }: { title: string; children: ReactNode }) {
  return (
    <ScrollFade className="flex-1">
      <div className="flex flex-col gap-4 p-gutter">
        <h2 className="text-title1 font-bold">{title}</h2>
        <Island variant="list">{children}</Island>
      </div>
    </ScrollFade>
  )
}

export default function TabBarDemo() {
  return (
    <TabBarRoot defaultValue="today" className="h-[600px] w-full max-w-[390px] overflow-hidden rounded-xl border border-line">
      <TabBarContent>
        <TabBarPanel value="today">
          <Screen title="Today">
            {today.map(([title, time]) => (
              <ListRow key={title}>
                <ListRowText>
                  <ListRowTitle>{title}</ListRowTitle>
                  <ListRowSubtitle>{time}</ListRowSubtitle>
                </ListRowText>
              </ListRow>
            ))}
          </Screen>
        </TabBarPanel>
        <TabBarPanel value="upcoming">
          <Screen title="Upcoming">
            {upcoming.map(([title, when]) => (
              <ListRow key={title}>
                <ListRowText>
                  <ListRowTitle>{title}</ListRowTitle>
                </ListRowText>
                <ListRowValue>{when}</ListRowValue>
              </ListRow>
            ))}
          </Screen>
        </TabBarPanel>
        <TabBarPanel value="settings">
          <Screen title="Settings">
            {settings.map(([title, value]) => (
              <ListRow key={title} render={<button type="button" />}>
                <ListRowText>
                  <ListRowTitle>{title}</ListRowTitle>
                </ListRowText>
                <ListRowValue>
                  {value}
                  <ListRowChevron />
                </ListRowValue>
              </ListRow>
            ))}
          </Screen>
        </TabBarPanel>
      </TabBarContent>
      <TabBarList aria-label="Main" className="pb-[22px]">
        <TabBarItem value="today" icon={<House strokeWidth={1.75} />}>
          Today
        </TabBarItem>
        <TabBarItem
          value="upcoming"
          icon={<CalendarDays strokeWidth={1.75} />}
          activeIcon={<Calendar strokeWidth={1.75} fill="currentColor" className="[&_path:last-child]:stroke-bg" />}
        >
          Upcoming
        </TabBarItem>
        <TabBarItem
          value="settings"
          icon={<Settings strokeWidth={1.75} />}
          activeIcon={<Settings strokeWidth={1.75} fill="currentColor" className="[&_circle]:fill-bg [&_circle]:stroke-bg" />}
        >
          Settings
        </TabBarItem>
      </TabBarList>
    </TabBarRoot>
  )
}
```

### With count badge

```tsx
import { CountBadge, CountBadgeAnchor } from "@/components/ui/count-badge"
import { TabBarContent, TabBarItem, TabBarList, TabBarPanel, TabBarRoot } from "@/components/ui/tab-bar"
import { Calendar, CalendarDays, Folder, House, Settings } from "lucide-react"

export default function TabBarWithCountBadge() {
  return (
    <TabBarRoot defaultValue="today" className="h-72 w-full max-w-[390px] overflow-hidden rounded-xl border border-line">
      <TabBarContent>
        <TabBarPanel value="today" className="p-gutter text-title2 font-bold">
          Today
        </TabBarPanel>
        <TabBarPanel value="upcoming" className="p-gutter text-title2 font-bold">
          Upcoming
        </TabBarPanel>
        <TabBarPanel value="lists" className="p-gutter text-title2 font-bold">
          Lists
        </TabBarPanel>
        <TabBarPanel value="settings" className="p-gutter text-title2 font-bold">
          Settings
        </TabBarPanel>
      </TabBarContent>
      <TabBarList aria-label="Main" className="pb-[22px]">
        <TabBarItem
          value="today"
          icon={
            <CountBadgeAnchor>
              <House strokeWidth={1.75} />
              <CountBadge count={3} label="3 tasks due" className="translate-x-1/2 -translate-y-1/4" />
            </CountBadgeAnchor>
          }
        >
          Today
        </TabBarItem>
        <TabBarItem
          value="upcoming"
          icon={<CalendarDays strokeWidth={1.75} />}
          activeIcon={<Calendar strokeWidth={1.75} fill="currentColor" className="[&_path:last-child]:stroke-bg" />}
        >
          Upcoming
        </TabBarItem>
        <TabBarItem value="lists" icon={<Folder strokeWidth={1.75} />}>
          Lists
        </TabBarItem>
        <TabBarItem
          value="settings"
          icon={<Settings strokeWidth={1.75} />}
          activeIcon={<Settings strokeWidth={1.75} fill="currentColor" className="[&_circle]:fill-bg [&_circle]:stroke-bg" />}
        >
          Settings
        </TabBarItem>
      </TabBarList>
    </TabBarRoot>
  )
}
```

### Scroll to top

```tsx
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle, ListRowValue } from "@/components/ui/island"
import { ScrollFade } from "@/components/ui/scroll-fade"
import { TabBarContent, TabBarItem, TabBarList, TabBarPanel, TabBarRoot } from "@/components/ui/tab-bar"
import { CalendarCheck, Settings } from "lucide-react"
import { useRef } from "react"

const bookings = [
  ["12:00", "Marta Rossi", "2 guests"],
  ["12:15", "Jonas Weber", "4 guests"],
  ["12:30", "Ana Lima", "2 guests"],
  ["12:45", "Tom Okafor", "6 guests"],
  ["13:00", "Lucía Fernández", "3 guests"],
  ["13:15", "Henrik Dahl", "2 guests"],
  ["13:30", "Priya Nair", "5 guests"],
  ["13:45", "Sam Carter", "2 guests"],
  ["19:00", "Elena Petrova", "4 guests"],
  ["19:15", "Yusuf Demir", "2 guests"],
  ["19:30", "Claire Dubois", "8 guests"],
  ["20:00", "Kenji Sato", "2 guests"],
]

export default function TabBarScrollToTop() {
  const bookingsScroller = useRef<HTMLDivElement>(null)

  function scrollBookingsToTop() {
    bookingsScroller.current?.scrollTo({ top: 0, behavior: "smooth" })
  }

  return (
    <TabBarRoot defaultValue="bookings" className="h-96 w-full max-w-[390px] overflow-hidden rounded-xl border border-line">
      <TabBarContent>
        <TabBarPanel value="bookings">
          <ScrollFade viewportRef={bookingsScroller} className="flex-1">
            <div className="flex flex-col gap-4 p-gutter">
              <h2 className="text-title1 font-bold">Bookings</h2>
              <Island variant="list">
                {bookings.map(([time, name, party]) => (
                  <ListRow key={name}>
                    <ListRowText>
                      <ListRowTitle>{name}</ListRowTitle>
                      <ListRowSubtitle>{party}</ListRowSubtitle>
                    </ListRowText>
                    <ListRowValue className="tabular-nums">{time}</ListRowValue>
                  </ListRow>
                ))}
              </Island>
            </div>
          </ScrollFade>
        </TabBarPanel>
        <TabBarPanel value="settings" className="p-gutter text-title1 font-bold">
          Settings
        </TabBarPanel>
      </TabBarContent>
      <TabBarList aria-label="Main" className="pb-[22px]">
        <TabBarItem
          value="bookings"
          icon={<CalendarCheck strokeWidth={1.75} />}
          activeIcon={<CalendarCheck strokeWidth={1.75} fill="currentColor" className="[&_path:last-child]:stroke-bg" />}
          onReselect={scrollBookingsToTop}
        >
          Bookings
        </TabBarItem>
        <TabBarItem
          value="settings"
          icon={<Settings strokeWidth={1.75} />}
          activeIcon={<Settings strokeWidth={1.75} fill="currentColor" className="[&_circle]:fill-bg [&_circle]:stroke-bg" />}
        >
          Settings
        </TabBarItem>
      </TabBarList>
    </TabBarRoot>
  )
}
```

### As links

```tsx
import { TabBarItem, TabBarList, TabBarRoot } from "@/components/ui/tab-bar"
import { Calendar, CalendarDays, House, Settings } from "lucide-react"
import { type MouseEvent, useState } from "react"

export default function TabBarAsLinks() {
  const [path, setPath] = useState("/upcoming")

  function navigate(event: MouseEvent<HTMLAnchorElement>) {
    event.preventDefault()
    setPath(event.currentTarget.pathname)
  }

  return (
    <TabBarRoot value={path} className="w-full max-w-[390px] overflow-hidden rounded-xl border border-line">
      <TabBarList aria-label="Main" activateOnFocus={false}>
        <TabBarItem value="/today" nativeButton={false} render={<a href="/today" onClick={navigate} />} icon={<House strokeWidth={1.75} />}>
          Today
        </TabBarItem>
        <TabBarItem
          value="/upcoming"
          nativeButton={false}
          render={<a href="/upcoming" onClick={navigate} />}
          icon={<CalendarDays strokeWidth={1.75} />}
          activeIcon={<Calendar strokeWidth={1.75} fill="currentColor" className="[&_path:last-child]:stroke-bg" />}
        >
          Upcoming
        </TabBarItem>
        <TabBarItem
          value="/settings"
          nativeButton={false}
          render={<a href="/settings" onClick={navigate} />}
          icon={<Settings strokeWidth={1.75} />}
          activeIcon={<Settings strokeWidth={1.75} fill="currentColor" className="[&_circle]:fill-bg [&_circle]:stroke-bg" />}
        >
          Settings
        </TabBarItem>
      </TabBarList>
    </TabBarRoot>
  )
}
```

## Built on Base UI

Tab bar wraps these Base UI parts and keeps their props:

- [Tabs](https://base-ui.com/react/components/tabs)

## Source

```tsx
import { Tabs } from "@base-ui/react/tabs"
import { cn } from "@/lib/cn"
import type { ComponentProps, CSSProperties, MouseEvent, ReactNode } from "react"

const movingLeft = "left var(--pb-dur-spring-fast) var(--pb-spring-fast), right var(--pb-dur-spring-slow) var(--pb-spring-slow)"
const movingRight = "left var(--pb-dur-spring-slow) var(--pb-spring-slow), right var(--pb-dur-spring-fast) var(--pb-spring-fast)"
const reducedMotion = "left var(--pb-dur-base) var(--pb-ease-out), right var(--pb-dur-base) var(--pb-ease-out)"

export type TabBarRootProps = Omit<ComponentProps<typeof Tabs.Root>, "className"> & {
  className?: string
}

export function TabBarRoot({ className, ...props }: TabBarRootProps) {
  return <Tabs.Root data-slot="tab-bar" className={cn("flex min-h-0 flex-col bg-bg", className)} {...props} />
}

export type TabBarContentProps = Omit<ComponentProps<"div">, "className"> & {
  className?: string
}

export function TabBarContent({ className, ...props }: TabBarContentProps) {
  return (
    <div
      data-slot="tab-bar-content"
      className={cn("relative grid min-h-0 flex-1 grid-cols-[minmax(0,1fr)] grid-rows-[minmax(0,1fr)] overflow-hidden", className)}
      {...props}
    />
  )
}

export type TabBarPanelProps = Omit<ComponentProps<typeof Tabs.Panel>, "className"> & {
  className?: string
}

export function TabBarPanel({ className, ...props }: TabBarPanelProps) {
  return (
    <Tabs.Panel
      data-slot="tab-bar-panel"
      className={cn(
        "col-start-1 row-start-1 flex min-h-0 flex-col overflow-hidden outline-none",
        "transition-[opacity,filter,translate,scale] duration-spring ease-spring",
        "delay-100 data-ending-style:delay-0 data-ending-style:duration-[160ms] data-ending-style:ease-in",
        "data-starting-style:translate-y-1.5 data-starting-style:scale-[0.98] data-starting-style:opacity-0 data-starting-style:blur-[8px]",
        "data-ending-style:-translate-y-1 data-ending-style:scale-[0.98] data-ending-style:opacity-0 data-ending-style:blur-[8px]",
        "motion-reduce:data-starting-style:translate-y-0 motion-reduce:data-starting-style:scale-100 motion-reduce:data-starting-style:blur-none",
        "motion-reduce:data-ending-style:translate-y-0 motion-reduce:data-ending-style:scale-100 motion-reduce:data-ending-style:blur-none",
        "data-ending-style:pointer-events-none",
        className,
      )}
      {...props}
    />
  )
}

export type TabBarListProps = Omit<ComponentProps<typeof Tabs.List>, "className"> & {
  className?: string
}

export function TabBarList({ activateOnFocus = true, className, children, ...props }: TabBarListProps) {
  return (
    <Tabs.List
      data-slot="tab-bar-list"
      activateOnFocus={activateOnFocus}
      className={cn(
        "relative isolate flex shrink-0 bg-surface px-2 pt-1.5 pb-[calc(6px+env(safe-area-inset-bottom))] shadow-[inset_0_1px_0_var(--pb-line)]",
        className,
      )}
      {...props}
    >
      <Tabs.Indicator
        data-slot="tab-bar-indicator"
        className={cn(
          "pointer-events-none absolute top-1.5 left-(--active-tab-left) h-[50px] right-(--active-tab-right) -z-10 rounded-full bg-bg",
          "data-[activation-direction=left]:[transition:var(--moving-left)] data-[activation-direction=right]:[transition:var(--moving-right)]",
          "motion-reduce:data-[activation-direction]:[transition:var(--reduced-motion)]",
        )}
        style={{ "--moving-left": movingLeft, "--moving-right": movingRight, "--reduced-motion": reducedMotion } as CSSProperties}
      />
      {children}
    </Tabs.List>
  )
}

export type TabBarItemProps = Omit<ComponentProps<typeof Tabs.Tab>, "className" | "children"> & {
  icon: ReactNode
  activeIcon?: ReactNode
  children: ReactNode
  onReselect?: () => void
  className?: string
}

export function TabBarItem({ icon, activeIcon, children, onReselect, onClick, className, ...props }: TabBarItemProps) {
  function handleClick(event: MouseEvent<HTMLElement>) {
    onClick?.(event as never)
    if (event.currentTarget.hasAttribute("data-active")) onReselect?.()
  }

  const iconClasses = "col-start-1 row-start-1 inline-flex items-center justify-center [&_svg]:size-6"
  const swapClasses = "transition-[opacity,filter] duration-base ease-out"

  return (
    <Tabs.Tab
      data-slot="tab-bar-item"
      onClick={handleClick}
      className={cn(
        "group/tab flex h-[50px] min-w-0 flex-1 cursor-pointer flex-col items-center justify-center gap-0.5 rounded-full text-caption font-medium whitespace-nowrap text-ink-2 select-none [-webkit-tap-highlight-color:transparent]",
        "transition-colors duration-base ease-out hover:not-data-active:not-data-disabled:text-ink focus-visible:outline-offset-0",
        "data-active:text-ink data-disabled:cursor-default data-disabled:opacity-40",
        className,
      )}
      {...props}
    >
      <span aria-hidden="true" className="grid size-6">
        <span
          data-slot="tab-bar-icon"
          className={cn(
            iconClasses,
            activeIcon
              ? cn(swapClasses, "group-data-active/tab:opacity-0 motion-safe:group-data-active/tab:blur-[3px]")
              : "[&_svg]:fill-transparent [&_svg]:transition-[fill] [&_svg]:duration-base [&_svg]:ease-out group-data-active/tab:[&_svg]:fill-current",
          )}
        >
          {icon}
        </span>
        {activeIcon && (
          <span
            data-slot="tab-bar-active-icon"
            className={cn(iconClasses, swapClasses, "opacity-0 motion-safe:blur-[3px] group-data-active/tab:opacity-100 group-data-active/tab:blur-none")}
          >
            {activeIcon}
          </span>
        )}
      </span>
      <span className="grid max-w-full min-w-0">
        <span className="col-start-1 row-start-1 truncate group-data-active/tab:font-semibold">{children}</span>
        <span aria-hidden="true" className="invisible col-start-1 row-start-1 truncate font-semibold">
          {children}
        </span>
      </span>
    </Tabs.Tab>
  )
}
```
