Skip to main content

Button

Preview
View as Markdown

A native button with a loading state and a disabled state that can keep focus.

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.

Read more about installing Components and Primitives.

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.

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.

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:

In a form, set type="submit" and pass form.api.isSubmitting as loading, see Forms.

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.

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

The following tables cover the available props of the Fluid Primitives.

The button. Renders a <button> element.

NameDescriptionRequiredDefault
typestring
The `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'
disabledboolean
Whether the button is disabled.
No
false
loadingboolean
Whether the button is loading. A loading button is disabled too.
No
false
focusableWhenDisabledboolean
Keeps a disabled button focusable. It then gets `aria-disabled` instead of the `disabled` attribute, and clicks and key presses are ignored.
No
false
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
rootIdstring
The root ID of the component, used for hydration and identification.
No
-
idsarray
The IDs of of the component parts for composition.
No
[]
autoMountboolean
Whether the client initializes the component on its own. Set it to false to mount it yourself with `mount()`
No
true
providerstring
The `id` of the `.provider` this root takes its `rootId` and props from.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-

Rendered data attributes

AttributeDescription
data-disabledPresent when disabled
data-loadingPresent while the button is loading.

Shows its content only while the button is in its state. Renders a <span> element.

NameDescriptionRequiredDefault
stateEnum\ButtonIndicatorState
The state of the button this indicator is shown in.
Yes
-
attributesarray
Additional attributes that should be rendered on the component where ui:attributes is used.
No
[]
asChildboolean
If true the component uses its child only without the component template. Like Radix UI asChild or Base UI render props.
No
-
classstring
The CSS class(es) to be applied to the component.
No
-
NameDescriptionDefault
idstring
The unique identifier of the button.
-
idsRecord<string, string> | undefined
The ids of the parts other elements reference: root.
-
disabledboolean | undefined
Whether the button is disabled.
-
loadingboolean | undefined
Whether the button is loading. A loading button is disabled too.
-
focusableWhenDisabledboolean | undefined
Keeps a disabled button focusable. It then gets aria-disabled instead of the disabled attribute, and clicks and key presses are ignored.
-
NameTypeDescription
stateButtonStateThe state of the button, which decides the indicators that are shown.
disabledbooleanWhether the button is disabled, either on its own or because it is loading.
loadingbooleanWhether the button is loading.
setDisabled(value: boolean) => voidDisables or enables the button. A loading button stays disabled.
setLoading(value: boolean) => voidStarts or stops loading. A loading button is disabled too.
addEventListener(...args: Parameters<HTMLButtonElement['addEventListener']>) => voidAdds an event listener to the button element, the same as addEventListener of the element.