Skip to content

Commit 126e90f

Browse files
committed
Add anchors
1 parent 548541b commit 126e90f

File tree

3 files changed

+85
-11
lines changed

3 files changed

+85
-11
lines changed

docs/syntax/dropdowns.md

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ 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
1413
Dropdown content
1514
:::
1615

@@ -19,7 +18,6 @@ Dropdown content
1918
::::{tab-item} Markdown
2019
```markdown
2120
:::{dropdown} Dropdown Title
22-
:name: basic-dropdown
2321
Dropdown content
2422
:::
2523
```
@@ -37,7 +35,6 @@ You can specify that the dropdown content should be visible by default. Do this
3735

3836
:::{dropdown} Dropdown Title
3937
:open:
40-
:name: open-dropdown
4138
Dropdown content
4239
:::
4340

@@ -47,7 +44,6 @@ Dropdown content
4744
```markdown
4845
:::{dropdown} Dropdown Title
4946
:open:
50-
:name: open-dropdown
5147
Dropdown content
5248
:::
5349
```
@@ -57,7 +53,14 @@ Dropdown content
5753

5854
## Deeplinking
5955

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.
56+
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. When you manually open a dropdown that has a name/anchor, the URL will automatically update to reflect the current state.
57+
58+
### Features
59+
60+
- **Automatic opening**: Navigate to `#dropdown-name` and the dropdown opens automatically
61+
- **URL updates**: Open a dropdown manually and the URL updates to show the anchor
62+
- **Nested content**: Link directly to headings or content within dropdowns
63+
- **Browser navigation**: Proper back/forward button support
6164

6265
:::::{tab-set}
6366

@@ -68,15 +71,18 @@ Dropdowns support deeplinking via anchor links. When you navigate to a URL with
6871

6972
This dropdown can be opened by navigating to `#deeplink-example`.
7073

74+
When you open this dropdown manually by clicking the title, the URL will automatically update to show `#deeplink-example`.
75+
7176
#### Nested Content [#nested-content]
7277

7378
You can also link directly to content within dropdowns. This content has the anchor `#nested-content`.
7479

7580
:::
7681

77-
Test links:
78-
- [Link to dropdown](#deeplink-example)
79-
- [Link to nested content](#nested-content)
82+
**Test the features:**
83+
- [Link to dropdown](#deeplink-example) - Opens the dropdown and updates URL
84+
- [Link to nested content](#nested-content) - Opens dropdown and scrolls to nested content
85+
- Try opening/closing the dropdown manually and watch the URL change
8086

8187
::::
8288

@@ -87,16 +93,30 @@ Test links:
8793

8894
This dropdown can be opened by navigating to `#deeplink-example`.
8995

96+
When you open this dropdown manually by clicking the title, the URL will automatically update to show `#deeplink-example`.
97+
9098
#### Nested Content [#nested-content]
9199

92100
You can also link directly to content within dropdowns. This content has the anchor `#nested-content`.
93101

94102
:::
95103

96-
Test links:
97-
- [Link to dropdown](#deeplink-example)
98-
- [Link to nested content](#nested-content)
104+
**Test the features:**
105+
- [Link to dropdown](#deeplink-example) - Opens the dropdown and updates URL
106+
- [Link to nested content](#nested-content) - Opens dropdown and scrolls to nested content
107+
- Try opening/closing the dropdown manually and watch the URL change
99108
```
100109
::::
101110

102111
:::::
112+
113+
### Use Cases
114+
115+
Deeplinking is particularly useful for:
116+
117+
- **FAQ sections**: Allow users to share links to specific questions
118+
- **Documentation**: Link directly to explanations that might be collapsed by default
119+
- **Troubleshooting guides**: Share direct links to specific solutions
120+
- **API documentation**: Link to specific endpoint details within collapsed sections
121+
122+
The URL behaves just like clicking on a heading with an anchor - it updates automatically when you interact with the content.

src/Elastic.Documentation.Site/Assets/open-details-with-anchor.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,41 @@ export function openDetailsWithAnchor() {
1919
}
2020
}
2121

22+
// Updates the URL when a dropdown is manually opened/closed
23+
function updateUrlForDropdown(details: HTMLDetailsElement, isOpening: boolean) {
24+
const dropdownId = details.id
25+
if (!dropdownId) return
26+
27+
if (isOpening) {
28+
// Update URL to show the dropdown anchor (like clicking a heading link)
29+
window.history.pushState(null, '', `#${dropdownId}`)
30+
}
31+
// Note: We don't remove the hash when closing, just like headings don't
32+
// This keeps the URL consistent with how headings behave
33+
}
34+
2235
// Initialize the anchor handling functionality
2336
export function initOpenDetailsWithAnchor() {
2437
// Handle initial page load
2538
openDetailsWithAnchor()
2639

2740
// Handle hash changes within the same page (e.g., clicking anchor links)
2841
window.addEventListener('hashchange', openDetailsWithAnchor)
42+
43+
// Handle manual dropdown toggling to update URL
44+
// Use event delegation to catch all toggle events
45+
document.addEventListener('toggle', (event) => {
46+
const target = event.target as HTMLElement
47+
48+
// Check if the target is a details element with dropdown class
49+
if (target.tagName === 'DETAILS' && target.classList.contains('dropdown')) {
50+
const details = target as HTMLDetailsElement
51+
const isOpening = details.open
52+
53+
// Use setTimeout to ensure the toggle state has been processed
54+
setTimeout(() => {
55+
updateUrlForDropdown(details, isOpening)
56+
}, 0)
57+
}
58+
}, true) // Use capture phase to ensure we catch the event
2959
}

tests/Elastic.Markdown.Tests/Directives/AdmonitionTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,27 @@ A regular paragraph.
9999
[Fact]
100100
public void SetsDropdownOpen() => Block!.DropdownOpen.Should().BeTrue();
101101
}
102+
103+
public class DropdownWithNameTests(ITestOutputHelper output) : DirectiveTest<AdmonitionBlock>(output,
104+
"""
105+
:::{dropdown} Dropdown with name
106+
:name: test-dropdown
107+
:open:
108+
This is a dropdown with a name
109+
:::
110+
A regular paragraph.
111+
"""
112+
)
113+
{
114+
[Fact]
115+
public void SetsCorrectAdmonitionType() => Block!.Admonition.Should().Be("dropdown");
116+
117+
[Fact]
118+
public void SetsCustomTitle() => Block!.Title.Should().Be("Dropdown with name");
119+
120+
[Fact]
121+
public void SetsDropdownOpen() => Block!.DropdownOpen.Should().BeTrue();
122+
123+
[Fact]
124+
public void SetsCrossReferenceName() => Block!.CrossReferenceName.Should().Be("test-dropdown");
125+
}

0 commit comments

Comments
 (0)