feat(theming): changed theme selector from cycling to menu
LocationBasedServices/smartguide/pipeline/head This commit looks good

This commit is contained in:
Mats Paul Tončik
2026-08-02 13:34:21 +02:00
parent 4dd124d003
commit 26c4dc8fe7
3 changed files with 65 additions and 19 deletions
+40 -14
View File
@@ -10,12 +10,20 @@ import {
export type ThemeMode = 'light' | 'dark' | 'system';
export const THEME_LIGHT_ICON = 'light_mode';
export const THEME_DARK_ICON = 'dark_mode';
export const THEME_SYSTEM_ICON = 'desktop_windows';
export const THEME_MODES = ['light', 'dark', 'system'] as const;
@Injectable({
providedIn: 'root',
})
export class ThemeService {
private document = inject(DOCUMENT);
private renderer = inject(RendererFactory2).createRenderer(null, null);
private readonly document = inject(DOCUMENT);
private readonly renderer = inject(RendererFactory2).createRenderer(
null,
null,
);
private readonly storageKey = 'sg-theme-mode';
@@ -37,16 +45,37 @@ export class ThemeService {
}
});
/** Icon for current mode in UI */
readonly currentModeIcon = computed(() => {
switch (this.mode()) {
getIcon(mode: ThemeMode): string {
switch (mode) {
case 'light':
return 'light_mode';
return THEME_LIGHT_ICON;
case 'dark':
return 'dark_mode';
return THEME_DARK_ICON;
case 'system':
return 'desktop_windows';
return THEME_SYSTEM_ICON;
}
}
getLabel(mode: ThemeMode): string {
switch (mode) {
case 'light':
return 'Hell';
case 'dark':
return 'Dunkel';
case 'system':
return 'System';
}
}
/** Icon for current mode in UI */
readonly currentModeIcon = computed(() => this.getIcon(this.mode()));
readonly menuOptions = computed(() => {
return THEME_MODES.map((mode) => ({
mode: mode,
label: this.getLabel(mode),
icon: this.getIcon(mode),
}));
});
constructor() {
@@ -84,17 +113,14 @@ export class ThemeService {
}
cycleTheme(): void {
const modes: ThemeMode[] = ['system', 'light', 'dark'];
const modes = THEME_MODES;
const currentIndex = modes.indexOf(this.mode());
const nextIndex = (currentIndex + 1) % modes.length;
this.setTheme(modes[nextIndex]);
}
private readInitialMode(): ThemeMode {
const stored = localStorage.getItem(this.storageKey);
if (stored === 'light' || stored === 'dark' || stored === 'system') {
return stored;
}
return 'system';
const stored = localStorage.getItem(this.storageKey) as ThemeMode | null;
return stored ?? 'system';
}
}
@@ -137,15 +137,17 @@
<div class="sidebar-actions">
@if (collapsed()) {
<button
aria-label="Design ändern"
mat-icon-button
class="utility-icon-button"
type="button"
(click)="themeService.cycleTheme()"
[matTooltip]="'Design: ' + themeService.currentModeLabel()"
class="utility-icon-button"
ariaLabel="Design ändern"
matTooltip="Design ändern"
matTooltipPosition="right"
[attr.aria-label]="'Design: ' + themeService.currentModeLabel()">
[matMenuTriggerFor]="styleMenu">
<mat-icon>{{ themeService.currentModeIcon() }}</mat-icon>
</button>
<button
mat-icon-button
class="utility-icon-button"
@@ -158,9 +160,13 @@
</button>
} @else {
<button
aria-label="Design ändern"
class="utility-link"
type="button"
(click)="themeService.cycleTheme()">
ariaLabel="Design ändern"
matTooltip="Design ändern"
matTooltipPosition="right"
[matMenuTriggerFor]="styleMenu">
<mat-icon>{{ themeService.currentModeIcon() }}</mat-icon>
<span>Design: {{ themeService.currentModeLabel() }}</span>
</button>
@@ -171,3 +177,15 @@
}
</div>
</aside>
<mat-menu #styleMenu="matMenu">
@for (menuOption of themeService.menuOptions(); track $index) {
<button
[ariaLabel]="menuOption.label"
mat-menu-item
(click)="themeService.setTheme(menuOption.mode)">
<mat-icon>{{ menuOption.icon }}</mat-icon>
<span>{{ menuOption.label }}</span>
</button>
}
</mat-menu>
@@ -16,6 +16,7 @@ import { activeInstitutionPersonListQueryParams } from '../../person-list-query-
import { topicQueryParamKeys } from '../../../resolvers/topic-page.resolver';
import { SortDirection, TopicSortBy, WorkSortBy } from '../../../api';
import { workQueryParamKeys } from '../../../resolvers/work-page.resolver';
import { MatMenuModule } from '@angular/material/menu';
type NavItem = {
label: string;
@@ -49,6 +50,7 @@ type NavItemGroup = {
MatCardModule,
MatCardContent,
MatProgressSpinner,
MatMenuModule,
],
templateUrl: './app-menu.component.html',
styleUrl: './app-menu.component.scss',