|
48 | 48 | document.addEventListener("click", hide, true); |
49 | 49 | })(); |
50 | 50 |
|
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. */ |
52 | 54 | (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; |
58 | 57 | const block = el.closest("li, p, tr") || el; |
59 | 58 | 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 |
61 | 60 | 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); |
62 | 69 | } |
| 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); |
63 | 88 | document.addEventListener("animationend", function (e) { |
64 | 89 | if (e.target.classList && e.target.classList.contains("api-target-flash")) { |
65 | 90 | e.target.classList.remove("api-target-flash"); |
66 | 91 | } |
67 | 92 | }); |
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 | 93 | })(); |
0 commit comments