# Switch Component

The Switch component toggles a single setting on or off.

## Usage {#usage}

Import the component:

```js
import 'mdui/components/switch.js';
```

Import the TypeScript type:

```ts
import type { Switch } from 'mdui/components/switch.js';
```

Example:

```html
<mdui-switch></mdui-switch>
```

## Examples {#examples}

### Checked State {#example-checked}

The `checked` attribute indicates whether the switch is on or off. Add the `checked` attribute to turn it on by default.

```html
<mdui-switch checked></mdui-switch>
```

### Disabled State {#example-disabled}

Use the `disabled` attribute to disable the switch.

```html
<mdui-switch disabled></mdui-switch>
<mdui-switch disabled checked></mdui-switch>
```

### Icons {#example-icon}

Use the `unchecked-icon` and `checked-icon` attributes to set the Material Icons for the unchecked and checked states, respectively. Alternatively, use the `unchecked-icon` and `checked-icon` slots to set the icons for the unchecked and checked states.

```html
<mdui-switch unchecked-icon="remove_moderator" checked-icon="verified_user"></mdui-switch>
<mdui-switch>
  <mdui-icon slot="unchecked-icon" name="remove_moderator"></mdui-icon>
  <mdui-icon slot="checked-icon" name="verified_user"></mdui-icon>
</mdui-switch>
```

## mdui-switch API

### Properties

<table>
<thead>
  <tr>
    <th>Attribute</th>
    <th>Property</th>
    <th>Reflect</th>
    <th>Type</th>
    <th>Default</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>disabled</td>
    <td>disabled</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Disables the switch.</p>
</td>
  </tr>
  <tr>
    <td>checked</td>
    <td>checked</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Sets the switch to the checked state.</p>
</td>
  </tr>
  <tr>
    <td>undefined</td>
    <td>defaultChecked</td>
    <td>false</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>The default checked state. The switch resets to this state when the form is reset. JavaScript only.</p>
</td>
  </tr>
  <tr>
    <td>unchecked-icon</td>
    <td>uncheckedIcon</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>The Material Icons name for the unchecked state. Alternatively, use <code>slot=&quot;unchecked-icon&quot;</code>.</p>
</td>
  </tr>
  <tr>
    <td>checked-icon</td>
    <td>checkedIcon</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>The Material Icons name for the checked state. Alternatively, use <code>slot=&quot;checked-icon&quot;</code>. Defaults to the <code>check</code> icon; setting an empty string removes the default icon.</p>
</td>
  </tr>
  <tr>
    <td>required</td>
    <td>required</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>The switch must be checked before the form is submitted.</p>
</td>
  </tr>
  <tr>
    <td>form</td>
    <td>form</td>
    <td>true</td>
    <td>string</td>
    <td></td>
    <td><p>Associates the switch with a <code>&lt;form&gt;</code> element. Set this to the <code>id</code> of a <code>&lt;form&gt;</code> in the same document. If omitted, the switch uses its parent <code>&lt;form&gt;</code>, if any.</p>
<p>This lets the switch work with any form in the document, not just the one it is nested in.</p>
</td>
  </tr>
  <tr>
    <td>name</td>
    <td>name</td>
    <td>true</td>
    <td>string</td>
    <td>''</td>
    <td><p>The name of the switch, which is submitted with form data.</p>
</td>
  </tr>
  <tr>
    <td>value</td>
    <td>value</td>
    <td>true</td>
    <td>string</td>
    <td>'on'</td>
    <td><p>The value of the switch, which is submitted with form data.</p>
</td>
  </tr>
  <tr>
    <td>undefined</td>
    <td>validity</td>
    <td>false</td>
    <td>ValidityState</td>
    <td></td>
    <td><p>A <a href="https://developer.mozilla.org/en-US/docs/Web/API/ValidityState" target="_blank" rel="noopener nofollow"><code>ValidityState</code></a> object that represents the element&#39;s validity states.</p>
