Components / Buttons
Segmented Toggle Button
Free Buttons React component — SegmentedToggleButton. MIT licensed, copy-paste ready for Next.js and Tailwind CSS.
iOS segmented control — sliding white pill between Day, Week, and Month. Pass your own options array for view modes or filters.
Preview
Setup
How to use
Free for personal and commercial use. No UI attribution required. Include the MIT copyright notice when copying component source into your project. Read the MIT license.
- 1
Run in your terminal:
$npm install clsx tailwind-merge lucide-react - 2
Copy
lib/cn.tsbelow. Skip if you already havecn(). - 3
Copy the code below and create
components/buttons/segmented-toggle-button.tsxin your project. - 4
Import and render:
Exampleimport { SegmentedToggleButton } from "@/components/buttons/segmented-toggle-button";
lib/cn.ts
import { type ClassValue, clsx } from "clsx";import { twMerge } from "tailwind-merge";export function cn(...inputs: ClassValue[]) {return twMerge(clsx(inputs));}
components/buttons/segmented-toggle-button.tsx
"use client";import { forwardRef, useState, type ComponentPropsWithoutRef } from "react";import { cn } from "@/lib/cn";export type SegmentedToggleButtonProps = Readonly<{options?: readonly string[];defaultIndex?: number;onChange?: (index: number, value: string) => void;} & ComponentPropsWithoutRef<"div">>;// Segmented toggle — iOS-style sliding pill between two or three options.export const SegmentedToggleButton = forwardRef<HTMLDivElement,SegmentedToggleButtonProps>(({className,options = ["Day", "Week", "Month"],defaultIndex = 0,onChange,...props},ref,) => {const [active, setActive] = useState(defaultIndex);const count = options.length;const select = (index: number) => {setActive(index);onChange?.(index, options[index] ?? "");};return (<divref={ref}role="tablist"data-slot="segmented-toggle-button"className={cn("relative inline-flex h-10 rounded-xl bg-neutral-100 p-1 font-sans text-sm font-medium select-none",className,)}{...props}><spanaria-hiddenclassName="absolute top-1 bottom-1 rounded-lg bg-white shadow-sm transition-[left,width] duration-200 ease-out"style={{left: `calc(${active} * (100% / ${count}) + 4px)`,width: `calc(100% / ${count} - 8px)`,}}/>{options.map((option, index) => (<buttonkey={option}type="button"role="tab"aria-selected={active === index}onClick={() => select(index)}className={cn("relative z-10 flex-1 cursor-pointer px-4 py-1.5 transition-colors duration-200",active === index ? "text-neutral-900" : "text-neutral-500",)}>{option}</button>))}</div>);},);SegmentedToggleButton.displayName = "SegmentedToggleButton";