Toggle Button

A Toggle Button is a button that stays pressed, for a setting that belongs on a toolbar rather than in a form — bold, italic, mute, pin. It reports its state through its appearance, so a row of them reads as the current formatting.

Demo

Rendered live, with the same build of the library you install.

Icon only:

With a label:

Usage

ToggleButton.tsx
import { useState } from 'react';
import { NToggleButton } from '@nayan-ui/react';
import { Bold, Italic, Underline } from 'lucide-react';
const ToggleButton = () => {
const [bold, setBold] = useState(true);
const [italic, setItalic] = useState(false);
return (
<div>
<h3 className="text-foreground mb-2 mt-4 text-sm font-semibold">Icon only:</h3>
<div className="mb-5 flex flex-wrap items-center gap-2">
<NToggleButton isIconOnly aria-label="Bold" isSelected={bold} onChange={setBold}>
<Bold className="h-4 w-4" />
</NToggleButton>
<NToggleButton isIconOnly aria-label="Italic" isSelected={italic} onChange={setItalic}>
<Italic className="h-4 w-4" />
</NToggleButton>
<NToggleButton isIconOnly aria-label="Underline" disabled>
<Underline className="h-4 w-4" />
</NToggleButton>
</div>
<h3 className="text-foreground mb-2 mt-4 text-sm font-semibold">With a label:</h3>
<div className="flex flex-wrap items-center gap-2">
<NToggleButton size="sm">Small</NToggleButton>
<NToggleButton>Medium</NToggleButton>
<NToggleButton variant="ghost">Ghost</NToggleButton>
</div>
</div>
);
};
export default ToggleButton;

Attributes

11 props
NameTypeDefaultDetails
childrenReactNodeRequiredThe children prop.
isSelectedbooleanOptionalThe isSelected prop.
defaultSelectedbooleanfalseThe defaultSelected prop.
disabledbooleanfalseThe disabled prop.
isIconOnlybooleanfalseThe isIconOnly prop.
variant'default' | 'ghost''default'The variant prop.
size'sm' | 'md' | 'lg''md'The size prop.
onChange(isSelected: boolean) => voidOptionalThe onChange prop.
classNamestring''The className prop.
aria-labelstringOptionalThe aria-label prop.
aria-labelledbystringOptionalThe aria-labelledby prop.

Tags

Other components solving nearby problems.