A button component that can be rendered as another tag or focusable when disabled.
Usage guidelines
- Submit buttons: Unlike the native button element,
ButtonRootneedstype="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>Rendering links as buttons
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.
API Reference
ButtonRoot
Renders a <button> element.
Other attributes are passed to the element it renders.