Toggle Group

Group of toggle buttons for single or multiple selection. Install Toggle Group from the VLLNT UI registry with the shadcn CLI.

Report a bug

Group of toggle buttons for single or multiple selection. Part of the core family in VLLNT UI, it ships as a machine-readable registry entry — copy the source directly into your app with the shadcn CLI and own it, no runtime dependency on a component library.

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Installation

Add Toggle Group to your project with the shadcn CLI. The source lands in your codebase, ready to adapt:

pnpm dlx shadcn@latest add https://ui.vllnt.ai/r/toggle-group.json

Source

"use client"; import { createContext, use, useMemo } from "react"; import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"; import type { VariantProps } from "class-variance-authority"; import { cn } from "../../lib/utils"; import { toggleVariants } from "../toggle/toggle"; const ToggleGroupContext = createContext<VariantProps<typeof toggleVariants>>({ size: "default", variant: "default", }); const ToggleGroup = ({ children, className, ref, size, variant, ...props }: React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root> & VariantProps<typeof toggleVariants> & { ref?: React.Ref<React.ComponentRef<typeof ToggleGroupPrimitive.Root>>; }) => { const contextValue = useMemo(() => ({ size, variant }), [size, variant]); return ( <ToggleGroupPrimitive.Root className={cn("flex items-center justify-center gap-1", className)} ref={ref} {...props} > <ToggleGroupContext.Provider value={contextValue}> {children} </ToggleGroupContext.Provider> </ToggleGroupPrimitive.Root> ); }; ToggleGroup.displayName = ToggleGroupPrimitive.Root.displayName; const ToggleGroupItem = ({ children, className, ref, size, variant, ...props }: React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item> & VariantProps<typeof toggleVariants> & { ref?: React.Ref<React.ComponentRef<typeof ToggleGroupPrimitive.Item>>; }) => { const context = use(ToggleGroupContext); return ( <ToggleGroupPrimitive.Item className={cn( toggleVariants({ size: context.size ?? size, variant: context.variant ?? variant, }), className, )} ref={ref} {...props} > {children} </ToggleGroupPrimitive.Item> ); }; ToggleGroupItem.displayName = ToggleGroupPrimitive.Item.displayName; export { ToggleGroup, ToggleGroupItem };

Stories

Explore every variant and state in the interactive Storybook:

Preview

Switch between light and dark to inspect the embedded Storybook preview.

Dependencies

  • @vllnt/ui@^0.3.0