Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 7f657ec

Browse files
committed
Have link no longer maintain its own 'active' state. Handle that in the toc.
1 parent 082db46 commit 7f657ec

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

src/app/shared/table-of-contents/table-of-contents.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<a [href]="_rootUrl + '#' + link.id"
55
*ngFor="let link of links; let i = index"
66
class="docs-level-{{link.type}} docs-link"
7-
[class.docs-active]="link.active">
7+
[class.docs-active]="_activeLinkIndex === i">
88
{{link.name}}
99
</a>
1010
</nav>

src/app/shared/table-of-contents/table-of-contents.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ describe('TableOfContents', () => {
4545
id: 'test',
4646
name: 'test',
4747
top: 0,
48-
active: false
4948
}
5049
];
5150

src/app/shared/table-of-contents/table-of-contents.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ interface Link {
1414
/* header type h3/h4 */
1515
type: string;
1616

17-
/* If the anchor is in view of the page */
18-
active: boolean;
19-
2017
/* name of the anchor */
2118
name: string;
2219

@@ -27,14 +24,15 @@ interface Link {
2724
@Component({
2825
selector: 'table-of-contents',
2926
styleUrls: ['./table-of-contents.scss'],
30-
templateUrl: './table-of-contents.html'
27+
templateUrl: './table-of-contents.html',
3128
})
3229
export class TableOfContents implements OnInit {
3330

3431
@Input() links: Link[] = [];
3532
@Input() container: string;
3633
@Input() headerSelectors = '.docs-markdown-h3,.docs-markdown-h4';
3734

35+
_activeLinkIndex: number;
3836
_rootUrl: string;
3937
private _scrollContainer: any;
4038
private _destroyed = new Subject();
@@ -103,7 +101,7 @@ export class TableOfContents implements OnInit {
103101
}
104102

105103
private createLinks(): Link[] {
106-
const links = [];
104+
const links: Link[] = [];
107105
const headers =
108106
Array.from(this._document.querySelectorAll(this.headerSelectors)) as HTMLElement[];
109107

@@ -114,10 +112,9 @@ export class TableOfContents implements OnInit {
114112
const {top} = header.getBoundingClientRect();
115113
links.push({
116114
name,
115+
top,
117116
type: header.tagName.toLowerCase(),
118-
top: top,
119-
id: header.id,
120-
active: false
117+
id: header.id
121118
});
122119
}
123120
}
@@ -126,9 +123,8 @@ export class TableOfContents implements OnInit {
126123
}
127124

128125
private onScroll(): void {
129-
for (let i = 0; i < this.links.length; i++) {
130-
this.links[i].active = this.isLinkActive(this.links[i], this.links[i + 1]);
131-
}
126+
this._activeLinkIndex = this.links
127+
.findIndex((link, i) => this.isLinkActive(link, this.links[i + 1]));
132128
}
133129

134130
private isLinkActive(currentLink: any, nextLink: any): boolean {

0 commit comments

Comments
 (0)