Skip to content

Commit 4583828

Browse files
committed
Flash-highlight the definition you jump to from a cross-link
1 parent df473c7 commit 4583828

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

docs/javascripts/peek.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,28 @@
4747
});
4848
document.addEventListener("click", hide, true);
4949
})();
50+
51+
/* Briefly highlight the definition you jump to, so it's easy to spot. */
52+
(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;
58+
const block = el.closest("li, p, tr") || el;
59+
block.classList.remove("api-target-flash");
60+
void block.offsetWidth; // force reflow so the animation restarts on every click
61+
block.classList.add("api-target-flash");
62+
}
63+
document.addEventListener("animationend", function (e) {
64+
if (e.target.classList && e.target.classList.contains("api-target-flash")) {
65+
e.target.classList.remove("api-target-flash");
66+
}
67+
});
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+
}
74+
})();

docs/stylesheets/extra.css

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,24 @@
3636
line-height: 1.5;
3737
pointer-events: none;
3838
}
39+
40+
/* Keep a jumped-to definition clear of the sticky header. */
41+
.md-typeset code[id^="ref-"] {
42+
scroll-margin-top: 4rem;
43+
}
44+
45+
/* Brief highlight flash on the definition you jump to. */
46+
@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);
50+
}
51+
to {
52+
background-color: transparent;
53+
}
54+
}
55+
.md-typeset .api-target-flash {
56+
animation: api-target-flash 1.8s ease-out;
57+
border-radius: 0.15rem;
58+
box-decoration-break: clone;
59+
}

0 commit comments

Comments
 (0)