# Tabs

A sunken tray with one raised tile that slides, with a bounce, to the active tab.

Docs: https://cozy-ui.solo.engineer/docs/components/tabs/

Install: `npx shadcn@latest add @cozy/tabs`

### Usage

```tsx
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs"
```

### Example

```tsx
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";

export default function TabsDemo() {
  return (
    <Tabs defaultValue="fields" className="w-full max-w-md">
      <TabsList>
        <TabsTrigger value="fields">Fields</TabsTrigger>
        <TabsTrigger value="orchard">Orchard</TabsTrigger>
        <TabsTrigger value="barn">Barn</TabsTrigger>
        <TabsTrigger value="tools">Tools</TabsTrigger>
      </TabsList>
      <TabsContent value="fields">Rice and vegetable plots: plant, wait, harvest.</TabsContent>
      <TabsContent value="orchard">Banana grove and luffa trellis.</TabsContent>
      <TabsContent value="barn">Chicken coop and fish pond.</TabsContent>
      <TabsContent value="tools">Pedal thresher and rice mill.</TabsContent>
    </Tabs>
  );
}
```

### Source (components/ui/tabs.tsx)

```tsx
import * as React from "react";
import { Tabs as TabsPrimitive } from "radix-ui";
import { cn } from "@/lib/utils";

function Tabs({ className, ...props }: React.ComponentProps<typeof TabsPrimitive.Root>) {
  return <TabsPrimitive.Root data-slot="tabs" className={cn("flex flex-col", className)} {...props} />;
}

/** The list measures its active trigger and slides one raised indicator under it. */
function TabsList({ className, children, ...props }: React.ComponentProps<typeof TabsPrimitive.List>) {
  const ref = React.useRef<HTMLDivElement>(null);
  const [box, setBox] = React.useState<{ x: number; y: number; w: number; h: number } | null>(null);

  React.useLayoutEffect(() => {
    const list = ref.current;
    if (!list) return;
    const measure = () => {
      const active = list.querySelector<HTMLElement>('[data-state="active"]');
      if (!active) return setBox(null);
      setBox({ x: active.offsetLeft, y: active.offsetTop, w: active.offsetWidth, h: active.offsetHeight });
    };
    measure();
    const mo = new MutationObserver(measure);
    mo.observe(list, { subtree: true, attributes: true, attributeFilter: ["data-state"] });
    const ro = new ResizeObserver(measure);
    ro.observe(list);
    return () => { mo.disconnect(); ro.disconnect(); };
  }, []);

  return (
    <TabsPrimitive.List ref={ref} data-slot="tabs-list" data-measured={box ? "" : undefined} className={cn("cz-tabs__list", className)} {...props}>
      {box && (
        <span
          aria-hidden
          className="cz-tabs__indicator"
          style={{ width: box.w, height: box.h, translate: `${box.x}px ${box.y - 3}px`, top: 0 }}
        />
      )}
      {children}
    </TabsPrimitive.List>
  );
}
function TabsTrigger({ className, ...props }: React.ComponentProps<typeof TabsPrimitive.Trigger>) {
  return <TabsPrimitive.Trigger data-slot="tabs-trigger" className={cn("cz-tabs__trigger", className)} {...props} />;
}
function TabsContent({ className, ...props }: React.ComponentProps<typeof TabsPrimitive.Content>) {
  return <TabsPrimitive.Content data-slot="tabs-content" className={cn("cz-tabs__content", className)} {...props} />;
}

export { Tabs, TabsList, TabsTrigger, TabsContent };
```

npm: `import { Tabs, TabsList, TabsTrigger, TabsContent } from "@0xtrou/cozy-ui"` (after `npm install @0xtrou/cozy-ui` and `import "@0xtrou/cozy-ui/styles.css"`).
