Skip to content
Primitives

Segmented Control

Pill toggle where the active background glides between options via a layoutId.

MotionlayoutId indicator on spring.snappy

Install

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

bash
npm install kineto
tsx
import { SegmentedControl } from "kineto/segmented-control";

Preview

tsx
"use client";

import * as React from "react";
import { motion, useReducedMotion } from "motion/react";

import { cn } from "@/lib/cn";
import { spring } 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];
}

export interface SegmentedOption {
  label: React.ReactNode;
  value: string;
}

export interface SegmentedControlProps
  extends Omit<React.HTMLAttributes<HTMLDivElement>, "onChange" | "defaultValue"> {
  options: SegmentedOption[];
  value?: string;
  defaultValue?: string;
  onValueChange?: (value: string) => void;
  size?: "sm" | "default";
}

const SegmentedControl = React.forwardRef<HTMLDivElement, SegmentedControlProps>(
  (
    {
      className,
      options,
      value,
      defaultValue,
      onValueChange,
      size = "default",
      ...props
    },
    ref
  ) => {
    const [active, setActive] = useControllableState({
      prop: value,
      defaultProp: defaultValue ?? options[0]?.value ?? "",
      onChange: onValueChange,
    });
    const layoutId = React.useId();
    const reduced = useReducedMotion();

    return (
      <div
        ref={ref}
        role="tablist"
        className={cn(
          "inline-flex items-center gap-1 rounded-lg bg-muted p-1",
          className
        )}
        {...props}
      >
        {options.map((option) => {
          const isActive = option.value === active;
          return (
            <button
              key={option.value}
              type="button"
              role="tab"
              aria-selected={isActive}
              onClick={() => setActive(option.value)}
              className={cn(
                "relative inline-flex items-center justify-center rounded-md font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
                size === "sm" ? "px-2.5 py-1 text-xs" : "px-3.5 py-1.5 text-sm",
                isActive
                  ? "text-foreground"
                  : "text-muted-foreground hover:text-foreground"
              )}
            >
              {isActive ? (
                <motion.span
                  layoutId={`${layoutId}-segment`}
                  transition={reduced ? { duration: 0 } : spring.snappy}
                  className="absolute inset-0 z-0 rounded-md bg-card shadow-soft"
                />
              ) : null}
              <span className="relative z-10">{option.label}</span>
            </button>
          );
        })}
      </div>
    );
  }
);
SegmentedControl.displayName = "SegmentedControl";

export { SegmentedControl };

src/components/ui/segmented-control.tsx

Props

PropTypeDefaultDescription
options{ label: ReactNode; value: string }[]—Segments to render.
value / defaultValuestring—Controlled or uncontrolled active segment.
size"sm" | "default""default"Control sizing.