# Selection State Pattern

## Objective

Make selected rows, cards, and objects look and behave the same everywhere.

One selection model and one visual treatment shared by tables, card grids, lists, and canvas surfaces.

## 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 surface that already has a notion of selection — table rows, card grids, file lists, canvas objects, option pickers — and reduce them to a single selection model rather than the several that have accumulated.
2. Hold selection as state keyed by stable record identity, never by row index or position in the DOM, so sorting, filtering, and paging cannot move the highlight onto a different record.
3. Support click to select, modifier click to add and remove, and shift click to extend a range, and make each of those reachable from the keyboard as well as the pointer.
4. Always show how many items are selected and give a single control that clears the selection, placed where the user can find it without scrolling back to the top of a long list.
5. Do not express selection with a background tint alone. A row can be hovered, focused, and selected at once, and one tint cannot carry three meanings — pair selection with a persistent marker such as a checkbox or a border, and leave the focus ring itself to the Hover Active Focus Checklist.

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

- Hover, keyboard focus, and selection frequently land on the same row at the same time; all three must stay readable together and none may visually cancel another.
- Range selection needs a defined anchor. The anchor must move on a plain click and be preserved across a shift click, or extending a range a second time selects the wrong span.
- Forced-colours and high contrast modes discard background colours, so selection must also be carried by a border, an outline, or a control that genuinely exists in the accessibility tree.
- Re-sorting, filtering, or paging must keep the same records selected, including records that have scrolled out of view, rather than keeping the same positions selected.
- Select-all needs a stated scope: everything on this page, or everything matching the current filter. Conflating the two ends with a bulk action applied to records the user never saw.
- A selected record deleted by someone else, or moved out of the filter, must drop out of the selection quietly and the count must update.
- Screen readers need the selected state exposed through the control's role rather than implied by colour; a selected row that reads identically to an unselected one is unusable.

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

- [ ] A single selection model backs tables, grids, lists, and canvas surfaces.
- [ ] Selection is keyed to record identity and survives sorting, filtering, and paging.
- [ ] Single, additive, and range selection all work with the pointer and from the keyboard.
- [ ] Hover, focus, and selected states remain distinguishable when applied to the same element.
- [ ] Selection stays visible under forced-colours and high contrast settings.
- [ ] The selected count is visible and can be cleared in one action.
- [ ] Select-all states its scope explicitly before it is applied.
- [ ] 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.
