---
title: "Switch"
description: "Switch with accessible presentation and local state."
---

# Switch

Switch with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Switch from "@rightui/ui"
```

## Usage

Import Switch from @rightui/ui. Store its value in state and update it through the provided action callback.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, Switch, Field from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  derive narrow = width < 600
  state notifications = true
  derive reduce = reduced
  action setNotifications(v: bool)
    notifications = v
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      Switch(id="notifications", label="Notifications", checked=notifications, disabled=false, reduced=reduce, change=setNotifications)
      text "Disabled" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Switch(id="variation-switch-disabled", label="Notifications", checked=notifications, disabled=true, reduced=reduce, change=setNotifications)
      text "Description" font-size="ui.text-sm" font-weight=uiWeights.semibold font-family=uiFontFamily(uiFont) data-rightui-font="body"
      Field(id="variation-switch-description", description="Receive updates when an export completes.", error="")
        Switch(id="variation-switch-described", label="Export updates", checked=notifications, disabled=false, reduced=reduced, change=setNotifications)

```

## API Reference

| Property | Type |
| --- | --- |
| Switch.id | string |
| Switch.label | string |
| Switch.checked | bool |
| Switch.disabled | bool |
| Switch.reduced | bool |
| Switch.change | action |

## Source

File: packages/ui/switch.contract

```contract
use uiFontFamily from "./typography-context.contract"
use Pressable from "./styles.contract"
component SwitchFrame
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    reduced: bool
    change: action
    showLabel: bool
  inject
    uiFont: string
  action toggle
    if not disabled
      change(not checked)
  view
    button id=id testId=id appearance="none" class=Pressable press=toggle disabled=disabled
      role="switch" aria-checked=checked aria-label=label
      display="flex" align-items="center" justify-content="space-between" gap="ui.space-16"
      min-height="ui.touch-target" width=(showLabel ? "auto" : "ui.touch-target") padding="ui.space-8 0px" flex-wrap="wrap" border="0" background-color="transparent" color="ui.foreground"
      :focus-visible
        box-shadow="ui.shadow-focus"
      :disabled
        opacity="ui.opacity-disabled"
      if showLabel
        text label text-align="left" font-size="ui.text-sm" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      view flex-shrink=0 width="ui.switch-track-width" height="ui.switch-track-height" border-radius="ui.radius-full" background-color=(checked ? "ui.primary" : "ui.border") position="relative" aria-hidden=true
        transition=(reduced ? "none" : "background-color ui.duration-fast ui.easing")
        view width="ui.switch-thumb-size" height="ui.switch-thumb-size" border-radius="ui.radius-full" position="absolute" left=(checked ? "ui.switch-thumb-on" : "ui.switch-inset") top="ui.switch-inset"
          background-color=(checked ? "ui.primary-foreground" : "ui.surface")
          -exact-layout-transition=(reduced ? "none" : "ui.duration-fast ui.easing")

component Switch
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    reduced: bool
    change: action
  view
    SwitchFrame(id=id, label=label, checked=checked, disabled=disabled, reduced=reduced, change=change, showLabel=true)

component SwitchControl
  props
    id: string
    label: string
    checked: bool
    disabled: bool
    reduced: bool
    change: action
  view
    SwitchFrame(id=id, label=label, checked=checked, disabled=disabled, reduced=reduced, change=change, showLabel=false)

```
