# Switch Primitive

Switch is a Starwind Runtime primitive in the single-boolean-control contract family.
## Anatomy
### Astro
Use the Astro primitive adapter to render Switch anatomy with the Runtime wiring included.
```astro
---
import { Switch } from "@starwind-ui/astro/switch";
---

<Switch.Root>
  <Switch.Thumb />
</Switch.Root>
```

### React
Use the React primitive adapter when Switch state participates in React rendering.
```tsx
import { Switch } from "@starwind-ui/react/switch";

export function Example() {
  return (
    <Switch.Root>
      <Switch.Thumb />
    </Switch.Root>
  );
}
```

### HTML
Render the Switch data-sw-* contract yourself, then initialize createSwitch.
```html
<span data-sw-switch role="switch">
  <span data-sw-switch-thumb></span>
  <input data-sw-switch-input type="checkbox" aria-hidden="true" tabindex="-1" />
</span>

<script type="module">
  import { createSwitch } from "@starwind-ui/runtime/switch";

  const root = document.querySelector("[data-sw-switch]");
  if (root) {
    createSwitch(root);
  }
</script>
```
## API Reference
### Root
The main element that owns the Switch Runtime instance.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-switch` |
| Role | `switch` |
#### Props
| Prop | Type | Default | Kind | Description |
| --- | --- | --- | --- | --- |
| checked | `boolean` | - | control | Controls whether Switch is checked. |
| defaultChecked | `boolean` | false | control | Sets whether Switch starts checked for uncontrolled usage. |
| disabled | `boolean` | false | option | Disables the Root part. |
| form | `string` | - | option | Associates the control with a form element. |
| id | `string` | - | option | Sets the id used by the associated native control. |
| name | `string` | - | option | Sets the submitted form field name. |
| nativeButton | `boolean` | false | rendering | Renders the control as a native button element. |
| readOnly | `boolean` | false | option | Marks the control as read-only. |
| required | `boolean` | false | option | Marks the form control as required. |
| uncheckedValue | `string` | - | option | Sets the value submitted when the checkbox is unchecked. |
| value | `string` | - | option | Controls the current Switch value. |
| onCheckedChange | `(checked: boolean, details: SwitchCheckedChangeDetails) => void` | - | callback | Runs when the Switch checked state changes. |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-switch` | runtime | - | Marks the Root part so Starwind Runtime can find it. |
| `data-checked` | state | - | Reflects the checked state on the Root part. |
| `data-default-checked` | prop | - | Reflects the default checked prop on the Root part. |
| `data-disabled` | prop | - | Reflects the disabled prop on the Root part. |
| `data-filled` | state | - | Reflects the filled state on the Root part. |
| `data-form` | prop | - | Reflects the form prop on the Root part. |
| `data-id` | prop | - | Reflects the id prop on the Root part. |
| `data-name` | prop | - | Reflects the name prop on the Root part. |
| `data-readonly` | prop | - | Reflects the readonly prop on the Root part. |
| `data-required` | prop | - | Reflects the required prop on the Root part. |
| `data-unchecked` | state | - | Reflects the unchecked state on the Root part. |
| `data-unchecked-value` | prop | - | Reflects the unchecked value prop on the Root part. |
| `data-value` | prop | - | Reflects the value prop on the Root part. |
#### State
| State | Value Type | Controlled Prop | Default Prop | Initial Attribute | Runtime Getter | Runtime Setter | Description | State Control Support |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| checked | `boolean` | checked | defaultChecked | `data-default-checked` | `getChecked` | `setChecked` | Tracks whether Switch is checked. | React supports controlled and default state with checked and defaultChecked props. Runtime/HTML reads initial state from data-default-checked, emits starwind:checked-change, and updates with setChecked. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state. |
#### Events
| Event | Callback | DOM Event | Value | Details | Timing | Cancelable | Description |
| --- | --- | --- | --- | --- | --- | --- | --- |
| checkedChange | onCheckedChange | starwind:checked-change | checked: `boolean` | SwitchCheckedChangeDetails | - | - | Fires when the checked state changes for Switch. |
#### Runtime Setters
| Method | Target | Options | Suppresses Emit | Description |
| --- | --- | --- | --- | --- |
| `setChecked` | state: checked | emit: false | Yes | Updates whether Switch is checked from Runtime code. |
| `setDisabled` | prop: disabled | - | No | Updates whether Switch is disabled from Runtime code. |
| `setFormOptions` | props: form, name, required, uncheckedValue, value | - | No | Updates Switch form-related options from Runtime code. |
#### Refs
| Part | Public |
| --- | --- |
| root | Yes |
#### Initial Markup
| Attributes | Reason |
| --- | --- |
| `data-sw-switch`, `role`, `aria-checked`, `data-checked`, `data-unchecked`, `data-default-checked`, `data-disabled` | The visible switch needs ARIA and state styling before the controller attaches. |
#### Form
| Fact | Value |
| --- | --- |
| Form props | form, id, name, required, uncheckedValue, value |
| Hidden input | input (checkbox) |

