-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72d08f0
commit a48602d
Showing
9 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
webapp/src/app/components/theme-switcher/theme-switcher.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<app-button (click)="toggleTheme()" variant="outline" size="icon"> | ||
<div [@iconTrigger]="this.themeSwitcherService.currentTheme() ?? 'dark'"> | ||
<lucide-angular [name]="this.themeSwitcherService.currentTheme() === 'dark' ? 'sun' : 'moon'" class="size-5" /> | ||
</div> | ||
</app-button> |
30 changes: 30 additions & 0 deletions
30
webapp/src/app/components/theme-switcher/theme-switcher.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Component, inject } from '@angular/core'; | ||
import { LucideAngularModule } from 'lucide-angular'; | ||
import { ButtonComponent } from 'app/ui/button/button.component'; | ||
import { AppTheme, ThemeSwitcherService } from './theme-switcher.service'; | ||
import { animate, state, style, transition, trigger } from '@angular/animations'; | ||
|
||
@Component({ | ||
selector: 'app-theme-switcher', | ||
standalone: true, | ||
imports: [ButtonComponent, LucideAngularModule], | ||
templateUrl: './theme-switcher.component.html', | ||
animations: [ | ||
trigger('iconTrigger', [ | ||
state('*', style({ transform: 'rotate(0deg)' })), | ||
transition('light => dark', animate('0.25s ease-out', style({ transform: 'rotate(90deg)' }))), | ||
transition('dark => light', animate('0.25s ease-out', style({ transform: 'rotate(360deg)' }))) | ||
]) | ||
] | ||
}) | ||
export class ThemeSwitcherComponent { | ||
themeSwitcherService = inject(ThemeSwitcherService); | ||
|
||
toggleTheme() { | ||
if (this.themeSwitcherService.currentTheme() === AppTheme.DARK) { | ||
this.themeSwitcherService.setLightTheme(); | ||
} else { | ||
this.themeSwitcherService.setDarkTheme(); | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
webapp/src/app/components/theme-switcher/theme-switcher.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Injectable, signal } from '@angular/core'; | ||
|
||
export enum AppTheme { | ||
LIGHT = 'light', | ||
DARK = 'dark' | ||
} | ||
|
||
const IS_CLIENT_RENDER = typeof localStorage !== 'undefined'; | ||
const LOCAL_STORAGE_THEME_KEY = 'theme'; | ||
|
||
let selectedTheme: AppTheme | undefined = undefined; | ||
|
||
if (IS_CLIENT_RENDER) { | ||
selectedTheme = (localStorage.getItem(LOCAL_STORAGE_THEME_KEY) as AppTheme) || undefined; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class ThemeSwitcherService { | ||
currentTheme = signal<AppTheme | undefined>(selectedTheme); | ||
|
||
setLightTheme() { | ||
this.currentTheme.set(AppTheme.LIGHT); | ||
this.setToLocalStorage(AppTheme.LIGHT); | ||
this.removeClassFromHtml('dark'); | ||
} | ||
|
||
setDarkTheme() { | ||
this.currentTheme.set(AppTheme.DARK); | ||
this.setToLocalStorage(AppTheme.DARK); | ||
this.addClassToHtml('dark'); | ||
} | ||
|
||
setSystemTheme() { | ||
this.currentTheme.set(undefined); | ||
this.removeFromLocalStorage(); | ||
|
||
const isSystemDark = window?.matchMedia('(prefers-color-scheme: dark)').matches ?? false; | ||
if (isSystemDark) { | ||
this.addClassToHtml('dark'); | ||
} else { | ||
this.removeClassFromHtml('dark'); | ||
} | ||
} | ||
|
||
private addClassToHtml(className: string) { | ||
if (IS_CLIENT_RENDER) { | ||
this.removeClassFromHtml(className); | ||
document.documentElement.classList.add(className); | ||
} | ||
} | ||
|
||
private removeClassFromHtml(className: string) { | ||
if (IS_CLIENT_RENDER) { | ||
document.documentElement.classList.remove(className); | ||
} | ||
} | ||
|
||
private setToLocalStorage(theme: AppTheme) { | ||
if (IS_CLIENT_RENDER) { | ||
localStorage.setItem(LOCAL_STORAGE_THEME_KEY, theme); | ||
} | ||
} | ||
|
||
private removeFromLocalStorage() { | ||
if (IS_CLIENT_RENDER) { | ||
localStorage.removeItem(LOCAL_STORAGE_THEME_KEY); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
webapp/src/app/components/theme-switcher/theme-switcher.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { moduleMetadata, type Meta, type StoryObj } from '@storybook/angular'; | ||
import { LucideAngularModule, Sun, Moon } from 'lucide-angular'; | ||
import { ThemeSwitcherComponent } from './theme-switcher.component'; | ||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/writing-stories | ||
const meta: Meta<ThemeSwitcherComponent> = { | ||
title: 'Components/ThemeSwitcher', | ||
component: ThemeSwitcherComponent, | ||
tags: ['autodocs'], | ||
decorators: [ | ||
moduleMetadata({ | ||
imports: [LucideAngularModule.pick({ Sun, Moon }), BrowserAnimationsModule] | ||
}) | ||
] | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<ThemeSwitcherComponent>; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args | ||
export const Default: Story = { | ||
render: () => ({ | ||
template: `<app-theme-switcher />` | ||
}) | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters