Skip to content

Commit 1e13e91

Browse files
committed
add navbar highlighting to guides
1 parent ec4558a commit 1e13e91

File tree

2 files changed

+147
-6
lines changed

2 files changed

+147
-6
lines changed

guides/assets/javascripts/guides.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
backToTop.addEventListener("click", function(e) {
5353
e.preventDefault();
5454
window.scrollTo({ top: 0, behavior: "smooth" });
55+
resetNavPosition();
5556
});
5657

5758
var toggleBackToTop = function() {
@@ -102,5 +103,87 @@
102103
}, 3000);
103104
e.clearSelection();
104105
});
106+
107+
var mainColElems = Array.from(document.getElementById("mainCol").children);
108+
var subCol = document.querySelector("#subCol");
109+
var navLinks = subCol.querySelectorAll("a");
110+
var DESKTOP_THRESHOLD = 1024;
111+
112+
var matchingNavLink = function (elem) {
113+
if(!elem) return;
114+
var index = mainColElems.indexOf(elem);
115+
116+
var match;
117+
while (index >= 0 && !match) {
118+
var link = mainColElems[index].querySelector(".anchorlink");
119+
if (link) {
120+
match = subCol.querySelector('[href="' + link.getAttribute("href") + '"]');
121+
}
122+
index--;
123+
}
124+
return match;
125+
}
126+
127+
var removeHighlight = function () {
128+
for (var i = 0, n = navLinks.length; i < n; i++) {
129+
navLinks[i].classList.remove("active");
130+
}
131+
}
132+
133+
var updateHighlight = function (elem) {
134+
if (window.innerWidth > DESKTOP_THRESHOLD && !elem?.classList.contains("active")) {
135+
removeHighlight();
136+
if (!elem) return;
137+
elem.classList.add("active");
138+
elem.scrollIntoView({ block: 'center', inline: 'end' });
139+
}
140+
}
141+
142+
var resetNavPosition = function () {
143+
var chapters = subCol.querySelector(".chapters");
144+
chapters?.scroll({ top: 0 });
145+
}
146+
147+
var belowBottomHalf = function (i) {
148+
return i.boundingClientRect.bottom > (i.rootBounds.bottom + i.rootBounds.top) / 2;
149+
}
150+
151+
var prevElem = function (elem) {
152+
var index = mainColElems.indexOf(elem);
153+
if (index <= 0) {
154+
return null;
155+
}
156+
return mainColElems[index - 1];
157+
}
158+
159+
var PAGE_LOAD_BUFFER = 1000;
160+
161+
var navHighlight = function (entries) {
162+
entries.forEach(function (entry) {
163+
if (entry.isIntersecting) {
164+
updateHighlight(matchingNavLink(entry.target));
165+
} else if (entry.time >= PAGE_LOAD_BUFFER && belowBottomHalf(entry)) {
166+
updateHighlight(matchingNavLink(prevElem(entry.target)));
167+
}
168+
});
169+
}
170+
171+
var observer = new IntersectionObserver(navHighlight, {
172+
threshold: 0,
173+
rootMargin: "0% 0px -95% 0px"
174+
});
175+
176+
mainColElems.forEach(function (elem) {
177+
observer.observe(elem);
178+
})
179+
180+
observer.observe(document.getElementById("feature"));
181+
182+
subCol.addEventListener("click", function(e) {
183+
var link = e.target.closest("a");
184+
if (link) {
185+
setTimeout(function() { updateHighlight(link) }, 100);
186+
}
187+
})
105188
});
106189
}).call(this);

guides/assets/stylesrc/_main.scss

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,9 @@ body.guide {
727727
}
728728

729729
ol.chapters {
730+
padding-inline-start: 3.5em;
731+
padding-inline-end: 1em;
732+
730733
&::-webkit-scrollbar
731734
{
732735
width: 6px;
@@ -761,6 +764,12 @@ body.guide {
761764
li {
762765
font-weight: bold;
763766

767+
a {
768+
position: relative;
769+
display: block;
770+
word-wrap: break-word;
771+
}
772+
764773
a,
765774
a:link,
766775
a:visited {
@@ -778,19 +787,68 @@ body.guide {
778787
margin-top: 0.5em;
779788
margin-bottom: 0.75em;
780789
padding-inline-start: 1em;
781-
@include media('>desktop') {
782-
max-width: 310px;
783-
width: 310px;
784-
}
790+
padding-inline-end: 0.25em;
785791

786792
li {
787793
font-weight: 400;
788-
@include media('>desktop') {
789-
width: 310px;
794+
795+
a.active::after { // highlight
796+
@include media('>desktop') {
797+
width: 328px;
798+
}
799+
left: -3em;
800+
}
801+
802+
a.active::before { // red square
803+
left: -2.5em;
804+
top: 7px;
790805
}
791806
} // li
792807
} // ul
808+
809+
a::after { // highlight
810+
content: '';
811+
position: absolute;
812+
display: block;
813+
height: calc(100% + 10px);
814+
@include media('>desktop') {
815+
width: 352px;
816+
}
817+
top: 50%;
818+
transform: translateY(-50%);
819+
left: -3.5em;
820+
border-radius: 8px;
821+
background-color: $stop-bkgnd;
822+
opacity: 0;
823+
transition: opacity 0.2s ease-in-out;
824+
}
825+
826+
a.active::after {
827+
opacity: 1;
828+
}
829+
830+
a::before { // red square
831+
content: '';
832+
display: block;
833+
position: absolute;
834+
height: 10px;
835+
width: 10px;
836+
top: 6px;
837+
left: -3em;
838+
border-radius: 3px;
839+
background-color: $rf-brand;
840+
opacity: 0;
841+
transition: opacity 0.2s ease-in-out;
842+
}
843+
844+
a.active::before {
845+
opacity: 1;
846+
}
793847
} // li
848+
849+
> li:first-child {
850+
margin-top: 0.5em;
851+
}
794852
} // ol
795853
}
796854
}

0 commit comments

Comments
 (0)