Skip to content

Commit

Permalink
chore(demo): fix smooth scrolling for anchor links (#1400)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsbarsukov committed Jul 22, 2024
1 parent dce3a7b commit 59ba22b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
43 changes: 41 additions & 2 deletions projects/demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {ViewportScroller} from '@angular/common';
import {ChangeDetectionStrategy, Component, inject} from '@angular/core';
import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
import {RouterLink} from '@angular/router';
import {DemoPath} from '@demo/constants';
import {TuiDocMain} from '@taiga-ui/addon-doc';
import {ResizeObserverService} from '@ng-web-apis/resize-observer';
import {TUI_DOC_PAGE_LOADED, TuiDocMain} from '@taiga-ui/addon-doc';
import {tuiInjectElement} from '@taiga-ui/cdk';
import {TuiLink} from '@taiga-ui/core';
import {debounceTime, map, startWith} from 'rxjs';

@Component({
standalone: true,
Expand All @@ -11,7 +16,41 @@ import {TuiLink} from '@taiga-ui/core';
templateUrl: './app.component.html',
styleUrls: ['./app.style.less'],
changeDetection: ChangeDetectionStrategy.OnPush,
providers: [
ResizeObserverService,
{
provide: TUI_DOC_PAGE_LOADED,
useFactory: () => {
const host = tuiInjectElement();

return inject(ResizeObserverService).pipe(
startWith(null),
debounceTime(0), // Synchronous scrollIntoView (after click) does not work https://stackoverflow.com/a/56971002
map(() => {
const exampleElements = Array.from(
host.querySelectorAll('tui-doc-example'),
);

const codeElements = Array.from(
host.querySelectorAll('tui-doc-code'),
);

return (
exampleElements.every((el) =>
el.querySelector('.t-example'),
) && codeElements.every((el) => el.querySelector('.t-code'))
);
}),
takeUntilDestroyed(),
);
},
},
],
})
export class App {
protected readonly stackblitzStarterPath = `/${DemoPath.Stackblitz}`;

constructor() {
inject(ViewportScroller).setOffset([0, 64]);
}
}
1 change: 1 addition & 0 deletions projects/demo/src/app/app.style.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@import '@taiga-ui/core/styles/taiga-ui-local.less';

:host {
display: block;
font: var(--tui-font-text-m);
color: var(--tui-text-primary);
}
Expand Down
7 changes: 7 additions & 0 deletions projects/demo/src/styles.less
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
@import '@taiga-ui/core/styles/taiga-ui-local.less';

body {
margin: 0;
}

html,
body {
.scroll-behavior();
height: 100%;

@media (prefers-reduced-motion) {
scroll-behavior: auto;
}
}

html {
Expand Down

0 comments on commit 59ba22b

Please sign in to comment.