Skip to content

Commit bda02f5

Browse files
committed
fix: Immediately apply changes to theme variables #188
1 parent 0a2c280 commit bda02f5

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

projects/stream-chat-angular/src/lib/theme.service.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,21 @@ describe('ThemeService', () => {
5656
);
5757
});
5858

59-
it('should delete previously set custom light theme properties', () => {
59+
it('should delete previously set custom light theme properties (theme is currently inactive)', () => {
60+
service.theme$.next('dark');
6061
spyOn(document.documentElement.style, 'setProperty');
6162
service.customLightThemeVariables = { '--white-snow': 'white' };
6263
service.customLightThemeVariables = { '--grey': 'darkgrey' };
64+
service.theme$.next('light');
6365

6466
expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
6567
'--white-snow',
6668
null
6769
);
6870
});
6971

70-
it('should delete previously set custom dark theme properties', () => {
72+
it('should delete previously set custom dark theme properties (theme is currently active)', () => {
73+
service.theme$.next('dark');
7174
spyOn(document.documentElement.style, 'setProperty');
7275
service.customDarkThemeVariables = { '--white-snow': 'black' };
7376
service.customDarkThemeVariables = { '--grey': 'darkgrey' };
@@ -88,4 +91,25 @@ describe('ThemeService', () => {
8891
'#005fff'
8992
);
9093
});
94+
95+
it('should apply changes to current theme immediately', () => {
96+
spyOn(document.documentElement.style, 'setProperty');
97+
service.customLightThemeVariables = { '--white-snow': 'white' };
98+
99+
expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
100+
'--white-snow',
101+
'white'
102+
);
103+
});
104+
105+
it('should apply changes to inactive theme when theme becomes active', () => {
106+
spyOn(document.documentElement.style, 'setProperty');
107+
service.customDarkThemeVariables = { '--white-snow': 'black' };
108+
service.theme$.next('dark');
109+
110+
expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
111+
'--white-snow',
112+
'black'
113+
);
114+
});
91115
});

projects/stream-chat-angular/src/lib/theme.service.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export class ThemeService {
3131
'--white-smoke': '#13151b',
3232
'--white-snow': '#070a0d',
3333
};
34+
private variablesToDelete: { [key: string]: string }[] = [];
3435

3536
constructor() {
3637
this.theme$.subscribe((theme) => {
@@ -40,6 +41,9 @@ export class ThemeService {
4041
const lightVariables = this.customLightThemeVariables
4142
? this.customLightThemeVariables
4243
: {};
44+
this.variablesToDelete.forEach((variables) =>
45+
this.deleteVariables(variables)
46+
);
4347
if (theme === 'dark') {
4448
this.deleteVariables(lightVariables);
4549
this.setVariables(darkVariables);
@@ -58,8 +62,13 @@ export class ThemeService {
5862
variables: { [key: string]: string } | undefined
5963
) {
6064
const prevVariables = this.customLightThemeVariables;
61-
this.deleteVariables(prevVariables);
65+
if (prevVariables) {
66+
this.variablesToDelete.push(prevVariables);
67+
}
6268
this._customLightThemeVariables = variables;
69+
if (this.theme$.getValue() === 'light') {
70+
this.theme$.next('light');
71+
}
6372
}
6473

6574
get customDarkThemeVariables() {
@@ -70,8 +79,13 @@ export class ThemeService {
7079
variables: { [key: string]: string } | undefined
7180
) {
7281
const prevVariables = this.customDarkThemeVariables;
73-
this.deleteVariables(prevVariables);
82+
if (prevVariables) {
83+
this.variablesToDelete.push(prevVariables);
84+
}
7485
this._customDarkThemeVariables = variables;
86+
if (this.theme$.getValue() === 'dark') {
87+
this.theme$.next('dark');
88+
}
7589
}
7690

7791
private deleteVariables(variables: { [key: string]: string } | undefined) {

0 commit comments

Comments
 (0)