Skip to main content

Switch

Use cases:

  • Toggling visibility of a UI section
  • Disabling & enabling features

Example

Declaration

<Switch></Switch>

Usage

import { Switch } from '@genesislcap/rapid-design-system/react';

export function MyComponent() {

const [switchValue, setSwitchValue] = useState(false);

const handleSwitchChange = (event) => {
const target = event.target as HTMLInputElement;
console.log(target.value);
setSwitchValue(target.value);
};

return (
<Switch onChange={handleSwitchChange}>
<span slot="checked-message">Dark</span>
<span slot="unchecked-message">Light</span>
</Switch>
);
}

API

Property and attribute binding examples for Genesis Component syntax (closing tag omitted).

NameTypeDescriptionExample
checkedbooleanSets or retrieves the current state of the component. Default: false
<rapid-switch ?checked=${sync(x => x.isSelected, 'boolean')}>
readonlybooleanConfigures the component so the user cannot alter the state via the web browser.
<rapid-switch readonly>
disabledbooleanSame functionality as readonly, and additionally greys out the component.
<rapid-switch disabled>
formstringAssociates this component to a form. Form id needs to be passed. If no Id informed, then it will be associated with the ancestor form.
<rapid-switch form="myForm">

Slots

SlotUse
switchThe control element.

Parts

PartDescription
labelThe label text.
checked-indicatorThe checked indicator.
checked-messageThe text label when the switch is selected.
switchThe input control.
unchecked-messageThe text label when the switch is not selected.

Events fired

NameDescription
changeFires the event when the radio component changes its state

Events fired

EventTypeUseExample
changeeventEmits a custom change event when the component state changes. Access the value on the event via .target.checked.
<rapid-switch @change="${(x) => x.myChangeHandler(event)}">

Events listened to

This component doesn't listen to any events.

Further details

This component is an implementation of an HTML switch element.