@theplaintheory/svelte 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +54 -3
- package/dist/index.d.ts +36 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +47 -0
- package/dist/index.js.map +1 -0
- package/package.json +50 -6
package/README.md
CHANGED
|
@@ -1,3 +1,54 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
# @theplaintheory/svelte
|
|
2
|
+
|
|
3
|
+
Svelte and SvelteKit stores for [Plain Theory](https://www.theplaintheory.in) consent, with types. Svelte 4 and 5. SSR-safe.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @theplaintheory/svelte
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Set up
|
|
10
|
+
|
|
11
|
+
Put the script tag first in `<head>` in `src/app.html` (copy it from the dashboard's **Install** page), or load it from your root layout:
|
|
12
|
+
|
|
13
|
+
```svelte
|
|
14
|
+
<!-- src/routes/+layout.svelte -->
|
|
15
|
+
<script lang="ts">
|
|
16
|
+
import { onMount } from "svelte";
|
|
17
|
+
import { init } from "@theplaintheory/svelte";
|
|
18
|
+
onMount(() => init({ siteKey: "pk_YOUR_SITE_KEY" }));
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<slot />
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Use it
|
|
25
|
+
|
|
26
|
+
```svelte
|
|
27
|
+
<script lang="ts">
|
|
28
|
+
import { allowed, consent, consentScript, openPreferences, ready, setLanguage } from "@theplaintheory/svelte";
|
|
29
|
+
const marketing = allowed("marketing");
|
|
30
|
+
</script>
|
|
31
|
+
|
|
32
|
+
<div use:consentScript={{ category: "analytics", src: "https://www.googletagmanager.com/gtag/js?id=G-XXXX" }}></div>
|
|
33
|
+
|
|
34
|
+
{#if $marketing}
|
|
35
|
+
<iframe src="https://www.youtube.com/embed/…" title="Video"></iframe>
|
|
36
|
+
{:else}
|
|
37
|
+
<p>Allow marketing cookies to watch this video.</p>
|
|
38
|
+
{/if}
|
|
39
|
+
|
|
40
|
+
{#if $ready}<p>Language: {$consent?.language}</p>{/if}
|
|
41
|
+
<button on:click={openPreferences}>Privacy choices</button>
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
| Export | What it is |
|
|
45
|
+
| --- | --- |
|
|
46
|
+
| `init(options)` | Loads the script once (browser only). |
|
|
47
|
+
| `consent` | Store: the state, `null` on the server and until ready. |
|
|
48
|
+
| `ready`, `allowed(category)` | Boolean stores. |
|
|
49
|
+
| `consentScript` | Action: add a third-party script once allowed. |
|
|
50
|
+
| `acceptAll`, `rejectAll`, `setConsent`, `openPreferences`, `revoke`, `setLanguage` | Actions; queued until the script loads. |
|
|
51
|
+
|
|
52
|
+
Everything from [`@theplaintheory/consent`](https://www.npmjs.com/package/@theplaintheory/consent) is re-exported.
|
|
53
|
+
|
|
54
|
+
MIT · [Docs](https://www.theplaintheory.in/docs#frameworks)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type Category, type ConsentState, type LoadOptions } from "@theplaintheory/consent";
|
|
2
|
+
import { type Readable } from "svelte/store";
|
|
3
|
+
export type { Categories, Category, ConsentState, Framework, LoadOptions, PlainConsentApi } from "@theplaintheory/consent";
|
|
4
|
+
export { getConsent, isAllowed, loadPlainConsent, loadScriptWhenAllowed, onConsentChange, whenAllowed } from "@theplaintheory/consent";
|
|
5
|
+
/**
|
|
6
|
+
* Load plain-consent.js once. Call from a browser-only place, e.g. `onMount` in your root
|
|
7
|
+
* +layout.svelte. Safe to call on the server: it resolves to undefined there.
|
|
8
|
+
*/
|
|
9
|
+
export declare function init(options: LoadOptions): Promise<import("@theplaintheory/consent").PlainConsentApi | undefined>;
|
|
10
|
+
/** Consent state: null on the server and until the script is ready. Works with `$consent` in Svelte 4 and 5. */
|
|
11
|
+
export declare const consent: Readable<ConsentState | null>;
|
|
12
|
+
/** True once the script has loaded the site's settings (false on the server). */
|
|
13
|
+
export declare const ready: Readable<boolean>;
|
|
14
|
+
/** A store that is true while `category` is allowed (`essential` is always true). */
|
|
15
|
+
export declare const allowed: (category: Category) => Readable<boolean>;
|
|
16
|
+
/**
|
|
17
|
+
* Svelte action: add a third-party script once `category` is allowed (deduplicated, never removed).
|
|
18
|
+
*
|
|
19
|
+
* <div use:consentScript={{ category: "analytics", src: "https://www.googletagmanager.com/gtag/js?id=G-XXXX" }} />
|
|
20
|
+
*/
|
|
21
|
+
export declare function consentScript(_node: Element, params: {
|
|
22
|
+
category: Category;
|
|
23
|
+
src: string;
|
|
24
|
+
attributes?: Record<string, string>;
|
|
25
|
+
}): {};
|
|
26
|
+
/** Actions that queue safely before the script loads. */
|
|
27
|
+
export declare const acceptAll: () => void;
|
|
28
|
+
export declare const rejectAll: () => void;
|
|
29
|
+
export declare const setConsent: (partial: Partial<Record<Category, boolean>>) => void;
|
|
30
|
+
/** open the preferences panel (wire it to a "Privacy choices" link in your footer) */
|
|
31
|
+
export declare const openPreferences: () => void;
|
|
32
|
+
/** forget the choice and ask again */
|
|
33
|
+
export declare const revoke: () => void;
|
|
34
|
+
/** show the notice in one of the site's languages; "" for the default */
|
|
35
|
+
export declare const setLanguage: (code: string) => void;
|
|
36
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoF,KAAK,QAAQ,EAAE,KAAK,YAAY,EAAE,KAAK,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAC/K,OAAO,EAAqB,KAAK,QAAQ,EAAE,MAAM,cAAc,CAAC;AAEhE,YAAY,EAAE,UAAU,EAAE,QAAQ,EAAE,YAAY,EAAE,SAAS,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC3H,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEvI;;;GAGG;AACH,wBAAgB,IAAI,CAAC,OAAO,EAAE,WAAW,0EAMxC;AAED,gHAAgH;AAChH,eAAO,MAAM,OAAO,EAAE,QAAQ,CAAC,YAAY,GAAG,IAAI,CAIhD,CAAC;AAEH,iFAAiF;AACjF,eAAO,MAAM,KAAK,EAAE,QAAQ,CAAC,OAAO,CAAuC,CAAC;AAE5E,qFAAqF;AACrF,eAAO,MAAM,OAAO,GAAI,UAAU,QAAQ,KAAG,QAAQ,CAAC,OAAO,CAAoD,CAAC;AAElH;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE;IAAE,QAAQ,EAAE,QAAQ,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAAE,MAG7H;AAED,yDAAyD;AACzD,eAAO,MAAM,SAAS,YAA2B,CAAC;AAClD,eAAO,MAAM,SAAS,YAA2B,CAAC;AAClD,eAAO,MAAM,UAAU,uDAAqB,CAAC;AAC7C,sFAAsF;AACtF,eAAO,MAAM,eAAe,YAAsB,CAAC;AACnD,sCAAsC;AACtC,eAAO,MAAM,MAAM,YAAwB,CAAC;AAC5C,yEAAyE;AACzE,eAAO,MAAM,WAAW,wBAA6B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { consentActions, consentStore, isAllowed, loadPlainConsent, loadScriptWhenAllowed } from "@theplaintheory/consent";
|
|
2
|
+
import { derived, readable } from "svelte/store";
|
|
3
|
+
export { getConsent, isAllowed, loadPlainConsent, loadScriptWhenAllowed, onConsentChange, whenAllowed } from "@theplaintheory/consent";
|
|
4
|
+
/**
|
|
5
|
+
* Load plain-consent.js once. Call from a browser-only place, e.g. `onMount` in your root
|
|
6
|
+
* +layout.svelte. Safe to call on the server: it resolves to undefined there.
|
|
7
|
+
*/
|
|
8
|
+
export function init(options) {
|
|
9
|
+
if (typeof window === "undefined")
|
|
10
|
+
return Promise.resolve(undefined);
|
|
11
|
+
return loadPlainConsent(options).catch((e) => {
|
|
12
|
+
console.warn(e);
|
|
13
|
+
return undefined;
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/** Consent state: null on the server and until the script is ready. Works with `$consent` in Svelte 4 and 5. */
|
|
17
|
+
export const consent = readable(null, (set) => {
|
|
18
|
+
if (typeof window === "undefined")
|
|
19
|
+
return;
|
|
20
|
+
set(consentStore.getSnapshot());
|
|
21
|
+
return consentStore.subscribe(() => set(consentStore.getSnapshot()));
|
|
22
|
+
});
|
|
23
|
+
/** True once the script has loaded the site's settings (false on the server). */
|
|
24
|
+
export const ready = derived(consent, (s) => s !== null);
|
|
25
|
+
/** A store that is true while `category` is allowed (`essential` is always true). */
|
|
26
|
+
export const allowed = (category) => derived(consent, (s) => isAllowed(category, s));
|
|
27
|
+
/**
|
|
28
|
+
* Svelte action: add a third-party script once `category` is allowed (deduplicated, never removed).
|
|
29
|
+
*
|
|
30
|
+
* <div use:consentScript={{ category: "analytics", src: "https://www.googletagmanager.com/gtag/js?id=G-XXXX" }} />
|
|
31
|
+
*/
|
|
32
|
+
export function consentScript(_node, params) {
|
|
33
|
+
var _a;
|
|
34
|
+
void loadScriptWhenAllowed(params.category, params.src, (_a = params.attributes) !== null && _a !== void 0 ? _a : {});
|
|
35
|
+
return {};
|
|
36
|
+
}
|
|
37
|
+
/** Actions that queue safely before the script loads. */
|
|
38
|
+
export const acceptAll = consentActions.acceptAll;
|
|
39
|
+
export const rejectAll = consentActions.rejectAll;
|
|
40
|
+
export const setConsent = consentActions.set;
|
|
41
|
+
/** open the preferences panel (wire it to a "Privacy choices" link in your footer) */
|
|
42
|
+
export const openPreferences = consentActions.open;
|
|
43
|
+
/** forget the choice and ask again */
|
|
44
|
+
export const revoke = consentActions.revoke;
|
|
45
|
+
/** show the notice in one of the site's languages; "" for the default */
|
|
46
|
+
export const setLanguage = consentActions.setLanguage;
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,SAAS,EAAE,gBAAgB,EAAE,qBAAqB,EAAsD,MAAM,yBAAyB,CAAC;AAC/K,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAiB,MAAM,cAAc,CAAC;AAGhE,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAEvI;;;GAGG;AACH,MAAM,UAAU,IAAI,CAAC,OAAoB;IACvC,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACrE,OAAO,gBAAgB,CAAC,OAAO,CAAC,CAAC,KAAK,CAAC,CAAC,CAAU,EAAE,EAAE;QACpD,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAChB,OAAO,SAAS,CAAC;IACnB,CAAC,CAAC,CAAC;AACL,CAAC;AAED,gHAAgH;AAChH,MAAM,CAAC,MAAM,OAAO,GAAkC,QAAQ,CAAsB,IAAI,EAAE,CAAC,GAAG,EAAE,EAAE;IAChG,IAAI,OAAO,MAAM,KAAK,WAAW;QAAE,OAAO;IAC1C,GAAG,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,CAAC;IAChC,OAAO,YAAY,CAAC,SAAS,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;AACvE,CAAC,CAAC,CAAC;AAEH,iFAAiF;AACjF,MAAM,CAAC,MAAM,KAAK,GAAsB,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC;AAE5E,qFAAqF;AACrF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,QAAkB,EAAqB,EAAE,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,CAAC;AAElH;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,KAAc,EAAE,MAAgF;;IAC5H,KAAK,qBAAqB,CAAC,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,EAAE,MAAA,MAAM,CAAC,UAAU,mCAAI,EAAE,CAAC,CAAC;IACjF,OAAO,EAAE,CAAC;AACZ,CAAC;AAED,yDAAyD;AACzD,MAAM,CAAC,MAAM,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC;AAClD,MAAM,CAAC,MAAM,SAAS,GAAG,cAAc,CAAC,SAAS,CAAC;AAClD,MAAM,CAAC,MAAM,UAAU,GAAG,cAAc,CAAC,GAAG,CAAC;AAC7C,sFAAsF;AACtF,MAAM,CAAC,MAAM,eAAe,GAAG,cAAc,CAAC,IAAI,CAAC;AACnD,sCAAsC;AACtC,MAAM,CAAC,MAAM,MAAM,GAAG,cAAc,CAAC,MAAM,CAAC;AAC5C,yEAAyE;AACzE,MAAM,CAAC,MAAM,WAAW,GAAG,cAAc,CAAC,WAAW,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,50 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@theplaintheory/svelte",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "@theplaintheory/svelte",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Svelte and SvelteKit stores for Plain Theory consent. Works with Svelte 4 and 5.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"sideEffects": false,
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc -p tsconfig.json",
|
|
22
|
+
"typecheck": "tsc -p tsconfig.json --noEmit"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@theplaintheory/consent": "^1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"peerDependencies": {
|
|
28
|
+
"svelte": ">=4"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"repository": {
|
|
34
|
+
"type": "git",
|
|
35
|
+
"url": "git+https://github.com/gagankarthik/theplaintheory.git",
|
|
36
|
+
"directory": "packages/svelte"
|
|
37
|
+
},
|
|
38
|
+
"homepage": "https://www.theplaintheory.in/docs#frameworks",
|
|
39
|
+
"keywords": [
|
|
40
|
+
"consent",
|
|
41
|
+
"cmp",
|
|
42
|
+
"cookie banner",
|
|
43
|
+
"gdpr",
|
|
44
|
+
"ccpa",
|
|
45
|
+
"dpdpa",
|
|
46
|
+
"svelte",
|
|
47
|
+
"sveltekit",
|
|
48
|
+
"store"
|
|
49
|
+
]
|
|
50
|
+
}
|