ToggleButton
ToggleButton
component represents a button that can be toggled on or off.
#
Documentation
#
#
Usage
#
import type { ComponentPropsWithRef } from "react";
import { ToggleButton } from "@optiaxiom/react";
import { IconLayoutSidebar } from "@tabler/icons-react";
export function App({
size = "md",
}: Pick<ComponentPropsWithRef<typeof ToggleButton>, "size">) {
return (
<ToggleButton
aria-label="Toggle sidebar"
icon={<IconLayoutSidebar />}
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.
import { Button, Flex, ToggleButton } from "@optiaxiom/react";
import { IconLayoutSidebar } from "@tabler/icons-react";
import { useState } from "react";
export function App() {
const [value, setValue] = useState(false);
return (
<Flex flexDirection="row">
<ToggleButton
aria-label="Toggle sidebar"
icon={<IconLayoutSidebar />}
onPressedChange={setValue}
pressed={value}
/>
<Button disabled={!value} onClick={() => setValue(false)}>
Reset
</Button>
</Flex>
);
}
#
Appearance
#
Use the appearance
prop to select between the different button appearances.
import { Flex, ToggleButton } from "@optiaxiom/react";
import { IconLayoutSidebar } from "@tabler/icons-react";
export function App() {
return (
<Flex flexDirection="row">
<ToggleButton
appearance="subtle"
aria-label="Toggle sidebar"
icon={<IconLayoutSidebar />}
/>
<ToggleButton aria-label="Toggle sidebar" icon={<IconLayoutSidebar />} />
</Flex>
);
}
#
Accessibility
#
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.
import { ToggleButton, Tooltip } from "@optiaxiom/react";
import { IconLayoutSidebar } from "@tabler/icons-react";
export function App() {
return (
<Tooltip content="Toggle sidebar">
<ToggleButton aria-label="Toggle sidebar" icon={<IconLayoutSidebar />} />
</Tooltip>
);
}
#
Disabled state
#
Set the disabled
prop to true
to disable buttons and prevent any interactions.
For accessibility please make sure to include text that explains why the button is disabled or use a tooltip.
import { ToggleButton, Tooltip } from "@optiaxiom/react";
import { IconLayoutSidebar } from "@tabler/icons-react";
export function App() {
return (
<Tooltip content="Disabled toggle demo">
<ToggleButton
aria-label="Toggle sidebar"
disabled
icon={<IconLayoutSidebar />}
/>
</Tooltip>
);
}
#
Related
#
Button
Button component is used to trigger actions.
SegmentedControl
Toggle buttons for switching between different values or views.
ToggleButton
ToggleButton component represents a button that can be toggled on or off.
#
Props
#
#
ToggleButton
#
Supports all Button props in addition to its own. Renders a <button>
element.
Prop |
---|
addonAfter Display content inside the button after
|
addonBefore Display content inside the button before
|
appearance Control the appearance by selecting between the different button types.
|
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.
|
className
|
defaultPressed The state of the toggle when initially rendered. Use
|
disabled Whether the button is disabled.
|
icon Display an icon before or after the button content or omit
|
iconPosition Control whether to show the icon before or after the button content.
|
loading Whether to show loading spinner inside the button.
|
onPressedChange The callback that fires when the state of the toggle changes.
|
pressed The controlled state of the toggle.
|
size Control the size of the button.
|
square Whether button should have square shape.
|
#
Changelog
#
#
0.1.0
#
- Added component