Skip to content

Commit d77c59d

Browse files
aduh95targos
authored andcommitted
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: #42739 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 459546b commit d77c59d

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

doc/api_assets/api.js

+32-12
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
}
1616
mq.addEventListener('change', mqChangeListener);
1717
if (themeToggleButton) {
18-
themeToggleButton.addEventListener('click', function() {
19-
mq.removeEventListener('change', mqChangeListener);
20-
}, { once: true });
18+
themeToggleButton.addEventListener(
19+
'click',
20+
function() {
21+
mq.removeEventListener('change', mqChangeListener);
22+
},
23+
{ once: true }
24+
);
2125
}
2226
}
2327

@@ -60,7 +64,7 @@
6064
for (const picker of pickers) {
6165
const parentNode = picker.parentNode;
6266

63-
picker.addEventListener('click', (e) => {
67+
picker.addEventListener('click', function(e) {
6468
e.preventDefault();
6569

6670
/*
@@ -76,7 +80,7 @@
7680
to close pickers if needed.
7781
*/
7882

79-
requestAnimationFrame(() => {
83+
requestAnimationFrame(function() {
8084
parentNode.classList.add('expanded');
8185
window.addEventListener('click', closeAllPickers);
8286
window.addEventListener('keydown', onKeyDown);
@@ -90,9 +94,9 @@
9094
let ignoreNextIntersection = false;
9195

9296
new IntersectionObserver(
93-
([e]) => {
97+
function(e) {
9498
const currentStatus = header.classList.contains('is-pinned');
95-
const newStatus = e.intersectionRatio < 1;
99+
const newStatus = e[0].intersectionRatio < 1;
96100

97101
// Same status, do nothing
98102
if (currentStatus === newStatus) {
@@ -109,7 +113,7 @@
109113
The timer is reset anyway after few milliseconds.
110114
*/
111115
ignoreNextIntersection = true;
112-
setTimeout(() => {
116+
setTimeout(function() {
113117
ignoreNextIntersection = false;
114118
}, 50);
115119

@@ -119,18 +123,34 @@
119123
).observe(header);
120124
}
121125

126+
function setupAltDocsLink() {
127+
const linkWrapper = document.getElementById('alt-docs');
128+
129+
function updateHashes() {
130+
for (const link of linkWrapper.querySelectorAll('a')) {
131+
link.hash = location.hash;
132+
}
133+
}
134+
135+
addEventListener('hashchange', updateHashes);
136+
updateHashes();
137+
}
138+
122139
function bootstrap() {
123-
// Check if we have JavaScript support
140+
// Check if we have JavaScript support.
124141
document.documentElement.classList.add('has-js');
125142

126-
// Restore user mode preferences
143+
// Restore user mode preferences.
127144
setupTheme();
128145

129-
// Handle pickers with click/taps rather than hovers
146+
// Handle pickers with click/taps rather than hovers.
130147
setupPickers();
131148

132-
// Track when the header is in sticky position
149+
// Track when the header is in sticky position.
133150
setupStickyHeaders();
151+
152+
// Make link to other versions of the doc open to the same hash target (if it exists).
153+
setupAltDocsLink();
134154
}
135155

136156
if (document.readyState === 'loading') {

doc/api_assets/style.css

+11
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@
4040
--color-text-secondary: var(--green2);
4141
}
4242

43+
h4 :target,
44+
h5 :target {
45+
scroll-margin-top: 55px;
46+
}
47+
48+
@supports not (content-visibility: auto) {
49+
h3 :target {
50+
scroll-margin-top: 55px;
51+
}
52+
}
53+
4354
.dark-mode {
4455
--background-color-highlight: var(--black2);
4556
--color-critical: var(--red4);

0 commit comments

Comments
 (0)