From 0d254f665daeb003e5ba0ab6ef48a771abc71e9e Mon Sep 17 00:00:00 2001 From: silllli <9334305+silllli@users.noreply.github.com> Date: Wed, 5 Jun 2024 23:00:20 +0200 Subject: [PATCH] Add consent agreement status to events --- README.MD | 14 +++++++++----- src/lib/Banner.svelte | 10 ++++------ src/routes/web-component/+page.svelte | 6 +++++- 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/README.MD b/README.MD index 1066936..6f55e4f 100644 --- a/README.MD +++ b/README.MD @@ -196,20 +196,24 @@ const props = { ### Events -Events are how you react to consent. Each type of consent emits an event when agreed to by the user. +Events are how you react to consent. Each type of consent emits an event when set by the user. The agreement state is passed in the event’s details. For convenience when using Web Components - and to work around race conditions whereby events are emitted by the component prior to the handler being attached, we also emit the same events, with the prefix `consent`, on `window`. #### Svelte ```svelte - + ``` @@ -228,4 +232,4 @@ For convenience when using Web Components - and to work around race conditions w // some fathom analytics tracking code or similar }) -``` \ No newline at end of file +``` diff --git a/src/lib/Banner.svelte b/src/lib/Banner.svelte index cf0575b..08f6846 100644 --- a/src/lib/Banner.svelte +++ b/src/lib/Banner.svelte @@ -159,12 +159,10 @@ if (choicesMerged[t]) { choicesMerged[t].value = agreed } - if (agreed) { - dispatch(t) - window.dispatchEvent( - new CustomEvent(`consent:${t}`) - ) - } + dispatch(t, { agreed }) + window.dispatchEvent( + new CustomEvent(`consent:${t}`, { detail: { agreed } }) + ) } shown = false diff --git a/src/routes/web-component/+page.svelte b/src/routes/web-component/+page.svelte index b972d7c..4ce9b5c 100644 --- a/src/routes/web-component/+page.svelte +++ b/src/routes/web-component/+page.svelte @@ -13,7 +13,11 @@ onMount(() => { for (const type of types) { cc.addEventListener(type, (e) => { - console.log(`${type} consent given`) + if (e.detail.agreed) { + console.log(`${type} consent given`) + } else { + console.log(`${type} consent rejected`) + } }) } })