-
Notifications
You must be signed in to change notification settings - Fork 21
/
a11y-toggle.min.js
1 lines (1 loc) · 1.34 KB
/
a11y-toggle.min.js
1
!function(){"use strict";function t(t,e){return Array.prototype.slice.call((e||document).querySelectorAll(t))}function e(t){if(t.closest)return t.closest("[data-a11y-toggle]");for(;t;){if(1===t.nodeType&&t.hasAttribute("data-a11y-toggle"))return t;t=t.parentNode}return null}function a(t){var e=t&&n[t.getAttribute("aria-controls")];if(!e)return!1;var a=i["#"+e.id],r="false"===e.getAttribute("aria-hidden");e.setAttribute("aria-hidden",r),a.forEach(function(t){t.setAttribute("aria-expanded",!r)})}var r=0,i={},n={},o=function(e){i=t("[data-a11y-toggle]",e).reduce(function(t,e){var a="#"+e.getAttribute("data-a11y-toggle");return t[a]=t[a]||[],t[a].push(e),t},i);var a=Object.keys(i);a.length&&t(a).forEach(function(t){var e=i["#"+t.id],a=t.hasAttribute("data-a11y-toggle-open"),o=[];e.forEach(function(e){e.id||e.setAttribute("id","a11y-toggle-"+r++),e.setAttribute("aria-controls",t.id),e.setAttribute("aria-expanded",a),o.push(e.id)}),t.setAttribute("aria-hidden",!a),t.hasAttribute("aria-labelledby")||t.setAttribute("aria-labelledby",o.join(" ")),n[t.id]=t})};document.addEventListener("DOMContentLoaded",function(){o()}),document.addEventListener("click",function(t){var r=e(t.target);a(r)}),document.addEventListener("keyup",function(t){if(13===t.which||32===t.which){var r=e(t.target);r&&"button"===r.getAttribute("role")&&a(r)}}),window&&(window.a11yToggle=o)}();