From 1ae66a0e1357b79fa10789a2c400e3c4d9614140 Mon Sep 17 00:00:00 2001 From: Matas Date: Mon, 15 Jul 2024 17:01:43 -0400 Subject: [PATCH] misc(docs): only run accessibility scripts after the sidebar has fully loaded (#1123) --- docs/dokka-presets/scripts/accessibility.js | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/docs/dokka-presets/scripts/accessibility.js b/docs/dokka-presets/scripts/accessibility.js index ab744ec60..17e17b161 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. + */ +async function dispatchNavigationLoadedEvent() { + while (!document.querySelectorAll('.navButton').length > 0) { + await new Promise(resolve => setTimeout(resolve, 100)); + } + window.dispatchEvent(new Event('navigationLoaded')); +} + +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);