Skip to content

Commit fb4cafa

Browse files
author
shaparenko_v
committed
fix: resolve task 21-anchor-navigation
1 parent fbb5e82 commit fb4cafa

5 files changed

Lines changed: 48 additions & 16 deletions

File tree

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
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';
4+
45
export const appConfig: ApplicationConfig = {
5-
providers: [provideRouter(appRoutes)],
6+
providers: [
7+
provideRouter(
8+
appRoutes,
9+
withInMemoryScrolling({
10+
anchorScrolling: 'enabled',
11+
scrollPositionRestoration: 'enabled',
12+
}),
13+
),
14+
],
615
};

apps/angular/21-anchor-navigation/src/app/foo.component.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import { Component } from '@angular/core';
2+
import { MatNavList } from '@angular/material/list';
3+
import { RouterLink } from '@angular/router';
24
import { NavButtonComponent } from './nav-button.component';
35

46
@Component({
5-
imports: [NavButtonComponent],
7+
imports: [NavButtonComponent, RouterLink, MatNavList],
68
selector: 'app-foo',
79
template: `
8-
Welcome to foo page
9-
<nav-button href="home" class="fixed left-1/2 top-3">Home Page</nav-button>
10-
<div class="h-screen bg-blue-200">section 1</div>
10+
<div class="h-screen bg-blue-200">
11+
<mat-nav-list>
12+
<h2>Welcome to foo page</h2>
13+
<app-nav-button [routerLink]="['/home']" class="fixed left-1/2 top-3">
14+
Home Page
15+
</app-nav-button>
16+
</mat-nav-list>
17+
</div>
1118
<div class="h-screen bg-red-200">section 2</div>
1219
`,
1320
})

apps/angular/21-anchor-navigation/src/app/home.component.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
import { Component } from '@angular/core';
2+
import { MatNavList } from '@angular/material/list';
3+
import { RouterLink } from '@angular/router';
24
import { NavButtonComponent } from './nav-button.component';
35

46
@Component({
5-
imports: [NavButtonComponent],
7+
imports: [NavButtonComponent, RouterLink, MatNavList, NavButtonComponent],
68
selector: 'app-home',
79
template: `
8-
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
910
<div id="top" class="h-screen bg-gray-500">
10-
Empty
11-
<nav-button href="#bottom">Scroll Bottom</nav-button>
11+
<mat-nav-list>
12+
<h2>Welcome to home page</h2>
13+
<app-nav-button [routerLink]="['/foo']" class="fixed left-1/2 top-3">
14+
Foo Page
15+
</app-nav-button>
16+
</mat-nav-list>
17+
18+
<div>
19+
<app-nav-button [routerLink]="[]" fragment="bottom">
20+
Scroll Bottom
21+
</app-nav-button>
22+
</div>
1223
</div>
24+
1325
<div id="bottom" class="h-screen bg-blue-300">
1426
I want to scroll each
15-
<nav-button href="#top">Scroll Top</nav-button>
27+
<app-nav-button [routerLink]="[]" fragment="top">
28+
Scroll Top
29+
</app-nav-button>
1630
</div>
1731
`,
1832
})
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
/* eslint-disable @angular-eslint/component-selector */
21
import { Component, input } from '@angular/core';
2+
import { RouterLink } from '@angular/router';
33

44
@Component({
5-
selector: 'nav-button',
5+
selector: 'app-nav-button',
66
template: `
7-
<a [href]="href()">
7+
<a [routerLink]="routerLink()">
88
<ng-content />
99
</a>
1010
`,
1111
host: {
1212
class: 'block w-fit border border-red-500 rounded-md p-4 m-2',
1313
},
14+
imports: [RouterLink],
1415
})
1516
export class NavButtonComponent {
16-
href = input('');
17+
routerLink = input<string[] | string>('');
18+
fragment = input<string | null>(null);
1719
}

apps/angular/21-anchor-navigation/src/styles.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
@tailwind components;
33
@tailwind utilities;
44

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

0 commit comments

Comments
 (0)