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
221
use ;
use ;
use component_doc;
use inject_style;
use switch_styles;
use ;
/// Switch toggles a boolean setting that takes effect immediately — dark mode,
/// notifications, feature flags.
///
/// Bind `checked` with [`SwitchBind`], add an inline `label` for the setting name, and
/// wrap in [`Field`](crate::Field) when the form needs a section label or validation message.
/// Use [`Checkbox`](crate::Checkbox) when the user must explicitly opt in before submit.
///
/// # When to use
///
/// - Immediate on/off settings (dark mode, notifications)
/// - Compact boolean controls where a checkbox label feels too heavy
/// - Forms that submit a named boolean value
///
/// # Usage
///
/// 1. Bind `checked` via [`SwitchBind`] (typically `RwSignal<bool>`).
/// 2. Pass `label` through [`SwitchLabel`] for the adjacent caption.
/// 3. Set `id` explicitly when multiple switches appear on one page.
/// 4. Attach `rules` on [`SwitchBind`] when Field should show validation errors.
///
/// Grouped props: [`SwitchBind`] (checked, name, value, rules, disabled),
/// [`SwitchLabel`] (label). At call sites you can write `bind=signal` via [`From`] on [`SwitchBind`].
///
/// # Boolean choice controls
///
/// When `Switch` is not the right fit:
///
/// - **Independent yes/no, submit with form** — [`Checkbox`](crate::Checkbox).
/// - **Immediate on/off setting** — `Switch` (this component).
/// - **Exactly one of several options** — [`Radio`](crate::Radio) inside [`RadioGroup`](crate::RadioGroup).
///
/// # Best Practices
///
/// ## Do's
///
/// * Wrap in [`Field`](crate::Field) when a visible label or validation message is required
/// * Bind `checked` with [`SwitchBind`] for two-way sync
/// * Use `disabled` on [`SwitchBind`] while saving or when the setting is locked
///
/// ## Don'ts
///
/// * Do not use for explicit opt-in before form submit — prefer [`Checkbox`](crate::Checkbox)
/// * Do not use for multi-select lists — use [`Checkbox`](crate::Checkbox)
/// * Do not put `data-testid` on the component — wrap with a native element
///
/// # Examples
///
/// ## On
/// Switch in the on position; bind `checked` via [`SwitchBind`] (or pass a signal with `bind=signal`).
/// <!-- preview -->
/// ```rust
/// let on = RwSignal::new(true);
/// view! {
/// <div data-testid="switch-preview">
/// <Switch bind=on label="Dark mode" />
/// </div>
/// }
/// ```
///
/// ## Off
/// Default off state for settings that start disabled until the user toggles them on.
/// <!-- preview -->
/// ```rust
/// let off = RwSignal::new(false);
/// view! {
/// <div data-testid="switch-off">
/// <Switch bind=off label="Notifications" />
/// </div>
/// }
/// ```
///
/// ## Disabled
/// Locked setting: shows the current on/off state but ignores pointer and keyboard toggles.
/// <!-- preview -->
/// ```rust
/// let on = RwSignal::new(true);
/// view! {
/// <div data-testid="switch-disabled">
/// <Switch
/// bind=SwitchBind {
/// checked: on.into(),
/// disabled: Signal::from(true),
/// ..Default::default()
/// }
/// label="Locked setting"
/// />
/// </div>
/// }
/// ```
///
/// ## Field wrapper
/// Field provides the form label and id wiring; the switch keeps its own inline caption for the setting.
/// <!-- preview -->
/// ```rust
/// use crate::{Field, Switch, SwitchBind};
/// let enabled = RwSignal::new(true);
/// view! {
/// <div data-testid="switch-field">
/// <Field label="Email alerts" name="alerts">
/// <Switch bind=enabled label="Send weekly summary" />
/// </Field>
/// </div>
/// }
/// ```
///
/// ## Keyboard
/// The native switch input receives focus; Space toggles the checked state.
/// <!-- preview -->
/// ```rust
/// let on = RwSignal::new(false);
/// view! {
/// <div data-testid="switch-keyboard">
/// <Switch bind=on label="Press Space to toggle" id="switch-keyboard-demo" />
/// </div>
/// }
/// ```