Skip to content

ToggleButton

The ToggleButton component is a versatile UI element that allows users to toggle between two states, such as “on” and “off.”

To use the ToggleButton component, you can customize its labels and behavior as shown below:

import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelLeft>Off</ToggleButton.LabelLeft>
<ToggleButton.LabelRight>On</ToggleButton.LabelRight>
</ToggleButton>
);
};
export default App;
Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the root element of the component.
classstring""Additional CSS classes to apply to the component.
refToggleButtonRef | undefinedundefinedA reference to the component, providing access to its methods and underlying HTML element.
disabledbooleanfalseDisables the toggle button, preventing user interaction.
class-checkedstring""Additional CSS classes applied when the toggle button is checked.
class-disabledstring""Additional CSS classes applied when the toggle button is disabled.
checkedbooleanfalseSpecifies the initial checked state of the toggle button.
onChange(checked: boolean) => voidundefinedCallback function triggered whenever the toggle button is toggled, providing the current checked state.
MethodParametersReturn ValueDescription
setCheckedSetter<boolean>voidProgrammatically sets the checked state of the toggle button based on the provided boolean value.

This slot enables customization of the left label of the toggle button. You can use it to include additional elements or modify the content of the left label.

Prop NameTypeDefaultDescription
childrenJSX.ElementundefinedThe content to display inside the left label, which can be any valid JSX element.
import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelLeft>Off</ToggleButton.LabelLeft>
</ToggleButton>
);
};
export default App;

This slot enables customization of the right label of the toggle button. You can use it to include additional elements or modify the content of the right label.

Prop NameTypeDefaultDescription
childrenJSX.ElementundefinedThe content to display inside the right label, which can be any valid JSX element.
import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelRight>On</ToggleButton.LabelRight>
</ToggleButton>
);
};
export default App;

This slot allows you to customize the appearance and behavior of the ToggleButton control when it is in its unchecked state. You can use it to apply custom styles, add CSS classes, or include additional elements within the control.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the control element.
classstring""Additional CSS classes to style the control element.
childrenJSX.ElementundefinedContent to render inside the control, which can be any valid JSX element.
import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelLeft>Off</ToggleButton.LabelLeft>
<ToggleButton.LabelRight>On</ToggleButton.LabelRight>
<ToggleButton.Control style={{ 'background-color': 'gray' }}>
</ToggleButton.Control>
</ToggleButton>
);
};
export default App;

This slot allows you to customize the indicator displayed when the toggle button is toggled. It can be styled or replaced with custom elements to suit your design needs.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the indicator element.
classstring""Additional CSS classes to style the indicator element.
childrenJSX.ElementundefinedContent to render inside the indicator, which can be any valid JSX element.
import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelLeft>Off</ToggleButton.LabelLeft>
<ToggleButton.LabelRight>On</ToggleButton.LabelRight>
<ToggleButton.Control style={{ 'background-color': 'gray' }}>
<ToggleButton.Indicator style={{ background: 'white' }}></ToggleButton.Indicator>
</ToggleButton.Control>
</ToggleButton>
);
};
export default App;

This slot allows you to customize the handle of the ToggleButton component. You can use it to apply custom styles, add CSS classes, or include additional elements.

Prop NameTypeDefaultDescription
styleJSX.CSSProperties{}Inline styles to apply directly to the handle element.
style-checkedJSX.CSSProperties{}Inline styles to apply when the toggle button is in the checked state.
classstring""Additional CSS classes to style the handle element.
class-checkedstring""Additional CSS classes to style the handle when the toggle button is checked.
childrenJSX.ElementundefinedContent to render inside the handle, which can be any valid JSX element.
import ToggleButton from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
return (
<ToggleButton>
<ToggleButton.LabelLeft>Off</ToggleButton.LabelLeft>
<ToggleButton.LabelRight>On</ToggleButton.LabelRight>
<ToggleButton.Control style={{ 'background-color': 'gray' }}>
<ToggleButton.Indicator style={{ background: 'white' }}></ToggleButton.Indicator>
<ToggleButton.Handle style={{ background: 'red' }} style-checked={{ background: 'black' }}></ToggleButton.Handle>
</ToggleButton.Control>
</ToggleButton>
);
};
export default App;

Set the value of the ToggleButton Programmatically

Section titled “Set the value of the ToggleButton Programmatically”

The ToggleButton component lets you set the checked state through code.

  1. Declare a ref variable with type ToggleButtonRef and pass it to the ToggleButton component.
  2. Call ref.setChecked() from anywhere (e.g., a keyboard event or external button) to set the checked state programmatically.
import ToggleButton, { ToggleButtonRef } from '@components/Basic/ToggleButton/ToggleButton';
const App = () => {
let toggleButtonRef!: ToggleButtonRef
const handleKeyPress = (e: KeyboardEvent) => {
if (e.keyCode !== 13) return
toggleButtonRef.setChecked(true);
}
return (
<>
<ToggleButton ref={toggleButtonRef} keypress={handleKeyPress}></ToggleButton>
</>
);
};
export default App;