50 lines
2.4 KiB
TypeScript
50 lines
2.4 KiB
TypeScript
import type { ConsentCategory } from './categories.js';
|
|
type ConsentScriptProps = {
|
|
/** Default true. */
|
|
async?: boolean;
|
|
/** Extra attributes on the <script> (e.g. { 'data-domain': 'x.com' }). */
|
|
attributes?: Record<string, string>;
|
|
/**
|
|
* Consent category that must be granted before the script loads. Typically
|
|
* 'analytics' (GA, Plausible) or 'marketing' (Meta Pixel, ads, chat widgets).
|
|
*/
|
|
category: ConsentCategory;
|
|
/**
|
|
* Inline JS to run once granted. If `src` is also set, it runs after the
|
|
* external script loads (so init code can rely on the library). A plain
|
|
* string of code — NOT user input (you control it), it goes into a <script>.
|
|
*/
|
|
code?: string;
|
|
defer?: boolean;
|
|
/** Stable id used to dedupe — the script is injected at most once. */
|
|
id?: string;
|
|
/** External script URL. Injected only once the category is granted. */
|
|
src?: string;
|
|
};
|
|
/**
|
|
* Loads a third-party script ONLY after the visitor has granted consent for its
|
|
* category — the GDPR-correct default for anything that isn't strictly
|
|
* necessary. Renders nothing until then. Reacts live to the consent context, so
|
|
* a script appears the moment the visitor accepts in the banner, with no reload.
|
|
*
|
|
* Use this for third parties that have NO Google Consent Mode of their own (Meta
|
|
* Pixel, LinkedIn Insight, chat widgets, custom analytics). GA/GTM are better
|
|
* handled by Consent Mode (the module's updateConsent already does that) — reach
|
|
* for ConsentScript when a vendor can't be gated that way.
|
|
*
|
|
* Must be rendered inside <ConsentProvider>.
|
|
*
|
|
* <ConsentScript category="marketing" src="https://connect.facebook.net/en_US/fbevents.js" />
|
|
* <ConsentScript category="analytics"
|
|
* src="https://plausible.io/js/script.js"
|
|
* attributes={{ 'data-domain': 'example.com' }} />
|
|
* <ConsentScript category="marketing" code={`fbq('init','123');fbq('track','PageView')`} />
|
|
*
|
|
* Note on withdrawal: once a script has loaded it keeps running for that page
|
|
* (a <script> tag can't be truly unloaded) — withdrawing consent stops the NEXT
|
|
* load. For vendors that support it, prefer Consent Mode so running tags honor
|
|
* the change immediately; ConsentScript gates the initial load.
|
|
*/
|
|
export declare function ConsentScript({ id, async: asyncAttr, attributes, category, code, defer, src, }: ConsentScriptProps): null;
|
|
export default ConsentScript;
|