Skip to content

Commit

Permalink
Merge branch 'feature-consent-agreement-status' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
silllli committed Jun 6, 2024
2 parents a3e0bc6 + 0d254f6 commit 8e09c55
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
14 changes: 9 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -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
<GdprBanner bind:this={gdprBanner} cookieName="foo" description="bar" on:analytics={initAnalytics} />
<GdprBanner bind:this={gdprBanner} cookieName="foo" description="bar" on:analytics={handleAnalytics} />
<script>
import GdprBanner from '@beyonk/gdpr-cookie-consent-banner'
function initAnalytics () {
// some fathom analytics tracking code or similar
function handleAnalytics (event) {
if (event.detail.agreed) {
// some fathom analytics tracking code or similar
} else {
// disable any previously initialized analytics tracking code
}
}
</script>
```
Expand All @@ -228,4 +232,4 @@ For convenience when using Web Components - and to work around race conditions w
// some fathom analytics tracking code or similar
})
</script>
```
```
10 changes: 4 additions & 6 deletions src/lib/Banner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -165,12 +165,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
Expand Down
6 changes: 5 additions & 1 deletion src/routes/web-component/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
}
})
}
})
Expand Down

0 comments on commit 8e09c55

Please sign in to comment.