# Accessible Focus Management

## Objective

Put keyboard and screen-reader users in the right place after the interface changes.

A deliberate policy for where focus goes whenever the page changes underneath the user — navigation, validation, deletion, insertion, and async updates.

## 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. Inventory every moment the interface changes without a full page load: client-side route changes, form submissions, rows being deleted, content being appended, filters re-rendering a list.
2. Decide for each one whether focus should move, and write it down. On a route change, move focus to the new page heading or main region so a screen reader starts reading the new page instead of staying on a link that no longer exists.
3. When validation fails, move focus to the first invalid field and make sure its error message is associated with it, so the user hears the problem rather than hunting for it.
4. When an element that had focus is removed, move focus to a stable neighbour — the next row, the list container, or the control that triggered the removal. Never let focus fall back to the document body; the user loses their position entirely.
5. Announce background changes (saved, imported, ten new results) through a polite live region, and route all such announcements through one shared mechanism rather than scattering live regions through the markup.
6. Do NOT move focus for updates the user did not initiate — a poll result, an incoming notification, an autosave. Stealing focus mid-sentence destroys typed input and is far worse than silence.

## 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

- Focus must never be sent to an element that is hidden, disabled, or already detached from the document.
- An element that only exists to receive focus needs tabindex="-1", not tabindex="0" — it should be programmatically focusable but stay out of the tab order.
- Focus moves must happen after the new content is actually in the DOM, not in the same tick as the state change.
- An assertive live region interrupts whatever is being read; reserve it for errors and use polite everywhere else.
- Rapid successive updates must be coalesced before announcing, or the user hears a stream of half-read messages.
- Deleting the last row of a list leaves nothing to focus — send focus to the empty state and make sure the empty state is focusable.
- Scrolling caused by a focus move must not jump the viewport unexpectedly; keep the target in view without an animated scroll when prefers-reduced-motion is set.

## 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 interface change that removes or replaces the focused element has a defined focus destination.
- [ ] A client-side route change moves focus into the new page content.
- [ ] A failed validation moves focus to the first invalid field with its error associated.
- [ ] Deleting an item leaves focus on a sensible neighbour, never on the body.
- [ ] Background updates are announced through a shared polite live region and never steal focus.
- [ ] Focus is never moved to a hidden, disabled, or removed element.
- [ ] 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.
