Skip to content

Commit abc4bbb

Browse files
moozzidutow
authored andcommitted
Added help navigation highlighting
1 parent 6babb12 commit abc4bbb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

app/assets/stylesheets/components/help.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,9 @@
290290
transform: translateY(-1px);
291291
text-decoration: none;
292292
}
293+
294+
.help-outline-link {
295+
&.active {
296+
font-weight: bold;
297+
}
298+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
static targets = ["link"]
5+
6+
connect() {
7+
this.observer = new IntersectionObserver(
8+
this.onIntersect.bind(this),
9+
{
10+
rootMargin: "0px 0px -80% 0px",
11+
threshold: 0
12+
}
13+
)
14+
15+
this.headings().forEach(heading => this.observer.observe(heading))
16+
}
17+
18+
disconnect() {
19+
this.observer.disconnect()
20+
}
21+
22+
onIntersect(entries) {
23+
entries.forEach(entry => {
24+
if (entry.isIntersecting) {
25+
this.setActive(entry.target.id)
26+
}
27+
})
28+
}
29+
30+
setActive(id) {
31+
this.linkTargets.forEach(link => {
32+
link.classList.toggle(
33+
"active",
34+
link.getAttribute("href") === `#${id}`
35+
)
36+
})
37+
}
38+
39+
headings() {
40+
return Array.from(
41+
document.querySelectorAll(".help-content h1, .help-content h2, .help-content h3")
42+
)
43+
}
44+
}

app/views/layouts/help.html.slim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
- if @outline.present?
77
.help-outline
88
.help-outline-header On this page
9-
nav.help-outline-nav
9+
nav.help-outline-nav data-controller="help-outline"
1010
- @outline.each do |heading|
11-
= link_to "##{heading[:id]}", class: "help-outline-link level-#{heading[:level]}" do
11+
= link_to "##{heading[:id]}",
12+
class: "help-outline-link level-#{heading[:level]}",
13+
data: { help_outline_target: "link" } do
1214
= heading[:title]
1315

1416
nav.help-nav

0 commit comments

Comments
 (0)