Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/assets/stylesheets/components/help.css
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,9 @@
transform: translateY(-1px);
text-decoration: none;
}

.help-outline-link {
&.active {
font-weight: bold;
}
}
44 changes: 44 additions & 0 deletions app/javascript/controllers/help_outline_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
static targets = ["link"]

connect() {
this.observer = new IntersectionObserver(
this.onIntersect.bind(this),
{
rootMargin: "0px 0px -80% 0px",
threshold: 0
}
)

this.headings().forEach(heading => this.observer.observe(heading))
}

disconnect() {
this.observer.disconnect()
}

onIntersect(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
this.setActive(entry.target.id)
}
})
}

setActive(id) {
this.linkTargets.forEach(link => {
link.classList.toggle(
"active",
link.getAttribute("href") === `#${id}`
)
})
}

headings() {
return Array.from(
document.querySelectorAll(".help-content h1, .help-content h2, .help-content h3")
)
}
}
6 changes: 4 additions & 2 deletions app/views/layouts/help.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
- if @outline.present?
.help-outline
.help-outline-header On this page
nav.help-outline-nav
nav.help-outline-nav data-controller="help-outline"
- @outline.each do |heading|
= link_to "##{heading[:id]}", class: "help-outline-link level-#{heading[:level]}" do
= link_to "##{heading[:id]}",
class: "help-outline-link level-#{heading[:level]}",
data: { help_outline_target: "link" } do
= heading[:title]

nav.help-nav
Expand Down