# ConsentScript

Consent-gated script loader: injects an external <script src> or inline code into document.head only while the given category is granted via ConsentProvider, and removes it again if consent is withdrawn. Renders nothing.

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

## Design intent

Analytics and marketing tags must never load before consent — and should unload when it is withdrawn. ConsentScript owns that lifecycle (inject on grant, remove on revoke, dedupe by id) so consumers don't hand-roll effect timing against the consent state.

## Props

| Prop | Type | Default | Required | Description |
|---|---|---|---|---|
| category | `string` | — | yes | Purpose/category id that must be granted; must match a purposes entry on ConsentProvider. |
| src | `string` | — | no | External script URL. Mutually exclusive with code. |
| code | `string` | — | no | Inline script source. Mutually exclusive with src. |
| id | `string` | — | no | id attribute on the injected element; also dedupes — an existing element with this id is left alone. |
| async | `boolean` | `true` | no | async attribute for external scripts. |
| onError | `() => void` | — | no | Called when an external script fails to load. |

## Examples

### Gate an analytics tag

Nothing loads until the visitor grants analytics; revoking consent removes the element.

```tsx
<ConsentProvider purposes={['analytics', 'marketing']}>
  <ConsentScript
    category="analytics"
    src="https://example.com/analytics.js"
    id="analytics"
  />
</ConsentProvider>
```

### Inline bootstrap snippet

code is injected as the script's textContent for small inline snippets.

```tsx
<ConsentScript
  category="marketing"
  id="marketing-boot"
  code="window.dataLayer = window.dataLayer || [];"
/>
```

## Anti-patterns

- **Don't render the script tag yourself** — A plain <script> in JSX fires regardless of consent state (and SSR markup can't be gated client-side). ConsentScript is the only safe path inside the provider.
- **Don't pass both src and code** — src wins when both are set — the loader ignores code. Pick one contract per script.

## Accessibility

- **No UI**: Renders null; the injected script is not perceivable content

## Composition

- Compose with **ConsentProvider** — Must be inside the provider; reads the shared decision via useConsentContext
  
  ```tsx
  <ConsentProvider purposes={['analytics']}>...<ConsentScript .../></ConsentProvider>
  ```
- Compose with **ConsentBanner** — The banner's accept/decline decisions gate whether scripts inject
  
  ```tsx
  <ConsentBanner onAccept={() => consent.setAll(true)} onDecline={() => consent.setAll(false)} />
  ```
- Related: ConsentProvider, ConsentBanner, ConsentPreferencesDialog

