This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(navbar): Add themepicker component with lazy loaded themes
- Loading branch information
Showing
17 changed files
with
267 additions
and
4 deletions.
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
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
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './style-manager'; |
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,52 @@ | ||
import {TestBed, inject} from '@angular/core/testing'; | ||
import {StyleManager} from './style-manager'; | ||
|
||
|
||
describe('StyleManager', () => { | ||
let styleManager: StyleManager; | ||
|
||
beforeEach(() => TestBed.configureTestingModule({ | ||
providers: [StyleManager] | ||
})); | ||
|
||
beforeEach(inject([StyleManager], (sm: StyleManager) => { | ||
styleManager = sm; | ||
})); | ||
|
||
afterEach(() => { | ||
let links = document.head.querySelectorAll('link'); | ||
for (let link of Array.prototype.slice.call(links)) { | ||
if (link.className.includes('style-manager-')) { | ||
document.head.removeChild(link); | ||
} | ||
} | ||
}); | ||
|
||
it('should add stylesheet to head', () => { | ||
styleManager.setStyle('test', 'test.css'); | ||
let styleEl = document.head.querySelector('.style-manager-test') as HTMLLinkElement; | ||
expect(styleEl).not.toBeNull(); | ||
expect(styleEl.href.endsWith('test.css')).toBe(true); | ||
}); | ||
|
||
it('should change existing stylesheet', () => { | ||
styleManager.setStyle('test', 'test.css'); | ||
let styleEl = document.head.querySelector('.style-manager-test') as HTMLLinkElement; | ||
expect(styleEl).not.toBeNull(); | ||
expect(styleEl.href.endsWith('test.css')).toBe(true); | ||
|
||
styleManager.setStyle('test', 'new.css'); | ||
expect(styleEl.href.endsWith('new.css')).toBe(true); | ||
}); | ||
|
||
it('should remove existing stylesheet', () => { | ||
styleManager.setStyle('test', 'test.css'); | ||
let styleEl = document.head.querySelector('.style-manager-test') as HTMLLinkElement; | ||
expect(styleEl).not.toBeNull(); | ||
expect(styleEl.href.endsWith('test.css')).toBe(true); | ||
|
||
styleManager.removeStyle('test'); | ||
styleEl = document.head.querySelector('.style-manager-test') as HTMLLinkElement; | ||
expect(styleEl).toBeNull(); | ||
}); | ||
}); |
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,46 @@ | ||
import {Injectable} from '@angular/core'; | ||
|
||
|
||
/** | ||
* Class for managing stylesheets. Stylesheets are loaded into named slots so that they can be | ||
* removed or changed later. | ||
*/ | ||
@Injectable() | ||
export class StyleManager { | ||
/** | ||
* Set the stylesheet with the specified key. | ||
* @param key The key for the slot to load the stylesheet into. | ||
* @param href The url for the stylesheet. | ||
*/ | ||
setStyle(key: string, href: string) { | ||
this._getLinkElementForKey(key, true).setAttribute('href', href); | ||
} | ||
|
||
/** | ||
* Remove the stylesheet with the specified key. | ||
* @param key The key for the slot to clear. | ||
*/ | ||
removeStyle(key: string) { | ||
let el = this._getLinkElementForKey(key); | ||
document.head.removeChild(el); | ||
} | ||
|
||
/** | ||
* Gets the `<link>` element for the specified key. | ||
* @param key The key for the slot whose element we want. | ||
* @param create Whether to create the element if it doesn't exist. | ||
* @returns {HTMLLinkElement} The `<link.` element. | ||
* @private | ||
*/ | ||
private _getLinkElementForKey(key: string, create: boolean = false): HTMLLinkElement { | ||
let className = `style-manager-${key}`; | ||
let linkEl = document.head.querySelector(`link[rel="stylesheet"].${className}`); | ||
if (!linkEl && create) { | ||
linkEl = document.createElement('link'); | ||
linkEl.setAttribute('rel', 'stylesheet'); | ||
linkEl.classList.add(className); | ||
document.head.appendChild(linkEl); | ||
} | ||
return linkEl as HTMLLinkElement; | ||
} | ||
} |
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 @@ | ||
export * from './theme-chooser'; |
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,16 @@ | ||
<button md-icon-button [md-menu-trigger-for]="themeMenu"> | ||
<md-icon>format_color_fill</md-icon> | ||
</button> | ||
|
||
<md-menu class="theme-chooser-menu" #themeMenu="mdMenu" x-position="before"> | ||
<md-grid-list cols="2"> | ||
<md-grid-tile *ngFor="let theme of themes"> | ||
<div md-menu-item (click)="installTheme(theme.href)"> | ||
<div class="theme-chooser-swatch"> | ||
<div class="theme-chooser-primary" [style.background]="theme.primary"></div> | ||
<div class="theme-chooser-accent" [style.background]="theme.accent"></div> | ||
</div> | ||
</div> | ||
</md-grid-tile> | ||
</md-grid-list> | ||
</md-menu> |
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,52 @@ | ||
$theme-chooser-menu-padding: 8px; | ||
$theme-chooser-grid-cell-size: 48px; | ||
$theme-chooser-grid-cells-per-row: 2; | ||
$theme-chooser-swatch-size: 36px; | ||
$theme-chooser-accent-stripe-size: 6px; | ||
|
||
|
||
.theme-chooser-menu { | ||
.md-menu-content { | ||
padding: $theme-chooser-menu-padding; | ||
} | ||
|
||
[md-menu-item] { | ||
flex: 0 0 auto; | ||
padding: 0; | ||
overflow: hidden; | ||
} | ||
|
||
.theme-chooser-swatch { | ||
position: relative; | ||
width: $theme-chooser-swatch-size; | ||
height: $theme-chooser-swatch-size; | ||
margin: ($theme-chooser-grid-cell-size - $theme-chooser-swatch-size) / 2; | ||
border-radius: 50%; | ||
overflow: hidden; | ||
transform: rotate(-45deg); | ||
|
||
&::after { | ||
content: ''; | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
width: 100%; | ||
height: 100%; | ||
box-sizing: border-box; | ||
border: 1px solid rgba(0,0,0,.2); | ||
border-radius: 50%; | ||
} | ||
} | ||
|
||
.theme-chooser-primary { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
.theme-chooser-accent { | ||
position: absolute; | ||
bottom: $theme-chooser-accent-stripe-size; | ||
width: 100%; | ||
height: $theme-chooser-accent-stripe-size; | ||
} | ||
} |
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,29 @@ | ||
import {MaterialModule} from '@angular/material'; | ||
import {async, TestBed} from '@angular/core/testing'; | ||
|
||
import {ThemeChooser} from './theme-chooser'; | ||
import {StyleManager} from '../style-manager'; | ||
|
||
|
||
describe('ThemeChooser', () => { | ||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [MaterialModule], | ||
declarations: [ThemeChooser], | ||
providers: [StyleManager] | ||
}); | ||
|
||
TestBed.compileComponents(); | ||
})); | ||
|
||
it('should install theme based on href', () => { | ||
const fixture = TestBed.createComponent(ThemeChooser); | ||
const component = fixture.componentInstance; | ||
const href = 'assets/pink-bluegrey.css'; | ||
spyOn(component._styleManager, 'setStyle'); | ||
component.installTheme(href); | ||
expect(component._styleManager.setStyle).toHaveBeenCalled(); | ||
expect(component._styleManager.setStyle).toHaveBeenCalledWith('theme', href); | ||
}); | ||
}); | ||
|
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,40 @@ | ||
import {Component, ViewEncapsulation, ChangeDetectionStrategy} from '@angular/core'; | ||
import {StyleManager} from '../style-manager/style-manager'; | ||
|
||
@Component({ | ||
selector: 'theme-chooser', | ||
templateUrl: 'theme-chooser.html', | ||
styleUrls: ['theme-chooser.scss'], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
encapsulation: ViewEncapsulation.None, | ||
}) | ||
export class ThemeChooser { | ||
themes = [ | ||
{ | ||
primary: '#673AB7', | ||
accent: '#FFC107', | ||
href: 'assets/deeppurple-amber.css' | ||
}, | ||
{ | ||
primary: '#3F51B5', | ||
accent: '#E91E63', | ||
href: 'assets/indigo-pink.css' | ||
}, | ||
{ | ||
primary: '#E91E63', | ||
accent: '#607D8B', | ||
href: 'assets/pink-bluegrey.css' | ||
}, | ||
{ | ||
primary: '#9C27B0', | ||
accent: '#4CAF50', | ||
href: 'assets/purple-green.css' | ||
}, | ||
]; | ||
|
||
constructor(private _styleManager : StyleManager) {} | ||
|
||
installTheme(href: string) { | ||
this._styleManager.setStyle('theme', href); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.