Toggle Group
Toggle Group with thoughtful defaults, semantic styling, and editable source.
Make it yours.
Install the editable source and its dependencies into your React + Tailwind project.
Scroll horizontally for long lines
pnpm dlx shadcn@4.0.8 add http://localhost:3000/r/toggle-group.jsonUse it
Scroll horizontally for long lines
"use client";
import { ToggleGroup } from "@/components/jez-ui/ui/toggle-group";
export default function Example() {
return (
<>
<ToggleGroup
type="single"
defaultValue="week"
aria-label="Period"
options={[
{ label: "Day", value: "day" },
{ label: "Week", value: "week" },
{ label: "Month", value: "month" },
]}
/>
</>
);
}
Compose your own
These styled parts are included in the same install. Use children to build your layout; add className only when you want an override.
Scroll horizontally for long lines
import { ToggleGroup, ToggleGroupItem } from "@/components/jez-ui/ui/toggle-group";Props & types
Scroll horizontally for long lines
React.ComponentProps<typeof P.Root> & {
options?: { label: React.ReactNode; value: string; disabled?: boolean }[];
}Source
View 2 source files
registry/ui/toggle-group.tsx
Scroll horizontally for long lines
"use client";
import * as React from "react";
import { ToggleGroup as P } from "radix-ui";
import { cn } from "./utils";
export function ToggleGroup({
options,
children,
className,
...props
}: React.ComponentProps<typeof P.Root> & {
options?: { label: React.ReactNode; value: string; disabled?: boolean }[];
}) {
return (
<P.Root
className={cn(
"inline-flex flex-wrap gap-1 rounded-xl border border-border p-1",
className,
)}
{...props}
>
{options
? options.map((o) => (
<ToggleGroupItem
key={o.value}
value={o.value}
disabled={o.disabled}
>
{o.label}
</ToggleGroupItem>
))
: children}
</P.Root>
);
}
export function ToggleGroupItem({
className,
...props
}: React.ComponentProps<typeof P.Item>) {
return (
<P.Item
className={cn(
"rounded-lg px-3 py-2 text-sm outline-none hover:bg-muted/60 focus-visible:ring-2 focus-visible:ring-primary disabled:opacity-50 data-[state=on]:bg-muted data-[state=on]:text-foreground",
className,
)}
{...props}
/>
);
}
registry/ui/utils.ts
Scroll horizontally for long lines
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
export function cn(...values: ClassValue[]) {
return twMerge(clsx(values));
}
Dependencies
radix-ui@1.6.7 · clsx@2.1.1 · tailwind-merge@3.5.0
Accessibility & behaviour
Provide meaningful labels and preserve keyboard focus styles. Check contrast when customising theme colours.
Read the accessibility and performance guide →