# Sticky Table Header

## Objective

Keep column labels on screen so people know what they are reading halfway down.

Column headers that remain visible while the table body scrolls, with the header and body staying in alignment.

## 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 the tables long enough for the header to leave the viewport and pin those headers only; a table that fits on one screen gains nothing and pays the cost.
2. Keep the header in the same table structure as the body so column widths are resolved once and the two cannot drift apart. Splitting the header into a separate element forces you to measure and copy widths, which fails on every resize and every content change.
3. Establish which scroll container owns the stickiness — the page, or the table's own overflow region — and pin against that one, accounting for any header, toolbar, or filter bar already occupying the top edge.
4. Recompute nothing on scroll. Alignment must hold structurally, so that resizing a column, toggling density, or loading another page of rows keeps the header and body in step without a measurement pass.
5. Do not let the pinned header cover the element that has just received focus. Scrolling a row into view must offset by the header's height, and cell rendering itself belongs to Data Table Cell Primitives rather than being reworked here.

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

- Header and body widths must stay synchronized as content changes; any approach that measures the body and applies the result to a detached header will fall out of alignment on the first column resize, font load, or long value.
- Nested scroll containers defeat sticky positioning silently — an ancestor with overflow hidden or auto, or a transform creating a containing block, will pin the header to the wrong thing or to nothing at all. Identify the real scroll parent before choosing an offset.
- Keyboard navigation and in-page search scroll a target to the top of the container, directly under the pinned header. Add scroll padding equal to the header height so the focused cell lands below it.
- On narrow screens where the table stacks into cards or rows, the pinned header has nothing to label and must be removed rather than left floating above a stacked layout with its own repeated labels.
- A frozen first column and a pinned header intersect at the top-left cell, which needs its own stacking treatment or it will slide under one of them.
- The pinned header must sit below modals, dropdowns, and the app's own sticky chrome in the stacking order, not above them.
- On short viewports a tall header plus surrounding chrome can leave only a few rows visible; cap the pinned height or fall back to a non-sticky header.

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

- [ ] Column labels remain visible at every scroll position in long tables.
- [ ] Header and body columns stay aligned through resizes, density changes, and newly loaded rows, with no measurement on scroll.
- [ ] The header pins against the correct scroll container and clears any existing sticky chrome.
- [ ] A cell reached by keyboard or by in-page search is never hidden behind the header.
- [ ] Stacked mobile layouts render without an orphaned pinned header.
- [ ] A frozen first column and the pinned header do not overlap at their intersection.
- [ ] Modals, menus, and toasts render above the pinned header.
- [ ] 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.
