implement
Toggle

Toggle

A button that stays pressed.

import { Div } from "@implementjs/core";
import { BoldIcon, ItalicIcon, UnderlineIcon } from "@implementjs/lucide";
import { Toggle } from "@/lib/components/ui/toggle";

export default function ToggleDemo() {
	return Div(
		{ class: "flex items-center gap-1" },
		Toggle({ pressed: true, "aria-label": "Toggle bold" }, BoldIcon({ "aria-hidden": true })),
		Toggle({ "aria-label": "Toggle italic" }, ItalicIcon({ "aria-hidden": true })),
		Toggle(
			{ "aria-label": "Toggle underline", disabled: true },
			UnderlineIcon({ "aria-hidden": true }),
		),
	);
}

Installation

npx jsrepo add @implementjs/ui/toggle

It installs tailwind-variants at the same time.

Usage

A two-state button: bold on or off, a filter applied or not. variant picks transparent or outlined, size the scale — both are also written out as data-variant and data-size for styling from outside.

For a set of them that share state, use a toggle group.

import { signal } from "@implementjs/core";
import { Toggle } from "@/lib/components/ui/toggle";

const bold = signal(false);

Toggle({ pressed: bold, "aria-label": "Toggle bold" }, BoldIcon({ "aria-hidden": true }));

Labelling an icon toggle

A toggle with only an icon in it has no accessible name, so give it one — and hide the icon from the accessibility tree, since it is not the label.

API Reference

Every prop the styling does not consume is forwarded to the Toggle primitive, so the tables below are the whole surface — the behavior props and the styling ones together.

Toggle

A two-state button that can be on or off. Sets aria-pressed. Give it a look and children; it handles the state. Renders a Button; extra props are forwarded onto it.

PropTypeDefaultDescription
variant"default" | "outline""default"Transparent by default; outline adds a border. Also set as data-variant.
size"default" | "sm" | "lg""default"The toggle's height and padding scale. Also set as data-size.
pressedSignal<boolean> | booleanfalseThe pressed state. Pass a signal to control it from outside; a boolean seeds uncontrolled state.
disabledSignal<boolean> | booleanfalsePrevents toggling. Sets the native disabled attribute and data-disabled.
Data attributeValue
[data-toggle-root]Present
[data-state]"on" | "off"
[data-disabled]Present when disabled