Tabs

Underlined tabs with a purple active line and arrow-key navigation.

PNG, JPEG or WebP at any viewport.

Install

$ npx shadcn@latest add @nudgekit/tabs

Or with no config: npx shadcn@latest add https://ui.nudgekit.app/r/tabs.json

Usage

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

<Tabs defaultValue="image">
  <TabsList>
    <TabsTrigger value="image">Image</TabsTrigger>
    <TabsTrigger value="pdf">PDF</TabsTrigger>
  </TabsList>
  <TabsContent value="image">…</TabsContent>
  <TabsContent value="pdf">…</TabsContent>
</Tabs>

Do

  • Use to switch between views of the same thing.
  • Keep labels to one or two words.

Don't

  • Use pill backgrounds for tabs; the purple underline is the active state.

Code

What the CLI writes into your project. It is yours to change.

components/ui/tabs.tsx
"use client";

import * as React from "react";

import { cn } from "@/lib/utils";

type TabsContextValue = { value: string; setValue: (v: string) => void; id: string };
const TabsContext = React.createContext<TabsContextValue | null>(null);

function useTabs() {
  const ctx = React.useContext(TabsContext);
  if (!ctx) throw new Error("Tabs parts must be used inside <Tabs>");
  return ctx;
}

function Tabs({
  defaultValue,
  value,
  onValueChange,
  className,
  children,
}: {
  defaultValue: string;
  value?: string;
  onValueChange?: (v: string) => void;
  className?: string;
  children: React.ReactNode;
}) {
  const [inner, setInner] = React.useState(defaultValue);
  const id = React.useId();
  const current = value ?? inner;
  const setValue = (v: string) => {
    if (value === undefined) setInner(v);
    onValueChange?.(v);
  };
  return (
    <TabsContext.Provider value={{ value: current, setValue, id }}>
      <div data-slot="tabs" className={className}>
        {children}
      </div>
    </TabsContext.Provider>
  );
}

function TabsList({ className, ...props }: React.HTMLAttributes<HTMLDivElement>) {
  return (
    <div
      role="tablist"
      data-slot="tabs-list"
      className={cn("flex items-center gap-1 border-b border-border", className)}
      onKeyDown={(e) => {
        const tabs = Array.from(e.currentTarget.querySelectorAll<HTMLButtonElement>('[role="tab"]'));
        const i = tabs.indexOf(document.activeElement as HTMLButtonElement);
        if (i < 0) return;
        const next = e.key === "ArrowRight" ? (i + 1) % tabs.length : e.key === "ArrowLeft" ? (i - 1 + tabs.length) % tabs.length : -1;
        if (next >= 0) {
          e.preventDefault();
          tabs[next].focus();
          tabs[next].click();
        }
      }}
      {...props}
    />
  );
}

function TabsTrigger({ value, className, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement> & { value: string }) {
  const ctx = useTabs();
  const on = ctx.value === value;
  return (
    <button
      type="button"
      role="tab"
      id={`${ctx.id}-tab-${value}`}
      aria-selected={on}
      aria-controls={`${ctx.id}-panel-${value}`}
      tabIndex={on ? 0 : -1}
      onClick={() => ctx.setValue(value)}
      className={cn(
        "-mb-px border-b-2 px-3 py-2.5 text-sm transition-colors duration-150 focus-visible:outline-2 focus-visible:outline-offset-[-2px] focus-visible:outline-ring",
        on ? "border-brand font-medium text-foreground" : "border-transparent text-muted-foreground hover:text-foreground",
        className,
      )}
      {...props}
    />
  );
}

function TabsContent({ value, className, ...props }: React.HTMLAttributes<HTMLDivElement> & { value: string }) {
  const ctx = useTabs();
  if (ctx.value !== value) return null;
  return (
    <div
      role="tabpanel"
      id={`${ctx.id}-panel-${value}`}
      aria-labelledby={`${ctx.id}-tab-${value}`}
      data-slot="tabs-content"
      className={cn("pt-4", className)}
      {...props}
    />
  );
}

export { Tabs, TabsList, TabsTrigger, TabsContent };