@versini/ui-togglegroup
v9.0.0
Published
[](https://www.npmjs.com/package/@versini/ui-togglegroup) ,
Home/End/PageUp/PageDownmove focus and skip disabled items;Enter/Spacetoggle - 🧪 Type Safe: Strongly typed props and context sharing
Installation
npm install @versini/ui-togglegroupNote: This component requires TailwindCSS and the
@versini/ui-stylesplugin for proper styling. See the installation documentation for complete setup instructions.
Usage
Uncontrolled
Use defaultValue when you don't need to manage the selection state yourself:
import { ToggleGroup, ToggleGroupItem } from "@versini/ui-togglegroup";
function App() {
return (
<ToggleGroup defaultValue="center">
<ToggleGroupItem value="left" />
<ToggleGroupItem value="center" />
<ToggleGroupItem value="right" />
</ToggleGroup>
);
}Controlled
Use value and onValueChange when you need full control over the selection:
import { useState } from "react";
import { ToggleGroup, ToggleGroupItem } from "@versini/ui-togglegroup";
function App() {
const [alignment, setAlignment] = useState("left");
return (
<ToggleGroup value={alignment} onValueChange={setAlignment}>
<ToggleGroupItem value="left" />
<ToggleGroupItem value="center" />
<ToggleGroupItem value="right" />
</ToggleGroup>
);
}Examples
Large Size Group
<ToggleGroup size="large" value={alignment} onValueChange={setAlignment}>
<ToggleGroupItem value="left" />
<ToggleGroupItem value="center" />
<ToggleGroupItem value="right" />
</ToggleGroup>Theme Picker
<ToggleGroup value={theme} onValueChange={setTheme}>
<ToggleGroupItem value="auto" />
<ToggleGroupItem value="light" />
<ToggleGroupItem value="dark" />
</ToggleGroup>API
ToggleGroup Props
| Prop | Type | Default | Description |
| --------------- | ---------------------------------- | ---------- | ----------------------------------------------------------------- |
| value | string | - | Current selected value (controlled). |
| defaultValue | string | - | Initial value (uncontrolled). |
| onValueChange | (value: string) => void | - | Fired when selection changes. Reports "" when nothing is pressed. |
| disabled | boolean | false | Disable the entire group. |
| size | "small" \| "medium" \| "large" | "medium" | Visual size of items. |
| flat | boolean | false | Render a flat solid background instead of the gradient overlay. |
| className | string | - | Extra class(es) applied to container. |
| children | React.ReactNode | - | One or more ToggleGroupItem components. |
Selecting the already-selected item clears the selection and reports an empty string, rather than being a no-op as in a plain radio group.
ToggleGroupItem Props
| Prop | Type | Default | Description |
| ---------- | --------- | ------- | -------------------------------------- |
| value | string | - | Value represented by this toggle item. |
| disabled | boolean | false | Disable just this item. |
Item content is the
valuestring by default; passchildrento customize it.
ToggleGroupItemaccepts all native<button>props exceptvalueandtype. Extra props are forwarded to the underlying button, andclassNameis merged with the component's own classes.
