Skip to content

Instantly share code, notes, and snippets.

@denov
Created April 30, 2019 00:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save denov/4f3d1300f90f2eea2ca9f541126ea3da to your computer and use it in GitHub Desktop.
Save denov/4f3d1300f90f2eea2ca9f541126ea3da to your computer and use it in GitHub Desktop.
dojo2 theme injector
import {themeContext} from "./main";
import darkStyle from "./themes/dark";
import lightStyle from "./themes/light";
import defaultStyle from "./themes/default";
import * as defaultColors from "./themes/default/app.m.css";
import * as darkColors from "./themes/dark/app.m.css";
import * as lightColors from "./themes/light/app.m.css";
export enum Themes {
DEFAULT = 'DEFAULT',
DARK = 'DARK',
LIGHT = 'LIGHT'
}
export default class ThemeContext {
private _currentTheme: Themes;
constructor(defaultTheme: Themes) {
this._currentTheme = defaultTheme;
}
public currentTheme(): Themes {
return this._currentTheme;
}
public currentThemeColorByName(colorName: string): string {
const colors: { [ index: string ]: any } = {
[Themes.DEFAULT]: defaultColors,
[Themes.DARK]: darkColors,
[Themes.LIGHT]: lightColors
};
return colors[this._currentTheme][colorName];
}
public currentThemeColorByNumber(colorNumber: number): string {
return this.currentThemeColors()[colorNumber];
}
public currentThemeColors(): string[] {
const _defaultColors: string[] = [
defaultColors.color0, defaultColors.color1, defaultColors.color2, defaultColors.color3, defaultColors.color4,
defaultColors.color5, defaultColors.color6, defaultColors.color7, defaultColors.color8, defaultColors.color9, defaultColors.color10
];
const _darkColors: string[] = [
darkColors.color0, darkColors.color1, darkColors.color2, darkColors.color3, darkColors.color4,
darkColors.color5, darkColors.color6, darkColors.color7, darkColors.color8, darkColors.color9, darkColors.color10
];
const _lightColors: string[] = [
lightColors.color0, lightColors.color1, lightColors.color2, lightColors.color3, lightColors.color4,
lightColors.color5, lightColors.color6, lightColors.color7, lightColors.color8, lightColors.color9, lightColors.color10
];
const themeColors: {[index: string]: string[]} = {
'DEFAULT': _defaultColors,
'DARK': _darkColors,
'LIGHT': _lightColors,
};
return themeColors[this._currentTheme];
}
public get() {
return themeContext;
}
public set(theme: Themes) {
this._currentTheme = theme;
switch (theme) {
case Themes.DARK:
themeContext.set(darkStyle);
break;
case Themes.LIGHT:
themeContext.set(lightStyle);
break;
case Themes.DEFAULT:
themeContext.set(defaultStyle);
break;
}
}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment