Skip to content

Commit 1ae66a0

Browse files
authored
misc(docs): only run accessibility scripts after the sidebar has fully loaded (#1123)
1 parent 683ae66 commit 1ae66a0

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

docs/dokka-presets/scripts/accessibility.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
/**
2+
* Check for elements with a navButton class, which indicates the sidebar has finished loading.
3+
*/
4+
async function dispatchNavigationLoadedEvent() {
5+
while (!document.querySelectorAll('.navButton').length > 0) {
6+
await new Promise(resolve => setTimeout(resolve, 100));
7+
}
8+
window.dispatchEvent(new Event('navigationLoaded'));
9+
}
10+
11+
document.addEventListener('DOMContentLoaded', dispatchNavigationLoadedEvent);
12+
if (document.readyState === "interactive" || document.readyState === "complete" ) { dispatchNavigationLoadedEvent() }
13+
114
/**
215
* Apply "skip to main content" buttons after each active left sidebar `sideMenuPart`.
316
* These are invisible and only accessible via keyboard
@@ -48,9 +61,7 @@ function applySkipLinks() {
4861
};
4962
observer.observe(document.body, observerConfig);
5063
}
51-
document.addEventListener('DOMContentLoaded', applySkipLinks);
52-
if (document.readyState === "interactive" || document.readyState === "complete" ) { applySkipLinks() }
53-
64+
window.addEventListener('navigationLoaded', applySkipLinks);
5465

5566
/**
5667
* Ensure `navButton` elements are interactable and have proper accessibility properties
@@ -89,9 +100,7 @@ function ensureNavButtonInteractable() {
89100
});
90101
});
91102
}
92-
93-
document.addEventListener('DOMContentLoaded', ensureNavButtonInteractable)
94-
if (document.readyState === "interactive" || document.readyState === "complete" ) { ensureNavButtonInteractable() }
103+
window.addEventListener('navigationLoaded', ensureNavButtonInteractable);
95104

96105
/**
97106
* Ensure that content (specifically, code blocks) reflows on small page sizes.
@@ -143,6 +152,4 @@ function ensureContentReflow() {
143152
});
144153
});
145154
}
146-
147-
document.addEventListener('DOMContentLoaded', ensureContentReflow)
148-
if (document.readyState === "interactive" || document.readyState === "complete" ) { ensureContentReflow() }
155+
window.addEventListener('navigationLoaded', ensureContentReflow);

0 commit comments

Comments
 (0)