Skip to content

Commit 2dccf93

Browse files
committed
docs(sink): add select to navigate
1 parent 877d006 commit 2dccf93

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
1-
import { Component } from '@angular/core';
2-
import { RouterOutlet } from '@angular/router';
1+
import { Component, effect, inject, signal } from '@angular/core';
2+
import { NavigationEnd, Router, RouterOutlet } from '@angular/router';
3+
import { filter, map, tap } from 'rxjs';
34

45
@Component({
56
selector: 'app-root',
67
standalone: true,
78
template: `
89
<router-outlet />
10+
<select class="absolute bottom-4 right-4 font-mono" [value]="currentRoute()" (change)="onChange($event)">
11+
<option value="soba">/soba</option>
12+
<option value="cannon">/cannon</option>
13+
<option value="postprocessing">/postprocessing</option>
14+
</select>
915
`,
1016
imports: [RouterOutlet],
1117
})
12-
export class AppComponent {}
18+
export class AppComponent {
19+
currentRoute = signal('soba');
20+
21+
private router = inject(Router);
22+
23+
constructor() {
24+
effect((onCleanup) => {
25+
const sub = this.router.events
26+
.pipe(
27+
filter((event) => event instanceof NavigationEnd),
28+
map(() => this.router.url),
29+
tap((url) => {
30+
const [segment] = url.split('/').filter(Boolean);
31+
this.currentRoute.set(segment);
32+
}),
33+
)
34+
.subscribe();
35+
36+
onCleanup(() => sub.unsubscribe());
37+
});
38+
}
39+
40+
onChange(event: Event) {
41+
const target = event.target as HTMLSelectElement;
42+
void this.router.navigate([target.value]);
43+
}
44+
}

0 commit comments

Comments
 (0)