Skip to main content

Switch

Switch component offers a "on" and "off" state similar to checkbox which adheres to the WAI-ARIA Switch requirement.

Core Features​

  • Out of the box Accessiblity
  • Internal control of labelling (You don't need to pass htmlFor on the label or id on the switch)
  • Can be controlled or uncontrolled
  • keyboard navigation

Installation​

npm i @dorai-ui/switch

or

yarn add @dorai-ui/switch

Basic Example​

The example below demonstrates using the exposed checked props by switch.

The aria-checked attribute is also present and can be used for styling.

API​

The switch component can be controlled or uncontrolled.

Aria attributes​

AttributesDefaultDescription
aria-checkedfalseHolds the checked state of the component

Components Composed​

import { Switch } from '@dorai-ui/switch'

const SwitchComp = () => (
<Switch.Group>
<Switch>
<Switch.Indicator />
</Switch>
<Switch.Label>Label</Switch.Label>
</Switch.Group>
)

Group​

Group is the parent component for all other components, it exposes the context and is required.

PropsDefaultDescription
as'div'This grants the ability to change the element this component should render as

Switch​

The main Switch component. It exposes the checked state as a render props

PropsDefaultTypeDescription
checked-booleanFor controlled state, the checked property is required and controls the state of the component
onChange-functionRequired for component state control ability
disabled-booleanPrevents switch component from been clickable
value-stringThe value of the component which can be read with the name attribute
as'button'-This grants the ability to change the element this component should render as
Render Props​
PropsTypeDescription
checkedbooleanExposes the checked state of the component which is useful for uncontrolled state

Indicator​

Stylable Indicator (or numb) which can be styled to show the toggled state

PropsDefaultDescription
as'span'This grants the ability to change the element this component should render as

Label​

Optional Label component. Out of the box, it is clickable and tied to the Switch component.

PropsDefaultDescription
as'label'This grants the ability to change the element this component should render as