Skip to content

Commit 9483d55

Browse files
committed
feat(challenge 21): enhance navigation with anchor scrolling and router link support
1 parent 543770b commit 9483d55

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
import { ApplicationConfig } from '@angular/core';
2-
import { provideRouter } from '@angular/router';
2+
import { provideRouter, withInMemoryScrolling } from '@angular/router';
33
import { appRoutes } from './app.routes';
44
export const appConfig: ApplicationConfig = {
5-
providers: [provideRouter(appRoutes)],
5+
providers: [
6+
provideRouter(
7+
appRoutes,
8+
withInMemoryScrolling({
9+
anchorScrolling: 'enabled',
10+
scrollPositionRestoration: 'enabled',
11+
}),
12+
),
13+
],
614
};
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
/* eslint-disable @angular-eslint/component-selector */
2-
import { Component, input } from '@angular/core';
2+
import { Component, computed, input } from '@angular/core';
3+
import { RouterLink } from '@angular/router';
34

45
@Component({
56
selector: 'nav-button',
7+
imports: [RouterLink],
68
template: `
7-
<a [href]="href()">
9+
<a [routerLink]="routerLink()" [fragment]="fragment()">
810
<ng-content />
911
</a>
1012
`,
@@ -14,4 +16,13 @@ import { Component, input } from '@angular/core';
1416
})
1517
export class NavButtonComponent {
1618
href = input('');
19+
20+
isAnchorLink = computed(() => this.href().startsWith('#'));
21+
routerLink = computed(() => {
22+
return this.isAnchorLink() ? [] : this.href();
23+
});
24+
25+
fragment = computed(() => {
26+
return this.isAnchorLink() ? this.href().substring(1) : undefined;
27+
});
1728
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
@import "tailwindcss";
22

33
/* You can add global styles to this file, and also import other style files */
4+
5+
html {
6+
scroll-behavior: smooth;
7+
}

0 commit comments

Comments
 (0)