</td>
  </tr>
  <tr>
    <td>undefined</td>
    <td>validationMessage</td>
    <td>false</td>
    <td>string</td>
    <td></td>
    <td><p>Validation message. Empty string when valid.</p>
</td>
  </tr>
  <tr>
    <td>autofocus</td>
    <td>autofocus</td>
    <td>true</td>
    <td>boolean</td>
    <td>false</td>
    <td><p>Whether the element is focused when the page loads.</p>
</td>
  </tr>
  <tr>
    <td>tabindex</td>
    <td>tabIndex</td>
    <td>false</td>
    <td>number</td>
    <td></td>
    <td><p>The element&#39;s tab order when navigating with the Tab key.</p>
</td>
  </tr>
</tbody>
</table>

### Methods

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>checkValidity(): boolean</td>
    <td><p>Checks the validity of the form field. If it is invalid, it triggers an <code>invalid</code> event and returns <code>false</code>. If valid, it returns <code>true</code>.</p>
</td>
  </tr>
  <tr>
    <td>reportValidity(): boolean</td>
    <td><p>Checks the validity of the form field. If it is invalid, it triggers an <code>invalid</code> event, returns <code>false</code>, and displays a validation message. If valid, it returns <code>true</code>.</p>
</td>
  </tr>
  <tr>
    <td>setCustomValidity(message: string): void</td>
    <td><p>Sets a custom error message. If the message is non-empty, the field is considered invalid.</p>
</td>
  </tr>
  <tr>
    <td>click(): void</td>
    <td><p>Simulates a mouse click on the element.</p>
</td>
  </tr>
  <tr>
    <td>focus(options?: FocusOptions): void</td>
    <td><p>Sets focus on the element. An optional object parameter may include a <code>preventScroll</code> property. If <code>preventScroll</code> is set to <code>true</code>, the page will not scroll to bring the focused element into view.</p>
</td>
  </tr>
  <tr>
    <td>blur(): void</td>
    <td><p>Removes focus from the element.</p>
</td>
  </tr>
</tbody>
</table>

### Events

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>focus</td>
    <td><p>Emitted when the switch gains focus.</p>
</td>
  </tr>
  <tr>
    <td>blur</td>
    <td><p>Emitted when the switch loses focus.</p>
</td>
  </tr>
  <tr>
    <td>change</td>
    <td><p>Emitted when the checked state changes.</p>
</td>
  </tr>
  <tr>
    <td>input</td>
    <td><p>Emitted when the checked state changes.</p>
</td>
  </tr>
  <tr>
    <td>invalid</td>
    <td><p>Emitted when the form control&#39;s validity is checked and it does not meet the constraints.</p>
</td>
  </tr>
</tbody>
</table>

### Slots

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>unchecked-icon</td>
    <td><p>Element for the unchecked state.</p>
</td>
  </tr>
  <tr>
    <td>checked-icon</td>
    <td><p>Element for the checked state.</p>
</td>
  </tr>
</tbody>
</table>

### CSS Parts

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>track</td>
    <td><p>Track.</p>
</td>
  </tr>
  <tr>
    <td>thumb</td>
    <td><p>Thumb.</p>
</td>
  </tr>
  <tr>
    <td>unchecked-icon</td>
    <td><p>Icon for the unchecked state.</p>
</td>
  </tr>
  <tr>
    <td>checked-icon</td>
    <td><p>Icon for the checked state.</p>
</td>
  </tr>
</tbody>
</table>

### CSS Custom Properties

<table>
<thead>
  <tr>
    <th>Name</th>
    <th>Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td>--shape-corner</td>
    <td><p>The corner radius of the track. You can use a specific pixel value, but it is recommended to reference <a href="/en/docs/2/styles/design-tokens#shape-corner">design tokens</a>.</p>
</td>
  </tr>
  <tr>
    <td>--shape-corner-thumb</td>
    <td><p>The corner radius of the icon container. You can use a specific pixel value, but it is recommended to reference <a href="/en/docs/2/styles/design-tokens#shape-corner">design tokens</a>.</p>
</td>
  </tr>
</tbody>
</table>

