# ConsentProvider

React context provider that shares a single useConsent decision across the tree so ConsentBanner, ConsentPreferencesDialog, ConsentScript and consumer code all read the same stored choice. Accepts every useConsent option including version (re-prompt on policy change) and a pluggable storage adapter.

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

## Design intent

Cookie consent needs one source of truth: the banner writes a decision, gated scripts read it, and a preferences dialog edits it. ConsentProvider holds that decision in context so components never duplicate storage keys or drift apart; standalone useConsent remains available for provider-free setups.

## Props

| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
| children | `ReactNode` | — | no | Application tree that consumes the consent context. |
| storageKey | `string` | `'slipstream-consent'` | no | Storage key for the persisted decision. |
| purposes | `readonly string[]` | `['analytics']` | no | Consent purposes persisted as booleans, e.g. ['necessary', 'analytics', 'marketing']. |
| maxAgeMs | `number` | `180 days in ms` | no | How long a stored decision stays valid before the visitor is re-prompted. |
| version | `string` | — | no | Consent-policy version; a stored decision written under a different version is treated as undecided. |
| storage | `ConsentStorage` | — | no | Pluggable persistence adapter (getItem/setItem/removeItem); defaults to window.localStorage. |

## Examples

### Wrap the app once

One provider near the root; every consent-aware component reads the same decision.

```tsx
<ConsentProvider
  purposes={['necessary', 'analytics', 'marketing']}
  version="2026-12"
>
  <App />
</ConsentProvider>
```

### Read the decision anywhere

useConsentContext returns the same Consent object the provider created.

```tsx
const consent = useConsentContext();

{consent.preferences?.analytics && <AnalyticsTracker />}
{!consent.decided && <ConsentBanner onAccept={...} onDecline={...} />}
<ConsentScript category="analytics" src="/analytics.js" />
```

## Anti-patterns

- **Don't nest providers with different keys** — Components read the nearest provider. Two providers with different storageKeys or purposes produce inconsistent consent state; use one provider per storage key.
- **Don't call useConsentContext outside the provider** — The hook throws a descriptive error outside ConsentProvider. For provider-free usage, call the standalone useConsent hook instead.

## Accessibility

- **No UI of its own**: Renders children only; all accessibility lives in the consuming components

## Composition

- Compose with **ConsentBanner** — Presentational banner wired to the provider's decision via useConsentContext or manual useConsent
  
  ```tsx
  const consent = useConsentContext(); <ConsentBanner onAccept={() => consent.setAll(true)} ... />
  ```
- Compose with **ConsentPreferencesDialog** — Edits per-category preferences through consent.set
  
  ```tsx
  <ConsentPreferencesDialog open={open} onOpenChange={setOpen} categories={categories} />
  ```
- Compose with **ConsentScript** — Injects scripts only while the matching category is granted
  
  ```tsx
  <ConsentScript category="analytics" src="https://example.com/a.js" />
  ```
- Related: ConsentBanner, ConsentPreferencesDialog, ConsentScript

