|
| 1 | +# Switch |
| 2 | +A switch allows users to toggle the state of a single setting. |
| 3 | + |
| 4 | +## Overview |
| 5 | +Switches provide a way to toggle between two states with a visual indicator of the current state. A label can be positioned on the left, right, top, or bottom of the switch. The switch can be set to a disabled state, display a loading indicator, or show a custom error message. |
| 6 | + |
| 7 | +## Usage |
| 8 | + |
| 9 | +### Within a standalone component |
| 10 | +To use a switch in a standalone component, import it directly into your custom component: |
| 11 | + |
| 12 | +```ts |
| 13 | +@Component({ |
| 14 | + selector: 'custom-component', |
| 15 | + templateUrl: './custom-component.component.html', |
| 16 | + standalone: true, |
| 17 | + imports: [ |
| 18 | + DAFF_SWITCH_COMPONENTS, |
| 19 | + ], |
| 20 | +}) |
| 21 | +export class CustomComponent {} |
| 22 | +``` |
| 23 | + |
| 24 | +## Examples |
| 25 | + |
| 26 | +### Basic Switch |
| 27 | +A basic switch can be toggled by setting the `checked` property to `true` or `false`. By default, this is set to `false`. |
| 28 | + |
| 29 | +<design-land-example-viewer-container example="basic-switch"></design-land-example-viewer-container> |
| 30 | + |
| 31 | +### Disabled Switch |
| 32 | +A switch with the `disabled` property will be non-interactive. |
| 33 | + |
| 34 | +<design-land-example-viewer-container example="disabled-switch"></design-land-example-viewer-container> |
| 35 | + |
| 36 | +### Loading Switch |
| 37 | +A switch can display a loading state by setting `loading` to `true`. This will also disable the switch. |
| 38 | + |
| 39 | +<design-land-example-viewer-container example="loading-switch"></design-land-example-viewer-container> |
| 40 | + |
| 41 | +### Switch with Error |
| 42 | +An error message can be displayed by setting `error` to `true` and including a `daff-error-message` to show the message text. |
| 43 | + |
| 44 | +<design-land-example-viewer-container example="switch-error"></design-land-example-viewer-container> |
| 45 | + |
| 46 | +### Changing Label Position |
| 47 | +The label position can be changed by setting the `labelPosition` property. The default position is `left`. |
| 48 | + |
| 49 | +Supported positions: `left | right | top | bottom` |
| 50 | + |
| 51 | +<design-land-example-viewer-container example="switch-label-positions"></design-land-example-viewer-container> |
| 52 | + |
| 53 | + |
| 54 | +## Accessibility |
| 55 | +Switches follow the [ARIA Switch design pattern](https://www.w3.org/WAI/ARIA/apg/patterns/switch/). |
| 56 | + |
| 57 | +### Keyboard Interactions |
| 58 | +| Key | Action | |
| 59 | +| --- | ------ | |
| 60 | +| Space | Toggles the switch to the opposite state. | |
| 61 | +| Tab | Moves to next component on the page. | |
0 commit comments