Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

feat: add cookie popup #988

Merged
merged 1 commit into from
May 26, 2021
Merged
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
2 changes: 2 additions & 0 deletions src/_app-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@use './app/shared/footer/footer-theme';
@use './app/shared/navbar/navbar-theme';
@use './app/shared/table-of-contents/table-of-contents-theme';
@use './app/shared/cookie-popup/cookie-popup-theme';
@use './styles/api-theme';
@use './styles/markdown-theme';
@use './styles/svg-theme';
Expand Down Expand Up @@ -70,4 +71,5 @@
@include not-found-theme.theme($theme);
@include navbar-theme.theme($theme);
@include table-of-contents-theme.theme($theme);
@include cookie-popup-theme.theme($theme);
}
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-cookie-popup></app-cookie-popup>
<app-navbar class="mat-elevation-z6"></app-navbar>
<router-outlet></router-outlet>
21 changes: 21 additions & 0 deletions src/app/shared/cookie-popup/_cookie-popup-theme.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
@use 'sass:map';
@use '~@angular/material' as mat;

@mixin theme($theme) {
$primary: map.get($theme, primary);
$accent: map.get($theme, accent);
$warn: map.get($theme, warn);
$background: map.get($theme, background);
$foreground: map.get($theme, foreground);
$is-dark-theme: map.get($theme, is-dark);

app-cookie-popup {
.popup {
color: if($is-dark-theme,
map.get(map.get(mat.$grey-palette, contrast), 50),
map.get(mat.$dark-theme-foreground-palette, secondary-text)
);
background: if($is-dark-theme, map.get(mat.$grey-palette, 50), #252525);
}
}
}
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 {CommonModule} from '@angular/common';
import {MatButtonModule} from '@angular/material/button';
import {CookiePopup} from './cookie-popup';

@NgModule({
imports: [CommonModule, MatButtonModule],
declarations: [CookiePopup],
exports: [CookiePopup]
})
export class CookiePopupModule {}
13 changes: 13 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div class="popup" *ngIf="!hasAccepted">
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"
rel="noopener">More details</a>
<button mat-button color="accent" (click)="accept()">Ok, Got it</button>
</div>
</div>
26 changes: 26 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
@use '~@angular/cdk' as cdk;
@use '~@angular/material' as mat;

$_inner-spacing: 16px;

.popup {
@include mat.elevation(6);
position: fixed;
bottom: 0;
left: 0;
margin: 24px;
max-width: 430px;
z-index: cdk.$overlay-container-z-index + 1;
padding: $_inner-spacing $_inner-spacing $_inner-spacing / 2;
border-radius: 4px;
}

.buttons {
display: flex;
justify-content: flex-end;
margin: $_inner-spacing $_inner-spacing / -2 0 0;

.mat-button {
text-transform: uppercase;
}
}
33 changes: 33 additions & 0 deletions src/app/shared/cookie-popup/cookie-popup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';

const STORAGE_KEY = 'docs-cookies';

@Component({
selector: 'app-cookie-popup',
templateUrl: './cookie-popup.html',
styleUrls: ['./cookie-popup.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CookiePopup {
/** Whether the user has accepted the cookie disclaimer. */
hasAccepted: boolean;

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

/** Accepts the cookie disclaimer. */
accept() {
try {
localStorage.setItem(STORAGE_KEY, 'true');
} catch {}

this.hasAccepted = true;
}
}