Skip to content

Commit

Permalink
basic cookie route
Browse files Browse the repository at this point in the history
  • Loading branch information
quantuminformation committed Oct 13, 2023
1 parent 37ba99a commit 4e33c29
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
2 changes: 2 additions & 0 deletions css/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
--secondary-hover-color: hsl(var(--base-hue), var(--base-saturation), calc(var(--base-lightness) + 30%));
--sidenav-color: hsl(var(--base-hue), var(--base-saturation), calc(var(--base-lightness) + 5%));

--border-color: var(--default-text);

--default-text: hsl(var(--base-hue), var(--base-saturation), 100%);
--default-text-inv: hsl(var(--base-hue), var(--base-saturation), 0%);
}
Expand Down
2 changes: 1 addition & 1 deletion css/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
--secondary-hover-color: hsl(var(--base-hue), var(--base-saturation), calc(var(--base-lightness) - 30%));
--sidenav-color: hsl(var(--base-hue), var(--base-saturation), calc(var(--base-lightness) - 5%));

--border-color: hsl(var(--base-hue), var(--base-saturation), calc(var(--base-lightness) + 10%));
--border-color: var(--default-text);

--default-text: hsl(var(--base-hue), var(--base-saturation), 0%);
--default-text-inv: hsl(var(--base-hue), var(--base-saturation), 100%);
Expand Down
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<span class="icon">🧬</span>
<span class="text">Multiple instances</span>
</a>
<a data-nav href="/cookies" class="button secondary squarify">
<span class="icon">🍪</span>
<span class="text">Cookie popup</span>
</a>
</nav>
<main data-component="router" data-use-hash class="p-md" >
<!--components in /pages that will be fetched by router when user clicks on a data-nav -->
Expand Down
140 changes: 140 additions & 0 deletions js/routes/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
// Stored in /js/routes/cookies.js

export default (hostComponent) => {
// Clear any existing content in the hostComponent
hostComponent.innerHTML = '';

const analyticsPreference = localStorage.getItem('analytics-cookies') === 'true';
const personalizationPreference = localStorage.getItem('personalization-cookies') === 'true';
const advertisementPreference = localStorage.getItem('advertisement-cookies') === 'true';

// Cookie Modal Styles & HTML
const cookieModalStyles = `
<style>
#cookie-modal-background {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
}
#cookie-modal {
padding: 1rem;
display: flex;
flex-direction: column;
align-items: center;
border-radius: 10px;
background-color: var(--bg-color);
border: 1px solid var(--border-color);
}
.preference {
display: flex;
justify-content: space-between;
align-items: center;
width: 250px;
margin-bottom: 1rem;
}
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.4s;
border-radius: 34px;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
border-radius: 50%;
transition: 0.4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(26px);
}
</style>
`;

// Insert this style string in the component logic where the styles are concatenated.

const cookieModalHTML = `
<div id="cookie-modal-background">
<div id="cookie-modal">
<p>We use cookies to enhance your experience. Choose the cookies you allow:</p>
<div class="preference">
<span>Analytics</span>
<label class="switch">
<input type="checkbox" id="analytics-cookies" ${analyticsPreference ? 'checked' : ''}>
<span class="slider"></span>
</label>
</div>
<div class="preference">
<span>Personalization</span>
<label class="switch">
<input type="checkbox" id="personalization-cookies" ${
personalizationPreference ? 'checked' : ''
}>
<span class="slider"></span>
</label>
</div>
<div class="preference">
<span>Advertisement</span>
<label class="switch">
<input type="checkbox" id="advertisement-cookies" ${
advertisementPreference ? 'checked' : ''
}>
<span class="slider"></span>
</label>
</div>
<button id="save-preferences">Save Preferences</button>
</div>
</div>
`;

hostComponent.innerHTML += cookieModalStyles + cookieModalHTML;
handleCookieChoice(hostComponent);

function handleCookieChoice(hostComponent) {
const modalBackground = hostComponent.querySelector('#cookie-modal-background');

hostComponent.querySelector('#save-preferences').addEventListener('click', () => {
localStorage.setItem('analytics-cookies', hostComponent.querySelector('#analytics-cookies').checked);
localStorage.setItem(
'personalization-cookies',
hostComponent.querySelector('#personalization-cookies').checked
);
localStorage.setItem(
'advertisement-cookies',
hostComponent.querySelector('#advertisement-cookies').checked
);

modalBackground.remove();
});
}
};

0 comments on commit 4e33c29

Please sign in to comment.