Skip to content

Commit 548541b

Browse files
committed
Add deeplink anchors
1 parent cf70003 commit 548541b

File tree

4 files changed

+75
-12
lines changed

4 files changed

+75
-12
lines changed

docs/syntax/dropdowns.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Dropdowns allow you to hide and reveal content on user interaction. By default,
1010
::::{tab-item} Output
1111

1212
:::{dropdown} Dropdown Title
13+
:name: basic-dropdown
1314
Dropdown content
1415
:::
1516

@@ -18,6 +19,7 @@ Dropdown content
1819
::::{tab-item} Markdown
1920
```markdown
2021
:::{dropdown} Dropdown Title
22+
:name: basic-dropdown
2123
Dropdown content
2224
:::
2325
```
@@ -35,6 +37,7 @@ You can specify that the dropdown content should be visible by default. Do this
3537

3638
:::{dropdown} Dropdown Title
3739
:open:
40+
:name: open-dropdown
3841
Dropdown content
3942
:::
4043

@@ -44,9 +47,56 @@ Dropdown content
4447
```markdown
4548
:::{dropdown} Dropdown Title
4649
:open:
50+
:name: open-dropdown
4751
Dropdown content
4852
:::
4953
```
5054
::::
5155

5256
:::::
57+
58+
## Deeplinking
59+
60+
Dropdowns support deeplinking via anchor links. When you navigate to a URL with a hash that points to a dropdown or content within a dropdown, the dropdown will automatically open.
61+
62+
:::::{tab-set}
63+
64+
::::{tab-item} Output
65+
66+
:::{dropdown} Deeplink Example
67+
:name: deeplink-example
68+
69+
This dropdown can be opened by navigating to `#deeplink-example`.
70+
71+
#### Nested Content [#nested-content]
72+
73+
You can also link directly to content within dropdowns. This content has the anchor `#nested-content`.
74+
75+
:::
76+
77+
Test links:
78+
- [Link to dropdown](#deeplink-example)
79+
- [Link to nested content](#nested-content)
80+
81+
::::
82+
83+
::::{tab-item} Markdown
84+
```markdown
85+
:::{dropdown} Deeplink Example
86+
:name: deeplink-example
87+
88+
This dropdown can be opened by navigating to `#deeplink-example`.
89+
90+
#### Nested Content [#nested-content]
91+
92+
You can also link directly to content within dropdowns. This content has the anchor `#nested-content`.
93+
94+
:::
95+
96+
Test links:
97+
- [Link to dropdown](#deeplink-example)
98+
- [Link to nested content](#nested-content)
99+
```
100+
::::
101+
102+
:::::

docs/testing/nested/index.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ The files in this directory are used for testing purposes. Do not edit these fil
1111
## Injecting a {{x}} is supported in headers.
1212

1313
This should show up in the file's table of contents too.
14+
15+
:::{dropdown} Dropdown Title
16+
:name: dropdown-title
17+
18+
Text.
19+
20+
:::

src/Elastic.Documentation.Site/Assets/main.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { initCopyButton } from './copybutton'
22
import { initHighlight } from './hljs'
33
import { initImageCarousel } from './image-carousel'
44
import './markdown/applies-to'
5-
import { openDetailsWithAnchor } from './open-details-with-anchor'
5+
import { initOpenDetailsWithAnchor } from './open-details-with-anchor'
66
import { initNav } from './pages-nav'
77
import { initSmoothScroll } from './smooth-scroll'
88
import { initTabs } from './tabs'
@@ -31,7 +31,7 @@ document.addEventListener('htmx:load', function (event) {
3131
initNav()
3232
}
3333
initSmoothScroll()
34-
openDetailsWithAnchor()
34+
initOpenDetailsWithAnchor()
3535
initImageCarousel()
3636

3737
const urlParams = new URLSearchParams(window.location.search)
Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
import { UAParser } from 'ua-parser-js'
2-
3-
const { getBrowser } = new UAParser()
4-
5-
// This is a fix for anchors in details elements in non-Chrome browsers.
1+
// Opens details elements (dropdowns) when navigating to an anchor link within them
2+
// This enables deeplinking to collapsed dropdown content
63
export function openDetailsWithAnchor() {
74
if (window.location.hash) {
85
const target = document.querySelector(window.location.hash)
96
if (target) {
107
const closestDetails = target.closest('details')
118
if (closestDetails) {
12-
const browser = getBrowser()
13-
if (browser.name !== 'Chrome') {
14-
closestDetails.open = true
9+
closestDetails.open = true
10+
// Small delay to ensure the details element is open before scrolling
11+
setTimeout(() => {
1512
target.scrollIntoView({
16-
behavior: 'instant',
13+
behavior: 'smooth',
1714
block: 'start',
1815
})
19-
}
16+
}, 50)
2017
}
2118
}
2219
}
2320
}
21+
22+
// Initialize the anchor handling functionality
23+
export function initOpenDetailsWithAnchor() {
24+
// Handle initial page load
25+
openDetailsWithAnchor()
26+
27+
// Handle hash changes within the same page (e.g., clicking anchor links)
28+
window.addEventListener('hashchange', openDetailsWithAnchor)
29+
}

0 commit comments

Comments
 (0)