# Windowed Grid Primitive

## Objective

Keep very large tables and grids responsive by rendering only the cells currently on screen.

A two-dimensional windowing layer that renders visible rows and columns while preserving normal table semantics and navigation.

## 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. Identify the surfaces that genuinely need windowing — thousands of rows, or wide column sets rendered at once — and leave the rest alone. A list of forty rows is slower to window than to render.
2. Support rows and columns of differing sizes by estimating first and correcting once measured, and cache the measurements so scrolling back does not re-measure and jump.
3. Keep keyboard focus stable when the focused cell scrolls out of the window. Either keep that cell mounted or move focus deliberately to a known position; never let it fall back to the document body.
4. Restore scroll position by the identity of the item that was at the top, not by a pixel offset, because a pixel offset points somewhere else as soon as sizes or filters change.
5. Publish the true row and column counts and each cell's real position to assistive technology, so the grid announces a cell as one of ninety thousand rather than one of the forty currently rendered. Memoisation of the cell renderer belongs to the Render Stability Guardrails brief; this brief owns which cells exist, not how cheaply each one re-renders.

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

- Variable row heights and column widths must be handled, not assumed away. A grid built on a fixed row height will misplace every item below the first row that wraps to two lines.
- Focus must survive scrolling and data updates. A user tabbing through cells who is thrown to the top of the document has lost their place and, with a screen reader, their context entirely.
- Scroll restoration has to land on the same record after a reload or a return from a detail view. Restoring to a stored pixel offset after the data changed puts the user in an arbitrary part of the grid.
- Assistive technology must be given the grid's real structure. Without explicit counts and positions it reports only the rendered window, which makes the grid unnavigable.
- Find-in-page, select-all, and copy only reach rendered cells. Provide an explicit export or copy path for the full dataset and say so, rather than letting users think they copied everything.
- Resizing the window, resizing a column, or toggling density mid-scroll invalidates cached measurements and must trigger a recalculation that keeps the current top item in place.
- Sticky headers and frozen columns must share the same scroll arithmetic as the body, or they drift apart by a row at speed.

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

- [ ] Only cells within and just beyond the viewport are rendered, at any scroll position.
- [ ] Rows and columns of differing sizes render in the correct positions with no drift or jumping.
- [ ] Keyboard focus remains on a known cell through scrolling, filtering, and data updates.
- [ ] Scroll position restores to the same record after a reload or a return to the view.
- [ ] Assistive technology reports the full row and column counts and each cell's true position.
- [ ] Sticky headers and frozen columns stay aligned with the body during fast scrolling.
- [ ] There is an explicit path to copy or export the full dataset rather than only the rendered window.
- [ ] 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.
