Button
PreviewA native button with a loading state and a disabled state that can keep focus.
<ui:button>Save changes</ui:button>
Features
Permalink to heading "Features"- Native
<button>withtype="button"by default disabledandloadingstatesfocusableWhenDisabledkeeps a disabled button focusableindicatorparts show content per state- Works as the
asChildtarget of triggers - Building block for your own components
The button is a single element with optional parts, so the file named after its folder is the root (<ui:button>). See Root or the Folder Name.
<primitives:button>
<primitives:button.indicator state="idle" />
<primitives:button.indicator state="loading" />
<primitives:button.indicator state="disabled" />
</primitives:button>
Installation
Permalink to heading "Installation"typo3 ui:add button
Please copy the files manually from GitHub into your project: Button
Read more about installing Components and Primitives.
Examples
Permalink to heading "Examples"Disable a button
Permalink to heading "Disable a button"Set disabled to render the native disabled attribute. Browsers skip a disabled button in the tab order, and a focused button loses focus the moment it is disabled.
<ui:button disabled="{true}">Save changes</ui:button>
Keep a disabled button focusable
Permalink to heading "Keep a disabled button focusable"Set focusableWhenDisabled to render aria-disabled="true" instead of disabled, so the button stays in the tab order. It swallows clicks and the Enter and Space keys before any other listener runs, so a disabled submit button leaves its form alone and a disabled trigger leaves its dialog closed. Press Tab to compare the two buttons below.
Loading states
Permalink to heading "Loading states"Click the button below: it shows a spinner and the label "Submitting" for four seconds. Each indicator is visible in its own state (idle, loading or disabled) and hidden in the others, and loading takes precedence over disabled.
Change the state from your entry file with api.setLoading() or api.setDisabled(). For that, mount the button yourself with autoMount="{false}" and a rootId, as in the demo. Then:
focusableWhenDisabled- keeps focus on the button while it loads.aria-labelledby- points at the element that wraps the label indicators, so the new label is announced. Some browser and screen reader combinations do not reliably announce text changes inside a focused button.- One
indicatorper state - a state without one renders nothing, so its content disappears. aria-hidden="true"- on decorative content like the spinner.
In a form, set type="submit" and pass form.api.isSubmitting as loading, see Forms.
Use a button as a trigger
Permalink to heading "Use a button as a trigger"Put a ui:button inside a trigger like ui:collapsible.trigger and set asChild on the trigger. The trigger adds aria-expanded and data-state, the button keeps its type and disabled handling.
The button keeps its own behavior and gets the attributes of the trigger on top.
Build your own component with a focusable button
Permalink to heading "Build your own component with a focusable button"Click "Back up now" in the card below: the button shows a spinner for two seconds, then the card updates its status line. The card's entry file mounts the button with a rootId derived from its own and drives it with api.setLoading().
Backup
Last backup 2 days ago
API Reference
Permalink to heading "API Reference"The following tables cover the available props of the Fluid Primitives.
The button. Renders a <button> element.
| Name | Description | Required | Default |
|---|---|---|---|
type | stringThe `type` of the button. It defaults to `button`, so a button inside a form does not submit it. Use `submit` or `reset` to opt in. | No | 'button' |
disabled | booleanWhether the button is disabled. | No | false |
loading | booleanWhether the button is loading. A loading button is disabled too. | No | false |
focusableWhenDisabled | booleanKeeps a disabled button focusable. It then gets `aria-disabled` instead of the `disabled` attribute, and clicks and key presses are ignored. | No | false |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
rootId | stringThe root ID of the component, used for hydration and identification. | No | - |
ids | arrayThe IDs of of the component parts for composition. | No | [] |
autoMount | booleanWhether the client initializes the component on its own. Set it to false to mount it yourself with `mount()` | No | true |
provider | stringThe `id` of the `.provider` this root takes its `rootId` and props from. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Rendered data attributes
| Attribute | Description |
|---|---|
data-disabled | Present when disabled |
data-loading | Present while the button is loading. |
button.indicator
Permalink to heading "button.indicator"Shows its content only while the button is in its state. Renders a <span> element.
| Name | Description | Required | Default |
|---|---|---|---|
state | Enum\ButtonIndicatorStateThe state of the button this indicator is shown in. | Yes | - |
attributes | arrayAdditional attributes that should be rendered on the component where ui:attributes is used. | No | [] |
asChild | booleanIf true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props. | No | - |
class | stringThe CSS class(es) to be applied to the component. | No | - |
Machine options
Permalink to heading "Machine options"| Name | Description | Default |
|---|---|---|
id | stringThe unique identifier of the button. | - |
ids | Record<string, string> | undefinedThe ids of the parts other elements reference: root. | - |
disabled | boolean | undefinedWhether the button is disabled. | - |
loading | boolean | undefinedWhether the button is loading. A loading button is disabled too. | - |
focusableWhenDisabled | boolean | undefinedKeeps a disabled button focusable. It then gets aria-disabled instead of the disabled attribute, and clicks and key presses are ignored. | - |
Machine JavaScript API
Permalink to heading "Machine JavaScript API"| Name | Type | Description |
|---|---|---|
state | ButtonState | The state of the button, which decides the indicators that are shown. |
disabled | boolean | Whether the button is disabled, either on its own or because it is loading. |
loading | boolean | Whether the button is loading. |
setDisabled | (value: boolean) => void | Disables or enables the button. A loading button stays disabled. |
setLoading | (value: boolean) => void | Starts or stops loading. A loading button is disabled too. |
addEventListener | (...args: Parameters<HTMLButtonElement['addEventListener']>) => void | Adds an event listener to the button element, the same as addEventListener of the element. |