-
Notifications
You must be signed in to change notification settings - Fork 27.2k
feat(docs-infra): add dark mode to documentation web site #40257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a4e170a
788a2a9
fdce7a6
f41ac5c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| <button mat-icon-button aria-label="Theme switcher button" (click)="currentTheme.name === 'night-theme' ? selectTheme('ng-io-theme') : selectTheme('night-theme')"> | ||
| <mat-icon>{{currentTheme.name === 'night-theme' ? themes[0].icon : themes[1].icon}}</mat-icon> | ||
| </button> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import {async, TestBed} from '@angular/core/testing'; | ||
| import { ThemePickerComponent } from './theme-picker.component'; | ||
| import { ThemeStorageService } from './theme-storage/theme-storage.service'; | ||
|
|
||
|
|
||
| describe('ThemePicker', () => { | ||
| let themeStorageService: any; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using |
||
|
|
||
| beforeEach(async(() => { | ||
| themeStorageService = jasmine.createSpyObj('themeStorageService', ['getStoredThemeName', 'storeTheme']); | ||
| TestBed.configureTestingModule({ | ||
| declarations: [ThemePickerComponent], | ||
| providers: [ { provide: ThemeStorageService, useValue: themeStorageService } ] | ||
| }).compileComponents(); | ||
| })); | ||
|
|
||
| it('should install theme based on name', () => { | ||
| const fixture = TestBed.createComponent(ThemePickerComponent); | ||
| const component = fixture.componentInstance; | ||
| const name = 'night-theme'; | ||
| component.selectTheme(name); | ||
| expect(themeStorageService.storeTheme).toHaveBeenCalledWith(component.themes[1]); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import { | ||
| ChangeDetectionStrategy, | ||
| Component, | ||
| ViewEncapsulation, | ||
| } from '@angular/core'; | ||
| import {DocsSiteTheme, ThemeStorageService} from './theme-storage/theme-storage.service'; | ||
|
|
||
| @Component({ | ||
| selector: 'theme-picker', | ||
| templateUrl: 'theme-picker.component.html', | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| encapsulation: ViewEncapsulation.None | ||
| }) | ||
| export class ThemePickerComponent { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just |
||
| readonly themes: DocsSiteTheme[] = [ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this theme data structure is necessary for angular.io. On the material site, we need these fields for additional behavior. For example, we need a display name because we show the options in a dropdown menu, which is not necessary here. I think this can be simplified down to a single string value, |
||
| { | ||
| primary: '#1976d2', | ||
| accent: '#e91e63', | ||
| displayName: 'Light Theme', | ||
| name: 'ng-io-theme', | ||
| icon: 'light_mode' | ||
| }, | ||
| { | ||
| primary: '#00796b', | ||
| accent: '#80cbc4', | ||
| displayName: 'Night Theme', | ||
| name: 'night-theme', | ||
| isDark: true, | ||
| icon: 'dark_mode' | ||
| }, | ||
| ]; | ||
|
|
||
| readonly defaultTheme: DocsSiteTheme = this.themes[0]; | ||
| currentTheme: DocsSiteTheme = this.themes[0]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is more a style preference, but I would use |
||
|
|
||
| constructor(private themeStorageService: ThemeStorageService) { | ||
| const themeName = this.themeStorageService.getStoredThemeName(); | ||
| if (themeName) { | ||
| this.selectTheme(themeName); | ||
| } | ||
| } | ||
|
|
||
| selectTheme(themeName: string) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is only doing light/dark, I would make this |
||
| const theme = this.themes.find(currentTheme => currentTheme.name === themeName); | ||
| this.currentTheme = theme || this.defaultTheme; | ||
| if (this.currentTheme.name === this.defaultTheme.name) { | ||
| document.body.classList.value = ''; | ||
| } else { | ||
| document.body.classList.value = this.currentTheme.name; | ||
| } | ||
| this.themeStorageService.storeTheme(this.currentTheme); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import {DocsSiteTheme, ThemeStorageService} from './theme-storage.service'; | ||
|
|
||
|
|
||
| const testStorageKey = ThemeStorageService.storageKey; | ||
| const testTheme: DocsSiteTheme = { | ||
| primary: '#000000', | ||
| accent: '#ffffff', | ||
| name: 'test-theme', | ||
| icon: 'test-icon' | ||
| }; | ||
|
|
||
| describe('ThemeStorage Service', () => { | ||
| const service = new ThemeStorageService(); | ||
| const getCurrTheme = () => window.localStorage.getItem(testStorageKey); | ||
| const secondTestTheme = { | ||
| primary: '#666666', | ||
| accent: '#333333', | ||
| name: 'other-test-theme', | ||
| icon: 'other-test-icon' | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| window.localStorage[testStorageKey] = testTheme.name; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| window.localStorage.clear(); | ||
| }); | ||
|
|
||
| it('should set the current theme name', () => { | ||
| expect(getCurrTheme()).toEqual(testTheme.name); | ||
| service.storeTheme(secondTestTheme); | ||
| expect(getCurrTheme()).toEqual(secondTestTheme.name); | ||
| }); | ||
|
|
||
| it('should get the current theme name', () => { | ||
| const theme = service.getStoredThemeName(); | ||
| expect(theme).toEqual(testTheme.name); | ||
| }); | ||
|
|
||
| it('should clear the stored theme data', () => { | ||
| expect(getCurrTheme()).not.toBeNull(); | ||
| service.clearStorage(); | ||
| expect(getCurrTheme()).toBeNull(); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import {Injectable} from '@angular/core'; | ||
|
|
||
| export interface DocsSiteTheme { | ||
| name: string; | ||
| displayName?: string; | ||
| accent: string; | ||
| primary: string; | ||
| isDark?: boolean; | ||
| icon: string; | ||
| isDefault?: boolean; | ||
| } | ||
|
|
||
|
|
||
| @Injectable() | ||
| export class ThemeStorageService { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would just do |
||
| static storageKey = 'docs-theme-storage-current-name'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe just |
||
|
|
||
| storeTheme(theme: DocsSiteTheme) { | ||
| try { | ||
| window.localStorage[ThemeStorageService.storageKey] = theme.name; | ||
| } catch { } | ||
| } | ||
|
|
||
| getStoredThemeName(): string | null { | ||
| try { | ||
| return window.localStorage[ThemeStorageService.storageKey] || null; | ||
| } catch { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| clearStorage() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method doesn't appear to ever be called in the app so it can probably be dropped |
||
| try { | ||
| window.localStorage.removeItem(ThemeStorageService.storageKey); | ||
| } catch { } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the aria-label here should be either
"Switch to light mode"or"Switch to dark mode"(based on the active theme).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the icon also conveys valuable information of if the site is currently in light or dark mode, I'd recommend adding "sun icon" and "moon icon" to aria-labels