Toggle
SaveA button that stays pressed, in ghost and outline styles, with text or a single icon.
- Category
- Forms
- Version
- 0.1.0
- Frameworks
- Next.js, Vite + React
- Styling
- Tailwind CSS v4
Preview
Toggle preview
Installation
npx @shakenui/cli add toggleReview the plan first. A dry run lists every file and package without changing anything:
npx @shakenui/cli add toggle --dry-runUsage
toggle-example.tsx
import { Toggle } from "@/components/shaken/ui/toggle";
function PinIcon() {
return (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
<path d="M9.5 2.25l4.25 4.25-2.5 1-2 2 .25 2.75-1 1-2.25-2.25L3 14l-.25-.25 3.25-3.25-2.25-2.25 1-1 2.75.25 2-2 1-2.5z" />
</svg>
);
}
function WrapIcon() {
return (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round">
<path d="M2.5 4h11M2.5 8h9a2 2 0 0 1 0 4H8.5M2.5 12h3M10 10.5L8.5 12l1.5 1.5" />
</svg>
);
}
function BoldIcon() {
return (
<svg viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinejoin="round">
<path d="M4.5 2.75h4.25a2.6 2.6 0 0 1 0 5.25H4.5zM4.5 8h5a2.6 2.6 0 0 1 0 5.25h-5z" />
</svg>
);
}
export default function ToggleExample() {
return (
<div className="flex flex-wrap items-center gap-3">
<Toggle aria-label="Bold" iconOnly defaultPressed>
<BoldIcon />
</Toggle>
<Toggle variant="outline" defaultPressed>
<PinIcon />
Pinned
</Toggle>
<Toggle variant="outline">
<WrapIcon />
Wrap lines
</Toggle>
<Toggle size="sm" disabled>
Read only
</Toggle>
</div>
);
}API
| Prop | Type | Default | Description |
|---|---|---|---|
| pressed / defaultPressed | boolean | — | Controlled or initial state. |
| onPressedChange | (pressed: boolean) => void | — | Called when the toggle is pressed or released. |
| variant | "ghost" | "outline" | "ghost" | Toolbar style, or with an edge for standalone use. |
| size | "sm" | "md" | "md" | 32px or 36px tall. |
| iconOnly | boolean | false | A square button for one icon; give it an aria-label. |
Accessibility
- Renders a native <button> with aria-pressed, so screen readers announce pressed or not pressed.
- Enter and Space toggle it; the focus ring shows on keyboard focus only.
- The pressed look combines a filled tint with the primary text color.
- The 100ms press scale is skipped under reduced motion.
Files
Installed into src/components/shaken, or components/shaken when the project has no src directory.
src/components/shaken/ui/toggle.tsxtoggle- npm packages: @base-ui/react@^1.9.0React and Tailwind CSS v4 required
Source
The exact source the CLI installs, from /r/items/react/toggle/0.1.0.json.
ui/toggle.tsxShowHide
ui/toggle.tsx
"use client";
import { Toggle as BaseToggle } from "@base-ui/react/toggle";
type ClassName<State> = string | ((state: State) => string | undefined) | undefined;
function cx(...classes: (string | false | null | undefined)[]) {
return classes.filter(Boolean).join(" ");
}
/** Prepends base classes while keeping Base UI's state-function form of className working. */
function withBase<State>(base: string, className: ClassName<State>) {
return typeof className === "function" ? (state: State) => cx(base, className(state)) : cx(base, className);
}
const variants = {
ghost: cx(
"text-zinc-600 hover:bg-zinc-950/[0.04] hover:text-zinc-950",
"dark:text-zinc-400 dark:hover:bg-white/[0.06] dark:hover:text-white",
"data-[pressed]:bg-zinc-950/[0.07] data-[pressed]:text-zinc-950 dark:data-[pressed]:bg-white/[0.1] dark:data-[pressed]:text-white",
),
outline: cx(
"border border-zinc-950/12 bg-white text-zinc-700 shadow-[0_1px_2px_rgb(0_0_0/0.04)] hover:border-zinc-950/20 hover:bg-zinc-950/[0.03] hover:text-zinc-950",
"dark:border-white/15 dark:bg-black dark:text-zinc-300 dark:shadow-none dark:hover:border-white/25 dark:hover:bg-white/[0.06] dark:hover:text-white",
"data-[pressed]:border-zinc-950/25 data-[pressed]:bg-zinc-950/[0.06] data-[pressed]:text-zinc-950 data-[pressed]:shadow-[inset_0_1px_1px_rgb(0_0_0/0.06)]",
"dark:data-[pressed]:border-white/30 dark:data-[pressed]:bg-white/[0.1] dark:data-[pressed]:text-white",
),
} as const;
const sizes = {
sm: { text: "h-8 px-2.5 text-sm", icon: "size-8" },
md: { text: "h-9 px-3 text-sm", icon: "size-9" },
} as const;
export type ToggleVariant = keyof typeof variants;
export type ToggleSize = keyof typeof sizes;
export interface ToggleProps extends BaseToggle.Props {
/** `ghost` for toolbars; `outline` for a standalone toggle that needs an edge. */
variant?: ToggleVariant;
size?: ToggleSize;
/** A square button for a single icon. Give it an `aria-label`. */
iconOnly?: boolean;
}
/**
* A button that stays pressed, such as Bold in an editor or Pin on a message. It sets
* `aria-pressed`, so screen readers announce the state; the pressed look is a filled tint
* plus the primary text color. Use `pressed` + `onPressedChange` to control it.
*/
export function Toggle({ variant = "ghost", size = "md", iconOnly = false, className, ...props }: ToggleProps) {
return (
<BaseToggle
className={withBase(
cx(
"inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 rounded-lg font-medium whitespace-nowrap select-none",
iconOnly ? sizes[size].icon : sizes[size].text,
variants[variant],
"transition-[background-color,border-color,color,box-shadow,scale] duration-150 ease-out active:duration-100",
"motion-safe:active:scale-[0.97] motion-reduce:transition-none",
"focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-500",
"data-[disabled]:pointer-events-none data-[disabled]:opacity-45",
"[&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0",
),
className,
)}
{...props}
/>
);
}