ConsentScript
Injects <script src> or inline code into document.head only while the given consent category is granted, and removes it if consent is withdrawn. Renders nothing. Must be used inside ConsentProvider.
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.
npx @slipstream-ui/cli@latest add consent-scriptanalytics: undecided
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| category | string | required | Purpose id that must be granted; matches a provider purposes entry. |
| src | string | — | External script URL; mutually exclusive with code. |
| code | string | — | Inline script source; mutually exclusive with src. |
| id | string | — | id attribute on the injected element; also dedupes existing scripts. |
| async | boolean | true | async attribute for external scripts. |
| onError | () => void | — | Called when an external script fails to load. |
Anti-patterns
validate_usage is the MCP tool that rejects these mistakes: the same list agents see over MCP and in the canonical markdown.
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.
tsx// Avoid{consent.preferences?.analytics && <script src="..." />}// Prefer<ConsentScript category="analytics" src="..." />Don't pass both src and code
src wins when both are set — the loader ignores code. Pick one contract per script.
tsx// Avoid<ConsentScript src="..." code="..." />
Accessibility
- Renders null — no perceivable UI; accessibility obligations live in the consent banner/dialog.
- Guarantees no tracker loads before consent and unloads on withdrawal.
Composition
Compose with ConsentProvider: Must be inside the provider; reads the shared decision via useConsentContext
<ConsentProvider purposes={['analytics']}>...<ConsentScript .../></ConsentProvider>
Compose with ConsentBanner: The banner's accept/decline decisions gate whether scripts inject
<ConsentBanner onAccept={() => consent.setAll(true)} onDecline={() => consent.setAll(false)} />
Related components: ConsentProvider, ConsentBanner, ConsentPreferencesDialog
Dependencies
What the registry installs alongside this component. Package imports already include these.
npm packages
- class-variance-authority
- clsx
- tailwind-merge
Browser support
| Browser | Tested versions |
|---|---|
| Chrome | 111+ (Mar 2023) |
| Edge | 111+ (Mar 2023) |
| Firefox | 121+ (Dec 2023) |
| Safari | 17.2+: macOS & iOS (Dec 2023) |
Slipstream components rely only on CSS custom properties, Flexbox/Grid, and ARIA, the everyday baseline of the last two years of evergreen releases. Anything newer is progressive enhancement: unsupported browsers keep full functionality with static behavior instead of animations.