# ConsentPreferencesDialog

Granular cookie-preferences dialog: one labelled Switch per category, saved through the nearest ConsentProvider via consent.set. Required categories render checked and disabled. Open it from a footer 'Cookie settings' link or the banner's manage action.

> Component ID: `consent-preferences-dialog`
> Import: `import { ConsentPreferencesDialog } from '@slipstream-ui/core'`
> Docs: https://slipstreamui.com/docs/components/consent-preferences-dialog
> Registry: https://slipstreamui.com/registry/consent-preferences-dialog.json
> Tier: organism · Package: @slipstream-ui/core@0.17.0
> Manifest schema: v1 · manifest: v1

## Design intent

GDPR-style consent requires letting visitors adjust each category after an initial choice. The dialog owns the per-category editing UX (draft state re-seeded on open, required categories locked) while ConsentProvider owns persistence, so reopening always reflects the stored decision.

## Props

| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
| open | `boolean` | — | yes | Whether the dialog is visible. |
| onOpenChange | `(open: boolean) => void` | — | yes | Called when the dialog requests open/close (Escape, backdrop, Cancel, Save). |
| categories | `readonly ConsentCategory[]` | — | yes | Consent categories shown as toggles; each id must match a purposes entry on ConsentProvider. |
| title | `string` | `'Cookie preferences'` | no | Dialog heading. |
| description | `string` | `Explains the choices and that necessary cookies are always on.` | no | Dialog description under the title. |
| saveLabel | `ReactNode` | `'Save preferences'` | no | Save button label. |

## Examples

### Categories matching the provider purposes

Category ids must match the provider's purposes or the toggle writes a purpose the store ignores.

```tsx
const categories = [
  { id: 'necessary', label: 'Necessary', required: true },
  { id: 'analytics', label: 'Analytics', description: 'Usage statistics.' },
  { id: 'marketing', label: 'Marketing', description: 'Personalised ads.' },
];

<ConsentProvider purposes={['necessary', 'analytics', 'marketing']}>
  <ConsentPreferencesDialog open={open} onOpenChange={setOpen} categories={categories} />
</ConsentProvider>
```

### Re-open from a footer link

Standard pattern: a persistent footer link re-opens preferences after the initial decision.

```tsx
<button type="button" onClick={() => setPrefsOpen(true)}>
  Cookie settings
</button>
```

## Anti-patterns

- **Don't use category ids the provider doesn't know** — consent.set only persists purposes declared on ConsentProvider; unknown ids are dropped silently. Keep categories and purposes in sync.
- **Don't make every category required** — Required categories render checked and disabled — marking optional tracking as required is a dark pattern and likely unlawful under GDPR/ePrivacy.

## Accessibility

- **Dialog semantics**: Built on Dialog: role=dialog, labelled by title/description, focus trapped, Escape and backdrop close
- **Switch labelling**: Each Switch is aria-labelledby its category label; required switches are disabled
- **Draft isolation**: Changes stay local until Save; Cancel discards them, so visitors can explore without committing

## Composition

- Compose with **ConsentProvider** — Must be inside the provider; reads and writes the shared decision via useConsentContext
  
  ```tsx
  <ConsentProvider purposes={PURPOSE_IDS}>...<ConsentPreferencesDialog .../></ConsentProvider>
  ```
- Compose with **ConsentBanner** — The banner's manage action typically opens this dialog
  
  ```tsx
  <ConsentBanner onManage={() => setPrefsOpen(true)} ... />
  ```
- Compose with **Switch** — Renders each category toggle with role=switch and aria-checked
  
  ```tsx
  <Switch checked={draft[id]} onCheckedChange={...} aria-labelledby={...} />
  ```
- Related: ConsentProvider, ConsentBanner, Dialog, Switch

