# Switch An on/off toggle that looks like a physical light switch. Use it for settings where the effect is immediate or where a simple checked/unchecked checkbox would feel too plain — "Enable notifications", "Dark mode", "Maintenance mode". --- ## Quick example ```csharp new Switch( id: "notifications", label: "Enable notifications", name: "enableNotifications", isChecked: true) ``` --- ## All the options ```csharp public Switch( string id, string label = "", string name = "", bool isChecked = false) ``` | Parameter | What it does | |---|---| | `id` | The element id for the hidden checkbox. | | `label` | Optional text shown to the right of the toggle. | | `name` | Form field name — required if you want the value submitted. | | `isChecked` | Whether the switch is on by default. | --- ## Real-world examples ### Preferences form with multiple toggles ```csharp new Switch(id: "email-alerts", label: "Email alerts", name: "emailAlerts", isChecked: prefs.EmailAlerts) new Switch(id: "push-notifs", label: "Push notifications", name: "pushNotifs", isChecked: prefs.PushNotifs) new Switch(id: "weekly-summary", label: "Weekly digest", name: "weeklySummary", isChecked: prefs.WeeklySummary) ``` Reading on the server: ```csharp public record Command( [property: FromForm] string? EmailAlerts = null, // null = off [property: FromForm] string? PushNotifs = null, [property: FromForm] string? WeeklySummary = null ); bool emailAlerts = command.EmailAlerts != null; ``` > **Important:** Like a checkbox, an unchecked switch is not included in the form submission. Always use `string?` (nullable) with a default of `null`. ### Toggle without a label (e.g. in a table row) ```csharp new Switch(id: $"active-{user.Id}", name: "isActive", isChecked: user.IsActive) ``` --- ## How it works Switch is a styled `