Skip to Content
ToggleButton

ToggleButton

ToggleButton component represents a button that can be toggled on or off.

#

Documentation

#

Usage

"use client"; import type { ComponentPropsWithRef } from "react"; import { IconSidebar } from "@optiaxiom/icons"; import { ToggleButton } from "@optiaxiom/react"; export function App({ size = "md", }: Pick<ComponentPropsWithRef<typeof ToggleButton>, "size">) { return ( <ToggleButton aria-label="Toggle sidebar" icon={<IconSidebar />} size={size} /> ); }

#

Controlled

Use the pressed and defaultPressed props to toggle between controlled and uncontrolled usage. And combine it with onPressedChange to listen for changes to the state.

"use client"; import { IconSidebar } from "@optiaxiom/icons"; import { Button, Group, ToggleButton } from "@optiaxiom/react"; import { useState } from "react"; export function App() { const [value, setValue] = useState(false); return ( <Group gap="16"> <ToggleButton aria-label="Toggle sidebar" icon={<IconSidebar />} onPressedChange={setValue} pressed={value} /> <Button disabled={!value} onClick={() => setValue(false)}> Reset </Button> </Group> ); }

#

Appearance

Use the appearance prop to select between the different button appearances.

import { IconSidebar } from "@optiaxiom/icons"; import { Group, ToggleButton } from "@optiaxiom/react"; export function App() { return ( <Group gap="16"> <ToggleButton appearance="subtle" aria-label="Toggle sidebar" icon={<IconSidebar />} /> <ToggleButton aria-label="Toggle sidebar" icon={<IconSidebar />} /> </Group> ); }

#

Icon only

When using icon only buttons please make sure to always include an aria-label prop for screen readers and optionally use tooltips to visually denote the button’s intent.

"use client"; import { IconSidebar } from "@optiaxiom/icons"; import { ToggleButton, Tooltip } from "@optiaxiom/react"; export function App() { return ( <Tooltip content="Toggle sidebar"> <ToggleButton aria-label="Toggle sidebar" icon={<IconSidebar />} /> </Tooltip> ); }

#

Disabled state

Set the disabled prop to true to disable buttons and prevent any interactions.

Include visible text that explains why it’s disabled: a disabled button leaves the tab order, so keyboard and screen reader users can’t reach a tooltip on it.

The sidebar is unavailable in full screen mode.

import { IconSidebar } from "@optiaxiom/icons"; import { Group, Text, ToggleButton } from "@optiaxiom/react"; export function App() { return ( <Group gap="8"> <ToggleButton aria-label="Toggle sidebar" disabled icon={<IconSidebar />} /> <Text color="fg.secondary" fontSize="sm"> The sidebar is unavailable in full screen mode. </Text> </Group> ); }

Button

Button component is used to trigger actions.

SegmentedControl

Toggle buttons for switching between different values or views.

Switch

Control to allow toggling between checked and not checked state.

#

Props

#

ToggleButton

import { ToggleButton } from "@optiaxiom/react"; // or import { ToggleButton } from "@optiaxiom/react/toggle-button";

Supports all Button props in addition to its own. Renders a <button> element.

Prop

addonAfter

Display content inside the button after children.

ReactNode

addonBefore

Display content inside the button before children.

ReactNode

appearance

Control the appearance by selecting between the different button types.

"default" | "subtle"

asChild

Change the default rendered element for the one passed as a child, merging their props and behavior.

Read the Composition guide for more details.

false | true

children

ReactNode

className

string

defaultPressed

The state of the toggle when initially rendered. Use defaultPressed if you do not need to control the state of the toggle. @defaultValue false

false | true

disabled

Whether the button is disabled.

false | true

icon

Display an icon before or after the button content or omit children to only show the icon.

ReactNode

iconPosition

Control whether to show the icon before or after the button content.

"end" | "start"

loading

Show a loading spinner and mark the button aria-busy. The button stays focusable and clickable: don’t also set disabled, which drops keyboard focus mid-action, and ignore repeat clicks in onClick if the action shouldn’t run twice. Nothing is announced when loading starts or ends, so tell screen reader users the outcome yourself, e.g. with a toast.

false | true

onPressedChange

The callback that fires when the state of the toggle changes.

(pressed: boolean) => void

pressed

The controlled state of the toggle.

false | true

size

Control the size of the button.

"sm" | "md" | "lg"

square

Whether button should have square shape (an icon-only button with no label).

false | true

type

The default behavior of the button.

"button" | "reset" | "submit"

#

Accessibility

#

Labels

  • Give an icon only toggle button an aria-label. ToggleButton exposes its state with aria-pressed, so keep the label the same in both states.
  • A disabled toggle button leaves the tab order. Explain why it’s disabled in visible text nearby.

#

Changelog

#

0.1.0

  • Added component
Last updated on