Skip to content

Commit

Permalink
feat(list): update active item on every navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickjahr committed Apr 16, 2024
1 parent d885b78 commit 00fb292
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Directive, HostListener, inject, input, OnInit } from '@angular/core';
import {
Directive,
effect,
HostListener,
inject,
input,
untracked,
} from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { toSignal } from '@angular/core/rxjs-interop';
import { map } from 'rxjs';
Expand All @@ -9,7 +16,7 @@ import { ListProviderDirective } from './list-provider.directive';
standalone: true,
selector: '[skListItemId]',
})
export class ListItemActiveDirective implements OnInit {
export class ListItemActiveDirective {
private readonly listService = inject(ListService);
private readonly listProvider = inject(ListProviderDirective, {
skipSelf: true,
Expand All @@ -21,7 +28,7 @@ export class ListItemActiveDirective implements OnInit {
this.activatedRoute.url.pipe(map((url) => url.join('/')))
);

itemId = input.required<string>({ alias: 'skListItemId' });
readonly itemId = input.required<string>({ alias: 'skListItemId' });

@HostListener('click', ['$event'])
onClick(event: MouseEvent): void {
Expand All @@ -37,15 +44,13 @@ export class ListItemActiveDirective implements OnInit {
}
}

ngOnInit(): void {
if (!this.listProvider.enableRouting()) {
return;
}
const active = this.activatedUrl()?.includes(this.itemId());
protected readonly updateActiveItemId = effect(() => {
const id = untracked(this.itemId);
const active = this.activatedUrl()?.includes(id);
if (active) {
setTimeout(() => {
this.listService.setActive(this.itemId());
this.listService.setActive(id);
}, 16);
}
}
});
}

0 comments on commit 00fb292

Please sign in to comment.