Skip to content

Commit f92cb41

Browse files
authored
Fix anchor within details for non Chrome browsers (#823)
* Fix anchor within details for non Chrome browsers * naming
1 parent 93c6769 commit f92cb41

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/Elastic.Markdown/Assets/main.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,22 @@ import {initHighlight} from "./hljs";
88
import {initTabs} from "./tabs";
99
import {initCopyButton} from "./copybutton";
1010
import {initNav} from "./pages-nav";
11+
import {openDetailsWithAnchor} from "./open-details-with-anchor";
1112
import {$, $$} from "select-dom"
1213

13-
1414
import { UAParser } from 'ua-parser-js';
1515
const { getOS } = new UAParser();
1616

17+
document.addEventListener('htmx:load', function() {
18+
initTocNav();
19+
initHighlight();
20+
initCopyButton();
21+
initTabs();
22+
initNav()
23+
openDetailsWithAnchor();
24+
});
25+
26+
1727
// Don't remove style tags because they are used by the elastic global nav.
1828
document.addEventListener('htmx:removingHeadElement', function(event) {
1929
const tagName = event.detail.headElement.tagName;
@@ -34,14 +44,6 @@ document.addEventListener('htmx:beforeRequest', function(event) {
3444
}
3545
});
3646

37-
document.addEventListener('htmx:load', function() {
38-
initTocNav();
39-
initHighlight();
40-
initCopyButton();
41-
initTabs();
42-
initNav();
43-
});
44-
4547
document.body.addEventListener('htmx:oobBeforeSwap', function(event) {
4648
// This is needed to scroll to the top of the page when the content is swapped
4749
if (event.target.id === 'markdown-content' || event.target.id === 'content-container') {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { UAParser } from 'ua-parser-js';
2+
const { getBrowser } = new UAParser();
3+
4+
// This is a fix for anchors in details elements in non-Chrome browsers.
5+
export function openDetailsWithAnchor() {
6+
if (window.location.hash) {
7+
const target = document.querySelector(window.location.hash);
8+
if (target) {
9+
const closestDetails = target.closest('details');
10+
if (closestDetails) {
11+
const browser = getBrowser();
12+
if (browser.name !== 'Chrome') {
13+
closestDetails.open = true;
14+
target.scrollIntoView({ behavior: "instant", block: "start" });
15+
}
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)