Skip to content

Commit

Permalink
feat: add theming, ngrx
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastrajan committed May 12, 2017
1 parent 0314346 commit 43a177f
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 25 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@ngrx/core": "^1.2.0",
"@ngrx/effects": "^2.0.3",
"@ngrx/store": "^2.2.2",
"classlist.js": "^1.1.20150312",
"core-js": "^2.4.1",
"hammerjs": "^2.0.8",
Expand Down
5 changes: 5 additions & 0 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { SettingsComponent } from './settings';

const routes: Routes = [
{
path: '',
children: []
}, {
path: 'settings',
component: SettingsComponent
}
];

Expand Down
5 changes: 4 additions & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<div [class]="themeClass">
<md-toolbar color="primary" class="toolbar">
<span>Angular NGrx Material Starter</span>
<span class="spacer"></span>
Expand All @@ -12,7 +13,7 @@
<md-icon>person</md-icon>
<span>Manage account</span>
</button>
<button md-menu-item>
<button md-menu-item routerLink="settings">
<md-icon>settings</md-icon>
<span>Settings</span>
</button>
Expand All @@ -25,4 +26,6 @@
<h1>
{{title}}
</h1>

<router-outlet></router-outlet>
</div>
7 changes: 5 additions & 2 deletions src/app/app.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { TestBed, async } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';

import { SharedModule } from './shared/shared.module';
import { SharedModule } from './shared';
import { CoreModule } from './core';

import { AppComponent } from './app.component';

describe('AppComponent', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
RouterTestingModule,
SharedModule
SharedModule,
CoreModule
],
declarations: [
AppComponent
Expand Down
32 changes: 30 additions & 2 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import { Component } from '@angular/core';
import { Component, OnDestroy } from '@angular/core';
import { OverlayContainer } from '@angular/material';
import { Store } from '@ngrx/store';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';

@Component({
selector: 'anms-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
export class AppComponent implements OnDestroy {

private unsubscribe$: Subject<void> = new Subject<void>();

themeClass: string;

title = 'anms works!';

constructor(
overlayContainer: OverlayContainer,
private store: Store<any>
) {
store.select('settings')
.takeUntil(this.unsubscribe$)
.subscribe(({ theme }) => {
const themeClass = theme.toLowerCase();
overlayContainer.themeClass = themeClass;
this.themeClass = themeClass;
});
}

ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}

}
9 changes: 8 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { HttpModule } from '@angular/http';

import { SharedModule } from './shared/shared.module';

import { CoreModule } from './core';
import { SettingsModule } from './settings';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

Expand All @@ -14,7 +17,11 @@ import { AppComponent } from './app.component';
BrowserModule,
HttpModule,
AppRoutingModule,
SharedModule

CoreModule,
SharedModule,

SettingsModule
],
declarations: [
AppComponent
Expand Down
6 changes: 5 additions & 1 deletion src/app/core/core.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StoreModule } from '@ngrx/store';

import { settingsReducer } from '../settings';

@NgModule({
imports: [
CommonModule
CommonModule,
StoreModule.provideStore({ settings: settingsReducer })
],
declarations: []
})
Expand Down
1 change: 1 addition & 0 deletions src/app/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './core.module';
3 changes: 3 additions & 0 deletions src/app/settings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './settings.module';
export * from './settings.reducer';
export * from './settings/settings.component';
15 changes: 15 additions & 0 deletions src/app/settings/settings.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { SharedModule } from '../shared';

import { SettingsComponent } from './settings/settings.component';

@NgModule({
imports: [
CommonModule,
SharedModule
],
declarations: [SettingsComponent]
})
export class SettingsModule { }
18 changes: 18 additions & 0 deletions src/app/settings/settings.reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Action } from '@ngrx/store';

export const SETTINGS_THEME = 'SETTINGS_THEME';

export const initialState = {
theme: 'default'
};

export function settingsReducer(state = initialState, action: Action) {
switch (action.type) {
case SETTINGS_THEME:
return { theme: action.payload };

default:
return state;
}
}

