Skip to content

Commit 332495b

Browse files
authoredFeb 7, 2025··
Clean up cookie notice implementation (#6399)
- Removes `Cookies.js` usage to match `flutter/website`, reduce external dependencies, and reduce network waterfall on site load. - Use a text button for "Learn more" instead of normal link. - Clean up styles to more closely align with `flutter/website`.
1 parent a6d3163 commit 332495b

File tree

5 files changed

+84
-64
lines changed

5 files changed

+84
-64
lines changed
 

‎src/_includes/cookie-notice.html

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<section id="cookie-notice">
22
<div class="container">
33
<p>dart.dev uses cookies from Google to deliver and
4-
enhance the quality of its services and to analyze traffic.
5-
<a href="https://policies.google.com/technologies/cookies" target="_blank" rel="noopener">Learn more</a>.
6-
</p>
7-
<button id="cookie-consent" class="filled-button">OK, got it</button>
4+
enhance the quality of its services and to analyze traffic.</p>
5+
<div class="button-group">
6+
<a class="text-button" href="https://policies.google.com/technologies/cookies" target="_blank" rel="noopener">Learn more</a>
7+
<button id="cookie-consent" class="filled-button">OK, got it</button>
8+
</div>
89
</div>
910
</section>

‎src/_includes/head.html

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% assign cache_bust = site.time|date:'?v=%s' %}
1+
{% assign cache_bust = '?v=2' %}
22

33
<head>
44
<meta charset="utf-8">
@@ -74,11 +74,6 @@
7474
{% endfor -%}
7575
{% endif -%}
7676

77-
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/3.0.5/js.cookie.min.js"
78-
integrity="sha512-nlp9/l96/EpjYBx7EP7pGASVXNe80hGhYAUrjeXnu/fyF5Py0/RXav4BBNs7n5Hx1WFhOEOWSAVjGeC3oKxDVQ=="
79-
crossorigin="anonymous"
80-
referrerpolicy="no-referrer"></script>
81-
8277
<script src="{{ '/assets/js/os-tabs.js' | append: cache_bust }}"></script>
8378
<script src="{{ '/assets/js/main.js' | append: cache_bust }}"></script>
8479

+15-29
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
@use '../core/variables' as *;
22

33
#cookie-notice {
4-
background-color: white;
5-
padding: 2rem;
4+
display: none;
5+
justify-content: center;
6+
background-color: $site-color-white;
7+
padding: 1.5rem;
68
position: fixed;
79
bottom: 0;
810
width: 100%;
9-
box-shadow: -1px 1px 4px rgba(0, 0, 0, 0.3);
11+
box-shadow: -1px 1px 4px rgba(0, 0, 0, 0.25);
1012
opacity: 0;
11-
display: none;
1213
z-index: 9999;
1314

1415
@keyframes fadein {
@@ -20,53 +21,38 @@
2021
}
2122
}
2223

23-
button.filled-button {
24-
font-size: 1rem;
25-
}
26-
2724
&.show {
28-
display: block;
29-
animation-duration: 500ms;
30-
animation-delay: 200ms;
31-
animation-name: fadein;
32-
animation-iteration-count: 1;
33-
animation-timing-function: ease;
34-
animation-fill-mode: forwards;
25+
display: flex;
26+
animation: fadein 500ms ease 200ms 1 forwards;
3527
}
3628

3729
.container {
3830
display: flex;
3931
justify-content: space-between;
4032
align-items: center;
4133
max-width: 1080px;
42-
min-width: 0 !important;
34+
min-width: 0;
4335
width: auto;
4436
gap: 1.5rem;
4537

4638
p {
47-
font-size: 1rem;
4839
line-height: 1.6;
4940
margin: 0;
5041
}
42+
43+
p, button, a {
44+
font-size: 1rem;
45+
}
5146
}
5247
}
5348

5449
body.homepage, body.error {
5550
#cookie-notice {
51+
--site-interaction-base-values: var(--site-interaction-base-values-inverse);
5652
background-color: $site-color-dark-background;
5753

58-
.container {
59-
p {
60-
color: white;
61-
62-
a {
63-
color: $site-color-card-link;
64-
65-
&:hover, &:focus, &:active {
66-
color: darken($site-color-card-link, 20%);
67-
}
68-
}
69-
}
54+
.container p {
55+
color: $site-color-white;
7056
}
7157
}
7258
}

