Toggle
A button that stays on or off: pressing it shades it in with the pen, pressing it again pulls the shading out.
import { InkIcon } from "@/components/ui/ink-icons";
import { Toggle } from "@/components/ui/toggle";
export default function ToggleDemo() {
return (
<div className="flex flex-wrap items-center gap-6">
<Toggle aria-label="Toggle bold" seed="toggle-bold">
<InkIcon name="bold" />
</Toggle>
<Toggle aria-label="Toggle italic" defaultPressed seed="toggle-italic">
<InkIcon name="italic" />
</Toggle>
<Toggle aria-label="Toggle underline" seed="toggle-underline">
<InkIcon name="underline" />
</Toggle>
</div>
);
}Installation
pnpm dlx shadcn@latest add @ballpoint/ toggle"use client";
import { useId, useMemo } from "react";
import { Toggle as TogglePrimitive } from "@base-ui/react/toggle";
import { cva, type VariantProps } from "class-variance-authority";
import { penStyle, useInkFrame, useInkSeed, usePen, type Pen } from "@/hooks/use-ink-box";
import { inkClassName, InkSvg, Stroke } from "@/lib/ink";
import { hatchStrokes, penBoxStrokes, roundedRectPath } from "@/lib/ink-sketch";
const toggleVariants = cva(
[
"group/toggle ink-hover relative isolate inline-flex shrink-0 cursor-pointer items-center justify-center gap-2 text-base whitespace-nowrap select-none",
"text-ink-2 transition-colors duration-(--dur-hover) outline-none hover:text-ink data-pressed:text-ink",
"focus-visible:outline-solid focus-visible:outline-[1.5px] focus-visible:outline-offset-3 focus-visible:outline-ring focus-visible:[border-radius:var(--ink-r,var(--hand-radius))]",
"disabled:pointer-events-none disabled:opacity-50 data-disabled:pointer-events-none data-disabled:opacity-50",
"[&_svg:not(.ink-sketch)]:pointer-events-none [&_svg:not(.ink-sketch)]:shrink-0 [&_svg:not(.ink-sketch):not([class*='size-'])]:size-4",
],
{
variants: {
variant: { default: "", outline: "" },
size: {
default: "h-9 min-w-9 px-2.5",
sm: "h-8 min-w-8 px-2 text-sm",
lg: "h-11 min-w-11 px-3 text-lg",
},
},
defaultVariants: { variant: "default", size: "default" },
},
);
const heights = { default: 36, sm: 32, lg: 44 } as const;
/**
* A button that stays on or off. Pressing it shades it in with the pen, and
* pressing it again pulls the shading out. The `outline` variant is boxed at
* rest; the plain one is only boxed when you point at it.
*/
function Toggle({
className,
variant = "default",
size = "default",
seed,
roughness,
passes,
radius,
corners,
draw,
weight,
speed,
children,
...props
}: TogglePrimitive.Props & VariantProps<typeof toggleVariants> & Pick<Pen, "roughness" | "passes" | "radius" | "corners" | "draw" | "weight" | "speed"> & { seed?: string | number }) {
const pen = usePen({ roughness, passes, radius, corners, draw, weight, speed });
return (
<TogglePrimitive data-slot="toggle" data-variant={variant} className={inkClassName(toggleVariants({ variant, size }), className)} {...props}>
<ToggleInk variant={variant ?? "default"} seed={seed} pen={pen} estimate={[heights[size ?? "default"], heights[size ?? "default"]]} />
{children}
</TogglePrimitive>
);
}
function ToggleInk({ variant, seed, pen, estimate }: { variant: "default" | "outline"; seed?: string | number; pen: Pen; estimate: readonly [number, number] }) {
const id = useId().replace(/[^\w-]/g, "");
const s = useInkSeed(seed);
const { ref, w, h, frame } = useInkFrame(estimate, { pad: 6 });
const r = pen.radius === "full" ? h / 2 : Math.min(pen.radius ?? 0, h / 2);
const mode = pen.draw ?? "auto";
const paths = useMemo(
() => ({
outline: penBoxStrokes(s, w, h, { roughness: pen.roughness ?? 1, passes: pen.passes ?? 1, corners: pen.corners ?? "crossed", radius: r }),
wash: hatchStrokes(s + 3, w, h, { gap: 4.8, angle: -50, jitter: 0.4, inset: 1.5 }).join(""),
body: roundedRectPath(w, h, r),
}),
[s, w, h, pen.roughness, pen.passes, pen.corners, r],
);
return (
<InkSvg ref={ref} {...frame} pending={variant === "outline" && mode === "auto"} className="-z-10" style={{ ...frame.style, ...penStyle(pen) }}>
<defs>
<clipPath id={`${id}-clip`}>
<path d={paths.body} />
</clipPath>
</defs>
<g clipPath={`url(#${id}-clip)`}>
<Stroke d={paths.wash} draw="checked" duration={420} width={0.9} opacity={0.42} />
</g>
{paths.outline.map((d, i) =>
variant === "outline" ? (
<Stroke key={i} d={d} draw={mode} delay={i * 200} duration={360} width={[1.3, 1, 0.9][i]} opacity={[1, 0.75, 0.55][i]} className="text-ink-line" />
) : (
<g key={i}>
<Stroke d={d} draw="hover" duration={320} width={1.2} />
<Stroke d={d} draw="checked" duration={320} width={1.3} />
</g>
),
)}
</InkSvg>
);
}
export { Toggle, toggleVariants };Usage
import { Toggle } from "@/components/ui/toggle"
<Toggle aria-label="Toggle bold">
<BoldIcon />
</Toggle>Examples
Outline
Boxed at rest, with and without a label.
import { InkIcon } from "@/components/ui/ink-icons";
import { Toggle } from "@/components/ui/toggle";
export default function ToggleOutline() {
return (
<div className="flex flex-wrap items-center gap-6">
<Toggle variant="outline" aria-label="Toggle italic" seed="to-italic">
<InkIcon name="italic" />
</Toggle>
<Toggle variant="outline" defaultPressed aria-label="Toggle bold" seed="to-bold">
<InkIcon name="bold" /> Bold
</Toggle>
<Toggle variant="outline" disabled aria-label="Toggle underline" seed="to-disabled">
<InkIcon name="underline" /> Disabled
</Toggle>
</div>
);
}Sizes
Small, default and large.
import { Toggle } from "@/components/ui/toggle";
export default function ToggleSizes() {
return (
<div className="flex flex-wrap items-center gap-6">
<Toggle variant="outline" size="sm" seed="ts-sm">
Small
</Toggle>
<Toggle variant="outline" seed="ts-default">
Default
</Toggle>
<Toggle variant="outline" size="lg" seed="ts-lg">
Large
</Toggle>
</div>
);
}Controlled
Keep the pressed state in React and read it back.
Not in your favourites.
"use client";
import { useState } from "react";
import { InkIcon } from "@/components/ui/ink-icons";
import { Toggle } from "@/components/ui/toggle";
export default function ToggleControlled() {
const [starred, setStarred] = useState(false);
return (
<div className="flex items-center gap-4">
<Toggle variant="outline" pressed={starred} onPressedChange={setStarred} aria-label="Star this page" seed="tc-star">
<InkIcon name="star" />
{starred ? "Starred" : "Star"}
</Toggle>
<p className="text-ink-2" aria-live="polite">
{starred ? "Saved to your favourites." : "Not in your favourites."}
</p>
</div>
);
}API reference
- variantdefault
"default" "default" | "outline"Boxed only when you point at it, or boxed all the time.
- sizedefault
"default" "default" | "sm" | "lg"Height and type size.
- pressed / defaultPresseddefault
false booleanPressed or not, controlled or not.
- onPressedChange
(pressed: boolean) => voidCalled as it is pressed.
- seed
string | numberPins the drawing.
Every other prop goes to Base UI's Toggle.
Pen settings
The drawing also takes draw, roughness, passes, radius, corners, weight and speed, as props or from the nearest InkProvider. What each one does.