Skip to content

Commit

Permalink
✨ add listenable events
Browse files Browse the repository at this point in the history
  • Loading branch information
Yago committed Jun 27, 2023
1 parent e91df09 commit f0590e2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,31 @@ And after a `JSON.parse()`:

Basically, it's the `baseData` values, if there is any, and for each `permissions.slug`, a **boolean**.

## 🎫 Events

Here is a list of events you can **listen**:

```js
// To listen to the permissions / cookie's updates
document.addEventListener('leckerli:permissions-updated');

// To listen the modal states
document.addEventListener('leckerli:modal-closed');
document.addEventListener('leckerli:modal-opened');
```

Here is a list of events you can **dispatch**:

```js
// To manage the banner, even after the user's choice
document.dispatchEvent(new CustomEvent('leckerli:open-banner'));
document.dispatchEvent(new CustomEvent('leckerli:close-banner'));

// To manage the settings modal
document.dispatchEvent(new CustomEvent('leckerli:open-modal'));
document.dispatchEvent(new CustomEvent('leckerli:close-modal'));
```

## 🎨 Theming

You can theme Leckerli with the following CSS custom properties (variables):
Expand Down
6 changes: 5 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ <h1>Your website here 🤗</h1>
}
],
}
*/
*/

document.addEventListener('leckerli:modal-closed', () => console.log('modal closed'))
document.addEventListener('leckerli:modal-opened', () => console.log('modal opened'))
document.addEventListener('leckerli:permissions-updated', () => console.log('permissions updated'))
</script>

<style>
Expand Down
5 changes: 5 additions & 0 deletions src/hooks/useSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const useSettings = create<SettingsStore>((set, getState) => ({

// Manage modal display
setModal: (value: boolean) => set(state => {
document.dispatchEvent(new CustomEvent(`leckerli:modal-${value ? 'opened' : 'closed'}`));
return { ...state, settingsOpen: value };
}),

Expand All @@ -84,6 +85,8 @@ const useSettings = create<SettingsStore>((set, getState) => ({

cookies.set(state.name, JSON.stringify(newCookie));

document.dispatchEvent(new CustomEvent('leckerli:permissions-updated'));

return { ...state, choiceMade: true, cookie: newCookie };
}),

Expand All @@ -101,6 +104,8 @@ const useSettings = create<SettingsStore>((set, getState) => ({

cookies.set(state.name, JSON.stringify(newCookie));

document.dispatchEvent(new CustomEvent('leckerli:permissions-updated'));

return { ...state, choiceMade: true, cookie: newCookie };
}),

Expand Down

0 comments on commit f0590e2

Please sign in to comment.