Skip to content
GitHub11 stars

A control that indicates whether a setting is on or off.

<label class="switch-label">
    <SwitchRoot Checked="true" class="switch">
        <SwitchThumb class="switch-thumb" />
    </SwitchRoot>
    Notifications
</label>

Usage guidelines

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

Anatomy

Import the primitives and assemble the parts:

@using Blazix.Primitives

<SwitchRoot>
    <SwitchThumb />
</SwitchRoot>

Examples

Labeling a switch

An enclosing <label> is the simplest labeling pattern:

Wrapping a label around a switch
<label>
    <SwitchRoot />
    Notifications
</label>

SwitchRoot renders a <span> with a hidden native <input> beside it. 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-switch">Notifications</label>
    <SwitchRoot InputId="notifications-switch">
        <SwitchThumb />
    </SwitchRoot>
</div>

API Reference

SwitchRoot

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>> -
Value string? -
UncheckedValue string? -
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<SwitchRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the switch is checked.

-
data-unchecked

Present when the switch is not checked.

-
data-disabled

Present when the switch is disabled.

-
data-readonly

Present when the switch is readonly.

-
data-required

Present when the switch is required.

-
data-valid

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

-
data-invalid

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

-
data-dirty

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

-
data-touched

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

-
data-filled

Present when the switch is active (when wrapped in FieldRoot).

-
data-focused

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

-

SwitchThumb

Renders a <span> element.

ParameterTypeDefault
Render RenderFragment<SwitchRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-checked

Present when the switch is checked.

-
data-unchecked

Present when the switch is not checked.

-
data-disabled

Present when the switch is disabled.

-
data-readonly

Present when the switch is readonly.

-
data-required

Present when the switch is required.

-
data-valid

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

-
data-invalid

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

-
data-dirty

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

-
data-touched

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

-
data-filled

Present when the switch is active (when wrapped in FieldRoot).

-
data-focused

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

-