Skip to content

Commit eeca85c

Browse files
committed
feat(theme-provider): theme provider changes #320
1 parent 91a67a7 commit eeca85c

2 files changed

Lines changed: 15 additions & 28 deletions

File tree

tedi/providers/tedi.provider.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,20 @@
11
import {
22
EnvironmentProviders,
3-
inject,
43
makeEnvironmentProviders,
5-
provideAppInitializer,
64
} from "@angular/core";
75
import { TEDI_THEME_DEFAULT_TOKEN } from "../tokens/theme.token";
86
import {
9-
AVAILABLE_THEMES,
107
Theme,
11-
THEME_CLASS_PREFIX,
128
THEME_FALLBACK_VALUE,
139
} from "../services/theme/theme.service";
1410
import { TEDI_TRANSLATION_DEFAULT_TOKEN } from "../tokens/translation.token";
1511
import { Language } from "../services/translation/translation.service";
16-
import { DOCUMENT } from "@angular/common";
1712

1813
export interface TediConfig {
1914
theme?: Theme;
2015
language?: Language;
2116
}
2217

23-
function applyInitialTheme() {
24-
return () => {
25-
const document = inject(DOCUMENT);
26-
const defaultTheme = inject(TEDI_THEME_DEFAULT_TOKEN);
27-
const html = document.documentElement;
28-
29-
for (const t of AVAILABLE_THEMES) {
30-
html.classList.remove(`${THEME_CLASS_PREFIX}${t}`);
31-
}
32-
33-
html.classList.add(`${THEME_CLASS_PREFIX}${defaultTheme}`);
34-
};
35-
}
36-
3718
export function provideTedi(config: TediConfig = {}): EnvironmentProviders {
3819
return makeEnvironmentProviders([
3920
{
@@ -44,6 +25,5 @@ export function provideTedi(config: TediConfig = {}): EnvironmentProviders {
4425
provide: TEDI_TRANSLATION_DEFAULT_TOKEN,
4526
useValue: config.language ?? "et",
4627
},
47-
provideAppInitializer(applyInitialTheme()),
4828
]);
49-
}
29+
}

tedi/services/theme/theme.service.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { DOCUMENT } from "@angular/common";
33
import { TEDI_THEME_DEFAULT_TOKEN } from "../../tokens/theme.token";
44
import { cookieSignal } from "../../utils/cookies.util";
55

6-
export type Theme = "default" | "dark" | "rit";
7-
export const AVAILABLE_THEMES: Theme[] = ["default", "dark", "rit"];
6+
export type TEDITheme = "default" | "dark";
7+
export type Theme = TEDITheme | string;
88
export const THEME_CLASS_PREFIX = "tedi-theme--";
99
export const THEME_COOKIE_NAME = "tedi-theme";
1010
export const THEME_FALLBACK_VALUE: Theme = "default";
@@ -14,17 +14,24 @@ export class ThemeService {
1414
private readonly document = inject(DOCUMENT);
1515
private readonly defaultTheme = inject(TEDI_THEME_DEFAULT_TOKEN);
1616

17-
readonly theme = cookieSignal<Theme>(THEME_COOKIE_NAME, this.defaultTheme);
17+
readonly theme = cookieSignal<Theme>(
18+
THEME_COOKIE_NAME,
19+
this.defaultTheme
20+
);
1821

1922
constructor() {
2023
effect(() => {
2124
const html = this.document.documentElement;
25+
const nextTheme = this.theme();
2226

23-
for (const t of AVAILABLE_THEMES) {
24-
html.classList.remove(`${THEME_CLASS_PREFIX}${t}`);
27+
for (let i = html.classList.length - 1; i >= 0; i--) {
28+
const className = html.classList.item(i);
29+
if (className?.startsWith(THEME_CLASS_PREFIX)) {
30+
html.classList.remove(className);
31+
}
2532
}
2633

27-
html.classList.add(`${THEME_CLASS_PREFIX}${this.theme()}`);
34+
html.classList.add(`${THEME_CLASS_PREFIX}${nextTheme}`);
2835
});
2936
}
30-
}
37+
}

0 commit comments

Comments
 (0)