# Animated Tabs
Tabs with a pill that travels to the selected trigger.
Source page: https://dinachi.dev/docs/components/animated-tabs

## Installation
```bash
npx @dinachi/cli@latest add animated-tabs
```
Adds to your project:
- `components/ui/animated-tabs/animated-tabs.tsx`
- `components/ui/animated-tabs/index.ts`
Installs:
- `@base-ui/react` ^1.9.0
- `motion` ^12.23.6

## Usage
```tsx
import {
  AnimatedTabs,
  AnimatedTabsContent,
  AnimatedTabsList,
  AnimatedTabsTrigger,
} from "@/components/ui/animated-tabs"

export function ProjectViews() {
  return (
    <AnimatedTabs aria-label="Views" defaultValue="overview">
      <AnimatedTabsList>
        <AnimatedTabsTrigger value="overview">Overview</AnimatedTabsTrigger>
        <AnimatedTabsTrigger value="activity">Activity</AnimatedTabsTrigger>
      </AnimatedTabsList>
      <AnimatedTabsContent value="overview">Everything at a glance.</AnimatedTabsContent>
      <AnimatedTabsContent value="activity">Who changed what, and when.</AnimatedTabsContent>
    </AnimatedTabs>
  )
}
```

## Anatomy
```tsx
<AnimatedTabs>
  <AnimatedTabsList>
    <AnimatedTabsTrigger />
  </AnimatedTabsList>
  <AnimatedTabsContent />
</AnimatedTabs>
```
- `AnimatedTabs`: Holds the selected tab and pill spring.
- `AnimatedTabsList`: The strip that holds the triggers.
- `AnimatedTabsTrigger`: One tab. Holds the pill when selected.
- `AnimatedTabsContent`: One tab's panel. Switches without animation.

## Examples

### Default
The pill travels to the tab you click.
```tsx
import {
  AnimatedTabs,
  AnimatedTabsList,
  AnimatedTabsTrigger,
  AnimatedTabsContent,
} from '@/components/ui/animated-tabs';

export function Example() {
  return (
    <AnimatedTabs defaultValue="overview">
      <AnimatedTabsList>
        <AnimatedTabsTrigger value="overview">Overview</AnimatedTabsTrigger>
        <AnimatedTabsTrigger value="activity">Activity</AnimatedTabsTrigger>
        <AnimatedTabsTrigger value="settings">Settings</AnimatedTabsTrigger>
      </AnimatedTabsList>
      <AnimatedTabsContent value="overview">Everything at a glance.</AnimatedTabsContent>
      <AnimatedTabsContent value="activity">Who changed what, and when.</AnimatedTabsContent>
      <AnimatedTabsContent value="settings">Names, keys, and members.</AnimatedTabsContent>
    </AnimatedTabs>
  );
}
```

### Styled indicator
The pill themed without restyling the tab.
```tsx
import {
  AnimatedTabs,
  AnimatedTabsList,
  AnimatedTabsTrigger,
} from '@/components/ui/animated-tabs';

export function Example() {
  return (
    <AnimatedTabs defaultValue="week">
      <AnimatedTabsList className="bg-transparent p-0">
        {['Day', 'Week', 'Month'].map((label) => (
          <AnimatedTabsTrigger
            key={label}
            value={label.toLowerCase()}
            indicatorClassName="rounded-md bg-primary"
            className="rounded-md data-[active]:text-primary-foreground"
          >
            {label}
          </AnimatedTabsTrigger>
        ))}
      </AnimatedTabsList>
    </AnimatedTabs>
  );
}
```

### Controlled
The selection held in React state.
```tsx
import { useState } from 'react';
import {
  AnimatedTabs,
  AnimatedTabsList,
  AnimatedTabsTrigger,
} from '@/components/ui/animated-tabs';

export function Example() {
  const [value, setValue] = useState('overview');

  return (
    <AnimatedTabs value={value} onValueChange={(next) => setValue(next as string)}>
      <AnimatedTabsList>
        <AnimatedTabsTrigger value="overview">Overview</AnimatedTabsTrigger>
        <AnimatedTabsTrigger value="activity">Activity</AnimatedTabsTrigger>
      </AnimatedTabsList>
    </AnimatedTabs>
  );
}
```

## Notes

