File: Switch.md

package info (click to toggle)
cockpit 354-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 308,956 kB
  • sloc: javascript: 775,606; python: 40,351; ansic: 35,655; cpp: 11,117; sh: 3,511; makefile: 580; xml: 261
file content (220 lines) | stat: -rw-r--r-- 6,245 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
id: Switch
section: components
cssPrefix: pf-v6-c-switch
---## Examples

### Basic

```html
<label class="pf-v6-c-switch" for="switch-with-label-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-with-label-1"
    aria-labelledby="switch-with-label-1-text"
    checked
  />

  <span class="pf-v6-c-switch__toggle"></span>

  <span
    class="pf-v6-c-switch__label"
    id="switch-with-label-1-text"
    aria-hidden="true"
  >Togglable option in basic example</span>
</label>

```

### Reverse (toggle on right)

```html
<label class="pf-v6-c-switch pf-m-reverse" for="switch-reverse-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-reverse-1"
    aria-labelledby="switch-reverse-1-text"
    checked
  />

  <span class="pf-v6-c-switch__toggle"></span>

  <span
    class="pf-v6-c-switch__label pf-m-on"
    id="switch-reverse-1-text"
    aria-hidden="true"
  >Togglable option in reverse example</span>
</label>

```

### Label and check icon

```html
<label class="pf-v6-c-switch" for="switch-label-check-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-label-check-1"
    aria-labelledby="switch-label-check-1-text"
    checked
  />

  <span class="pf-v6-c-switch__toggle">
    <span class="pf-v6-c-switch__toggle-icon">
      <i class="fas fa-check" aria-hidden="true"></i>
    </span>
  </span>
  <span
    class="pf-v6-c-switch__label pf-m-on"
    id="switch-label-check-1-text"
    aria-hidden="true"
  >Togglable option in check icon example</span>
</label>

```

### Without visible text label

```html
<label class="pf-v6-c-switch" for="switch-with-icon-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-with-icon-1"
    aria-label="Togglable option in no text label example"
    checked
  />

  <span class="pf-v6-c-switch__toggle">
    <span class="pf-v6-c-switch__toggle-icon">
      <i class="fas fa-check" aria-hidden="true"></i>
    </span>
  </span>
</label>

```

### Disabled

```html
<label class="pf-v6-c-switch" for="switch-disabled-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-disabled-1"
    aria-labelledby="switch-disabled-1-text"
    disabled
    checked
  />

  <span class="pf-v6-c-switch__toggle"></span>

  <span
    class="pf-v6-c-switch__label"
    id="switch-disabled-1-text"
    aria-hidden="true"
  >Togglable option in disabled example</span>
</label>

<br />
<br />
<label class="pf-v6-c-switch" for="switch-disabled-2">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-disabled-2"
    aria-labelledby="switch-disabled-2-text"
    disabled
  />

  <span class="pf-v6-c-switch__toggle"></span>

  <span
    class="pf-v6-c-switch__label"
    id="switch-disabled-2-text"
    aria-hidden="true"
  >Togglable option in basic example</span>
</label>

<br />
<br />
<label class="pf-v6-c-switch" for="switch-with-icon-disabled-1">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-with-icon-disabled-1"
    aria-label="Togglable option in disabled icon example"
    disabled
    checked
  />

  <span class="pf-v6-c-switch__toggle">
    <span class="pf-v6-c-switch__toggle-icon">
      <i class="fas fa-check" aria-hidden="true"></i>
    </span>
  </span>
</label>

<br />
<br />
<label class="pf-v6-c-switch" for="switch-with-icon-disabled-2">
  <input
    class="pf-v6-c-switch__input"
    type="checkbox"
    role="switch"
    id="switch-with-icon-disabled-2"
    aria-label="Togglable option in disabled icon example"
    disabled
  />
  <span class="pf-v6-c-switch__toggle">
    <span class="pf-v6-c-switch__toggle-icon">
      <i class="fas fa-check" aria-hidden="true"></i>
    </span>
  </span>
</label>

```

## Documentation

### Overview

A switch is an alternative to the checkbox component.

Use a switch when your user needs to perform instantaneous actions without confirmation.

Use checkbox if your user has to perform additional steps for changes to become effective.

### Accessibility

| Attribute | Applied to | Outcome |
| -- | -- | -- |
| `aria-labelledby="..."` or `aria-label="..."` | `.pf-v6-c-switch__input` | Indicates the behavior of the switch. This label should never dynamically change, and should always describe the behavior of the switch when its checked state is true. If an additional text label is included with the switch besides `.pf-v6-c-switch__label`, then `aria-labelledby` can reference the `id` of this text, but it must be in addition to the text of `.pf-v6-c-switch__label`. If there is no visible text label provided, then an `aria-label` must be provided. **Required** |
| `for` | `<label>` | Each `<label>` must have a `for` attribute that matches its input id. **Required** |
| `id` | `<input type="checkbox">` | Each `<input>` must have an `id` attribute that matches its label's `for` value. **Required** |
| `role="switch"` | `<input type="checkbox">` | Exposes the element as a switch for assistive technologies. **Required** |
| `id` | `.pf-v6-c-switch__label` | Each `.pf-v6-c-switch__label` must have an `id` attribute that matches the `aria-labelledby` on `.pf-v6-c-switch__input`. |
| `checked` | `.pf-v6-c-switch__input` |  Indicates that the input is checked |
| `disabled` | `.pf-v6-c-switch__input` |  Indicates that the input is disabled |
| `aria-hidden="true"` | `.pf-v6-c-switch__label` | Hides the text from the screen reader. |

### Usage

| Class | Applied to | Outcome |
| -- | -- | -- |
| `.pf-v6-c-switch` | `<label>` |  Initiates a switch. **Required**  |
| `.pf-v6-c-switch__input` | `<input type="checkbox">` |  Hide the checkbox inside the switch. **Required**  |
| `.pf-v6-c-switch__toggle` | `<span>` |  Initiates the toggle inside the switch. **Required**  |
| `.pf-v6-c-switch__toggle-icon` | `<span>` | Initiates the switch toggle icon wrapper. **Required when the switch is used without a label** |
| `.pf-v6-c-switch__label` | `<span>` |  Initiates a label inside the switch. |
| `.pf-m-reverse` | `.pf-v6-c-switch` | Positions the switch toggle to the right of the label. |