Skip to content

Commit

Permalink
feat(21-anchor-navigation): implement anchor navigation with smooth s…
Browse files Browse the repository at this point in the history
…crolling
  • Loading branch information
michalgrzegorczyk-dev committed Nov 16, 2024
1 parent bdc6d9e commit 72082c3
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion apps/angular/21-anchor-navigation/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { RouterOutlet } from '@angular/router';
imports: [RouterOutlet],
selector: 'app-root',
template: `
<router-outlet></router-outlet>
<router-outlet />
`,
})
export class AppComponent {}
13 changes: 11 additions & 2 deletions apps/angular/21-anchor-navigation/src/app/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { ApplicationConfig } from '@angular/core';
import { provideRouter } from '@angular/router';
import { provideRouter, withInMemoryScrolling } from '@angular/router';
import { appRoutes } from './app.routes';

export const appConfig: ApplicationConfig = {
providers: [provideRouter(appRoutes)],
providers: [
provideRouter(
appRoutes,
withInMemoryScrolling({
scrollPositionRestoration: 'enabled',
anchorScrolling: 'enabled',
}),
),
],
};
4 changes: 3 additions & 1 deletion apps/angular/21-anchor-navigation/src/app/foo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import { NavButtonComponent } from './nav-button.component';
selector: 'app-foo',
template: `
Welcome to foo page
<nav-button href="home" class="fixed left-1/2 top-3">Home Page</nav-button>
<nav-button route="/home" class="fixed left-1/2 top-3">
Home Page
</nav-button>
<div class="h-screen bg-blue-200">section 1</div>
<div class="h-screen bg-red-200">section 2</div>
`,
Expand Down
7 changes: 4 additions & 3 deletions apps/angular/21-anchor-navigation/src/app/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { NavButtonComponent } from './nav-button.component';
imports: [NavButtonComponent],
selector: 'app-home',
template: `
<nav-button href="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
<nav-button route="/foo" class="fixed left-1/2 top-3">Foo Page</nav-button>
<div id="top" class="h-screen bg-gray-500">
Empty
<nav-button href="#bottom">Scroll Bottom</nav-button>
<nav-button fragment="bottom">Scroll Bottom</nav-button>
</div>
<div id="bottom" class="h-screen bg-blue-300">
I want to scroll each
<nav-button href="#top">Scroll Top</nav-button>
<nav-button fragment="top">Scroll Top</nav-button>
</div>
`,
})
Expand Down
14 changes: 10 additions & 4 deletions apps/angular/21-anchor-navigation/src/app/nav-button.component.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
/* eslint-disable @angular-eslint/component-selector */
import { Component, Input } from '@angular/core';
import { Component, input } from '@angular/core';
import { RouterLink } from '@angular/router';

@Component({
selector: 'nav-button',
standalone: true,
imports: [RouterLink],
template: `
<a [href]="href">
<ng-content></ng-content>
<a
[routerLink]="route()"
[fragment]="fragment() === '' ? undefined : fragment()">
<ng-content />
</a>
`,
host: {
class: 'block w-fit border border-red-500 rounded-md p-4 m-2',
},
})
export class NavButtonComponent {
@Input() href = '';
route = input('');
fragment = input('');
}
3 changes: 3 additions & 0 deletions apps/angular/21-anchor-navigation/src/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
@tailwind utilities;

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

0 comments on commit 72082c3

Please sign in to comment.