Import
import { ToggleButton } from '@contentful/f36-components';
import { ToggleButton } from '@contentful/f36-button';
Examples
Basic
Example of simple usage:
function ToggleButtonBasicExample() {
const [isActive, setIsActive] = useState(false);
return (
<ToggleButton
isActive={isActive}
onToggle={() => {
setIsActive(!isActive);
}}
icon={<ThumbUpTrimmedIcon />}
>
Like
</ToggleButton>
);
}
States
Example of active and disabled toggle buttons:
function ToggleButtonStatesExample() {
return (
<Stack>
<ToggleButton isActive onToggle={() => {}}>
Active State
</ToggleButton>
<ToggleButton isDisabled onToggle={() => {}}>
Disabled State
</ToggleButton>
</Stack>
);
}
Grouped
You can group several ToggleButtons
using ButtonGroup
component.
function ToggleButtonGroupedExample() {
const [isItalic, setIsItalic] = useState(false);
const [isBold, setIsBold] = useState(true);
const [isUnderline, setIsUnderline] = useState(false);
return (
<ButtonGroup>
<ToggleButton
isActive={isItalic}
icon={<FormatItalicIcon />}
aria-label="Italic"
size="small"
onToggle={() => {
setIsItalic(!isItalic);
}}
/>
<ToggleButton
isActive={isBold}
icon={<FormatBoldIcon />}
aria-label="Bold"
size="small"
onToggle={() => {
setIsBold(!isBold);
}}
/>
<ToggleButton
isActive={isUnderline}
icon={<FormatUnderlinedIcon />}
aria-label="Underline"
size="small"
onToggle={() => {
setIsUnderline(!isUnderline);
}}
/>
</ButtonGroup>
);
}
Name | Type | Default |
---|
onToggle required | () => void Function triggered when the toggle button is clicked. | |
aria-label | string Aria label is required when using icon only | |
children | ReactNode | |
className | string CSS class to be appended to the root element | |
icon | ReactElement<any, string | JSXElementConstructor<any>> Expects any of the icon components | |
isActive | false true Applies active styles | false |
isDisabled | false true Disabled interaction and applies disabled styles | false |
size | "small" "medium" "large" Determines size variation of Button component | medium |
testId | string A [data-test-id] attribute used for testing purposes | |