Inputs
Toggle
One or more two-state buttons.
import { Toggle } from '@/components/Toggle';
export const ToggleHero = () => ( <div className="flex flex-col justify-center gap-6"> <Toggle aria-label="Toggle star"> <RiStarFill className="pointer-events-none size-4 shrink-0" /> </Toggle> <ToggleGroup type="multiple"> <ToggleGroupItem value="bold" aria-label="Toggle bold"> <RiBold className="size-4 shrink-0" /> </ToggleGroupItem> <ToggleGroupItem value="italic" aria-label="Toggle italic"> <RiItalic className="size-4 shrink-0" /> </ToggleGroupItem> <ToggleGroupItem value="strikethrough" aria-label="Toggle strikethrough"> <RiUnderline className="size-4 shrink-0" /> </ToggleGroupItem> </ToggleGroup> </div>);
Installation
npm install @radix-ui/react-toggle @radix-ui/react-toggle-group tailwind-variants
- Copy and paste the code into your project’s component directory. Do not forget to update the import paths.
// Tremor Toggle [v0.0.0]"use client" import React from "react"import * as TogglePrimitive from "@radix-ui/react-toggle"import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group" import { cx, focusRing } from "@/lib/utils" const toggleStyles = [ // base "group inline-flex h-9 min-w-9 items-center justify-center gap-2 rounded-md border px-2 text-sm font-medium shadow-sm transition-all duration-100 ease-in-out", "border-gray-300 dark:border-gray-800", // text color "text-gray-700 dark:text-gray-300", // background color "bg-white dark:bg-gray-950", //hover color "hover:bg-gray-50 dark:hover:bg-gray-900/60", // disabled "disabled:pointer-events-none disabled:text-gray-400 disabled:dark:text-gray-600", "data-[state=on]:bg-gray-100 data-[state=on]:text-gray-900 dark:data-[state=on]:bg-gray-800 dark:data-[state=on]:text-gray-50", focusRing,] const Toggle = React.forwardRef< React.ElementRef<typeof TogglePrimitive.Root>, React.ComponentPropsWithoutRef<typeof TogglePrimitive.Root>>(({ className, ...props }, ref) => ( <TogglePrimitive.Root ref={ref} className={cx(toggleStyles, className)} {...props} />)) Toggle.displayName = TogglePrimitive.Root.displayName export { Toggle } const ToggleGroup = React.forwardRef< React.ElementRef<typeof ToggleGroupPrimitive.Root>, React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root>>(({ className, children, ...props }, ref) => ( <ToggleGroupPrimitive.Root ref={ref} className={cx( "flex flex-nowrap items-center justify-center gap-1", className, )} {...props} > {children} </ToggleGroupPrimitive.Root>)) ToggleGroup.displayName = ToggleGroupPrimitive.Root.displayName const ToggleGroupItem = React.forwardRef< React.ElementRef<typeof ToggleGroupPrimitive.Item>, React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item>>(({ className, children, ...props }, ref) => ( <ToggleGroupPrimitive.Item ref={ref} className={cx(toggleStyles, className)} {...props} > {children} </ToggleGroupPrimitive.Item>)) ToggleGroupItem.displayName = ToggleGroupPrimitive.Item.displayName export { ToggleGroup, ToggleGroupItem }
Example: Disabled
"use client"
import { Toggle } from "@/components/Toggle"
export const ToggleDisabledExample = () => ( <div className="flex flex-col gap-6"> <div className="flex items-center justify-center gap-2"> <Toggle disabled>Disabled</Toggle> </div> </div>);
API Reference: Toggle
This component uses the Radix UI API.