Skip to content

Commit a41f7d2

Browse files
authored
Merge pull request #186 from IgnaceMaes/highlight-active-title
feat: highlight active title in "On This Page"
2 parents 66fd3a8 + 7cea871 commit a41f7d2

File tree

7 files changed

+393
-229
lines changed

7 files changed

+393
-229
lines changed

addon/components/on-this-page.hbs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{{#if @toc}}
2-
<div class="on-this-page-wrapper">
2+
<div class="on-this-page-wrapper" {{highlight-active-title @toc}}>
33
<div class="on-this-page-wrapper-header">On this page</div>
44
<hr>
55
<ul>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { modifier } from 'ember-modifier';
2+
3+
export default modifier(function highlightActiveTitle(element, [toc]) {
4+
const observer = new IntersectionObserver((tocSections) => {
5+
tocSections.forEach((tocSection) => {
6+
const tocId = tocSection.target.getAttribute('aria-labelledby');
7+
const tocHeader = element.querySelector(`a[href="#${tocId}"]`);
8+
if (tocSection.intersectionRatio > 0) {
9+
tocHeader?.parentElement.classList.add('in-viewport');
10+
} else {
11+
tocHeader?.parentElement.classList.remove('in-viewport');
12+
}
13+
});
14+
});
15+
16+
// Track all content sections by id
17+
toc?.forEach((tocItem) => {
18+
const tocSection = document.querySelector(
19+
`section[aria-labelledby="${tocItem.id}"]`
20+
);
21+
if (tocSection) {
22+
observer.observe(tocSection);
23+
}
24+
});
25+
26+
return () => {
27+
observer.disconnect();
28+
};
29+
});

addon/styles/on-this-page.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
}
2626

2727
main .on-this-page-wrapper a {
28+
position: relative;
2829
text-decoration: none;
2930
color: var(--color-gray-600);
3031
background: none;
@@ -35,6 +36,28 @@ main .on-this-page-wrapper a {
3536
padding-left: var(--spacing-2);
3637
}
3738

39+
main .on-this-page-wrapper .in-viewport a::before {
40+
position: absolute;
41+
top: 0;
42+
left: -2px;
43+
bottom: 0;
44+
width: 4px;
45+
content: '';
46+
background-color: var(--color-brand);
47+
}
48+
49+
main .on-this-page-wrapper .in-viewport ~ .in-viewport a::before {
50+
content: unset;
51+
}
52+
53+
main .on-this-page-wrapper .in-viewport a {
54+
color: var(--color-gray-800);
55+
}
56+
57+
main .on-this-page-wrapper .in-viewport ~ .in-viewport a {
58+
color: var(--color-gray-600);
59+
}
60+
3861
main .on-this-page-wrapper a:hover {
3962
color: var(--color-gray-800);
4063
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default } from 'guidemaker-ember-template/modifiers/highlight-active-title';

0 commit comments

Comments
 (0)