1
I’m having a problem registering my dynamic css tried to leave extension . scss and . css but both did not work.
Theme.service.ts
import { Injectable, Inject, EventEmitter } from '@angular/core';
import { ACTIVE_THEME, Theme, THEMES } from '../models/symbols';
@Injectable()
export class ThemeService {
themeChange = new EventEmitter<Theme>();
constructor(
@Inject(THEMES) public themes: Theme[],
@Inject(ACTIVE_THEME) public theme: string
) {
}
getTheme(name: string) {
const theme = this.themes.find(t => t.name === name);
if (!theme) {
throw new Error(`Theme not found: '${name}'`);
}
return theme;
}
getActiveTheme() {
return this.getTheme(this.theme);
}
getProperty(propName: string) {
return this.getActiveTheme().properties[propName];
}
setTheme(name: string) {
this.theme = name;
this.themeChange.emit( this.getActiveTheme());
}
registerTheme(theme: Theme) {
this.themes.push(theme);
}
updateTheme(name: string, properties: { [key: string]: string; }) {
const theme = this.getTheme(name);
theme.properties = {
...theme.properties,
...properties
};
if (name === this.theme) {
this.themeChange.emit(theme);
}
}
}
Symbols.ts
import { InjectionToken } from '@angular/core';
export const THEMES = new InjectionToken('THEMES');
export const ACTIVE_THEME = new InjectionToken('ACTIVE_THEME');
export interface Theme {
name: string;
properties: any;
}
export interface ThemeOptions {
themes: Theme[];
active: string;
}
Theme.ts
import { Theme } from './symbols';
export const lightTheme: Theme = {
name: 'light',
properties: {
'--background': '#f6f7f9',
'--on-background': '#E74E3C',
'--primary': '#1976d2',
'--on-primary': '#fff',
'--secondary': '#a8a8a8',
'--on-secondary': '#fff',
'--surface': '#fff',
'--on-surface': '#000',
'--error': '#E74E3C',
'--on-error': '#fff'
}
};
Themedirective.ts
import { Directive, OnInit, OnDestroy, ElementRef } from '@angular/core';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
import { Theme } from '../../models/symbols';
import { ThemeService } from '../../services/theme.service';
@Directive({
selector: '[theme]'
})
export class ThemeDirective implements OnInit, OnDestroy {
private _destroy$ = new Subject();
constructor(
private _elementRef: ElementRef,
private _themeService: ThemeService
) {}
ngOnInit() {
const active = this._themeService.getActiveTheme();
if (active) {
this.updateTheme(active);
}
this._themeService.themeChange
.pipe(takeUntil(this._destroy$))
.subscribe((theme: Theme) => this.updateTheme(theme));
}
ngOnDestroy() {
this._destroy$.next();
this._destroy$.complete();
}
updateTheme(theme: Theme) {
// project properties onto the element
for (const key in theme.properties) {
this._elementRef.nativeElement.style.setProperty(key, theme.properties[key]);
}
// remove old theme
for (const name of this._themeService.theme) {
this._elementRef.nativeElement.classList.remove(`${name}-theme`);
}
// alias element with theme name
this._elementRef.nativeElement.classList.add(`${theme.name}-theme`);
}
}
Appmodule
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { UtilModule } from './common/utils/util.module';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { CustomInterceptor } from './common/interceptors/http.interceptor';
import { ServiceWorkerModule } from '@angular/service-worker';
import { environment } from '../environments/environment';
import { JwtModule } from "@auth0/angular-jwt";
import { ThemeModule } from './common/directives/themes/theme.module';
import { lightTheme } from './common/models/light-theme';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
UtilModule,
HttpClientModule,
ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production }),
JwtModule,
ThemeModule.forRoot({
themes: [lightTheme],
active: 'light'
})
],
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: CustomInterceptor,
multi: true
}
],
bootstrap: [AppComponent]
})
export class AppModule { }
my modules and all content to have my dynamic css but is not working, the result is that the button should be the color #f6f7f9 . Someone can help ?