forked from edenspiekermann/a11y-toggle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha11y-toggle.min.js
1 lines (1 loc) · 1.05 KB
/
a11y-toggle.min.js
1
!function(){"use strict";function t(t){return Array.prototype.slice.call(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}var a=0,r=t("[data-a11y-toggle]").reduce(function(t,e){var a="#"+e.getAttribute("data-a11y-toggle");return t[a]=t[a]||[],t[a].push(e),t},{}),i={};t(Object.keys(r)).forEach(function(t){i[t.id]=t;var e=r["#"+t.id],n=t.hasAttribute("data-a11y-toggle-open"),u=[];e.forEach(function(e){e.id||e.setAttribute("id","a11y-toggle-"+a++),e.setAttribute("aria-controls",t.id),e.setAttribute("aria-expanded",n),u.push(e.id)}),t.setAttribute("aria-hidden",!n),t.hasAttribute("aria-labelledby")||t.setAttribute("aria-labelledby",u.join(" "))}),document.addEventListener("click",function(t){var a=e(t.target),n=a&&i[a.getAttribute("aria-controls")];if(!n)return!1;var u=r["#"+n.id],o="false"===n.getAttribute("aria-hidden");n.setAttribute("aria-hidden",o),u.forEach(function(t){t.setAttribute("aria-expanded",!o)})})}();