Toggle Group
A set of two-state buttons that can be toggled on or off.
Usage
ToggleGroup
component takes intype
andclassName
props.type
can be"single"
or"multiple"
. Default is"single"
.Children
ofToggleGroup
component are rendered as togglable buttons. Every child ofToggleGroup
component should havevalue
prop.value
prop of every child ofToggleGroup
component is the value of the button. If not passed, then inner text of the child is considered as value.
Example of using value generated by ToggleGroup of "single" type
Note how
data-value
prop is passed to every child of ToggleGroup
componentimport { ToggleGroup } from "dubsui"
import { useEffect, useState } from "react";
const Single = () => {
const [value, setValue] = useState();
useEffect(() => {
console.log(value);
}, [value]);
return (
<ToggleGroup value={value} onValueChange={setValue} type="single">
<div data-value="a">A</div>
<div data-value="b">B</div>
<div data-value="c">C</div>
</ToggleGroup>
);
};
Example of using value generated by ToggleGroup of "multiple" type
Note how
value
state us an array. In ToggleGroup of "multiple" type you have to use array to catch the valuesimport { ToggleGroup } from "dubsui"
import { useEffect, useState } from "react";
const Single = () => {
const [value, setValue] = useState([]);
useEffect(() => {
console.log(value);
}, [value]);
return (
<ToggleGroup value={value} onValueChange={setValue} type="multiple">
<div data-value="a">A</div>
<div data-value="b">B</div>
<div data-value="c">C</div>
</ToggleGroup>
);
};
Customizable Design
More granular components provide you with more control over the design of the ToggleGroup
component. You can customize the design of the ToggleGroup
component by using the following sub-components:
Note how
value
prop is passed to every child of ToggleGroupRoot
component which is different compared to ToggleGroup
which used data-value
prop- Style of every sub-component of the ToggleGroup component can be customized using the
className
prop. - Pass in a
CSS class
or useTailwind CSS
utility classes to style the ToggleGroup component.