### Thumb
The draggable thumb for Switch.
| Fact | Value |
| --- | --- |
| Default element | `span` |
| Discovery hook | `data-sw-switch-thumb` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-switch-thumb` | runtime | - | Marks the Thumb part so Starwind Runtime can find it. |
#### Refs
| Part | Public |
| --- | --- |
| thumb | Yes |

### Input
The native input synchronized by Switch.
| Fact | Value |
| --- | --- |
| Default element | `input` |
| Discovery hook | `data-sw-switch-input` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-switch-input` | runtime | - | Marks the Input part so Starwind Runtime can find it. |
#### Refs
| Part | Public |
| --- | --- |
| input | Yes |
#### Initial Markup
| Attributes | Reason |
| --- | --- |
| `data-sw-switch-input` | The runtime needs the sibling input placeholder so it can attach form state after hydration. |
#### Form
| Fact | Value |
| --- | --- |
| Form props | form, id, name, required, uncheckedValue, value |
| Hidden input | input (checkbox) |

### Unchecked Input
The hidden native input used when Switch submits an unchecked value.
| Fact | Value |
| --- | --- |
| Default element | `input` |
| Discovery hook | `data-sw-switch-unchecked-input` |
| Role | - |
#### Data Attributes
| Attribute | Source | Value | Description |
| --- | --- | --- | --- |
| `data-sw-switch-unchecked-input` | runtime | - | Marks the Unchecked Input part so Starwind Runtime can find it. |
## Runtime API
| Fact | Value |
| --- | --- |
| Factory | [`createSwitch`](/docs/runtime/#create-switch) |
| Import | `@starwind-ui/runtime/switch` |
| Root part | root |
| Option props | checked, defaultChecked, disabled, form, id, name, readOnly, required, uncheckedValue, value |
| Option lifecycles | - |
## Form Participation
| Fact | Value |
| --- | --- |
| Form props | form, id, name, required, uncheckedValue, value |
| Hidden input | input (checkbox) |
| Field integration | Yes |
## Related Styled Components
| Component | Relationship |
| --- | --- |
| [Switch](/docs/components/switch/) | Direct Primitive |
## Exports
| Group | Import | Exports |
| --- | --- | --- |
| Runtime | `@starwind-ui/runtime/switch` | `createSwitch` |
| Astro Primitive | `@starwind-ui/astro/switch` | `Switch`, `SwitchRoot`, `SwitchThumb` |
| React Primitive | `@starwind-ui/react/switch` | `Switch`, `SwitchRoot`, `SwitchThumb` |
## Canonical Names
| Kind | Name |
| --- | --- |
| namespace | `Switch` |
| runtime-factory | `createSwitch` |
| part | `Switch.Root` |
| part | `Switch.Thumb` |