Skip to content

Commit 5ecda54

Browse files
committed
refactor: TT-332 changes to the button to change the page theme
1 parent 181bc76 commit 5ecda54

File tree

5 files changed

+26
-54
lines changed

5 files changed

+26
-54
lines changed
Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
<button
2-
class="w-10 h-6 rounded-full bg-grayTW-lightest flex items-center transition duration-300 focus:outline-none shadow"
2+
type="button"
3+
class="p-2 transition duration-150 ease-in-out bg-whiteTW rounded-md hover:bg-grayTW-lightest dark:bg-grayTW-dark dark:hover:bg-grayTW-darker dark:hover:bg-gray-700 hover:text-gray-700 focus:outline-none focus:bg-grayTW-lightest dark:focus:bg-grayTW-darker"
34
(click)="changeToDarkOrLightTheme()"
45
*ngIf="isFeatureToggleDarkModeActive"
56
>
6-
<div
7-
#themeToggle
8-
class="w-7 h-7 relative rounded-full transition duration-500 transform bg-warningTW -translate-x-1 p-1"
9-
>
10-
<ng-container *ngIf="isDarkTheme(); else elseToggle">
11-
<img src="assets/icons/moon.svg" alt="moon icon" />
12-
</ng-container>
13-
<ng-template #elseToggle>
14-
<img src="assets/icons/sun.svg" alt="sun icon" />
15-
</ng-template>
16-
</div>
7+
<div *ngIf="isDarkTheme(); then sunIcon; else moonIcon"></div>
8+
<ng-template #moonIcon><img class="w-5 h-5" src="assets/icons/moon.svg" alt="moon icon" /></ng-template>
9+
<ng-template #sunIcon><img class="w-5 h-5" src="assets/icons/sun.svg" alt="sun icon" /></ng-template>
1710
</button>

src/app/modules/shared/components/dark-mode/dark-mode.component.scss

Whitespace-only changes.

src/app/modules/shared/components/dark-mode/dark-mode.component.spec.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { ComponentFixture, TestBed} from '@angular/core/testing';
22
import { DarkModeComponent } from './dark-mode.component';
33

44
describe('DarkModeComponent', () => {
55
let component: DarkModeComponent;
66
let fixture: ComponentFixture<DarkModeComponent>;
7+
let html: HTMLElement;
78

89
beforeEach(async () => {
910
await TestBed.configureTestingModule({
@@ -14,6 +15,7 @@ describe('DarkModeComponent', () => {
1415
beforeEach(() => {
1516
fixture = TestBed.createComponent(DarkModeComponent);
1617
component = fixture.componentInstance;
18+
html = document.documentElement;
1719
fixture.detectChanges();
1820
});
1921

@@ -57,24 +59,21 @@ describe('DarkModeComponent', () => {
5759
component.theme = 'dark';
5860
component.addOrRemoveDarkMode();
5961
fixture.detectChanges();
60-
const htmlContainsDarkClass = document.documentElement.classList.contains('dark');
61-
expect(htmlContainsDarkClass).toBe(true);
62+
expect(html.classList.contains('dark')).toBe(true);
6263
});
6364

64-
it('should be changed to dark mode if the mode toggle is selected', () => {
65-
component.isFeatureToggleDarkModeActive = true;
66-
fixture.detectChanges();
67-
component.themeToggle.nativeElement.click();
65+
it('should not have dark class in the html tag when theme is light', () => {
66+
component.addOrRemoveDarkMode();
6867
fixture.detectChanges();
69-
expect(component.theme).toEqual('dark');
70-
expect(localStorage.getItem('theme')).toEqual('dark');
68+
expect(component.theme).toEqual('light');
69+
expect(html.classList.contains('dark')).toBe(false);
7170
});
7271

73-
it('call switchThemeToggleStyles() when ngAfterViewInit is called and isFeatureToggleDarkModeActive is true', () => {
74-
component.isFeatureToggleDarkModeActive = true;
75-
fixture.detectChanges();
76-
component.ngAfterViewInit();
72+
it('should change the value of the theme property, save it in the local storage and add the dark class to the HTML tag to change the theme', () => {
73+
component.changeToDarkOrLightTheme();
7774
fixture.detectChanges();
78-
expect(component.themeToggle.nativeElement.classList.contains('bg-warningTW')).toBe(true);
75+
expect(component.theme).toEqual('dark');
76+
expect(localStorage.getItem('theme')).toEqual('dark');
77+
expect(html.classList.contains('dark')).toBe(true);
7978
});
8079
});

src/app/modules/shared/components/dark-mode/dark-mode.component.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,29 @@
1-
import { AfterViewInit, ElementRef, ViewChild, Component, OnInit } from '@angular/core';
1+
import { Component, OnInit } from '@angular/core';
22
import { CookieService } from 'ngx-cookie-service';
33
import { FeatureToggle } from 'src/environments/enum';
44
import { FeatureToggleGeneralService } from '../../feature-toggles/feature-toggle-general/feature-toggle-general.service';
55

66
@Component({
77
selector: 'app-dark-mode',
88
templateUrl: './dark-mode.component.html',
9-
styleUrls: ['./dark-mode.component.scss'],
109
})
11-
export class DarkModeComponent implements OnInit, AfterViewInit {
10+
export class DarkModeComponent implements OnInit {
1211
public theme = 'light';
1312
public isFeatureToggleDarkModeActive: boolean;
1413

15-
@ViewChild('themeToggle') themeToggle: ElementRef;
16-
1714
constructor(
1815
private cookiesService: CookieService,
1916
private featureToggleGeneralService: FeatureToggleGeneralService
2017
) {}
2118

22-
ngOnInit(): void {
19+
ngOnInit() {
2320
this.featureToggleGeneralService.getActivated().subscribe(() => {
2421
this.isFeatureToggleDarkModeActive = this.cookiesService.get(FeatureToggle.DARK_MODE) === 'true';
22+
if (this.isFeatureToggleDarkModeActive) {
23+
this.checkThemeInLocalStorage();
24+
this.addOrRemoveDarkMode();
25+
}
2526
});
26-
if (this.isFeatureToggleDarkModeActive) {
27-
this.checkThemeInLocalStorage();
28-
this.addOrRemoveDarkMode();
29-
}
30-
}
31-
32-
ngAfterViewInit(): void {
33-
if (this.isFeatureToggleDarkModeActive) {
34-
this.switchThemeToggleStyles();
35-
}
3627
}
3728

3829
getLocalStorageTheme(): string {
@@ -70,17 +61,6 @@ export class DarkModeComponent implements OnInit, AfterViewInit {
7061
changeToDarkOrLightTheme(): void {
7162
this.theme = this.setTheme();
7263
this.setLocalStorageTheme(this.theme);
73-
this.switchThemeToggleStyles();
7464
this.addOrRemoveDarkMode();
7565
}
76-
77-
switchThemeToggleStyles(): void {
78-
if (this.isDarkTheme()) {
79-
this.themeToggle.nativeElement.classList.remove('bg-warningTW', '-translate-x-1');
80-
this.themeToggle.nativeElement.classList.add('bg-grayTW-lighter', 'translate-x-1/2');
81-
} else {
82-
this.themeToggle.nativeElement.classList.remove('bg-grayTW-lighter', 'translate-x-1/2');
83-
this.themeToggle.nativeElement.classList.add('bg-warningTW', '-translate-x-1');
84-
}
85-
}
8666
}

src/assets/icons/sun.svg

Lines changed: 1 addition & 1 deletion
Loading

0 commit comments

Comments
 (0)