Toggle
Two-state button for toolbar actions, filters, and formatting controls.
Toggle
Two-state button for toolbar actions, filters, and formatting controls.
Press the outline Bold button — a muted fill springs in behind the icon, a one-shot light sheen sweeps across the surface, and the glyph squashes and snaps back as the release notes copy tightens to bold. Press again and the fill retracts while the label returns to normal weight.
Installation
Install the component with the Craft-UI Blocks CLI — it drops r-toggle.tsx into your project and wires up its runtime dependencies:
npx mvpblocks add r-togglepnpm dlx mvpblocks add r-toggleyarn dlx mvpblocks add r-togglebunx mvpblocks add r-toggleFile Structure
The CLI saves the component to your project's components/ui folder:
Import it from @/components/ui/r-toggle.
Usage
"use client";
import { Bold } from "lucide-react";
import { useState } from "react";
import { Toggle } from "@/components/ui/r-toggle";
export function TogglePreview() {
const [bold, setBold] = useState(false);
return (
<div className="flex min-h-[18rem] items-center justify-center px-4 py-6">
<p className="flex flex-wrap items-center justify-center gap-x-2.5 gap-y-2 text-balance font-medium text-lg text-neutral-800 leading-snug tracking-tight sm:text-xl dark:text-neutral-100">
<span>Ship the</span>
<span
className={
bold
? "font-bold text-neutral-950 transition-[font-weight,color] duration-200 dark:text-neutral-50"
: "text-neutral-800 transition-[font-weight,color] duration-200 dark:text-neutral-100"
}
>
release notes
</span>
<span className="inline-flex translate-y-px items-center align-middle">
<Toggle
aria-label="Toggle bold"
className="size-8 min-h-8 min-w-8 shrink-0 px-2 py-2"
onPressedChange={setBold}
pressed={bold}
size="sm"
variant="outline"
>
<Bold className="size-4" />
</Toggle>
</span>
</p>
</div>
);
}Pass pressed and onPressedChange to own the state yourself, or leave them off and the toggle manages its own defaultPressed. Children flow normally, so icon-plus-label content expands the control width on its own.
Theming
The toggle inherits the host shadcn theme tokens rather than shipping a bespoke palette, so it matches whatever theme your app already runs. The pressed surface is a bg-muted fill layered behind the content, the resting label sits on text-muted-foreground and lifts to text-foreground once pressed (data-[state=on]), and the outline variant adds a hairline border-input that stays put in both states. Focus draws a ring-ring/50 ring with a border-ring, invalid states use border-destructive with a ring-destructive/20 (or /40 in dark mode) ring, and disabled controls drop to opacity-50 while keeping their pressed appearance.
Because the fill and text read from the shared --muted, --muted-foreground, --foreground, --input, and --ring variables, both light and dark mode resolve automatically with no extra token block. Corners upgrade to a true squircle via corner-shape where the browser supports it, falling back to plain radii elsewhere.
Animations
- Liquid fill: the pressed fill is driven by a
fillProgressmotion value that springs between0and1on a heavy210 / 28 / 1.08spring, and its opacity is mapped straight from that progress — so thebg-mutedsurface blooms in and retracts with weight instead of a hard swap. - Sheen sweep: every state change fires a one-shot diagonal light sweep. A skewed gradient bar (
via-foreground/18,via-foreground/26in dark mode) shoots fromleft: -40%to140%over0.58sease-[0.22, 1, 0.36, 1]while its opacity fades out on[0.4, 0, 0.2, 1]. - Icon snap: on each toggle the icon scale springs
0.93 ↔ 1on a340 / 32 / 0.92spring, then squashes wide-and-short (scaleX 1.1,scaleY 0.91) on a520 / 34 / 0.72spring and settles back on a0.45sbounce: 0.32spring. - Press scale: pointer-down compresses the icon to
scaleX 0.86,scaleY 1.08; releasing or leaving the surface springs it back to rest. - Radix primitive: the Radix
Toggle.Rootis composed withasChildonto the animatedFluidToggleButton, sodata-state/aria-pressedfrom Radix drive the same motion layers and text states.
Motion and interaction behavior
Pressed state drives a horizontal liquid wipe, a one-shot light sheen, and tactile icon motion.
- Built on Radix toggle primitives while preserving the same bouncy fill pop, icon snap, and press scale as the Base UI version.
- The muted fill wipes in from the left when pressed and retracts to the right on release with a heavy spring.
- Each state change sends a diagonal sheen across the surface once.
outlinekeeps the same outer border in both states. - Icons scale and squash on press with a spring settle.
Props
Toggle
Prop
Type
toggleVariants
Prop
Type
Registry Bundle
Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: radix-ui, class-variance-authority, motion.
Installs a Radix toggle with inline spring fill, sheen sweep, and press-scale motion in the same file. It uses Radix toggle primitives under the same spring background, content lift, and press scale shell.
Contact
If you find any bug or issue, feel free to raise an issue.
Last updated on