> For the complete documentation index, see [llms.txt](https://docs.rierino.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rierino.com/sdk-core-ui/components/switch.md). Agents can also search and read this documentation through its [MCP server](https://docs.rierino.com/~gitbook/mcp).

# Switch

Turns a setting on or off, with an immediate effect.

{% code title="switch/demo.tsx" %}

```tsx
import { useState } from "react";
import { Switch } from "@rierino-open/core-ui";

export default function SwitchDemo() {
  const [active, setActive] = useState(true);

  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10, fontSize: 13 }}>
      <Switch aria-labelledby="active-label" checked={active} onChange={setActive} />
      <span id="active-label">Product is active</span>
    </div>
  );
}
```

{% endcode %}

## Usage

{% tabs %}
{% tab title="React app" %}

```tsx
import { Switch } from '@rierino-open/core-ui';

<Switch aria-label="Product is active" checked={active} onChange={setActive} />
```

{% endtab %}

{% tab title="Hosted editor" %}

```jsx
import CoreUI from '@rierino-open/core-ui';
const { Switch } = CoreUI;

<Switch aria-label="Product is active" checked={active} onChange={setActive} />
```

{% endtab %}
{% endtabs %}

Switch is controlled with `checked`, not React Aria's `isSelected`, which Switch ignores. `onChange` receives the new state as a boolean.

Use a switch for settings that apply as soon as they change. For a choice that is submitted with a form, a [Checkbox](https://docs.rierino.com/sdk-core-ui/components/checkbox.md) is usually clearer.

{% hint style="info" %}
**Switch draws only the switch**

Switch doesn't render a label: `children` are ignored. Put the label text next to it, and name the switch with `aria-label`, or with `aria-labelledby` pointing at that text.
{% endhint %}

## Examples

### Uncontrolled

Without `checked`, the switch keeps its own state. `defaultSelected` sets where it starts, and `name` submits its value with a form.

{% code title="switch/uncontrolled.tsx" %}

```tsx
import { Switch } from "@rierino-open/core-ui";

export default function SwitchUncontrolled() {
  return (
    <div style={{ display: "flex", alignItems: "center", gap: 10, fontSize: 13 }}>
      <Switch aria-label="Email notifications" defaultSelected name="notifications" />
      Email notifications
    </div>
  );
}
```

{% endcode %}

### Disabled

{% code title="switch/disabled.tsx" %}

```tsx
import { Switch } from "@rierino-open/core-ui";

const row = { display: "flex", alignItems: "center", gap: 10, fontSize: 13 } as const;

export default function SwitchDisabled() {
  return (
    <div style={{ display: "grid", gap: 12 }}>
      <div style={row}>
        <Switch aria-label="Disabled, off" isDisabled />
        Disabled, off
      </div>
      <div style={row}>
        <Switch aria-label="Disabled, on" isDisabled checked />
        Disabled, on
      </div>
    </div>
  );
}
```

{% endcode %}

## Accessibility

* Renders a native checkbox with the `switch` role, so screen readers announce it as a switch that is on or off. `Space` toggles it.
* **Every switch needs a name.** Pass `aria-label`, or `aria-labelledby` pointing at the text next to it.
* The track's colour isn't the only signal: the handle also moves to the right when the switch is on.
* **There is no visible keyboard focus yet.** Switch has no focus styles, so people using the keyboard can't see which switch is focused.

## Styling

| Attribute            | Present when                  |
| -------------------- | ----------------------------- |
| `data-selected`      | The switch is on              |
| `data-focus-visible` | The switch has keyboard focus |
| `data-pressed`       | The switch is being pressed   |
| `data-disabled`      | `isDisabled` is set           |

The track is `--primary` when on, and athens-gray when off. See [theme variables](https://docs.rierino.com/sdk-core-ui/foundations/colors.md#theme-variables).

## API reference

| Prop              | Type                            | Default | Description                                                                                                                                                                        |
| ----------------- | ------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `aria-label`      | `string`                        |         | Defines a string value that labels the current element.                                                                                                                            |
| `aria-labelledby` | `string`                        |         | Identifies the element (or elements) that labels the current element.                                                                                                              |
| `name`            | `string`                        |         | The name of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefname).                      |
| `isDisabled`      | `boolean`                       |         | Whether the input is disabled.                                                                                                                                                     |
| `isReadOnly`      | `boolean`                       |         | Whether the input can be selected but not changed by the user.                                                                                                                     |
| `defaultSelected` | `boolean`                       |         | Whether the Switch should be selected (uncontrolled).                                                                                                                              |
| `value`           | `string`                        |         | The value of the input element, used when submitting an HTML form. See [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#htmlattrdefvalue).                    |
| `className`       | `string \| ((state) => string)` |         | The CSS [className](https://developer.mozilla.org/en-US/docs/Web/API/Element/className) for the element. A function may be provided to compute the class based on component state. |
| `checked`         | `boolean`                       |         | Whether the switch is on, for a controlled switch. Use this instead of `isSelected`.                                                                                               |
| `onChange`        | `((val: boolean) => void)`      |         | Called with the new on/off state when the switch is toggled.                                                                                                                       |

Switch also accepts the other props of React Aria's [Switch](https://react-spectrum.adobe.com/react-aria/Switch.html#props), except `isSelected`.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.rierino.com/sdk-core-ui/components/switch.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
