Skip to content

Commit

Permalink
feat(addon-doc): improve UX when switch global dark mode on API page (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
splincode authored Sep 26, 2024
1 parent ff829fc commit f9579da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions projects/addon-doc/components/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
size="s"
tuiSwitch
type="checkbox"
[ngModel]="dark"
[ngModel]="dark()"
(ngModelChange)="onModeChange($event)"
/>
{{ texts[0] }}
Expand All @@ -23,7 +23,7 @@
<div
tuiResizable
class="t-wrapper"
[attr.tuiTheme]="dark ? 'dark' : 'light'"
[attr.tuiTheme]="theme()"
[class.t-wrapper_transparent]="!opaque"
>
<div class="t-content">
Expand Down
24 changes: 18 additions & 6 deletions projects/addon-doc/components/demo/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import {JsonPipe, Location, NgIf, NgTemplateOutlet} from '@angular/common';
import type {ElementRef, OnInit} from '@angular/core';
import {
ChangeDetectionStrategy,
Component,
computed,
ContentChild,
type ElementRef,
inject,
Input,
type OnInit,
signal,
TemplateRef,
ViewChild,
} from '@angular/core';
import {takeUntilDestroyed, toObservable} from '@angular/core/rxjs-interop';
import type {AbstractControl} from '@angular/forms';
import {FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
import type {Params, UrlTree} from '@angular/router';
Expand All @@ -29,6 +33,7 @@ import {TuiSwitch} from '@taiga-ui/kit/components/switch';
import {TuiChevron} from '@taiga-ui/kit/directives/chevron';
import {TuiSelectModule} from '@taiga-ui/legacy/components/select';
import {TuiTextfieldControllerModule} from '@taiga-ui/legacy/directives/textfield-controller';
import {skip} from 'rxjs';

const MIN_WIDTH = 160;

Expand Down Expand Up @@ -75,14 +80,21 @@ export class TuiDocDemo implements OnInit {
private readonly locationRef = inject(Location);
private readonly urlSerializer = inject(UrlSerializer);
private readonly urlStateHandler = inject(TUI_DOC_URL_STATE_HANDLER);
private readonly darkMode = inject(TUI_DARK_MODE);

@ContentChild(TemplateRef)
protected readonly template: TemplateRef<Record<string, unknown>> | null = null;

protected dark = tuiCoerceValueIsTrue(
this.params.darkMode ?? inject(TUI_DARK_MODE)(),
protected theme = computed(() => (this.dark() ? 'dark' : 'light'));

protected dark = signal(
tuiCoerceValueIsTrue(this.params.darkMode ?? this.darkMode()),
);

protected readonly $ = toObservable(this.darkMode)
.pipe(skip(1), takeUntilDestroyed())
.subscribe((mode) => this.onModeChange(mode));

protected testForm?: FormGroup;

protected readonly updateOnVariants = ['change', 'blur', 'submit'] as const;
Expand Down Expand Up @@ -115,9 +127,9 @@ export class TuiDocDemo implements OnInit {
this.updateUrl({sandboxWidth: this.sandboxWidth});
}

protected onModeChange(dark: boolean): void {
this.dark = dark;
this.updateUrl({sandboxWidth: this.sandboxWidth, darkMode: this.dark});
protected onModeChange(darkMode: boolean): void {
this.dark.set(darkMode);
this.updateUrl({sandboxWidth: this.sandboxWidth, darkMode});
}

protected toggleDetails(): void {
Expand Down

0 comments on commit f9579da

Please sign in to comment.