# Accessible Modal and Drawer Behaviour

## Objective

Make overlays trap focus, close predictably, and stay usable without a mouse.

One shared overlay behaviour covering focus trapping, background inertness, escape handling, and scroll lock, applied to every modal, drawer, and popover panel in the app.

## 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 overlay in the app — modals, drawers, sheets, command panels, image lightboxes — and route them through one implementation instead of fixing them individually.
2. While an overlay is open, keep Tab and Shift+Tab cycling inside it and make the background inert so assistive technology cannot reach it. Hiding the background visually is not enough; a screen reader will still read straight through it.
3. Give every overlay an accessible name from its visible title, and use dialog for things the user can dismiss versus alertdialog for interruptions that demand a decision.
4. Lock background scrolling while open, compensating for the scrollbar width so the page does not shift sideways when the overlay appears.
5. On close, return focus to whatever opened the overlay. If that trigger no longer exists — the row it lived in was deleted — fall back to a stable ancestor, and record the fallback rather than leaving it to chance.
6. Do NOT let Escape close every open layer at once. It closes the topmost layer only, so a user dismissing a confirmation inside a drawer does not lose the drawer as well.

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

- An overlay with no focusable content still needs to receive focus itself, or the trap has nothing to hold.
- Content added after open — a lazily loaded form, an appended row — must be inside the trap without rebuilding it.
- Nested overlays need a stack: only the top layer is interactive, and closing it restores the trap to the layer beneath.
- On iOS, locking scroll by hiding overflow on the body is not sufficient and lets the page scroll behind the overlay.
- A drawer that pins open at desktop widths is no longer a dialog; it must stop trapping focus at that breakpoint.
- An overlay opened by a keyboard shortcut has no trigger element, so it needs an explicit return target.
- Closing must not depend on the backdrop existing — a full-bleed mobile sheet has no backdrop to click.

## 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 overlay in the app uses the same focus-trap and dismissal implementation.
- [ ] Tab cannot leave an open overlay, and assistive technology cannot reach the background.
- [ ] Escape closes only the topmost layer.
- [ ] Each overlay has an accessible name and the correct dialog or alertdialog role.
- [ ] Opening an overlay does not shift the page horizontally, and background scroll is locked including on iOS.
- [ ] Focus returns to the trigger on close, or to a defined fallback when the trigger is gone.
- [ ] 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.
