Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace effects with linkedSignals #32

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions libs/ngx-sonner/src/lib/toast.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
effect,
ElementRef,
input,
linkedSignal,
OnDestroy,
signal,
untracked,
viewChild,
} from '@angular/core';
import { cn } from './internal/cn';
Expand Down Expand Up @@ -235,7 +235,14 @@ export class ToastComponent implements AfterViewInit, OnDestroy {
this.heights().findIndex(height => height.toastId === this.toast().id)
);

offset = signal(0);
offset = linkedSignal({
source: () => ({
heightIndex: this.heightIndex(),
toastsHeightBefore: this.toastsHeightBefore(),
}),
computation: ({ heightIndex, toastsHeightBefore }) =>
Math.round(heightIndex * GAP + toastsHeightBefore),
});

closeTimerStartTimeRef = 0;
lastCloseTimerStartTimeRef = 0;
Expand Down Expand Up @@ -283,14 +290,6 @@ export class ToastComponent implements AfterViewInit, OnDestroy {
}));

constructor() {
effect(() => {
const heightIndex = this.heightIndex();
const toastsHeightBefore = this.toastsHeightBefore();
untracked(() =>
this.offset.set(Math.round(heightIndex * GAP + toastsHeightBefore))
);
});

effect(() => {
if (this.toast().updated) {
// if the toast has been updated after the initial render,
Expand Down
61 changes: 26 additions & 35 deletions libs/ngx-sonner/src/lib/toaster.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
ChangeDetectionStrategy,
Component,
computed,
effect,
ElementRef,
inject,
input,
linkedSignal,
numberAttribute,
OnDestroy,
PLATFORM_ID,
signal,
untracked,
viewChild,
ViewEncapsulation,
} from '@angular/core';
Expand All @@ -31,9 +30,9 @@ import { ToastComponent } from './toast.component';
import { Position, Theme, ToasterProps } from './types';

@Component({
selector: 'ngx-sonner-toaster',
imports: [ToastComponent, ToastFilterPipe, IconComponent, LoaderComponent],
template: `
selector: 'ngx-sonner-toaster',
imports: [ToastComponent, ToastFilterPipe, IconComponent, LoaderComponent],
template: `
@if (toasts().length > 0) {
<section
[attr.aria-label]="'Notifications ' + hotKeyLabel()"
Expand Down Expand Up @@ -100,9 +99,9 @@ import { Position, Theme, ToasterProps } from './types';
</section>
}
`,
styleUrl: 'toaster.component.css',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush
styleUrl: 'toaster.component.css',
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
// eslint-disable-next-line @angular-eslint/component-class-suffix
export class NgxSonnerToaster implements OnDestroy {
Expand Down Expand Up @@ -154,9 +153,15 @@ export class NgxSonnerToaster implements OnDestroy {
) as Position[]
);

expanded = signal(false);
expanded = linkedSignal({
source: this.toasts,
computation: toasts => toasts.length < 1,
});
actualTheme = linkedSignal({
source: this.theme,
computation: newTheme => this.getActualTheme(newTheme),
});
interacting = signal(false);
actualTheme = signal(this.getActualTheme(this.theme()));

listRef = viewChild<ElementRef<HTMLOListElement>>('listRef');
lastFocusedElementRef = signal<HTMLElement | null>(null);
Expand Down Expand Up @@ -186,17 +191,6 @@ export class NgxSonnerToaster implements OnDestroy {
.matchMedia('(prefers-color-scheme: dark)')
.addEventListener('change', this.handleThemePreferenceChange);
}

effect(() => {
if (this.toasts().length >= 1) {
untracked(() => this.expanded.set(false));
}
});

effect(() => {
const theme = this.theme();
untracked(() => this.actualTheme.set(this.getActualTheme(theme)));
});
}

ngOnDestroy() {
Expand Down Expand Up @@ -279,32 +273,29 @@ export class NgxSonnerToaster implements OnDestroy {
}
};

private getActualTheme(t: Theme) {
if (t !== 'system') {
return t;
private getActualTheme(theme: Theme): Theme {
if (theme !== 'system') {
return theme;
}

if (isPlatformBrowser(this.platformId)) {
if (
window.matchMedia &&
window.matchMedia('(prefers-color-scheme: dark)').matches
) {
return 'dark';
}

return 'light';
const prefersDark = window.matchMedia?.(
'(prefers-color-scheme: dark)'
).matches;
return prefersDark ? 'dark' : 'light';
}

return 'light';
}

getDocumentDirection(): ToasterProps['dir'] {
if (typeof window === 'undefined') return 'ltr';
if (typeof document === 'undefined') return 'ltr'; // For Fresh purpose
if (typeof window === 'undefined' || typeof document === 'undefined') {
return 'ltr';
}

const dirAttribute = document.documentElement.getAttribute('dir');

if (dirAttribute === 'auto' || !dirAttribute) {
if (!dirAttribute || dirAttribute === 'auto') {
return window.getComputedStyle(document.documentElement)
.direction as ToasterProps['dir'];
}
Expand Down
Loading