forked from KittyGiraudel/a11y-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessible-modal-dialog.min.js
1 lines (1 loc) · 1.68 KB
/
accessible-modal-dialog.min.js
1
!function(t){"use strict";function e(t){var e=["a[href]","area[href]","input:not([disabled])","select:not([disabled])","textarea:not([disabled])","button:not([disabled])","iframe","object","embed","[contenteditable]",'[tabindex]:not([tabindex="-1"])'];return n(e.join(","),t).filter(function(t){return!!(t.offsetWidth||t.offsetHeight||t.getClientRects().length)})}function n(t,e){var n=(e||document).querySelectorAll(t);return n.length?Array.prototype.slice.call(n):[]}function i(t,n){var i=e(t),o=i.indexOf(document.activeElement);n.shiftKey&&0===o?(i[i.length-1].focus(),n.preventDefault()):n.shiftKey||o!==i.length-1||(i[0].focus(),n.preventDefault())}function o(t){t.$openers.forEach(function(e){e.addEventListener("click",function(){t.show()})}),t.$closers.forEach(function(e){e.addEventListener("click",function(){t.hide()})}),document.addEventListener("keydown",function(e){t.shown!==!1&&(27===e.which&&(e.preventDefault(),t.hide()),9===e.which&&i(t.$node,e))}),document.body.addEventListener("focus",function(e){t.shown&&!t.$node.contains(e.target)&&d(t.$node)},!0)}function d(t){var n=e(t);n.length&&n[0].focus()}var s,a=function(t,e){this.$main=e||document.querySelector("#main"),this.$node=t,this.$openers=n('[data-modal-show="'+this.$node.id+'"]'),this.$closers=n("[data-modal-hide]",this.$node).concat(n('[data-modal-hide="'+this.$node.id+'"]')),this.shown=!1,o(this)};a.prototype.show=function(){this.shown=!0,this.$node.setAttribute("aria-hidden","false"),this.$main.setAttribute("aria-hidden","true"),s=document.activeElement,d(this.$node)},a.prototype.hide=function(){this.shown=!1,this.$node.setAttribute("aria-hidden","true"),this.$main.setAttribute("aria-hidden","false"),s.focus()},t.Modal=a}(window);