Skip to content

Commit b9eaec9

Browse files
committed
Fix jump highlight: trigger on click (capture) so it fires under instant nav
1 parent 4583828 commit b9eaec9

2 files changed

Lines changed: 37 additions & 18 deletions

File tree

docs/javascripts/peek.js

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,46 @@
4848
document.addEventListener("click", hide, true);
4949
})();
5050

51-
/* Briefly highlight the definition you jump to, so it's easy to spot. */
51+
/* Briefly highlight the definition you jump to, so it's easy to spot.
52+
Triggered on the click itself (capture phase) so it works regardless of
53+
Material's instant navigation, which doesn't fire load/hashchange. */
5254
(function () {
53-
function flash() {
54-
const id = location.hash ? decodeURIComponent(location.hash.slice(1)) : "";
55-
if (!id) return;
56-
const el = document.getElementById(id);
57-
if (!el) return;
55+
function flashEl(el) {
56+
if (!el) return false;
5857
const block = el.closest("li, p, tr") || el;
5958
block.classList.remove("api-target-flash");
60-
void block.offsetWidth; // force reflow so the animation restarts on every click
59+
void block.offsetWidth; // force reflow so the animation restarts every time
6160
block.classList.add("api-target-flash");
61+
return true;
62+
}
63+
function flashId(id, tries) {
64+
if (!id) return;
65+
// Same-page: the element is already here. Cross-page (instant nav): retry
66+
// until the swapped-in content contains it.
67+
if (flashEl(document.getElementById(id))) return;
68+
if (tries > 0) setTimeout(function () { flashId(id, tries - 1); }, 120);
6269
}
70+
function idFromHref(href) {
71+
const i = href.indexOf("#");
72+
return i >= 0 ? decodeURIComponent(href.slice(i + 1)) : "";
73+
}
74+
document.addEventListener(
75+
"click",
76+
function (e) {
77+
const link = e.target.closest && e.target.closest("a.xref");
78+
if (link) flashId(idFromHref(link.getAttribute("href") || ""), 12);
79+
},
80+
true
81+
);
82+
// Also cover deep links opened directly and back/forward navigation.
83+
function flashFromHash() {
84+
flashId(location.hash ? decodeURIComponent(location.hash.slice(1)) : "", 6);
85+
}
86+
window.addEventListener("hashchange", flashFromHash);
87+
window.addEventListener("load", flashFromHash);
6388
document.addEventListener("animationend", function (e) {
6489
if (e.target.classList && e.target.classList.contains("api-target-flash")) {
6590
e.target.classList.remove("api-target-flash");
6691
}
6792
});
68-
window.addEventListener("hashchange", flash);
69-
window.addEventListener("load", flash);
70-
// Material instant navigation swaps content without a reload — re-run then too.
71-
if (window.document$ && window.document$.subscribe) {
72-
window.document$.subscribe(function () { setTimeout(flash, 30); });
73-
}
7493
})();

docs/stylesheets/extra.css

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,16 @@
4444

4545
/* Brief highlight flash on the definition you jump to. */
4646
@keyframes api-target-flash {
47-
from {
48-
background-color: rgba(124, 77, 162, 0.4); /* fallback */
49-
background-color: color-mix(in srgb, var(--md-accent-fg-color) 40%, transparent);
47+
0% {
48+
background-color: rgba(124, 77, 162, 0.55); /* fallback */
49+
background-color: color-mix(in srgb, var(--md-accent-fg-color) 55%, transparent);
5050
}
51-
to {
51+
100% {
5252
background-color: transparent;
5353
}
5454
}
5555
.md-typeset .api-target-flash {
56-
animation: api-target-flash 1.8s ease-out;
56+
animation: api-target-flash 2.2s ease-out;
5757
border-radius: 0.15rem;
5858
box-decoration-break: clone;
5959
}

0 commit comments

Comments
 (0)