# Image Carousel

## Objective

Let customers page through a set of images with arrows, dots, and swipe.

A horizontally paged row of images with previous and next controls, position indicators, optional autoplay, and touch swiping.

## 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. Reserve the slide dimensions before any image loads, using the app's existing aspect-ratio handling if it has one, so the page does not jump as each image arrives.
2. If the app already has an image lightbox, wire the carousel into it rather than building a second full-screen viewer. Tapping a slide should open the existing lightbox at the matching index.
3. Make the carousel operable without a pointer: arrow keys move between slides, the dots are real focusable controls, and the current position is announced rather than conveyed only by a filled dot.
4. Treat autoplay as an interruption the user is allowed to stop. Pause it on hover, on focus anywhere inside the carousel, when the tab is hidden, and permanently when reduced motion is preferred, and provide a visible pause control.
5. Do not load every slide upfront. Load the visible slide and the next one, defer the rest, and require alt text on each image rather than shipping a row of unlabelled pictures.

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

- Autoplay that keeps moving while someone is reading or interacting is hostile. Pause on hover, on keyboard focus, and when a reduced-motion preference is set, and do not resume automatically after a manual interaction.
- Keyboard navigation and swiping must drive the same index and the same state. If arrow keys move a separate pointer from the touch gesture, the dots and the visible slide will disagree.
- A carousel of one image should not show arrows or dots at all. Detect the single-slide case and render a plain image, and handle the zero-slide case with the app's empty state rather than an empty frame.
- Wrapping from the last slide back to the first must not yank the page scroll position or animate through every slide in between. Move directly and keep the surrounding page still.
- Loading every slide on first paint wastes bandwidth on images most people never see. Defer offscreen slides but preload the immediate next one so paging forward is not a blank frame.
- Images that fail to load must leave the slide sized and labelled rather than collapsing the carousel height, and the alt text must still be available.
- Slides of differing aspect ratios will make the carousel resize on every page. Fix the frame and fit images inside it consistently.
- A horizontal swipe must not swallow vertical page scrolling. Only capture the gesture once horizontal intent is clear.

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

- [ ] Slides are sized before images load and the page does not shift.
- [ ] Arrows, dots, keyboard, and swipe all move the same index consistently.
- [ ] Autoplay pauses on hover, focus, and hidden tabs, and is disabled under reduced motion.
- [ ] A single-slide carousel hides its arrows and dots.
- [ ] Offscreen slides are deferred while the next slide is preloaded.
- [ ] Every image has alt text and the current position is announced accessibly.
- [ ] Tapping a slide opens the app's existing lightbox rather than a second viewer.
- [ ] 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.
