diff --git a/docs/dokka-presets/scripts/accessibility.js b/docs/dokka-presets/scripts/accessibility.js index ab744ec60a8..59c24c37fa4 100644 --- a/docs/dokka-presets/scripts/accessibility.js +++ b/docs/dokka-presets/scripts/accessibility.js @@ -1,3 +1,16 @@ +/** + * Check for elements with a navButton class, which indicates the sidebar has finished loading. + */ +function dispatchNavigationLoadedEvent() { + if (document.querySelectorAll('.navButton').length > 0) { + window.dispatchEvent(new Event('navigationLoaded')); + } else { + setTimeout(dispatchNavigationLoadedEvent, 100); + } +} +document.addEventListener('DOMContentLoaded', dispatchNavigationLoadedEvent); +if (document.readyState === "interactive" || document.readyState === "complete" ) { dispatchNavigationLoadedEvent() } + /** * Apply "skip to main content" buttons after each active left sidebar `sideMenuPart`. * These are invisible and only accessible via keyboard @@ -48,9 +61,7 @@ function applySkipLinks() { }; observer.observe(document.body, observerConfig); } -document.addEventListener('DOMContentLoaded', applySkipLinks); -if (document.readyState === "interactive" || document.readyState === "complete" ) { applySkipLinks() } - +window.addEventListener('navigationLoaded', applySkipLinks); /** * Ensure `navButton` elements are interactable and have proper accessibility properties @@ -89,9 +100,7 @@ function ensureNavButtonInteractable() { }); }); } - -document.addEventListener('DOMContentLoaded', ensureNavButtonInteractable) -if (document.readyState === "interactive" || document.readyState === "complete" ) { ensureNavButtonInteractable() } +window.addEventListener('navigationLoaded', ensureNavButtonInteractable); /** * Ensure that content (specifically, code blocks) reflows on small page sizes. @@ -143,6 +152,4 @@ function ensureContentReflow() { }); }); } - -document.addEventListener('DOMContentLoaded', ensureContentReflow) -if (document.readyState === "interactive" || document.readyState === "complete" ) { ensureContentReflow() } +window.addEventListener('navigationLoaded', ensureContentReflow);