### Motion tier
Needs [motion](https://motion.dev). `add --all` skips it; install with `add --motion` or by name.

### When to use it
- A short, stable strip of three to five tabs: a settings pane, a profile, a chart's time range.
- For a strip that scrolls or has a dozen entries, use [Tabs](/docs/components/tabs).

### Styling
- The selected tab carries `data-active`. Recolour its label with `data-[active]:text-primary-foreground` when the pill is dark.
- The pill is measured, so labels of uneven width need no offsets.

### Behaviour
- Pointer changes travel; keyboard changes and reduced motion cut.
- The default spring has no bounce.

## API reference
Props marked **agent** change what an agent reads or may do.

### AnimatedTabs
Holds the selected tab and pill spring.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `any` | - | Controlled selected tab. |
| `defaultValue` | `any` | - | Starting tab when uncontrolled. The pill shows only on a matching tab, so set one. |
| `onValueChange` **agent** | `(value, details) => void` | - | Runs for people and agents. `details.cancel()` refuses the change. |
| `transition` | `Transition` | `{ type: "spring", bounce: 0, duration: 0.3 }` | Overrides the pill's motion. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | Which arrow keys move between tabs. |
| `aria-label` **agent** | `string` | - | Names the tabs. Also the name the agent reads. |

### AnimatedTabsList
The strip that holds the triggers.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `activateOnFocus` | `boolean` | `false` | Selects on arrow key instead of Enter or Space. |
| `loopFocus` | `boolean` | `true` | Wraps arrow-key focus from the last tab to the first. |

### AnimatedTabsTrigger
One tab. Holds the pill when selected.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `any` | - | Which panel it selects. |
| `indicatorClassName` | `string` | - | Class for the pill; themes it without restyling the tab. |
| `disabled` **agent** | `boolean` | `false` | Skipped by the keyboard, refused for agents. |
| `children` **agent** | `ReactNode` | - | The tab label. Also the name the agent selects by. |

### AnimatedTabsContent
One tab's panel. Switches without animation.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `any` | - | The tab this panel belongs to. |
| `keepMounted` | `boolean` | `false` | Keeps the panel in the DOM while hidden. |

## Agent
Inside an `AgentProvider`, Animated Tabs appears in `agent.snapshot()` as `tabs`.
Reads:
- `label`: The `AnimatedTabs` aria-label, or tabs when it has none.
- `value`: The selected tab's label.
- `items`: Every tab's label, in order.
- `disabled`: True when every tab is disabled.
Actions:
- `select("Activity")`: Picks by label, then by value. A miss lists the tabs.
The agent clicks the tab, so the change runs through `onValueChange`. `isAgentChange(details)` is true for it, and `details.cancel()` refuses it; the agent is told the app refused. A disabled tab refuses on its own.
```ts
await agent.act({
  steps: [{ ref: "tabs", action: "select", args: ["Activity"] }],
})
```

## Accessibility
- Roving focus, aria-selected and aria-controls come from Base UI Tabs.
- Tablist, tab and tabpanel roles; each panel is labelled by its tab.
- The pill is aria-hidden; selection is announced via aria-selected.
- Give `AnimatedTabs` an aria-label when a page has several tab sets. The agent reads it.
### Keyboard
| Keys | Does |
| --- | --- |
| `←` / `→` | Moves focus between tabs, or ↑ ↓ when vertical |
| `Home` / `End` | Moves focus to the first or last tab |
| `Space` / `Enter` | Selects the focused tab |
| `Tab` | Moves focus from the tab list into the panel |

## Source
`components/ui/animated-tabs/animated-tabs.tsx`
```tsx
"use client"

import * as React from "react"
import { Tabs as BaseTabs } from "@base-ui/react/tabs"
import {
  motion,
  MotionConfigContext,
  useReducedMotion,
  type Transition,
} from "motion/react"
import { cn } from "@/lib/utils"
import {
  agentChoicesOf,
  changeAsAgent,
  matchAgentChoice,
  useAgentControl,
  useAgentItem,
  useHasAgent,
  useMergedRef,
  type AgentItems,
} from "@/components/ui/agent"

/** `useReducedMotion` reads the OS setting only, and only at mount. Also honour an
 *  app-level `<MotionConfig reducedMotion>`, so a user preference in the host app
 *  reaches this component. */
function useReducedMotionConfig() {
  const config = React.useContext(MotionConfigContext)
  const prefersReduced = useReducedMotion()
  if (config.reducedMotion === "always") return true
  if (config.reducedMotion === "never") return false
  return prefersReduced
}

type BaseRootProps = React.ComponentProps<typeof BaseTabs.Root>

/** Critically damped: a click carries no momentum, so an overshoot on arrival is
 *  motion the gesture never asked for. 0.3s is the short end of the response range
 *  for a reposition, which is where a control this frequently used belongs. */
const DEFAULT_TRANSITION: Transition = {
  type: "spring",
  bounce: 0,
  duration: 0.3,
}

const INSTANT_TRANSITION: Transition = { duration: 0 }

/** Enter and Space on a focused tab reach Base UI as a synthesized click — no pointer
 *  type, no click count — while arrow keys under `activateOnFocus` arrive as a real key
 *  event. Both are keyboard, and keyboard-driven tab changes cut rather than travel:
 *  they repeat far more often than pointer changes and animating them puts the pill
 *  between the keypress and the answer. */
function isKeyboardActivation(event: Event | undefined) {
  if (!event) return false
  if (event.type.startsWith("key")) return true
  if (event.type !== "click") return false
  const { pointerType } = event as PointerEvent
  return pointerType === "" || (event as MouseEvent).detail === 0
}

interface AnimatedTabsContextValue {
  value: unknown
  layoutId: string
  transition: Transition
  /** Set inside an `AgentProvider`: the triggers, which the agent selects by name. */
  items: AgentItems | null
}

type Details = BaseTabs.Root.ChangeEventDetails

/**
 * The agent clicks the tab, so Base UI decides as it does for a person. Returns a reader
 * for the agent's details, which the change handler takes in place of Base UI's.
 */
function useTabsAgent(items: AgentItems, element: React.RefObject<HTMLElement | null>) {
  const pending = React.useRef<Details | null>(null)
  useAgentControl(() => {
    const choices = agentChoicesOf(items)
    const tabs = [...items]
    const active = tabs.find(([el]) => el.getAttribute("aria-selected") === "true")?.[1]
    return {
      kind: "tabs",
      element: choices.length ? element.current : null,
      value: choices.find((c) => c.value === active)?.label ?? "",
      items: choices.map((c) => c.label),
      disabled: choices.length > 0 && choices.every((c) => c.disabled),
      actions: {
        select(wanted: unknown) {
          const match = matchAgentChoice(choices, wanted)
          if (typeof match === "string") return match
          return changeAsAgent<unknown, Details>((_, details) => {
            pending.current = details
            tabs.find(([, v]) => v === match.value)?.[0].click()
            pending.current = null
          }, match.value)
        },
      },
    }
  })
  return React.useCallback(() => {
    const details = pending.current
    pending.current = null
    return details
  }, [])
}

const AnimatedTabsContext = React.createContext<AnimatedTabsContextValue | null>(
  null
)

function useAnimatedTabsContext(component: string) {
  const context = React.useContext(AnimatedTabsContext)
  if (!context) {
    throw new Error(`${component} must be used within an AnimatedTabs`)
  }
  return context
}

export interface AnimatedTabsProps extends BaseRootProps {
  /** Overrides the indicator spring. */
  transition?: Transition
}

/**
 * Tabs whose active pill travels between triggers instead of cutting.
 *
 * Built on Base UI's Tabs for roving focus, arrow-key navigation and panel wiring.
 * The indicator is a single `layoutId` element, so motion measures both positions
 * and interpolates — no width or offset maths, and it stays correct when the tab
 * labels are a different width on every render.
 *
 * The travel is for the pointer only. Keyboard-driven changes, and anyone who has
 * asked for reduced motion, get the pill in its new position immediately.
 */
const AnimatedTabs = React.forwardRef<
  React.ComponentRef<typeof BaseTabs.Root>,
  AnimatedTabsProps
>(
  (
    {
      value: valueProp,
      defaultValue,
      onValueChange,
      transition = DEFAULT_TRANSITION,
      className,
      children,
      ...props
    },
    ref
  ) => {
    const layoutId = React.useId()
    const [internalValue, setInternalValue] = React.useState(defaultValue)
    const [instant, setInstant] = React.useState(false)
    const isControlled = valueProp !== undefined
    const value = isControlled ? valueProp : internalValue
    const reducedMotion = useReducedMotionConfig()
    const element = React.useRef<HTMLDivElement | null>(null)
    const [agentItems] = React.useState<AgentItems>(() => new Map())
    const items = useHasAgent() ? agentItems : null
    const takeAgentDetails = useTabsAgent(agentItems, element)

    const handleValueChange = React.useCallback<
      NonNullable<BaseRootProps["onValueChange"]>
    >(
      (next, eventDetails) => {
        const details = takeAgentDetails() ?? eventDetails
        onValueChange?.(next, details)
        if (details.isCanceled) return eventDetails.cancel()
        // Batched with the value change, so the render that moves the pill is
        // already the one that knows whether it should move at all.
        setInstant(isKeyboardActivation(eventDetails?.event))
        if (!isControlled) setInternalValue(next)
      },
      [isControlled, onValueChange, takeAgentDetails]
    )

    const contextValue = React.useMemo<AnimatedTabsContextValue>(
      () => ({
        value,
        layoutId,
        transition: reducedMotion || instant ? INSTANT_TRANSITION : transition,
        items,
      }),
      [value, layoutId, transition, reducedMotion, instant, items]
    )

    return (
      <AnimatedTabsContext.Provider value={contextValue}>
        <BaseTabs.Root
          ref={useMergedRef(ref, element)}
          value={value}
          defaultValue={defaultValue}
          onValueChange={handleValueChange}
          className={cn("w-full", className)}
          data-slot="animated-tabs"
          {...props}
        >
          {children}
        </BaseTabs.Root>
      </AnimatedTabsContext.Provider>
    )
  }
)
AnimatedTabs.displayName = "AnimatedTabs"

export type AnimatedTabsListProps = React.ComponentProps<typeof BaseTabs.List>

const AnimatedTabsList = React.forwardRef<
  React.ComponentRef<typeof BaseTabs.List>,
  AnimatedTabsListProps
>(({ className, ...props }, ref) => (
  <BaseTabs.List
    ref={ref}
    className={cn(
      "relative inline-flex items-center gap-0.5 rounded-full bg-muted p-1",
      className
    )}
    data-slot="animated-tabs-list"
    {...props}
  />
))
AnimatedTabsList.displayName = "AnimatedTabsList"

export interface AnimatedTabsTriggerProps
  extends React.ComponentProps<typeof BaseTabs.Tab> {
  /** Class applied to the travelling indicator. */
  indicatorClassName?: string
}

const AnimatedTabsTrigger = React.forwardRef<
  React.ComponentRef<typeof BaseTabs.Tab>,
  AnimatedTabsTriggerProps
>(({ className, indicatorClassName, children, value, ...props }, ref) => {
  const context = useAnimatedTabsContext("AnimatedTabsTrigger")
  const isActive = context.value === value
  const element = React.useRef<HTMLButtonElement | null>(null)
  useAgentItem(context.items, element, value)

  return (
    <BaseTabs.Tab
      ref={useMergedRef(ref, element)}
      data-agent-part={context.items ? "" : undefined}
      value={value}
      className={cn(
        "relative isolate inline-flex select-none items-center justify-center whitespace-nowrap",
        "rounded-full px-4 py-1.5 text-sm font-medium text-muted-foreground",
        // Paced to land with the pill rather than ahead of it — a label that darkens
        // while the pill is still in transit reads as two things moving, not one.
        "transition-colors duration-200 ease-out",
        "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
        "data-[disabled]:pointer-events-none data-[disabled]:opacity-50",
        "data-[active]:text-foreground",
        // Only the tabs you could move to. Hovering the selected one has nothing to
        // offer, and a bare `hover:` would also outrank the colour it was given.
        "[&:not([data-active])]:hover:text-foreground",
        className
      )}
      data-slot="animated-tabs-trigger"
      {...props}
    >
      {isActive && (
        <motion.span
          aria-hidden
          layoutId={context.layoutId}
          transition={context.transition}
          className={cn(
            "absolute inset-0 -z-10 rounded-full bg-background shadow-sm",
            indicatorClassName
          )}
        />
      )}
      {children}
    </BaseTabs.Tab>
  )
})
AnimatedTabsTrigger.displayName = "AnimatedTabsTrigger"

export type AnimatedTabsContentProps = React.ComponentProps<
  typeof BaseTabs.Panel
>

/**
 * The panel cuts. The pill already carries the state change, and a second animation
 * behind it only delays the content the user switched tabs to read.
 */
const AnimatedTabsContent = React.forwardRef<
  React.ComponentRef<typeof BaseTabs.Panel>,
  AnimatedTabsContentProps
>(({ className, children, ...props }, ref) => (
  <BaseTabs.Panel
    ref={ref}
    className={cn(
      "mt-3 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
      className
    )}
    data-slot="animated-tabs-content"
    {...props}
  >
    {children}
  </BaseTabs.Panel>
))
AnimatedTabsContent.displayName = "AnimatedTabsContent"

export {
  AnimatedTabs,
  AnimatedTabsList,
  AnimatedTabsTrigger,
  AnimatedTabsContent,
}
```
