Skip to content
GitHub11 stars

An easily stylable checkbox component.

<label class="checkbox-label">
    <CheckboxRoot Checked="true" class="checkbox">
        <CheckboxIndicator class="checkbox-indicator">

Usage guidelines

  • Form controls must have an accessible name: give it one with a <label> element or the Field parts. See Labeling a checkbox.

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<CheckboxRoot>
    <CheckboxIndicator />
</CheckboxRoot>

Examples

Labeling a checkbox

An enclosing <label> is the simplest labeling pattern:

Wrapping a label around a checkbox
<label>
    <CheckboxRoot />
    Accept terms and conditions
</label>

CheckboxRoot renders a <span> with a hidden native <input> beside it, which carries the name, value and validity so the checkbox submits with forms. To label it with a sibling <label>, point the label's for at the hidden input's id, set with InputId:

Sibling label
<div>
    <label for="notifications-checkbox">Enable notifications</label>
    <CheckboxRoot InputId="notifications-checkbox">
        <CheckboxIndicator />
    </CheckboxRoot>
</div>

API Reference

CheckboxRoot

Renders a <span> element.

ParameterTypeDefault
Checked bool false
CheckedChanged

Called with the new value when Checked changes, for @bind-Checked.

EventCallback<bool> -
OnCheckedChange

Runs before the checked state changes; call Cancel() to keep it.

EventCallback<ValueChangeEventArgs<bool>> -
Indeterminate bool false
Value string? -
UncheckedValue string? -
Parent bool false
Disabled

Whether the component should ignore user interaction.

bool false
ReadOnly

Whether the user should be unable to change the value. It stays focusable.

bool false
Required

Whether the user must enter a value before submitting a form.

bool false
Name string? -
InputId string? -
Render RenderFragment<CheckboxRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the checkbox is checked.

-
data-unchecked

Present when the checkbox is not checked.

-
data-indeterminate

Present when the checkbox is in an indeterminate state.

-
data-disabled

Present when the checkbox is disabled.

-
data-readonly

Present when the checkbox is readonly.

-
data-required

Present when the checkbox is required.

-
data-valid

Present when the checkbox is in a valid state (when wrapped in FieldRoot).

-
data-invalid

Present when the checkbox is in an invalid state (when wrapped in FieldRoot).

-
data-dirty

Present when the checkbox's value has changed (when wrapped in FieldRoot).

-
data-touched

Present when the checkbox has been touched (when wrapped in FieldRoot).

-
data-filled

Present when the checkbox is checked (when wrapped in FieldRoot).

-
data-focused

Present when the checkbox is focused (when wrapped in FieldRoot).

-

CheckboxIndicator

Renders a <span> element.

ParameterTypeDefault
KeepMounted bool false
Render RenderFragment<CheckboxRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the checkbox is checked.

-
data-unchecked

Present when the checkbox is not checked.

-
data-indeterminate

Present when the checkbox is in an indeterminate state.

-
data-disabled

Present when the checkbox is disabled.

-
data-readonly

Present when the checkbox is readonly.

-
data-required

Present when the checkbox is required.

-
data-valid

Present when the checkbox is in a valid state (when wrapped in FieldRoot).

-
data-invalid

Present when the checkbox is in an invalid state (when wrapped in FieldRoot).

-
data-dirty

Present when the checkbox's value has changed (when wrapped in FieldRoot).

-
data-touched

Present when the checkbox has been touched (when wrapped in FieldRoot).

-
data-filled

Present when the checkbox is checked (when wrapped in FieldRoot).

-
data-focused

Present when the checkbox is focused (when wrapped in FieldRoot).

-
data-starting-style

Present when the checkbox indicator begins animating in.

-
data-ending-style

Present when the checkbox indicator is animating out.

-