# Animated Tabs

> Accessible tabs with a sliding indicator and soft content transitions.

Category: Navigation · Free · Updated 2026-10-20 · Live demo: https://www.motionkitui.com/components/animated-tabs

## When to use it

Pricing toggles (monthly or yearly), code examples by framework, settings panels and product feature switchers.

## How it works

The active tab renders a highlight with a shared layoutId, so Motion glides it from tab to tab when the selection changes. It follows the WAI-ARIA tabs pattern: arrow keys move between tabs, Home and End jump to the ends, and each panel is labelled by its tab.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/animated-tabs.json
```

Or copy `components/motionkit/animated-tabs.tsx` below. Requires react, motion and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `tabs` | `{ id, label, content }[]` |  | The tabs to render. |
| `defaultTab` | `string` |  | Id of the initially selected tab. |

## Accessibility

- **Keyboard: Pass.** Follows the WAI-ARIA tabs pattern: Tab enters the list, arrow keys move between tabs, Home and End jump to the ends.
- **Screen readers: Pass.** Uses tablist, tab and tabpanel roles with aria-selected, aria-controls and aria-labelledby.
- **Reduced motion: Pass.** The sliding indicator jumps and panels fade only, for reduced-motion users.
- **Colour contrast: Pass.** Active tab 4.95:1, inactive tabs 7.7:1.

## Source

```tsx
"use client";

import { useId, useRef, useState, type KeyboardEvent, type ReactNode } from "react";
import { AnimatePresence, motion, useReducedMotion } from "motion/react";

export type Tab = {
  id: string;
  label: string;
  content: ReactNode;
};

type AnimatedTabsProps = {
  tabs: Tab[];
  defaultTab?: string;
  className?: string;
};

export function AnimatedTabs({ tabs, defaultTab, className = "" }: AnimatedTabsProps) {
  const [active, setActive] = useState(defaultTab ?? tabs[0]?.id);
  const layoutId = useId();
  const reduceMotion = useReducedMotion();
  const buttons = useRef<(HTMLButtonElement | null)[]>([]);
  const current = tabs.find((tab) => tab.id === active);

  // WAI-ARIA tabs pattern: arrows move between tabs, Home/End jump to the ends
  function handleKeyDown(event: KeyboardEvent<HTMLDivElement>) {
    const index = tabs.findIndex((tab) => tab.id === active);
    const next = {
      ArrowRight: (index + 1) % tabs.length,
      ArrowLeft: (index - 1 + tabs.length) % tabs.length,
      Home: 0,
      End: tabs.length - 1,
    }[event.key];
    if (next === undefined) return;
    event.preventDefault();
    setActive(tabs[next].id);
    buttons.current[next]?.focus();
  }

  return (
    <div className={`w-full ${className}`}>
      <div
        role="tablist"
        onKeyDown={handleKeyDown}
        className="inline-flex flex-wrap gap-1 rounded-xl border border-white/10 bg-white/[0.03] p-1"
      >
        {tabs.map((tab, index) => (
          <button
            key={tab.id}
            ref={(element) => {
              buttons.current[index] = element;
            }}
            role="tab"
            tabIndex={active === tab.id ? 0 : -1}
            id={`${layoutId}-tab-${tab.id}`}
            aria-selected={active === tab.id}
            aria-controls={`${layoutId}-panel`}
            onClick={() => setActive(tab.id)}
            className={`relative rounded-lg px-3.5 py-2 text-sm transition-colors focus-visible:outline-2 focus-visible:outline-[#5FAEB4] ${
              active === tab.id ? "text-white" : "text-[#94A9A7] hover:text-[#DCE3E2]"
            }`}
          >
            {active === tab.id && (
              <motion.span
                layoutId={layoutId}
                className="absolute inset-0 rounded-lg bg-[#0E7C86] shadow-[0_4px_20px_-6px_rgb(14_124_134/0.6)]"
                transition={reduceMotion ? { duration: 0 } : { type: "spring", bounce: 0.2, duration: 0.45 }}
              />
            )}
            <span className="relative">{tab.label}</span>
          </button>
        ))}
      </div>

      <div
        role="tabpanel"
        id={`${layoutId}-panel`}
        aria-labelledby={`${layoutId}-tab-${active}`}
        className="relative mt-4"
      >
        <AnimatePresence mode="wait" initial={false}>
          <motion.div
            key={active}
            initial={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 8, filter: "blur(4px)" }}
            animate={{ opacity: 1, y: 0, filter: "blur(0px)" }}
            exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -8, filter: "blur(4px)" }}
            transition={{ duration: 0.2 }}
          >
            {current?.content}
          </motion.div>
        </AnimatePresence>
      </div>
    </div>
  );
}
```
