|
| 1 | +/** |
| 2 | + * Apply "skip to main content" buttons after each active left sidebar `sideMenuPart`. |
| 3 | + * These are invisible and only accessible via keyboard |
| 4 | + * Fixes accessibility violation: "Provide a mechanism for skipping past repetitive content" |
| 5 | + */ |
| 6 | +function applySkipLinks() { |
| 7 | + function insertSkipLink(element) { |
| 8 | + if (element.querySelectorAll(".skip-to-content").length > 0) { return } |
| 9 | + |
| 10 | + const skipLink = document.createElement('div'); |
| 11 | + // Create an anchor element with the href pointing to the main content |
| 12 | + const anchor = document.createElement('a'); |
| 13 | + anchor.classList.add('skip-to-content'); |
| 14 | + anchor.href = '#content'; |
| 15 | + anchor.innerHTML = 'Skip to Main Content'; |
| 16 | + anchor.setAttribute("tabindex", "0"); |
| 17 | + skipLink.appendChild(anchor); |
| 18 | + if (element.children.length > 1) { |
| 19 | + element.insertBefore(skipLink, element.children[1]); |
| 20 | + } else { |
| 21 | + element.appendChild(skipLink); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + function handleChanges(mutationsList) { |
| 26 | + for (const mutation of mutationsList) { |
| 27 | + if (mutation.type === 'attributes' && mutation.target.classList.contains('sideMenuPart') && !mutation.target.classList.contains('hidden')) { |
| 28 | + insertSkipLink(mutation.target); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + // Insert a skip link on all sideMenuParts with [data-active] property |
| 33 | + document.querySelectorAll('.sideMenuPart[data-active]').forEach(function(sideMenuPart) { |
| 34 | + insertSkipLink(sideMenuPart) |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + const observer = new MutationObserver(handleChanges); |
| 39 | + const observerConfig = { |
| 40 | + childList: true, |
| 41 | + subtree: true, |
| 42 | + attributes: true, |
| 43 | + attributeFilter: ['class'] |
| 44 | + }; |
| 45 | + observer.observe(document.body, observerConfig); |
| 46 | +} |
| 47 | +document.addEventListener('DOMContentLoaded', applySkipLinks); |
| 48 | +if (document.readyState === "interactive" || document.readyState === "complete" ) { applySkipLinks() } |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * Ensure `navButton` elements are interactable and have proper accessibility properties |
| 53 | + * Fixes accessibilty violation: "Ensure all interactive functionality is operable with the keyboard" |
| 54 | + */ |
| 55 | +function ensureNavButtonInteractable() { |
| 56 | + const navButtons = document.querySelectorAll('.navButton'); |
| 57 | + |
| 58 | + navButtons.forEach(function(navButton) { |
| 59 | + // Make the navButton focusable, add accessibility information |
| 60 | + navButton.setAttribute('tabindex', '0'); |
| 61 | + navButton.setAttribute('role', 'button'); |
| 62 | + navButton.setAttribute('aria-expanded', 'false'); |
| 63 | + |
| 64 | + // Grab the page ID, use it for aria-label and aria-controls |
| 65 | + const sectionName = navButton.parentElement.parentElement.getAttribute('pageid') |
| 66 | + // Remove the page ID suffix auto-generated by Dokka |
| 67 | + const cleanedSectionName = sectionName.substring(0, sectionName.indexOf("////PointingToDeclaration")) |
| 68 | + navButton.setAttribute('aria-label', cleanedSectionName); |
| 69 | + |
| 70 | + const sectionID = navButton.parentElement.parentElement.id |
| 71 | + navButton.setAttribute('aria-controls', sectionID); |
| 72 | + |
| 73 | + // Add event listener for Enter and Space keys |
| 74 | + navButton.addEventListener('keydown', function(event) { |
| 75 | + if (event.key === 'Enter' || event.key === ' ') { |
| 76 | + event.preventDefault(); // Prevent the default action to avoid navigation |
| 77 | + this.click(); // Trigger the onclick event |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // Update aria-expanded attribute on click |
| 82 | + navButton.addEventListener('click', function() { |
| 83 | + const isExpanded = navButton.getAttribute('aria-expanded') === 'true'; |
| 84 | + navButton.setAttribute('aria-expanded', (!isExpanded).toString()); |
| 85 | + }); |
| 86 | + }); |
| 87 | +} |
| 88 | + |
| 89 | +window.onload = function() { |
| 90 | + ensureNavButtonInteractable() |
| 91 | +} |
| 92 | + |
| 93 | +// |
| 94 | +/** |
| 95 | + * Ensure that content (specifically, code blocks) reflows on small page sizes. |
| 96 | + * Fixes accessibility violation: "Ensure pages reflow without requiring two-dimensional scrolling without loss of content or functionality" |
| 97 | + */ |
| 98 | +function ensureContentReflow() { |
| 99 | + const MIN_WINDOW_SIZE = 550 |
| 100 | + |
| 101 | + // Function to insert 'toggle content' button |
| 102 | + function insertToggleContentButton(element) { |
| 103 | + const initiallyVisible = window.innerWidth >= MIN_WINDOW_SIZE |
| 104 | + |
| 105 | + const toggleContent = document.createElement('button'); |
| 106 | + toggleContent.className = 'aws-toggle-content-btn'; |
| 107 | + toggleContent.textContent = initiallyVisible ? '▼' : '▶' |
| 108 | + toggleContent.setAttribute('aria-expanded', initiallyVisible.toString()); |
| 109 | + toggleContent.setAttribute('aria-label', 'Toggle code block for' + element.getAttribute("data-togglable")); |
| 110 | + toggleContent.setAttribute('aria-controls', element.id); |
| 111 | + |
| 112 | + // Set initial visibility based on window size |
| 113 | + element.style.display = initiallyVisible ? 'block' : 'none' |
| 114 | + |
| 115 | + // Toggle visibility onclick |
| 116 | + toggleContent.onclick = function() { |
| 117 | + const isExpanded = toggleContent.getAttribute('aria-expanded') === 'true'; |
| 118 | + toggleContent.setAttribute('aria-expanded', (!isExpanded).toString()); |
| 119 | + element.style.display = isExpanded ? 'none' : 'block' |
| 120 | + toggleContent.textContent = isExpanded ? '▶' : '▼' |
| 121 | + }; |
| 122 | + |
| 123 | + element.parentNode.insertBefore(toggleContent, element); |
| 124 | + } |
| 125 | + |
| 126 | + document.querySelectorAll('.content[data-togglable]').forEach(insertToggleContentButton); |
| 127 | + |
| 128 | + // Update content visibility on resize |
| 129 | + window.addEventListener('resize', function() { |
| 130 | + document.querySelectorAll('.content[data-togglable]').forEach(function(element) { |
| 131 | + const toggleContent = element.previousSibling; |
| 132 | + if (window.innerWidth < MIN_WINDOW_SIZE) { |
| 133 | + element.style.display = 'none'; |
| 134 | + toggleContent.setAttribute('aria-expanded', 'false'); |
| 135 | + } else { |
| 136 | + element.style.display = 'block'; |
| 137 | + toggleContent.setAttribute('aria-expanded', 'true'); |
| 138 | + } |
| 139 | + }); |
| 140 | + }); |
| 141 | +} |
| 142 | + |
| 143 | +document.addEventListener('DOMContentLoaded', ensureContentReflow) |
| 144 | +if (document.readyState === "interactive" || document.readyState === "complete" ) { ensureContentReflow() } |
0 commit comments