Skip to content
GitHub11 stars

A button component that can be rendered as another tag or focusable when disabled.

<ButtonRoot class="button">
    Submit
</ButtonRoot>

Usage guidelines

  • Submit buttons: Unlike the native button element, ButtonRoot needs type="submit" to act as a submit button.
  • Links: The button enforces button semantics (role="button", keyboard interaction, disabled state). Don't use it for links. See Rendering links as buttons.

Anatomy

Import the primitives and use the part:

@using Blazix.Primitives

<ButtonRoot />

It's named ButtonRoot rather than Button, so it never clashes with a styled Button in your app.

Examples

Rendering as another tag

The button stays keyboard accessible when you render it as another tag, such as a <div>, with Render. The browser reports the element it got, and the button adds role="button", a tab stop and Enter and Space to anything that isn't a native <button>:

<ButtonRoot>
    <Render Context="button">
        <div @attributes="button.Attributes">Button that can contain complex children</div>
    </Render>
</ButtonRoot>

The button enforces button semantics, so the element you render must be one that can take them (role="button" and the keyboard handling). Links (<a>) have semantics of their own and shouldn't be rendered as buttons. If a link needs to look like a button, style the <a> itself.

Loading states

For a button that enters a loading state when pressed, set FocusableWhenDisabled so focus stays on the button while it's disabled. A button that's focusable while disabled still receives clicks, so ignore them while it's loading. Some browsers and screen readers don't reliably announce changes to the text inside a focused button, so use aria-labelledby to make the changing text the button's accessible name.

<ButtonRoot Disabled="_loading" FocusableWhenDisabled="true" aria-labelledby="@_labelId" @onclick="SubmitAsync" class="button">
    <span id="@_labelId">@(_loading ? "Submitting" : "Submit")</span>
</ButtonRoot>

API Reference

ButtonRoot

Renders a <button> element.

ParameterTypeDefault
Disabled

Whether the component should ignore user interaction.

bool false
FocusableWhenDisabled bool false
NativeButton bool? -
Render RenderFragment<ButtonRenderProps>? -
ChildContent

The part's content.

RenderFragment? -

Other attributes are passed to the element it renders.

AttributeType
data-disabled

Present when the button is disabled.

-