Skip to content

Commit

Permalink
feat: add cookie popup
Browse files Browse the repository at this point in the history
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 angular/components#22746.
  • Loading branch information
crisbeto committed May 22, 2021
1 parent 4e08b84 commit 91e7d78
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/app-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {RouterModule} from '@angular/router';
import {MaterialDocsApp} from './material-docs-app';
import {MATERIAL_DOCS_ROUTES} from './routes';
import {NavBarModule} from './shared/navbar';
import {CookiePopupModule} from './shared/cookie-popup/cookie-popup-module';

@NgModule({
imports: [
Expand All @@ -18,6 +19,7 @@ import {NavBarModule} from './shared/navbar';
relativeLinkResolution: 'corrected'
}),
NavBarModule,
CookiePopupModule,
],
declarations: [MaterialDocsApp],
providers: [{provide: LocationStrategy, useClass: PathLocationStrategy}],
Expand Down
1 change: 1 addition & 0 deletions src/app/material-docs-app.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<app-navbar class="mat-elevation-z6"></app-navbar>
<router-outlet></router-outlet>
<app-cookie-popup></app-cookie-popup>
11 changes: 11 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {NgModule} from '@angular/core';
import {MatButtonModule} from '@angular/material/button';
import {MatSnackBarModule} from '@angular/material/snack-bar';
import {CookiePopup} from './cookie-popup';

@NgModule({
imports: [MatSnackBarModule, MatButtonModule],
declarations: [CookiePopup],
exports: [CookiePopup]
})
export class CookiePopupModule {}
12 changes: 12 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<ng-template #content>
This site uses cookies from Google to deliver its services and to analyze traffic.

<div class="buttons">
<a
href="https://policies.google.com/technologies/cookies"
mat-button
color="accent"
target="_blank">Learn more</a>
<button mat-button color="accent" (click)="accept()">Ok, Got it</button>
</div>
</ng-template>
9 changes: 9 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.buttons {
display: flex;
justify-content: flex-end;
margin: 16px -8px 0 0;

.mat-button {
text-transform: uppercase;
}
}
50 changes: 50 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {AfterViewInit, Component, OnDestroy, TemplateRef, ViewChild} from '@angular/core';
import {MatSnackBar} from '@angular/material/snack-bar';

const STORAGE_KEY = 'docs-cookies';

@Component({
selector: 'app-cookie-popup',
templateUrl: './cookie-popup.html',
styleUrls: ['./cookie-popup.scss']
})
export class CookiePopup implements AfterViewInit, OnDestroy {
/** Reference to the content that will be shown in the snack bar. */
@ViewChild('content')
private _content!: TemplateRef<unknown>;

constructor(private _snackBar: MatSnackBar) {}

/** Accepts the cookie disclaimer. */
accept() {
this._snackBar.dismiss();

try {
localStorage.setItem(STORAGE_KEY, 'true');
} catch {}
}

ngAfterViewInit() {
let hasAccepted = false;

// Needs to be in a try/catch, because some browsers will
// throw when using `localStorage` in private mode.
try {
hasAccepted = localStorage.getItem(STORAGE_KEY) === 'true';
} catch {}

if (!hasAccepted) {
this._snackBar.openFromTemplate(this._content, {
horizontalPosition: 'start',
verticalPosition: 'bottom',
// Turn off live announcements for now, because the snack bar wraps all of the
// content in an `aria-live` element which will announce the buttons as well.
politeness: 'off'
});
}
}

ngOnDestroy() {
this._snackBar.dismiss();
}
}
5 changes: 4 additions & 1 deletion tools/audit-docs.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const MIN_SCORES_PER_PAGE = [
'performance': 28,
'seo': 98,
'best-practices': 100,
'accessibility': 100
// We lose a couple of points on accessibility, because there isn't quite
// enough contrast on the buttons in the cookie popup. This is a known issue
// with the Material Design theming system.
'accessibility': 98
}
}
];
Expand Down

0 comments on commit 91e7d78

Please sign in to comment.