# Popup Builder

## Objective

Design an on-site popup and control who sees it, when it appears, and how often.

An authoring tool for on-site popups covering content, targeting rules, trigger conditions and frequency, plus the runtime that displays them.

## 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. Render popups through the app's existing dialog behaviour rather than a new overlay. If Accessible Modal and Drawer Behaviour or Focus Containment Primitive exists, the popup is built on it and inherits focus trapping, escape handling and scroll locking for free.
2. Keep the content model to a few block types the app can actually render: heading, body text, an image, a button, and an email capture field. A free-form layout editor inside a popup is a page builder in disguise and should be deferred.
3. Make targeting explicit and default to nobody. An author chooses audience, pages and trigger before the popup can be published, so an unfinished draft never reaches a customer.
4. Give the author a preview that runs the real trigger conditions, and a way to see the popup on their own session without publishing it to everyone.
5. Do not treat frequency as a per-session detail. Dismissal and conversion must be remembered across sessions for the configured period, because a popup that returns on every visit is the most common reason people leave a site.

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

- Two popups qualifying at the same moment must not both appear. Queue by author-set priority, show one, and decide explicitly whether the second appears later in the session or not at all.
- A dismissal must survive the session. Store it per visitor for the period the author configured, and reset it deliberately when the popup's content changes rather than resurrecting an old message.
- On a small screen a popup can cover the whole page with a close control off the edge. Suppress on viewports below a threshold, or use a layout whose dismiss control is always reachable.
- Keyboard users must be able to reach the popup and leave it. Focus moves into the popup, stays inside it while open, escape closes it, and focus returns to where it was.
- A popup that appears before the page settles shifts content under the reader. Delay until after first paint, and overlay rather than inserting into the document flow.
- Anything the popup collects is personal data. Route email capture through the app's existing consent and storage rules, and never store an address the visitor did not submit.
- Popup content is authored by customers and shown to the public. Sanitize what authors write and keep the popup unable to run script or embed a third party the site does not control.
- If a popup embeds anything external, it must render sensibly when that third party is blocked or down, rather than showing an empty box the visitor cannot dismiss.

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

- [ ] Authors can build a popup, set audience, pages, trigger and frequency, and publish it without a deploy.
- [ ] The popup is built on the app's existing dialog behaviour and traps focus, closes on escape, and restores focus.
- [ ] Only one popup shows at a time, chosen by priority.
- [ ] Dismissals persist across sessions for the configured period and reset when the content changes.
- [ ] Popups appear after first paint and never shift page content.
- [ ] Small viewports either suppress the popup or always present a reachable close control.
- [ ] An unpublished or untargeted popup reaches no visitors.
- [ ] 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.
