doc: improve fragment (:target) anchors behavior on HTML version

This commit aims to improve the UX when navigating the docs using links
to subsections. Previously the browser would scroll down to the section
body, skipping the section heading. Using `scroll-margin-top` CSS
property, we can fix this behavior (at least on some browsers).

Links to other versions are now updated with the current targeted hash
to improve the UX when navigating from docs of one release line to
another.

I've also removed syntax not parsable by older browsers (arrow functions
and array destructuring) since the diff is pretty small and should
improve UX on those browsers.

PR-URL: https://github.com/nodejs/node/pull/42739
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Antoine du Hamel
2022-04-17 16:51:42 +02:00
committed by GitHub
parent 036c4d407b
commit 528fef2285
2 changed files with 43 additions and 12 deletions

View File

@@ -15,9 +15,13 @@
}
mq.addEventListener('change', mqChangeListener);
if (themeToggleButton) {
themeToggleButton.addEventListener('click', function() {
mq.removeEventListener('change', mqChangeListener);
}, { once: true });
themeToggleButton.addEventListener(
'click',
function() {
mq.removeEventListener('change', mqChangeListener);
},
{ once: true }
);
}
}
@@ -60,7 +64,7 @@
for (const picker of pickers) {
const parentNode = picker.parentNode;
picker.addEventListener('click', (e) => {
picker.addEventListener('click', function(e) {
e.preventDefault();
/*
@@ -76,7 +80,7 @@
to close pickers if needed.
*/
requestAnimationFrame(() => {
requestAnimationFrame(function() {
parentNode.classList.add('expanded');
window.addEventListener('click', closeAllPickers);
window.addEventListener('keydown', onKeyDown);
@@ -90,9 +94,9 @@
let ignoreNextIntersection = false;
new IntersectionObserver(
([e]) => {
function(e) {
const currentStatus = header.classList.contains('is-pinned');
const newStatus = e.intersectionRatio < 1;
const newStatus = e[0].intersectionRatio < 1;
// Same status, do nothing
if (currentStatus === newStatus) {
@@ -109,7 +113,7 @@
The timer is reset anyway after few milliseconds.
*/
ignoreNextIntersection = true;
setTimeout(() => {
setTimeout(function() {
ignoreNextIntersection = false;
}, 50);
@@ -119,18 +123,34 @@
).observe(header);
}
function setupAltDocsLink() {
const linkWrapper = document.getElementById('alt-docs');
function updateHashes() {
for (const link of linkWrapper.querySelectorAll('a')) {
link.hash = location.hash;
}
}
addEventListener('hashchange', updateHashes);
updateHashes();
}
function bootstrap() {
// Check if we have JavaScript support
// Check if we have JavaScript support.
document.documentElement.classList.add('has-js');
// Restore user mode preferences
// Restore user mode preferences.
setupTheme();
// Handle pickers with click/taps rather than hovers
// Handle pickers with click/taps rather than hovers.
setupPickers();
// Track when the header is in sticky position
// Track when the header is in sticky position.
setupStickyHeaders();
// Make link to other versions of the doc open to the same hash target (if it exists).
setupAltDocsLink();
}
if (document.readyState === 'loading') {

View File

@@ -40,6 +40,17 @@
--color-text-secondary: var(--green2);
}
h4 :target,
h5 :target {
scroll-margin-top: 55px;
}
@supports not (content-visibility: auto) {
h3 :target {
scroll-margin-top: 55px;
}
}
.dark-mode {
--background-color-highlight: var(--black2);
--color-critical: var(--red4);