Debian Codegraph @

node/@patternfly/patternfly/docs/components/Switch/examples/Switch.md

raw
1 ---
2 id: Switch
3 section: components
4 cssPrefix: pf-v6-c-switch
5 ---## Examples
6
7 ### Basic
8
9 ```html
10 <label class="pf-v6-c-switch" for="switch-with-label-1">
11 <input
12 class="pf-v6-c-switch__input"
13 type="checkbox"
14 role="switch"
15 id="switch-with-label-1"
16 aria-labelledby="switch-with-label-1-text"
17 checked
18 />
19
20 <span class="pf-v6-c-switch__toggle"></span>
21
22 <span
23 class="pf-v6-c-switch__label"
24 id="switch-with-label-1-text"
25 aria-hidden="true"
26 >Togglable option in basic example</span>
27 </label>
28
29 ```
30
31 ### Reverse (toggle on right)
32
33 ```html
34 <label class="pf-v6-c-switch pf-m-reverse" for="switch-reverse-1">
35 <input
36 class="pf-v6-c-switch__input"
37 type="checkbox"
38 role="switch"
39 id="switch-reverse-1"
40 aria-labelledby="switch-reverse-1-text"
41 checked
42 />
43
44 <span class="pf-v6-c-switch__toggle"></span>
45
46 <span
47 class="pf-v6-c-switch__label pf-m-on"
48 id="switch-reverse-1-text"
49 aria-hidden="true"
50 >Togglable option in reverse example</span>
51 </label>
52
53 ```
54
55 ### Label and check icon
56
57 ```html
58 <label class="pf-v6-c-switch" for="switch-label-check-1">
59 <input
60 class="pf-v6-c-switch__input"
61 type="checkbox"
62 role="switch"
63 id="switch-label-check-1"
64 aria-labelledby="switch-label-check-1-text"
65 checked
66 />
67
68 <span class="pf-v6-c-switch__toggle">
69 <span class="pf-v6-c-switch__toggle-icon">
70 <i class="fas fa-check" aria-hidden="true"></i>
71 </span>
72 </span>
73 <span
74 class="pf-v6-c-switch__label pf-m-on"
75 id="switch-label-check-1-text"
76 aria-hidden="true"
77 >Togglable option in check icon example</span>
78 </label>
79
80 ```
81
82 ### Without visible text label
83
84 ```html
85 <label class="pf-v6-c-switch" for="switch-with-icon-1">
86 <input
87 class="pf-v6-c-switch__input"
88 type="checkbox"
89 role="switch"
90 id="switch-with-icon-1"
91 aria-label="Togglable option in no text label example"
92 checked
93 />
94
95 <span class="pf-v6-c-switch__toggle">
96 <span class="pf-v6-c-switch__toggle-icon">
97 <i class="fas fa-check" aria-hidden="true"></i>
98 </span>
99 </span>
100 </label>
101
102 ```
103
104 ### Disabled
105
106 ```html
107 <label class="pf-v6-c-switch" for="switch-disabled-1">
108 <input
109 class="pf-v6-c-switch__input"
110 type="checkbox"
111 role="switch"
112 id="switch-disabled-1"
113 aria-labelledby="switch-disabled-1-text"
114 disabled
115 checked
116 />
117
118 <span class="pf-v6-c-switch__toggle"></span>
119
120 <span
121 class="pf-v6-c-switch__label"
122 id="switch-disabled-1-text"
123 aria-hidden="true"
124 >Togglable option in disabled example</span>
125 </label>
126
127 <br />
128 <br />
129 <label class="pf-v6-c-switch" for="switch-disabled-2">
130 <input
131 class="pf-v6-c-switch__input"
132 type="checkbox"
133 role="switch"
134 id="switch-disabled-2"
135 aria-labelledby="switch-disabled-2-text"
136 disabled
137 />
138
139 <span class="pf-v6-c-switch__toggle"></span>
140
141 <span
142 class="pf-v6-c-switch__label"
143 id="switch-disabled-2-text"
144 aria-hidden="true"
145 >Togglable option in basic example</span>
146 </label>
147
148 <br />
149 <br />
150 <label class="pf-v6-c-switch" for="switch-with-icon-disabled-1">
151 <input
152 class="pf-v6-c-switch__input"
153 type="checkbox"
154 role="switch"
155 id="switch-with-icon-disabled-1"
156 aria-label="Togglable option in disabled icon example"
157 disabled
158 checked
159 />
160
161 <span class="pf-v6-c-switch__toggle">
162 <span class="pf-v6-c-switch__toggle-icon">
163 <i class="fas fa-check" aria-hidden="true"></i>
164 </span>
165 </span>
166 </label>
167
168 <br />
169 <br />
170 <label class="pf-v6-c-switch" for="switch-with-icon-disabled-2">
171 <input
172 class="pf-v6-c-switch__input"
173 type="checkbox"
174 role="switch"
175 id="switch-with-icon-disabled-2"
176 aria-label="Togglable option in disabled icon example"
177 disabled
178 />
179 <span class="pf-v6-c-switch__toggle">
180 <span class="pf-v6-c-switch__toggle-icon">
181 <i class="fas fa-check" aria-hidden="true"></i>
182 </span>
183 </span>
184 </label>
185
186 ```
187
188 ## Documentation
189
190 ### Overview
191
192 A switch is an alternative to the checkbox component.
193
194 Use a switch when your user needs to perform instantaneous actions without confirmation.
195
196 Use checkbox if your user has to perform additional steps for changes to become effective.
197
198 ### Accessibility
199
200 | Attribute | Applied to | Outcome |
201 | -- | -- | -- |
202 | `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** |
203 | `for` | `<label>` | Each `<label>` must have a `for` attribute that matches its input id. **Required** |
204 | `id` | `<input type="checkbox">` | Each `<input>` must have an `id` attribute that matches its label's `for` value. **Required** |
205 | `role="switch"` | `<input type="checkbox">` | Exposes the element as a switch for assistive technologies. **Required** |
206 | `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`. |
207 | `checked` | `.pf-v6-c-switch__input` | Indicates that the input is checked |
208 | `disabled` | `.pf-v6-c-switch__input` | Indicates that the input is disabled |
209 | `aria-hidden="true"` | `.pf-v6-c-switch__label` | Hides the text from the screen reader. |
210
211 ### Usage
212
213 | Class | Applied to | Outcome |
214 | -- | -- | -- |
215 | `.pf-v6-c-switch` | `<label>` | Initiates a switch. **Required** |
216 | `.pf-v6-c-switch__input` | `<input type="checkbox">` | Hide the checkbox inside the switch. **Required** |
217 | `.pf-v6-c-switch__toggle` | `<span>` | Initiates the toggle inside the switch. **Required** |
218 | `.pf-v6-c-switch__toggle-icon` | `<span>` | Initiates the switch toggle icon wrapper. **Required when the switch is used without a label** |
219 | `.pf-v6-c-switch__label` | `<span>` | Initiates a label inside the switch. |
220 | `.pf-m-reverse` | `.pf-v6-c-switch` | Positions the switch toggle to the right of the label. |