Toggle

A button that stays pressed. Uses aria-pressed rather than a checkbox, because this is a control that changes state, not a value submitted with a form.

src/components/ui/toggle.tsx

About

From the component's own source, where it was written.

A button that stays pressed.

aria-pressed rather than a checkbox: this is a control that changes state, not a value submitted with a form. ToggleGroup adds single or multiple selection on top without changing the item.

Install

npx astralyx-ui add toggle

Usage

import { Toggle, ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle'

<Toggle aria-label="Bold"><Bold /></Toggle>

<ToggleGroup type="multiple" defaultValue={['bold']}>
  <ToggleGroupItem value="bold" aria-label="Bold"><Bold /></ToggleGroupItem>
</ToggleGroup>

Composer

Change the props and the source below follows.

Props
tsx
<ToggleGroup type="multiple" variant="outline" size="icon" defaultValue={['bold']}>
  <ToggleGroupItem value="bold" aria-label="Bold"><Bold /></ToggleGroupItem>
  <ToggleGroupItem value="italic" aria-label="Italic"><Italic /></ToggleGroupItem>
</ToggleGroup>

Examples

Standalone

Group

API

Generated from the source, so it cannot fall behind the code. One table per exported component, then hooks, functions and types.

Toggle

Extends Omit<ComponentProps<'button'>, 'value'>.

PropTypeDefaultDescription
variant'default' | 'outline'——
size'sm' | 'default' | 'lg' | 'icon-sm' | 'icon' | 'icon-lg'——
pressedboolean—Controlled and uncontrolled state, on Toggle.
defaultPressedbooleanfalseControlled and uncontrolled state, on Toggle.
onPressedChange(pressed: boolean) => void—Fires with the new state.

ToggleGroup

Extends Omit<ComponentProps<'div'>, 'defaultValue' | 'onChange'>.

PropTypeDefaultDescription
childrenReactNoderequired—
type'single' | 'multiple''single'Whether one item or many can be pressed at once.
valuestring[]—An array in both modes, so switching type needs no change.
defaultValuestring[][]—
onValueChange(value: string[]) => void——
variantToggleProps['variant']——
sizeToggleProps['size']——

ToggleGroupItem

Extends Omit<ToggleProps, 'pressed' | 'onPressedChange'>.

PropTypeDefaultDescription
variant'default' | 'outline'——
size'sm' | 'default' | 'lg' | 'icon-sm' | 'icon' | 'icon-lg'——
defaultPressedboolean—Controlled and uncontrolled state, on Toggle.
valuestringrequired—

Functions

FunctionSignatureDescription
toggleVariants(props?: { variant?: 'default' | 'outline'; size?: 'sm' | 'default' | 'lg' | 'icon-sm' | 'icon' | 'icon-lg' }) => stringA button that stays pressed.