5 changes: 5 additions & 0 deletions src/app/settings/settings/settings.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<md-select placeholder="Select theme" [ngModel]="theme" (change)="onThemeSelect($event)" name="theme">
<md-option *ngFor="let t of themes" [value]="t.value">
{{t.label}}
</md-option>
</md-select>
File renamed without changes.
34 changes: 34 additions & 0 deletions src/app/settings/settings/settings.component.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';

import { CoreModule } from '../../core';
import { SharedModule } from '../../shared';

import { SettingsComponent } from './settings.component';

describe('SettingsComponent', () => {
let component: SettingsComponent;
let fixture: ComponentFixture<SettingsComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
NoopAnimationsModule,
CoreModule,
SharedModule
],
declarations: [ SettingsComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(SettingsComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should be created', () => {
expect(component).toBeTruthy();
});
});
40 changes: 40 additions & 0 deletions src/app/settings/settings/settings.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Store } from '@ngrx/store';
import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/takeUntil';
import { SETTINGS_THEME } from '../settings.reducer';

@Component({
selector: 'anms-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss']
})
export class SettingsComponent implements OnInit, OnDestroy {

private unsubscribe$: Subject<void> = new Subject<void>();
theme: string;

themes = [
{ value: 'DEFAULT-THEME', label: 'Default' },
{ value: 'DARK-THEME', label: 'Dark' }
];

constructor(private store: Store<any>) {
store.select('settings')
.takeUntil(this.unsubscribe$)
.subscribe(({ theme }) => this.theme = theme);
}

ngOnInit() {
}

ngOnDestroy(): void {
this.unsubscribe$.next();
this.unsubscribe$.complete();
}

onThemeSelect({ value }) {
this.store.dispatch({ type: SETTINGS_THEME, payload: value });
}

}
1 change: 1 addition & 0 deletions src/app/shared/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './shared.module';
3 changes: 3 additions & 0 deletions src/app/shared/shared.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
MdButtonModule,
MdToolbarModule,
MdMenuModule,
MdSelectModule,
MdIconModule
} from '@angular/material';

Expand All @@ -15,6 +16,7 @@ import {

MdButtonModule,
MdToolbarModule,
MdSelectModule,
MdMenuModule,
MdIconModule
],
Expand All @@ -25,6 +27,7 @@ import {

MdButtonModule,
MdMenuModule,
MdSelectModule,
MdToolbarModule,
MdIconModule
]
Expand Down
4 changes: 4 additions & 0 deletions src/styles-theme-dark.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$anms-dark-primary: mat-palette($mat-indigo, 900);
$anms-dark-accent: mat-palette($mat-amber, A200, A100, A400);
$anms-dark-warn: mat-palette($mat-deep-orange);
$anms-dark-theme: mat-dark-theme($anms-dark-primary, $anms-dark-accent, $anms-dark-warn);
5 changes: 5 additions & 0 deletions src/styles-theme-default.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$anms-primary: mat-palette($mat-amber);
$anms-accent: mat-palette($mat-pink, A200, A100, A400);
$anms-warn: mat-palette($mat-red);

$anms-theme: mat-light-theme($anms-primary, $anms-accent, $anms-warn);
24 changes: 6 additions & 18 deletions src/styles.scss
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@
@import 'reset.scss';

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@include mat-core();

// Define the palettes for your theme using the Material Design palettes available in palette.scss
// (imported above). For each palette, you can optionally specify a default, lighter, and darker
// hue.
$anms-primary: mat-palette($mat-amber);
$anms-accent: mat-palette($mat-pink, A200, A100, A400);
@import 'styles-theme-default.scss';
@import 'styles-theme-dark.scss';

// The warn palette is optional (defaults to red).
$anms-warn: mat-palette($mat-red);

// Create the theme object (a Sass map containing all of the palettes).
$anms-theme: mat-light-theme($anms-primary, $anms-accent, $anms-warn);

// Include theme styles for core and each component used in your app.
// Alternatively, you can import and @include the theme mixins for each component
// that you are using.
@include angular-material-theme($anms-theme);

.dark-theme {
@include angular-material-theme($anms-dark-theme);
}

0 comments on commit 43a177f

Please sign in to comment.