# Checkbox A styled checkbox input with an optional visible label. Uses the `accent-primary` Tailwind class so the checkmark color follows your primary theme color. --- ## HTML structure ``` div.flex.items-center.space-x-2 input[type=checkbox, id, name, value, class, $$Checked$$] label[for={id}] ← omitted when label is empty {label text} ``` --- ## CSS mechanics | Class | Effect | |---|---| | `accent-primary` | Checkmark color follows the `--color-primary` CSS variable | | `h-4 w-4 rounded` | Consistent 16×16 size with slightly rounded corners | | `cursor-pointer` | Pointer cursor on label | | `text-sm font-medium leading-none peer-disabled:opacity-70` | Standard label styling | --- ## Constructor signature ```csharp public Checkbox( string id, string label = "", string name = "", string value = "true", bool @checked = false) ``` | Parameter | Description | |---|---| | `id` | Element id and the `for` attribute on the label | | `label` | Visible text next to the checkbox; omit for a standalone checkbox | | `name` | Form field name (required when used in a form) | | `value` | Submitted value when checked (default: `"true"`) | | `checked` | Pre-checked state | --- ## Usage examples ### Basic opt-in checkbox ```csharp new Checkbox( id: "newsletter", label: "Subscribe to newsletter", name: "newsletter") ``` ### Pre-checked ```csharp new Checkbox( id: "remember", label: "Remember me", name: "rememberMe", checked: true) ``` ### No visible label ```csharp new Checkbox(id: "select-all", name: "selectAll") ``` ### Custom submitted value ```csharp new Checkbox( id: "agree", label: "I agree to the terms", name: "terms", value: "accepted") ``` ### Reading in a form handler ```csharp public record Command( [property: FromForm] string? Newsletter = null, // null when unchecked [property: FromForm] string? RememberMe = null ); bool wantsNewsletter = command.Newsletter == "true"; bool rememberUser = command.RememberMe == "true"; ``` > Note: Unchecked checkboxes are not included in form data. Always use a nullable string or a default value of `null`. --- ## Tips and tricks - Because HTML forms only submit checked checkboxes, pair a checkbox with a hidden input of the same name and value `"false"` if you need the unchecked state explicitly in your command. - The label `for` attribute ties to the `id`, so clicking the label text toggles the checkbox — always set `id`. - If you need multi-select (select multiple rows in a table), use the same `name` for all checkboxes; they will be submitted as a comma-separated list or multiple values depending on form binding. - `accent-primary` is a modern CSS property — all current browsers support it. - `accent-primary` is a modern CSS property — all current browsers support it. --- ## Complete page example **`Templates/PreferencesPage.htmx`** ```html