# Async Button

## Objective

Make every button that talks to the network behave the same way while it waits.

A pending behaviour for buttons that trigger requests, covering submission locking, result feedback, and cancellation.

## Before You Begin

This feature is being added to an application that already exists and already
works. Do not scaffold a new project, and do not assume a blank slate.

Inspect the codebase first and establish:

- The existing application structure and where code of this kind already lives.
- The framework and version in use.
- The existing design system — colours, spacing, typography, and component conventions.
- Existing UI components you can reuse instead of writing new ones.
- The existing database structure, if this feature needs to persist anything.
- The existing authentication and authorization system, if this feature is user-scoped.
- Dependencies already installed, so you don't add a library that duplicates one.
- The existing test setup and conventions.

Only start writing code once you understand the above. If the application
already implements part of this feature, extend it rather than replacing it.

## Implementation Instructions

1. Find every control that fires a request, a job, or an export, and give it a pending state that begins on activation rather than when the response is nearly back.
2. Lock the button against a second activation for the whole life of the request, including double clicks, the enter key held down, and a repeat tap on a laggy mobile connection.
3. Define what happens on each outcome: success, a handled error, a network failure, and a request that outlives a sensible timeout. Silence on any of those reads as a broken button.
4. The visual hierarchy, sizing, and hit area of these buttons belong to Button System; this brief owns only what happens between activation and resolution. Do not redefine the button's appearance here.
5. Do not disable the surrounding form while one button is pending. Locking unrelated fields and the cancel control traps the user inside a request they can no longer escape.

## UI and UX Requirements

Match the application's existing design system exactly. Reuse its components,
spacing, and typography. This feature should look like it was always there.

## Responsive Requirements

Works on mobile, tablet, and desktop. Touch targets are large enough to hit on a
phone, and nothing overflows horizontally at 320px.

## Accessibility Requirements

- Fully keyboard navigable.
- Correct semantic elements and ARIA roles.
- Visible focus states.
- Meets WCAG AA contrast.
- Dynamic changes are announced to screen readers.
- Respects prefers-reduced-motion.

## Edge Cases

- Two activations must produce one request. Guard on the in-flight request itself rather than on a visual disabled attribute, which arrives a frame late and can be bypassed by the keyboard.
- Swapping the label for the word Loading destroys the button's accessible name, so a screen reader user who tabs back to it no longer knows what it does. Keep the original label and announce the busy state separately.
- Success, error, timeout, and cancellation are four distinct endings and each needs its own resolution. A button that simply returns to idle after a failure tells the user the work succeeded.
- Disabling unrelated controls while a request is in flight blocks the user from correcting the very field that caused the error.
- A response that arrives after the user has navigated away must not update a button that no longer exists or resurrect a stale error.
- Work expected to take longer than a few seconds needs progress or a cancel affordance rather than an indefinite spinner.
- A request that fails instantly must still show the pending state long enough to be perceived, or the button appears not to have reacted at all.

## Testing

Exercise the feature end to end in the running application. Cover every edge case
above, then run the existing test suite and confirm nothing regressed.

## Acceptance Criteria

- [ ] Every network-triggered button enters a pending state on activation.
- [ ] Repeated activation during a pending request produces exactly one request.
- [ ] The button's accessible name is unchanged while pending, with the busy state conveyed separately.
- [ ] Success, error, timeout, and cancellation each resolve to a distinct, visible outcome.
- [ ] Controls unrelated to the pending action remain usable throughout.
- [ ] A response arriving after the view has been left causes no error and no stray state update.
- [ ] The feature matches the existing design system.
- [ ] No existing functionality is broken.

## Adaptation Rules

- Match the existing design system. Do not introduce a new colour palette,
  spacing scale, or component library.
- Reuse existing components and utilities wherever they fit.
- Follow the naming, file layout, and code style already present.
- Do not upgrade, replace, or remove existing dependencies to make this
  feature fit. Adapt the feature to the app, not the app to the feature.
- Do not break existing functionality. If a change is genuinely required in
  existing code, make the smallest one that works and say so.
- If something in these instructions conflicts with how the application is
  built, follow the application and explain the deviation.

## Final Verification

Before you report the work as done:

1. Re-read the acceptance criteria above and check each one against what you
   actually built.
2. Run the application and exercise the feature end to end.
3. Run the existing test suite and confirm you have broken nothing.
4. Check the feature on mobile, tablet, and desktop widths.
5. Check keyboard navigation and focus handling.
6. Summarize what changed: files added, files modified, and anything you
   deliberately did differently because of how this application is built.

If any acceptance criterion is unmet, fix it before reporting completion.
