Run an action, show that it's working, and keep it reachable when disabled.
Button goes on a native button and adds what it doesn't do on its own: it stays pending while an async action runs, so a form can't submit twice, announces slow actions to screen readers, can stay focusable while disabled, and keeps the same behavior on a link. For a plain (click) handler, a native button is enough.
import { Component } from "@angular/core"
import { useLog } from "./log"
import { Button } from "@ui-sdk/angular/button"
@Component({
imports: [Button],
template: ` <button uiButton (click)="log('Pressed')">Button</button> `,
})
class Example {
readonly log = useLog()
}
export { Example }Installation
npm install @ui-sdk/angular @ui-sdk/coreUsage
Put uiButton on a <button>, and listen to (action) to run an action. The button gets type="button" unless you set one.
import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
@Component({
selector: "app-save",
imports: [Button],
template: `<button uiButton (action)="save()">Save</button>`,
})
export class Save {
save() {}
}Pending
Pass a Promise to $event.waitUntil() to keep the button pending until it settles. The button keeps focus and ignores further presses while it waits.
<button
uiButton
(action)="$event.waitUntil(save())"
pendingAnnouncement="Saving"
>
Save
</button>After one second, it announces "Loading" to screen readers. Set pendingAnnouncement to change that message, as above, or use an empty string to silence it.
import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
@Component({
imports: [Button],
template: `
<button
uiButton
(action)="$event.waitUntil(wait(1500))"
pendingAnnouncement="Saving"
>
Save
</button>
`,
})
class Example {
wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
}
export { Example }Pending Content
To change what the button shows while it waits, mark content with data-show="idle" or data-show="pending" and hide it in the other state with one CSS rule each, or use Tailwind's in-data-pending variant:
[data-slot="button"]:not([data-pending]) [data-show="pending"],
[data-slot="button"][data-pending] [data-show="idle"] {
display: none;
}import { Component } from "@angular/core"
import { LucideArrowDownToLine, LucideLoaderCircle } from "@lucide/angular"
import { Button } from "@ui-sdk/angular/button"
@Component({
imports: [Button, LucideArrowDownToLine, LucideLoaderCircle],
template: `
<button
uiButton
(action)="$event.waitUntil(wait(1500))"
pendingAnnouncement="Saving"
>
<svg lucideArrowDownToLine data-show="idle"></svg>
<svg lucideLoaderCircle data-show="pending" data-slot="spinner"></svg>
Save
</button>
`,
})
class Example {
wait(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
}
export { Example }Keep the label and change only the visual, as above. If the text has to change too, swap it the same way, without an aria-label. Pending Buttons explains why:
<button uiButton (action)="$event.waitUntil(save())">
<span data-show="idle">Save</span>
<span data-show="pending">Saving…</span>
</button>Handling Errors
If the Promise passed to waitUntil() rejects, the button's state().error holds the error. Use it to show a message or a retry label. The next press clears the error and runs the handler again.
The demo reads the state through #button="uiButton" to change the label.
import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
import { useLog } from "./log"
@Component({
imports: [Button],
template: `
<div data-slot="stack">
<button uiButton #button="uiButton" (action)="$event.waitUntil(save())">
{{
button.state().pending
? "Saving…"
: button.state().error
? "Try Again"
: "Save Changes"
}}
</button>
</div>
`,
})
class Example {
readonly log = useLog()
async save() {
await new Promise((resolve) => setTimeout(resolve, 1000))
this.log("Couldn't save. Check your connection and try again.")
throw new Error("Couldn't save")
}
}
export { Example }Changes After an Await
(action) emits the press with its source, which says how the button was pressed. If the handler changes another ui-sdk component after an await, wrap that change in withSource to keep the original source. Otherwise, the later change is reported as "programmatic".
Here save() is your request and status is an Input's controller:
import { Component } from "@angular/core"
import { Button, type ButtonAction } from "@ui-sdk/angular/button"
import { createInputController } from "@ui-sdk/angular/input"
import { withSource } from "@ui-sdk/angular/observe"
const save = () => fetch("/api/save", { method: "POST" })
@Component({
selector: "app-save",
imports: [Button],
template: `
<button uiButton (action)="$event.waitUntil(onSave($event))">Save</button>
`,
})
export class Save {
readonly status = createInputController()
async onSave({ source }: ButtonAction) {
await save()
withSource(source, () => this.status.setValue("Saved"))
}
}Observe explains source tracking. Preserving "agent" lets undoSource("agent") include that later change.
Disabled
Set disabled to prevent presses and remove the button from the tab order. A disabled <fieldset> around the button disables it too.
<button uiButton disabled>Disabled</button>import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
@Component({
imports: [Button],
template: ` <button uiButton disabled>Disabled</button> `,
})
class Example {}
export { Example }Focusable When Disabled
Add focusableWhenDisabled when people should still be able to focus a disabled button. It uses aria-disabled instead of the native disabled attribute. Presses still do nothing.
Use aria-describedby to explain why the action is unavailable:
<button uiButton disabled focusableWhenDisabled aria-describedby="publish-hint">
Publish
</button>
<p id="publish-hint">Add a title to publish.</p>import { Component, computed, signal } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import { Input } from "@ui-sdk/angular/input"
import { useLog } from "./log"
@Component({
imports: [Button, Field, FieldLabel, Input],
template: `
<div data-slot="stack">
<div uiField>
<label uiFieldLabel>Title</label>
<input uiInput [(value)]="title" />
</div>
<div data-slot="row">
<button
uiButton
[disabled]="!ready()"
focusableWhenDisabled
aria-describedby="publish-hint"
(action)="log('Published “' + title() + '”')"
>
Publish
</button>
<p id="publish-hint" data-slot="hint">
{{ ready() ? "Ready to publish." : "Add a title to publish." }}
</p>
</div>
</div>
`,
})
class Example {
readonly log = useLog()
readonly title = signal("")
readonly ready = computed(() => this.title().trim() !== "")
}
export { Example }Links
Put uiButton on an <a>. It keeps its href and link semantics, including opening in a new tab. A disabled link cannot navigate and leaves the tab order.
<a uiButton href="/docs/angular">Get started</a>import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
@Component({
imports: [Button],
template: `
<div data-slot="row">
<a uiButton href="#installation">Get Started</a>
<a uiButton disabled href="#changelog">Changelog</a>
</div>
`,
})
class Example {}
export { Example }For your own components, see Parts and Composition.
Forms
Set type="submit" to submit a form or type="reset" to restore its default values. The default, type="button", does neither.
<button uiButton type="submit">Subscribe</button>
<button uiButton type="reset">Reset</button>import { Component } from "@angular/core"
import { Button } from "@ui-sdk/angular/button"
import { Field, FieldLabel } from "@ui-sdk/angular/field"
import { Input } from "@ui-sdk/angular/input"
import { useLog } from "./log"
@Component({
imports: [Button, Field, FieldLabel, Input],
template: `
<form data-slot="stack" (submit)="onSubmit($event)">
<div uiField>
<label uiFieldLabel>Email</label>
<input
uiInput
name="email"
type="email"
defaultValue="ada@example.com"
/>
</div>
<div data-slot="row">
<button uiButton type="submit">Subscribe</button>
<button uiButton type="reset">Reset</button>
</div>
</form>
`,
})
class Example {
readonly log = useLog()
onSubmit(event: SubmitEvent) {
event.preventDefault()
const email = new FormData(event.currentTarget as HTMLFormElement).get(
"email"
)
this.log(`Subscribed ${email}`)
}
}
export { Example }Button tracks the Promise passed to its own waitUntil(), not one from the form’s submit handler. For an async submit handler, keep your own pending flag and set disabled on the button while it runs, with focusableWhenDisabled so the button keeps focus while it waits.
Styling
Use data-pending to show a spinner or waiting cursor, and data-disabled to style an unavailable action. The button's data-slot is button, and its attributes are listed under Data Attributes.
Styling and Animation covers the styling patterns shared by all components.
Data Attributes
Button
[data-slot="button"]
| Attribute | What it means |
|---|---|
data-pending | Present while the Promise its press handler returned is running. |
data-disabled | Present when disabled. |
API Reference
Each option below is an input on uiButton. Everything else stays on your element. #button="uiButton" gives the template the button's state(); see Parts and Composition.
Button
disabled?boolean- Stops presses. The button is natively disabled, so it leaves the tab order and its form doesn't submit through it, unless
focusableWhenDisabledis set. focusableWhenDisabled?boolean- Keeps a disabled button in the tab order with
aria-disabledinstead of the native attribute, so it can hold focus while it becomes disabled and host a tooltip that says why. Presses still do nothing. type?"button" | "reset" | "submit"- What a native button does in a form:
"submit"submits it and"reset"resets it. Ignored when the button renders as something else. pendingAnnouncement?string- What screen readers hear once the button has been pending for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
debug?boolean- Logs every state change with its reason to the console, in development builds.
State
disabledboolean- Whether it's disabled, by the
disabledoption or a disabled fieldset around it. A running press shows inpendinginstead. pendingboolean- Whether the Promise the last press returned is still running. Presses do nothing meanwhile.
errorunknown- Why the last press failed: what its handler threw, or its Promise rejected with. Cleared by the next press.
Events
| Output | Receives |
|---|---|
(action) | The press: its event, its source, and waitUntil(), which keeps the button pending until the Promise it's given settles. |
Accessibility
Give the button a clear text label. If it contains only an icon, add an aria-label. Native buttons respond to Enter and Space; links respond to Enter.
Pending Buttons
While its action runs, the button tells assistive technology it's busy, so you don't need to add anything:
- It sets
aria-busy="true"andaria-disabled="true", and ignores presses until the action settles. It keeps focus the whole time. - After a second, it announces
pendingAnnouncement("Loading" by default) through a polite live region. Quick actions stay silent. This is a status message: people hear it without focus moving.
To show progress, keep the label and change only the visual, like a spinner. Changing the text renames the button while it has focus: screen readers don't reliably announce a new name on the focused element, so the change mostly helps sighted users, and the button's width shifts as the text changes length. The announcement already tells everyone it's working.
<button
uiButton
(action)="$event.waitUntil(save())"
pendingAnnouncement="Saving"
>
<svg lucideArrowDownToLine data-show="idle"></svg>
<svg lucideLoaderCircle data-show="pending"></svg>
Save
</button><button uiButton (click)="save()" aria-label="Save">
{{ saving() ? "Saving…" : "Save" }}
<span aria-live="polite">{{ saving() ? "Saving" : "" }}</span>
</button>The second button tracks saving itself, shows text its accessible name doesn't contain, and announces the same state twice. If the text has to change anyway, swap it with data-show and leave out aria-label, so the button's name always contains the text on screen. See Pending Content for the markup.
References:
Tested Behavior
The following behaviors are covered by automated tests:
- Clicking a button runs its handler.
- Space presses a focused button.
- Enter presses a focused button.
- A submit button submits its form.
- A reset button resets its form.
- A disabled button can't be focused or pressed.
- A button rendered as a link stays a link.
- It stays focusable and says it's disabled.
- Tab reaches it.
- Clicking it runs nothing and submits nothing.
- Space on it runs nothing and submits nothing.
- Enter on it runs nothing and submits nothing.
- A returned Promise makes it busy, and it keeps focus until the Promise settles.
- While pending, it says it's unavailable with aria-disabled and keeps focus.
- Still pending after a second, it tells screen readers it's loading.
- A rejected Promise ends the pending state.
Runs in React, Vue and web components.