Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions projects/stream-chat-angular/src/lib/theme.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,21 @@ describe('ThemeService', () => {
);
});

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

expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
'--white-snow',
null
);
});

it('should delete previously set custom dark theme properties', () => {
it('should delete previously set custom dark theme properties (theme is currently active)', () => {
service.theme$.next('dark');
spyOn(document.documentElement.style, 'setProperty');
service.customDarkThemeVariables = { '--white-snow': 'black' };
service.customDarkThemeVariables = { '--grey': 'darkgrey' };
Expand All @@ -88,4 +91,25 @@ describe('ThemeService', () => {
'#005fff'
);
});

it('should apply changes to current theme immediately', () => {
spyOn(document.documentElement.style, 'setProperty');
service.customLightThemeVariables = { '--white-snow': 'white' };

expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
'--white-snow',
'white'
);
});

it('should apply changes to inactive theme when theme becomes active', () => {
spyOn(document.documentElement.style, 'setProperty');
service.customDarkThemeVariables = { '--white-snow': 'black' };
service.theme$.next('dark');

expect(document.documentElement.style.setProperty).toHaveBeenCalledWith(
'--white-snow',
'black'
);
});
});
18 changes: 16 additions & 2 deletions projects/stream-chat-angular/src/lib/theme.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class ThemeService {
'--white-smoke': '#13151b',
'--white-snow': '#070a0d',
};
private variablesToDelete: { [key: string]: string }[] = [];

constructor() {
this.theme$.subscribe((theme) => {
Expand All @@ -40,6 +41,9 @@ export class ThemeService {
const lightVariables = this.customLightThemeVariables
? this.customLightThemeVariables
: {};
this.variablesToDelete.forEach((variables) =>
this.deleteVariables(variables)
);
if (theme === 'dark') {
this.deleteVariables(lightVariables);
this.setVariables(darkVariables);
Expand All @@ -58,8 +62,13 @@ export class ThemeService {
variables: { [key: string]: string } | undefined
) {
const prevVariables = this.customLightThemeVariables;
this.deleteVariables(prevVariables);
if (prevVariables) {
this.variablesToDelete.push(prevVariables);
}
this._customLightThemeVariables = variables;
if (this.theme$.getValue() === 'light') {
this.theme$.next('light');
}
}

get customDarkThemeVariables() {
Expand All @@ -70,8 +79,13 @@ export class ThemeService {
variables: { [key: string]: string } | undefined
) {
const prevVariables = this.customDarkThemeVariables;
this.deleteVariables(prevVariables);
if (prevVariables) {
this.variablesToDelete.push(prevVariables);
}
this._customDarkThemeVariables = variables;
if (this.theme$.getValue() === 'dark') {
this.theme$.next('dark');
}
}

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