Skip to content

Commit f075ba9

Browse files
committed
Optimizations.
- rename events - tidy event payloads - simplify JS - simplify CSS - Update README
1 parent fcd25c2 commit f075ba9

9 files changed

Lines changed: 115 additions & 126 deletions

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ The goal was to make something as small as possible and versatile enough that I
3232
- add 'data-consent' property with consent section name (e.g. analytics, functional, performance)
3333
-->
3434
<script data-consent="analytics" async src="https://www.googletagmanager.com/gtag/js?id=G-TEST" type="text/plain" id="js-analytics-gtm">
35-
console.log(google_tag_manager) // evaluated 'onload' of src
35+
// The contents of this script is injected when the parent script is loaded, for convenience
36+
console.log(google_tag_manager)
3637
</script>
3738
<script data-consent="analytics" type="text/plain" id="js-analytics-gtm-after">
3839
console.log('This script runs as soon as analytics section consent is granted')
@@ -138,25 +139,24 @@ html:not(.js-bm-hidden)::after {
138139
}
139140
```
140141
- example usage: `if (Consent && Consent.analytics) { doAnalyticsThing() }`
141-
- `bmInvalidateConsent()` – Delete stored consent data and reinstate UI
142-
- `bmUpdateConsent()` – Opens My Consent Settings modal
143-
- example usage: `<a href="javascript:bmUpdateConsent();"> Update my consent settings</a>`
142+
- `bmInvalidate()` – Delete stored consent data and reinstate UI
143+
- `bmOpen()` – Opens My Consent Settings modal
144+
- example usage: `<a href="javascript:bmOpen();"> Update my consent settings</a>`
144145

145146
## Events
146147

147148
The easiest way to see how events work is to view the `console.debug()` calls in the [demo](https://replete.github.io/biscuitman)
148-
- `biscuitman:openModal`
149-
- `biscuitman:closeModel`
150-
- `biscuitman:buttonPress`
151-
- `biscuitman:saveConsent`
152-
- `biscuitman:scriptInjected`
153-
- `biscuitman:scriptLoaded`
154-
- `biscuitman:invalidateConsent`
155-
- `biscuitman:updateConsent`
149+
- `biscuitman:open` => `{time: 1718915128298}` modal opened
150+
- `biscuitman:close` => `{time: 1718915128298}` modal closed
151+
- `biscuitman:button` => `{id: "settings", time: 1718915128298}` button clicked
152+
- `biscuitman:save` => `{data: {consentTime: 1718914784624, advertisement:true, advertisement: fal..}, time: 1718914784624}` consent choice saved
153+
- `biscuitman:inject` => `{el: $Element, parent?: $Element, time: 1718914784624}` script injected to DOM. if parent exists, it's a new tag inserted after a `src` script loaded which also had text content (a 'dependent' script = tidier convenient markup)
154+
- `biscuitman:invalidate` => `{data: {..consentObjectJustDeleted}, time: 1718915128298}` consent invalidated
155+
- `biscuitman:update` => `{data: {...currentConsentObject}, time: 1718914784624}` returns current consent object and time
156156

157157
You can watch for these events like this:
158158
```js
159-
document.addEventListener('biscuitman:openModal', (e) => {
159+
document.addEventListener('biscuitman:open', (e) => {
160160
console.log('modal opened')
161161
}, true);
162162
```

biscuitman.css

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
and is processed to un-nested native CSS nesting
77
and include vendor
88
*/
9-
109
.biscuitman {
1110
--text: #444;
1211
--bg: #fff;
@@ -19,14 +18,6 @@
1918
font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
2019
box-sizing: border-box;
2120

22-
&:has(dialog[open]) {
23-
padding:0;
24-
25-
article{
26-
display:none;
27-
}
28-
}
29-
3021
* {
3122
box-sizing: border-box;
3223
font-size: 16px;
@@ -66,7 +57,7 @@
6657
background: var(--bg);
6758
border:2px solid var(--color);
6859
line-height:1em;
69-
padding:0.8em;
60+
padding:.8em;
7061
margin-top:10px;
7162
color: var(--color);
7263
font-size:13px;
@@ -87,7 +78,7 @@
8778
top: 0;
8879
right:10px;
8980
line-height:1em;
90-
opacity:0.6;
81+
opacity:.6;
9182
font-size:24px;
9283
user-select:none;
9384
z-index:2;
@@ -100,7 +91,7 @@
10091
}
10192

10293
&:hover {
103-
opacity:0.8;
94+
opacity:.8;
10495
}
10596
}
10697

@@ -123,7 +114,7 @@
123114
}
124115
}
125116

126-
dialog[open] {
117+
dialog {
127118
border:0;
128119
width:100%;
129120
height:100%;
@@ -139,7 +130,7 @@
139130
border-radius:10px;
140131
}
141132
@media (min-width:576px) and (min-height:1134px) {
142-
max-height:908px;
133+
height:890px; /* KLUDGE: magic number based on 6 sections */
143134
}
144135

145136
nav {
@@ -245,7 +236,7 @@
245236
list-style:none;
246237

247238
&[open] summary b::after {
248-
transform:rotate(45deg) scale(0.3);
239+
transform:rotate(45deg) scale(.3);
249240
margin:-3px 0 0 -2px;
250241
}
251242
}
@@ -271,7 +262,7 @@
271262
border-left-color:transparent;
272263
border-top-color:transparent;
273264
border-radius:2px;
274-
transform:rotate(-45deg) scale(0.3);
265+
transform:rotate(-45deg) scale(.3);
275266
margin:-2px 0 0 -4px;
276267
}
277268
}
@@ -338,7 +329,7 @@
338329
left:var(--gap);
339330
left:0;
340331
top:50%;
341-
transform: translateY(-50%) scale(0.8);
332+
transform: translateY(-50%) scale(.8);
342333
transform-origin: center center;
343334
}
344335

@@ -358,7 +349,7 @@
358349
}
359350

360351
&:has([disabled]:checked) {
361-
opacity:0.6
352+
opacity:.6
362353
}
363354

364355
input {

biscuitman.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@
128128
case 'reject': saveConsent(false); break;
129129
default: break
130130
}
131-
dispatch('buttonPress', {id})
131+
dispatch('button', {id})
132132
}
133133

134134
function closeModalHandler() {
135-
dispatch('closeModal')
135+
dispatch('close')
136136
}
137137

138138
function cancelModalHandler(e) {
@@ -141,7 +141,7 @@
141141

142142
function openModal() {
143143
dialog.showModal()
144-
dispatch('openModal')
144+
dispatch('open')
145145
}
146146

147147
const displayUI = (show) => d.documentElement.classList[show ? 'remove' : 'add']('js-bm-hidden')
@@ -150,7 +150,7 @@
150150
try {
151151
return JSON.parse(localStorage.getItem(o.storageKey))
152152
} catch (err) {
153-
console.error(`${bm} readConsent error:`, err.message)
153+
console.error(bm, err)
154154
localStorage.removeItem(o.storageKey) // If there's an error in localstorage, wipe consent data
155155
return {}
156156
}
@@ -170,7 +170,7 @@
170170
})
171171
localStorage.setItem(o.storageKey, JSON.stringify(w[o.global]))
172172
insertScripts()
173-
dispatch('saveConsent', {data: w[o.global]})
173+
dispatch('save', {data: w[o.global]})
174174
dialog.close()
175175
displayUI(false)
176176
}
@@ -205,14 +205,14 @@
205205
if (!script.src) newScript.textContent = script.textContent
206206
// Insert script to DOM
207207
script.parentNode.replaceChild(newScript, script)
208-
dispatch('scriptInjected', {data: script.outerHTML})
208+
dispatch('inject', {el: script})
209209

210210
// If tag has src and inline script, insert dependent inline script as new tag on load
211211
if (script.src && script.textContent.trim() !== '') newScript.addEventListener('load', () => {
212212
let depScript = d.createElement('script')
213213
depScript.textContent = script.textContent
214214
newScript.insertAdjacentElement('afterend', depScript)
215-
dispatch('scriptLoaded', {data: depScript.outerHTML})
215+
dispatch('inject', {el: depScript, parent: script})
216216
})
217217
});
218218
}
@@ -244,13 +244,13 @@
244244
// Helper global methods
245245
// <a onclick="bmUpdateConsent()" href="javascript:void(0)">Update Consent Preferences</a>
246246
w.bmInvalidateConsent = () => {
247+
dispatch('invalidate', {data: readConsent()})
247248
saveConsent(false) // resets UI
248249
localStorage.removeItem(o.storageKey)
249250
displayUI(true)
250-
dispatch('invalidateConsent', readConsent())
251251
}
252252
w.bmUpdateConsent = () => {
253+
dispatch('update', {data: readConsent()})
253254
openModal()
254-
dispatch('updateConsent', readConsent())
255255
}
256256
})(document, window)

dist/biscuitman.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)