‎src/_sass/core/_base.scss

+40-12
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
:root {
55
--site-interaction-base-values: 0 0 0;
6+
--site-interaction-base-values-inverse: 255 255 255;
67
--focus-outline-color: #1967D2;
78
}
89

@@ -72,38 +73,65 @@ a, button {
7273
color: $brand-primary;
7374
}
7475

75-
&.filled-button {
76+
&.filled-button, &.text-button {
7677
display: flex;
7778
align-items: center;
7879
padding: 0.5rem 1rem;
7980
gap: 0.5rem;
8081
width: fit-content;
81-
background-color: $site-color-primary;
82-
color: $site-color-white;
83-
text-decoration: none;
84-
border-radius: 0.25rem;
8582
cursor: pointer;
86-
font-family: $site-font-family-alt;
8783
font-size: 1.25rem;
8884
white-space: nowrap;
85+
86+
font-family: $site-font-family-alt;
87+
88+
&:focus-visible {
89+
outline: 2px solid var(--focus-outline-color);
90+
}
91+
}
92+
93+
&.filled-button {
94+
background-color: $site-color-primary;
95+
border-radius: 0.25rem;
8996
outline-offset: 2px;
9097

91-
&:hover {
92-
@include interaction-style(4%);
98+
&, &:hover, &:focus, &:active {
9399
color: $site-color-white;
94100
text-decoration: none;
95101
}
96102

97-
&:focus {
98-
color: $site-color-white;
99-
outline: 2px solid var(--focus-outline-color);
103+
&:hover {
104+
@include interaction-style(4%);
100105
}
101106

102107
&:active {
103108
@include interaction-style(8%);
104-
color: $site-color-white;
105109
}
106110
}
111+
112+
&.text-button {
113+
border-radius: 1.5rem;
114+
115+
&, &:hover, &:focus, &:active {
116+
color: $site-color-primary;
117+
background: none;
118+
text-decoration: none;
119+
}
120+
121+
&:hover {
122+
@include interaction-style(4%);
123+
}
124+
125+
&:active {
126+
@include interaction-style(7%);
127+
}
128+
}
129+
}
130+
131+
.button-group {
132+
display: flex;
133+
flex-direction: row;
134+
gap: 0.75rem;
107135
}
108136

109137
button {

‎src/content/assets/js/main.js

+23-13
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,33 @@ function createGallery() {
7878
}
7979

8080
function initCookieNotice() {
81-
const notice = document.getElementById('cookie-notice');
82-
const agreeBtn = document.getElementById('cookie-consent');
83-
const cookieKey = 'dart-site-cookie-consent';
84-
const cookieConsentValue = 'true'
85-
const activeClass = 'show';
86-
87-
if (Cookies.get(cookieKey) === cookieConsentValue) {
88-
return;
81+
const currentDate = Date.now();
82+
const cookieKey = 'cookie-consent';
83+
84+
// Check if they have already recently agreed.
85+
const existingDateString = window.localStorage.getItem(cookieKey);
86+
if (existingDateString) {
87+
const existingDate = parseInt(existingDateString);
88+
if (Number.isInteger(existingDate)) {
89+
const halfYearMs = 1000 * 60 * 60 * 24 * 180;
90+
// If the last consent is less than 180 days old, don't show the notice.
91+
if (currentDate - existingDate < halfYearMs) {
92+
return;
93+
}
94+
}
8995
}
9096

91-
notice.classList.add(activeClass);
97+
const activeClass = 'show';
9298

93-
agreeBtn.addEventListener('click', (e) => {
99+
// Set up the "OK" button to update storage and hide the banner.
100+
document.getElementById('cookie-consent')
101+
?.addEventListener('click', (e) => {
94102
e.preventDefault();
95-
Cookies.set(cookieKey, cookieConsentValue, { sameSite: 'strict', expires: 30});
96-
notice.classList.remove(activeClass);
97-
});
103+
window.localStorage.setItem(cookieKey, currentDate.toString());
104+
document.getElementById('cookie-notice')?.classList.remove(activeClass);
105+
}, { once: true });
106+
107+
document.getElementById('cookie-notice').classList.add(activeClass);
98108
}
99109

100110
// A pattern to remove terminal command markers when copying code blocks.

0 commit comments

Comments
 (0)
Please sign in to comment.