Skip to content

Commit 8576f16

Browse files
erkamyamancrisbeto
authored andcommitted
fix(docs-infra): highlight home page code for the resolved theme
`CodeBlock` picked `github-light` only when the theme was exactly `light`, but `Theme` also has `auto`, the value for anyone who has not opened the theme menu, and `theme()` is `null` during prerendering. Both fell to `github-dark`, so the samples in the Signals, Control Flow and Deferrable Views tabs on https://angular.dev rendered on a dark slab inside a light page. Resolve the theme in `ThemeManager`, which already owns the `auto` translation. `resolvedTheme` returns `light` when `theme()` is `null`, which happens only during prerendering, so `window` is never read. The device scheme moves into a signal so `auto` also reacts to OS scheme changes.
1 parent 947f3ff commit 8576f16

2 files changed

Lines changed: 15 additions & 6 deletions

File tree

‎adev/src/app/core/services/theme-manager.service.ts‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {DOCUMENT, isPlatformBrowser} from '@angular/common';
10-
import {PLATFORM_ID, Service, inject, signal} from '@angular/core';
10+
import {PLATFORM_ID, Service, computed, inject, signal} from '@angular/core';
1111
import {LOCAL_STORAGE} from '@angular/docs';
1212

1313
// Keep these constants in sync with the code in index.html
@@ -26,20 +26,29 @@ export class ThemeManager {
2626
private readonly platformId = inject(PLATFORM_ID);
2727

2828
readonly theme = signal<Theme | null>(this.getThemeFromLocalStorageValue());
29+
private readonly osScheme = signal<'dark' | 'light'>('light');
30+
readonly resolvedTheme = computed<'dark' | 'light'>(() => {
31+
const theme = this.theme();
32+
if (theme === null) {
33+
return 'light';
34+
}
35+
return theme === 'auto' ? this.osScheme() : theme;
36+
});
2937

3038
constructor() {
3139
if (!isPlatformBrowser(this.platformId)) {
3240
return;
3341
}
3442

43+
this.osScheme.set(preferredScheme());
3544
this.loadThemePreference();
3645
this.watchPreferredColorScheme();
3746
}
3847

3948
setTheme(theme: Theme): void {
4049
this.theme.set(theme);
4150
this.setThemeInLocalStorage();
42-
this.setThemeBodyClasses(theme === 'auto' ? preferredScheme() : theme);
51+
this.setThemeBodyClasses(this.resolvedTheme());
4352
}
4453

4554
// 1. Read theme preferences stored in localStorage
@@ -49,7 +58,7 @@ export class ThemeManager {
4958
const useTheme = savedUserPreference ?? 'auto';
5059

5160
this.theme.set(useTheme);
52-
this.setThemeBodyClasses(useTheme === 'auto' ? preferredScheme() : useTheme);
61+
this.setThemeBodyClasses(this.resolvedTheme());
5362
}
5463

5564
// Set theme classes on the body element
@@ -77,11 +86,11 @@ export class ThemeManager {
7786

7887
private watchPreferredColorScheme() {
7988
window.matchMedia(PREFERS_COLOR_SCHEME_DARK).addEventListener('change', (event) => {
89+
this.osScheme.set(event.matches ? 'dark' : 'light');
8090
if (this.theme() !== 'auto') {
8191
return;
8292
}
83-
const preferredScheme = event.matches ? 'dark' : 'light';
84-
this.setThemeBodyClasses(preferredScheme);
93+
this.setThemeBodyClasses(this.resolvedTheme());
8594
});
8695
}
8796
}

‎adev/src/app/features/home/components/code-block/code-block.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class CodeBlock {
3434
.codeToHtml(this.code(), {
3535
cssVariablePrefix: '--shiki-',
3636
lang: this.language(),
37-
theme: this.theme.theme() === 'light' ? 'github-light' : 'github-dark',
37+
theme: this.theme.resolvedTheme() === 'dark' ? 'github-dark' : 'github-light',
3838
})
3939
.then((hightlightedHtml) => {
4040
return this.sanitizer.bypassSecurityTrustHtml(hightlightedHtml);

0 commit comments

Comments
 (0)