forked from angular/material.angular.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
7 changed files
with
89 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters