-
Notifications
You must be signed in to change notification settings - Fork 473
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
Showing
14 changed files
with
161 additions
and
28 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import {effect, inject, InjectionToken, signal, type WritableSignal} from '@angular/core'; | ||
import {takeUntilDestroyed} from '@angular/core/rxjs-interop'; | ||
import {WA_LOCAL_STORAGE, WA_WINDOW} from '@ng-web-apis/common'; | ||
import {tuiCreateToken} from '@taiga-ui/cdk/utils/miscellaneous'; | ||
import {filter, fromEvent} from 'rxjs'; | ||
|
||
export const TUI_DARK_MODE_DEFAULT_KEY = 'tuiDark'; | ||
export const TUI_DARK_MODE_KEY = tuiCreateToken(TUI_DARK_MODE_DEFAULT_KEY); | ||
export const TUI_DARK_MODE = new InjectionToken< | ||
WritableSignal<boolean> & {reset(): void} | ||
>('', { | ||
factory: () => { | ||
let automatic = true; | ||
|
||
const storage = inject(WA_LOCAL_STORAGE); | ||
const key = inject(TUI_DARK_MODE_KEY); | ||
const saved = storage.getItem(key); | ||
const media = inject(WA_WINDOW).matchMedia('(prefers-color-scheme: dark)'); | ||
const result = signal(Boolean((saved && JSON.parse(saved)) ?? media.matches)); | ||
|
||
fromEvent(media, 'change') | ||
.pipe( | ||
filter(() => !storage.getItem(key)), | ||
takeUntilDestroyed(), | ||
) | ||
.subscribe(() => { | ||
automatic = true; | ||
result.set(media.matches); | ||
}); | ||
|
||
effect(() => { | ||
const value = String(result()); | ||
|
||
if (automatic) { | ||
automatic = false; | ||
} else { | ||
storage.setItem(key, value); | ||
} | ||
}); | ||
|
||
return Object.assign(result, { | ||
reset: () => { | ||
storage.removeItem(key); | ||
automatic = true; | ||
result.set(media.matches); | ||
}, | ||
}); | ||
}, | ||
}); |
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
17 changes: 17 additions & 0 deletions
17
projects/demo/src/modules/directives/theme/examples/2/index.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,17 @@ | ||
Dark mode enabled: {{ darkMode() }} | ||
<p> | ||
<button | ||
tuiButton | ||
(click)="darkMode.set(!darkMode())" | ||
> | ||
Toggle | ||
</button> | ||
<button | ||
tuiButton | ||
(click)="darkMode.reset()" | ||
> | ||
Reset | ||
</button> | ||
</p> | ||
<p>Add to Root to enable:</p> | ||
<code><tui-root [attr.tuiTheme]="darkMode() ? 'dark' : null'"></code> |
8 changes: 8 additions & 0 deletions
8
projects/demo/src/modules/directives/theme/examples/2/index.less
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,8 @@ | ||
p { | ||
display: flex; | ||
gap: 1rem; | ||
} | ||
|
||
code { | ||
white-space: nowrap !important; | ||
} |
26 changes: 26 additions & 0 deletions
26
projects/demo/src/modules/directives/theme/examples/2/index.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 {Component, inject} from '@angular/core'; | ||
import {changeDetection} from '@demo/emulate/change-detection'; | ||
import {encapsulation} from '@demo/emulate/encapsulation'; | ||
import {WA_LOCAL_STORAGE, WA_WINDOW} from '@ng-web-apis/common'; | ||
import {TUI_DARK_MODE, TUI_DARK_MODE_KEY, TuiButton} from '@taiga-ui/core'; | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [TuiButton], | ||
templateUrl: './index.html', | ||
styleUrls: ['./index.less'], | ||
encapsulation, | ||
changeDetection, | ||
}) | ||
export default class Example { | ||
private readonly key = inject(TUI_DARK_MODE_KEY); | ||
private readonly storage = inject(WA_LOCAL_STORAGE); | ||
private readonly media = inject(WA_WINDOW).matchMedia('(prefers-color-scheme: dark)'); | ||
|
||
protected readonly darkMode = inject(TUI_DARK_MODE); | ||
|
||
protected reset(): void { | ||
this.darkMode.set(this.media.matches); | ||
this.storage.removeItem(this.key); | ||
} | ||
} |
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