# Switch

Source: https://ecosphere.dev/docs/forms/switch (Ecosphere 1.5). Install `vue-ecosphere` for Vue 3 or `react-ecosphere` for React 18.2+/19: same components, props, markup and stylesheet.

An on/off setting that takes effect at once: a native checkbox with role=switch, so Space toggles it and it posts with a form.

`import { EcoSwitch } from "vue-ecosphere"`

## States

Use a switch when flipping it applies the change; use a checkbox when the choice waits for Save. The label names what is on; no On/Off word beside it.

Vue (vue-ecosphere):

```vue
<EcoSwitch v-model="assigned" label="Email me when a request is assigned" />
<EcoSwitch v-model="digest" label="Send me a weekly digest" />
<EcoSwitch label="Share usage data" disabled />
<EcoSwitch :model-value="true" label="Single sign-on" disabled />
```

React (react-ecosphere):

```tsx
const [assigned, setAssigned] = useState(true);
const [digest, setDigest] = useState(false);

<EcoSwitch checked={assigned} onChange={setAssigned} label="Email me when a request is assigned" />
<EcoSwitch checked={digest} onChange={setDigest} label="Send me a weekly digest" />
<EcoSwitch label="Share usage data" disabled />
<EcoSwitch checked label="Single sign-on" disabled />
```

## With a hint

Vue (vue-ecosphere):

```vue
<EcoSwitch v-model="assigned" label="Email me when a request is assigned"
  hint="Sent once per request, never as a digest." />
<EcoSwitch label="Require two-step sign-in" disabled
  hint="Only workspace admins can change this." />
```

React (react-ecosphere):

```tsx
<EcoSwitch checked={assigned} onChange={setAssigned} label="Email me when a request is assigned"
  hint="Sent once per request, never as a digest." />
<EcoSwitch label="Require two-step sign-in" disabled
  hint="Only workspace admins can change this." />
```

## Sizes

The small switch fits dense rows and table cells.

Vue (vue-ecosphere):

```vue
<EcoSwitch v-model="compact" label="Compact rows" />
<EcoSwitch v-model="compact" label="Compact rows" size="sm" />
```

React (react-ecosphere):

```tsx
const [compact, setCompact] = useState(true);

<EcoSwitch checked={compact} onChange={setCompact} label="Compact rows" />
<EcoSwitch checked={compact} onChange={setCompact} label="Compact rows" size="sm" />
```

## Saving

Set loading while the change saves: the switch shows the new state, marks itself busy and ignores input until the save settles.

Vue (vue-ecosphere):

```vue
<EcoSwitch :model-value="renew" :loading="saving" label="Renew the plan automatically"
  @update:model-value="save" />
```

React (react-ecosphere):

```tsx
const [renew, setRenew] = useState(false);
const [saving, setSaving] = useState(false);

function save(v: boolean) {
  setRenew(v);
  setSaving(true);
  persist(v).finally(() => setSaving(false));
}

<EcoSwitch checked={renew} loading={saving} label="Renew the plan automatically"
  onChange={save} />
```

## EcoSwitch

Vue (vue-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `v-model` | `boolean` | false | On state. |
| `label` | `string` | — | The thing that is true while it is on, beside the track. Default slot overrides. |
| `hint` | `string` | — | Second line (switch__h), wired with aria-describedby. #hint slot. |
| `disabled` | `boolean` | false | Say why in the hint. |
| `loading` | `boolean` | false | A save in flight: aria-busy, an indicator in the thumb, and input is ignored. |
| `size` | `"md" \| "sm"` | "md" | sm for dense rows and table cells. |
| `name` | `string` | — | Native name; the form posts on (or the value attribute) while it is on. |
| `id` | `string` | — | Input id; generated when omitted. |
| `…attrs` | `—` | — | aria-label (for a switch with no visible label), value and listeners go to the <input>; class/style to the label. |

React (react-ecosphere):

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `checked / onChange` | `boolean` | false | On state (defaultChecked when uncontrolled); onChange receives the boolean. |
| `label` | `string` | — | The thing that is true while it is on, beside the track. children override. |
| `hint` | `ReactNode` | — | Second line (switch__h), wired with aria-describedby. |
| `disabled` | `boolean` | false | Say why in the hint. |
| `loading` | `boolean` | false | A save in flight: aria-busy, an indicator in the thumb, and input is ignored. |
| `size` | `"md" \| "sm"` | "md" | sm for dense rows and table cells. |
| `name` | `string` | — | Native name; the form posts on (or the value attribute) while it is on. |
| `id` | `string` | — | Input id; generated when omitted. |
| `…rest` | `—` | — | aria-label (for a switch with no visible label), value, other input props and the ref go to the <input>; className/style to the label. |
