Skip to content
Primitives

Tabs

Radix tabs with a shared active indicator that glides between triggers.

MotionlayoutId indicator on spring.snappy; content fade on tween.quick

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { Tabs, TabsList, TabsTrigger, TabsContent } from "kineto/tabs";

Preview

The active pill is one shared element animated between triggers with a layoutId.

tsx
"use client";

import * as React from "react";
import * as TabsPrimitive from "@radix-ui/react-tabs";
import { motion, useReducedMotion } from "motion/react";

import { cn } from "@/lib/cn";
import { spring, tween } from "@/lib/motion-presets";

function useControllableState<T>({
  prop,
  defaultProp,
  onChange,
}: {
  prop: T | undefined;
  defaultProp: T;
  onChange?: (value: T) => void;
}): [T, (next: T) => void] {
  const [uncontrolled, setUncontrolled] = React.useState(defaultProp);
  const isControlled = prop !== undefined;
  const value = isControlled ? (prop as T) : uncontrolled;
  const setValue = React.useCallback(
    (next: T) => {
      if (!isControlled) setUncontrolled(next);
      onChange?.(next);
    },
    [isControlled, onChange]
  );
  return [value, setValue];
}

interface TabsContextValue {
  active: string;
  /** Unique per Tabs instance so multiple tab groups don't share one sliding indicator. */
  layoutId: string;
}

const TabsContext = React.createContext<TabsContextValue>({
  active: "",
  layoutId: "tabs",
});

const Tabs = ({
  value,
  defaultValue,
  onValueChange,
  children,
  ...props
}: TabsPrimitive.TabsProps) => {
  const [active, setActive] = useControllableState({
    prop: value,
    defaultProp: defaultValue ?? "",
    onChange: onValueChange,
  });
  const layoutId = React.useId();

  return (
    <TabsPrimitive.Root value={active} onValueChange={setActive} {...props}>
      <TabsContext.Provider value={{ active, layoutId }}>
        {children}
      </TabsContext.Provider>
    </TabsPrimitive.Root>
  );
};
Tabs.displayName = "Tabs";

const TabsList = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.List>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.List>
>(({ className, ...props }, ref) => (
  <TabsPrimitive.List
    ref={ref}
    className={cn(
      "inline-flex h-11 items-center justify-center gap-1 rounded-lg bg-muted p-1 text-muted-foreground",
      className
    )}
    {...props}
  />
));
TabsList.displayName = "TabsList";

const TabsTrigger = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Trigger>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Trigger>
>(({ className, value, children, ...props }, ref) => {
  const { active, layoutId } = React.useContext(TabsContext);
  const reduced = useReducedMotion();
  const isActive = active === value;

  return (
    <TabsPrimitive.Trigger
      ref={ref}
      value={value}
      className={cn(
        "relative inline-flex items-center justify-center gap-2 whitespace-nowrap rounded-md px-3 py-1.5 text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50 data-[state=active]:text-foreground",
        className
      )}
      {...props}
    >
      {isActive ? (
        <motion.span
          layoutId={`${layoutId}-active`}
          transition={reduced ? { duration: 0 } : spring.snappy}
          className="absolute inset-0 z-0 rounded-md bg-background shadow-soft"
        />
      ) : null}
      <span className="relative z-10 inline-flex items-center gap-2">
        {children}
      </span>
    </TabsPrimitive.Trigger>
  );
});
TabsTrigger.displayName = "TabsTrigger";

const TabsContent = React.forwardRef<
  React.ElementRef<typeof TabsPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof TabsPrimitive.Content>
>(({ className, children, ...props }, ref) => {
  const reduced = useReducedMotion();

  return (
    <TabsPrimitive.Content
      ref={ref}
      className={cn(
        "mt-4 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background",
        className
      )}
      {...props}
    >
      <motion.div
        initial={reduced ? { opacity: 0 } : { opacity: 0, y: 8 }}
        animate={{ opacity: 1, y: 0 }}
        transition={reduced ? { duration: 0 } : tween.quick}
      >
        {children}
      </motion.div>
    </TabsPrimitive.Content>
  );
});
TabsContent.displayName = "TabsContent";

export { Tabs, TabsList, TabsTrigger, TabsContent };

src/components/ui/tabs.tsx

Props

PropTypeDefaultDescription
value / defaultValuestring—Controlled or uncontrolled active tab.
onValueChange(value: string) => void—Fires when the active tab changes.