# Infinite Scroll with Load More

## Objective

Load a long feed progressively without trapping people in an endless page.

One growing list that appends the next batch as the user nears the end, driven by a sentinel and a visible load-more button.

## 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. Append to a single accumulating list rather than replacing it. This is for feeds people graze. Lists people navigate deliberately, or need to link to a specific position in, want numbered pages instead.
2. Place a sentinel element after the last item and load the next batch when it comes into view, with a generous margin so the content arrives before the user reaches it. Always render a visible load-more button alongside it.
3. Move focus to the first newly appended item when the user loads more by button, so keyboard users are not thrown back to the top of the list.
4. Track loaded batches so the same record is never appended twice, and dedupe by ID on append rather than trusting batch boundaries.
5. Cancel in-flight requests when the filter, sort, or search term changes, and clear the accumulated list before loading the new one.
6. Show a clear end-of-results state. A feed that simply stops loading is indistinguishable from one that is broken.
7. Do NOT make the page footer unreachable. If there are links or legal text below the list, either move them or keep the feed inside its own scrolling region.

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

- Navigating to a detail view and back must restore both the loaded range and the scroll position, or the user loses everything they scrolled past.
- A failed page load needs a visible retry in place, not a silent stop that looks like the end of the list.
- Keyboard and screen reader users need the load-more button as the primary path. An observer that only fires on scroll never fires for them.
- Announce the count of newly appended items politely. Do not read the whole batch out, and do not leave the append silent.
- The sentinel must not sit inside a container it can never intersect, and must be disconnected once results are exhausted or it will fire forever.
- Very long sessions accumulate DOM nodes until the page stutters. Virtualize, or cap the list and offer a jump back to the top.
- Items deleted from earlier pages while the user scrolls must not leave gaps or cause the next page to skip records.
- Restoring position must wait until the restored items have rendered, or the browser will scroll to the wrong place.

## 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 manual load-more control is always present and works without scrolling.
- [ ] Loading more appends to the existing list and moves focus to the first new item when triggered by the button.
- [ ] No record appears twice, including when data changes between requests.
- [ ] Changing filters or search cancels in-flight requests and resets the list.
- [ ] The end of results is explicitly stated.
- [ ] Back navigation restores the loaded range and scroll position.
- [ ] Failed page loads show an inline retry.
- [ ] Footer content remains reachable.
- [ ] 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.
