-
Notifications
You must be signed in to change notification settings - Fork 30
Add deeplink anchors to dropdown directive #1765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
theletterf
wants to merge
24
commits into
main
Choose a base branch
from
add-deeplink-anchors-dropdown
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
548541b
Add deeplink anchors
theletterf 126e90f
Add anchors
theletterf 9a92523
Edit docs
theletterf fa2ea6b
Format file
theletterf 26c8fa5
Merge branch 'main' into add-deeplink-anchors-dropdown
theletterf bda669a
Instant and autogenerate anchors
theletterf 035b3b5
Prettify
theletterf 5a0d5f6
Merge branch 'main' into add-deeplink-anchors-dropdown
theletterf e5dfcf9
Remove test file
theletterf d597bec
Merge branch 'add-deeplink-anchors-dropdown' of github.com:elastic/do…
theletterf 0fd16ed
Address peer edits
theletterf 1008fd9
Merge branch 'main' into add-deeplink-anchors-dropdown
theletterf 8a8418b
Update src/Elastic.Markdown/Myst/ParserContext.cs
theletterf b9bdaf9
Add tests
theletterf 0c3ab5c
Merge branch 'add-deeplink-anchors-dropdown' of github.com:elastic/do…
theletterf 29607ec
Prettify
theletterf 520ade3
Merge branch 'main' into add-deeplink-anchors-dropdown
theletterf 08e8136
Simplify dupe detection
theletterf f03502f
Merge branch 'main' into add-deeplink-anchors-dropdown
theletterf 6568772
Docs
theletterf 60d2b6a
Reconcile changes from main
theletterf e925652
Add hints
theletterf dbffa7d
Fix line numbers in hints
theletterf f078aea
Merge remote-tracking branch 'origin/main' into add-deeplink-anchors-…
theletterf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 53 additions & 10 deletions
63
src/Elastic.Documentation.Site/Assets/open-details-with-anchor.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,66 @@ | ||
import { UAParser } from 'ua-parser-js' | ||
|
||
const { getBrowser } = new UAParser() | ||
|
||
// This is a fix for anchors in details elements in non-Chrome browsers. | ||
// Opens details elements (dropdowns) when navigating to an anchor link within them | ||
// This enables deeplinking to collapsed dropdown content | ||
export function openDetailsWithAnchor() { | ||
if (window.location.hash) { | ||
const target = document.querySelector(window.location.hash) | ||
if (target) { | ||
const closestDetails = target.closest('details') | ||
if (closestDetails) { | ||
const browser = getBrowser() | ||
if (browser.name !== 'Chrome') { | ||
closestDetails.open = true | ||
closestDetails.open = true | ||
// Small delay to ensure the details element is open before scrolling | ||
setTimeout(() => { | ||
theletterf marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
target.scrollIntoView({ | ||
behavior: 'instant', | ||
behavior: 'smooth', | ||
block: 'start', | ||
}) | ||
} | ||
}, 50) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Updates the URL when a dropdown is manually opened/closed | ||
function updateUrlForDropdown(details: HTMLDetailsElement, isOpening: boolean) { | ||
const dropdownId = details.id | ||
if (!dropdownId) return | ||
|
||
if (isOpening) { | ||
// Update URL to show the dropdown anchor (like clicking a heading link) | ||
window.history.pushState(null, '', `#${dropdownId}`) | ||
} | ||
// Note: We don't remove the hash when closing, just like headings don't | ||
// This keeps the URL consistent with how headings behave | ||
} | ||
|
||
// Initialize the anchor handling functionality | ||
export function initOpenDetailsWithAnchor() { | ||
// Handle initial page load | ||
openDetailsWithAnchor() | ||
|
||
// Handle hash changes within the same page (e.g., clicking anchor links) | ||
window.addEventListener('hashchange', openDetailsWithAnchor) | ||
theletterf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Handle manual dropdown toggling to update URL | ||
// Use event delegation to catch all toggle events | ||
document.addEventListener( | ||
'toggle', | ||
(event) => { | ||
const target = event.target as HTMLElement | ||
|
||
// Check if the target is a details element with dropdown class | ||
if ( | ||
target.tagName === 'DETAILS' && | ||
target.classList.contains('dropdown') | ||
) { | ||
const details = target as HTMLDetailsElement | ||
const isOpening = details.open | ||
|
||
// Use setTimeout to ensure the toggle state has been processed | ||
setTimeout(() => { | ||
updateUrlForDropdown(details, isOpening) | ||
}, 0) | ||
} | ||
}, | ||
true | ||
) // Use capture phase to ensure we catch the event | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.