Skip to content

Adjust the style of cookie consent #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 97 additions & 14 deletions javascript/doc-structure/article-2022.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,104 @@ function showExample (path) {
}


function stickyConneg (filename, cLang, targetLang) {
var response = false
var msg = '['+cLang+'] '+cn[cLang]
msg += '\n\n'+'['+targetLang+'] '+cn[targetLang]
if (targetLang !== 'en' && cLang !== 'en') msg += '\n\n'+'[en] '+cn.en
response = confirm(msg)
if (response == true) {
var d = new Date()
d.setTime(d.getTime() + 60*24*60*60*1000)
var expires = ';expires='+d.toUTCString()
var path = ";path=/"
document.cookie = 'w3ci18nlang='+targetLang+expires+path
}
document.location.assign(filename+'.'+targetLang+'.html')
// COOKIE BANNER UTILITIES
function createCookieBanner(targetLang, filename, cLang) {
// Check if banner already exists
if (document.getElementById('cookieBanner')) {
return;
}

const banner = document.createElement('div');
banner.id = 'cookieBanner';
banner.className = 'cookie-banner';
banner.setAttribute('role', 'dialog');
banner.setAttribute('aria-labelledby', 'cookieBannerTitle');
banner.setAttribute('aria-describedby', 'cookieBannerDesc');

// Build message with both current and target language
let message = '';
if (cn[cLang]) {
message += `<strong>[${cLang}]</strong> ${cn[cLang]}`;
}
if (cn[targetLang] && targetLang !== cLang) {
message += `<br><br><strong>[${targetLang}]</strong> ${cn[targetLang]}`;
}
if (targetLang !== 'en' && cLang !== 'en') {
message += `<br><br><strong>[en]</strong> ${cn.en}`;
}

banner.innerHTML = `
<div class="cookie-banner-content">
<div class="cookie-banner-text">
<h3 id="cookieBannerTitle" class="cookie-banner-title">Language Preference</h3>
<div id="cookieBannerDesc">${message}</div>
</div>
<div class="cookie-banner-actions">
<button id="cookieAccept" class="cookie-btn cookie-btn-primary">
${targetLang === 'en' ? 'Yes, remember my choice' : 'Yes'}
</button>
<button id="cookieDecline" class="cookie-btn cookie-btn-secondary">
${targetLang === 'en' ? 'No, just this time' : 'No'}
</button>
</div>
</div>
`;

document.body.appendChild(banner);

// Event handlers
document.getElementById('cookieAccept').addEventListener('click', () => {
setCookiePreference(targetLang);
removeCookieBanner();
navigateToLanguage(filename, targetLang);
});

document.getElementById('cookieDecline').addEventListener('click', () => {
removeCookieBanner();
navigateToLanguage(filename, targetLang);
});
}

function setCookiePreference(targetLang) {
const d = new Date();
d.setTime(d.getTime() + 60*24*60*60*1000); // 60 days
const expires = ';expires=' + d.toUTCString();
const path = ';path=/';
const secure = location.protocol === 'https:' ? ';secure' : '';
const sameSite = ';samesite=lax';
document.cookie = `w3ci18nlang=${targetLang}${expires}${path}${secure}${sameSite}`;
}

function removeCookieBanner() {
const banner = document.getElementById('cookieBanner');
if (banner) {
banner.style.animation = 'fadeOutScale 0.3s ease-in';
setTimeout(() => banner.remove(), 300);
}
}

function navigateToLanguage(filename, targetLang) {
const extension = targetLang === 'en' ? '.en.html' : `.${targetLang}.html`;
document.location.assign(filename + extension);
}

// Check if user has already set a preference
function hasLanguagePreference() {
return document.cookie.split(';').some(cookie =>
cookie.trim().startsWith('w3ci18nlang=')
);
}

function stickyConneg(filename, cLang, targetLang) {
// If user already has a preference set, just navigate
if (hasLanguagePreference()) {
navigateToLanguage(filename, targetLang);
return;
}

// Show the less intrusive banner instead of confirm dialog
createCookieBanner(targetLang, filename, cLang);
}



Expand Down
149 changes: 149 additions & 0 deletions style/article-2022.css
Original file line number Diff line number Diff line change
Expand Up @@ -1861,3 +1861,152 @@ h2:hover > a.selflink, h3 > a.selflink, h4 > a.selflink, h5 > a.selflink, h6 > a



/* COOKIE CONSENT */

.cookie-banner {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
max-width: 500px;
width: 90%;
background: #ffffff;
border: 1px solid #ccc;
border-radius: 8px;
box-shadow: 0 8px 32px rgba(0,0,0,0.2);
z-index: 10000;
font-family: Arial, sans-serif;
font-size: 14px;
line-height: 1.4;
animation: fadeInScale 0.3s ease-out;
}

.cookie-banner::before {
content: '';
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.1);
z-index: -1;
}

@keyframes fadeInScale {
from {
transform: translate(-50%, -50%) scale(0.8);
opacity: 0;
}
to {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
}

@keyframes fadeOutScale {
from {
transform: translate(-50%, -50%) scale(1);
opacity: 1;
}
to {
transform: translate(-50%, -50%) scale(0.8);
opacity: 0;
}
}

.cookie-banner-content {
padding: 16px;
}

.cookie-banner-title {
margin: 0 0 8px 0;
font-size: 16px;
font-weight: bold;
color: #333;
}

.cookie-banner-text div {
margin: 0 0 12px 0;
color: #000;
font-weight: 500;
line-height: 1.5;
}

.cookie-banner-text strong {
color: #005a9c;
font-weight: bold;
}

.cookie-banner-actions {
display: flex;
gap: 8px;
flex-wrap: wrap;
}

.cookie-btn {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
background: #f8f9fa;
color: #333;
cursor: pointer;
font-size: 13px;
transition: all 0.2s;
}

.cookie-btn:hover {
background: #e9ecef;
}

.cookie-btn-primary {
background: #005a9c;
color: white;
border-color: #005a9c;
}

.cookie-btn-primary:hover {
background: #004080;
}

.cookie-btn-close {
margin-left: auto;
padding: 4px 8px;
background: transparent;
border: none;
font-size: 18px;
color: #999;
}

.cookie-btn-close:hover {
color: #333;
background: #f0f0f0;
}

@media (max-width: 480px) {
.cookie-banner {
width: 95%;
max-width: none;
margin: 0 auto;
}

.cookie-banner-content {
padding: 20px;
}

.cookie-banner-actions {
flex-direction: column;
}

.cookie-btn {
width: 100%;
margin-bottom: 8px;
}

.cookie-btn-close {
width: auto;
margin-left: 0;
align-self: flex-end;
}
}

/* END COOKIE CONSENT */