Skip to content

Commit 39ab48f

Browse files
authored
feat: add cookie popup (#988)
Due to legal requirements we have to have a cookie disclaimer popup. It is always shown until the user agrees to it. After the user has agreed, the popup won't be shown on subsequent sessions. Fixes #22746.
1 parent 79c8e98 commit 39ab48f

File tree

8 files changed

+109
-0
lines changed

8 files changed

+109
-0
lines changed

material.angular.io/src/_app-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
@use './app/shared/footer/footer-theme';
1313
@use './app/shared/navbar/navbar-theme';
1414
@use './app/shared/table-of-contents/table-of-contents-theme';
15+
@use './app/shared/cookie-popup/cookie-popup-theme';
1516
@use './styles/api-theme';
1617
@use './styles/markdown-theme';
1718
@use './styles/svg-theme';
@@ -70,4 +71,5 @@
7071
@include not-found-theme.theme($theme);
7172
@include navbar-theme.theme($theme);
7273
@include table-of-contents-theme.theme($theme);
74+
@include cookie-popup-theme.theme($theme);
7375
}

material.angular.io/src/app/app-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {RouterModule} from '@angular/router';
77
import {MaterialDocsApp} from './material-docs-app';
88
import {MATERIAL_DOCS_ROUTES} from './routes';
99
import {NavBarModule} from './shared/navbar';
10+
import {CookiePopupModule} from './shared/cookie-popup/cookie-popup-module';
1011

1112
@NgModule({
1213
imports: [
@@ -18,6 +19,7 @@ import {NavBarModule} from './shared/navbar';
1819
relativeLinkResolution: 'corrected'
1920
}),
2021
NavBarModule,
22+
CookiePopupModule,
2123
],
2224
declarations: [MaterialDocsApp],
2325
providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}],
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
<app-cookie-popup></app-cookie-popup>
12
<app-navbar class="mat-elevation-z6"></app-navbar>
23
<router-outlet></router-outlet>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@use 'sass:map';
2+
@use '~@angular/material' as mat;
3+
4+
@mixin theme($theme) {
5+
$primary: map.get($theme, primary);
6+
$accent: map.get($theme, accent);
7+
$warn: map.get($theme, warn);
8+
$background: map.get($theme, background);
9+
$foreground: map.get($theme, foreground);
10+
$is-dark-theme: map.get($theme, is-dark);
11+
12+
app-cookie-popup {
13+
.popup {
14+
color: if($is-dark-theme,
15+
map.get(map.get(mat.$grey-palette, contrast), 50),
16+
map.get(mat.$dark-theme-foreground-palette, secondary-text)
17+
);
18+
background: if($is-dark-theme, map.get(mat.$grey-palette, 50), #252525);
19+
}
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import {NgModule} from '@angular/core';
2+
import {CommonModule} from '@angular/common';
3+
import {MatButtonModule} from '@angular/material/button';
4+
import {CookiePopup} from './cookie-popup';
5+
6+
@NgModule({
7+
imports: [CommonModule, MatButtonModule],
8+
declarations: [CookiePopup],
9+
exports: [CookiePopup]
10+
})
11+
export class CookiePopupModule {}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div class="popup" *ngIf="!hasAccepted">
2+
This site uses cookies from Google to deliver its services and to analyze traffic.
3+
4+
<div class="buttons">
5+
<a
6+
href="https://policies.google.com/technologies/cookies"
7+
mat-button
8+
color="accent"
9+
target="_blank"
10+
rel="noopener">More details</a>
11+
<button mat-button color="accent" (click)="accept()">Ok, Got it</button>
12+
</div>
13+
</div>
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@use '~@angular/cdk' as cdk;
2+
@use '~@angular/material' as mat;
3+
4+
$_inner-spacing: 16px;
5+
6+
.popup {
7+
@include mat.elevation(6);
8+
position: fixed;
9+
bottom: 0;
10+
left: 0;
11+
margin: 24px;
12+
max-width: 430px;
13+
z-index: cdk.$overlay-container-z-index + 1;
14+
padding: $_inner-spacing $_inner-spacing $_inner-spacing / 2;
15+
border-radius: 4px;
16+
}
17+
18+
.buttons {
19+
display: flex;
20+
justify-content: flex-end;
21+
margin: $_inner-spacing $_inner-spacing / -2 0 0;
22+
23+
.mat-button {
24+
text-transform: uppercase;
25+
}
26+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {ChangeDetectionStrategy, Component} from '@angular/core';
2+
3+
const STORAGE_KEY = 'docs-cookies';
4+
5+
@Component({
6+
selector: 'app-cookie-popup',
7+
templateUrl: './cookie-popup.html',
8+
styleUrls: ['./cookie-popup.scss'],
9+
changeDetection: ChangeDetectionStrategy.OnPush
10+
})
11+
export class CookiePopup {
12+
/** Whether the user has accepted the cookie disclaimer. */
13+
hasAccepted: boolean;
14+
15+
constructor() {
16+
// Needs to be in a try/catch, because some browsers will
17+
// throw when using `localStorage` in private mode.
18+
try {
19+
this.hasAccepted = localStorage.getItem(STORAGE_KEY) === 'true';
20+
} catch {
21+
this.hasAccepted = false;
22+
}
23+
}
24+
25+
/** Accepts the cookie disclaimer. */
26+
accept() {
27+
try {
28+
localStorage.setItem(STORAGE_KEY, 'true');
29+
} catch {}
30+
31+
this.hasAccepted = true;
32+
}
33+
}

0 commit comments

Comments
 (0)