2 lines
5.1 KiB
JavaScript
2 lines
5.1 KiB
JavaScript
import{e as g}from"./index.modern.D46RI4Wq.js";import{l as y}from"./Swup.DoubZhVr.js";(function(){if(!(typeof window>"u"||typeof document>"u"||typeof HTMLElement>"u")){var s=!1;try{var t=document.createElement("div");t.addEventListener("focus",function(i){i.preventDefault(),i.stopPropagation()},!0),t.focus(Object.defineProperty({},"preventScroll",{get:function(){if(navigator&&typeof navigator.userAgent<"u"&&navigator.userAgent&&navigator.userAgent.match(/Edge\/1[7-8]/))return s=!1;s=!0}}))}catch{}if(HTMLElement.prototype.nativeFocus===void 0&&!s){HTMLElement.prototype.nativeFocus=HTMLElement.prototype.focus;var n=function(i){for(var e=i.parentNode,u=[],a=document.scrollingElement||document.documentElement;e&&e!==a;)(e.offsetHeight<e.scrollHeight||e.offsetWidth<e.scrollWidth)&&u.push([e,e.scrollTop,e.scrollLeft]),e=e.parentNode;return e=a,u.push([e,e.scrollTop,e.scrollLeft]),u},o=function(i){for(var e=0;e<i.length;e++)i[e][0].scrollTop=i[e][1],i[e][0].scrollLeft=i[e][2];i=[]},r=function(i){if(i&&i.preventScroll){var e=n(this);if(typeof setTimeout=="function"){var u=this;setTimeout(function(){u.nativeFocus(),o(e)},0)}else this.nativeFocus(),o(e)}else this.nativeFocus()};HTMLElement.prototype.focus=r}}})();function h(){return h=Object.assign?Object.assign.bind():function(s){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var o in n)Object.prototype.hasOwnProperty.call(n,o)&&(s[o]=n[o])}return s},h.apply(this,arguments)}function p(s,t){return Object.keys(t).reduce((n,o)=>n.replace(`{${o}}`,t[o]||""),s||"")}class w{constructor(){var t;this.id="swup-announcer",this.style="position:absolute;top:0;left:0;clip:rect(0 0 0 0);clip-path:inset(50%);overflow:hidden;white-space:nowrap;word-wrap:normal;width:1px;height:1px;",this.region=void 0,this.region=(t=this.getRegion())!=null?t:this.createRegion()}getRegion(){return document.getElementById(this.id)}createRegion(){const t=(function(n){const o=document.createElement("template");return o.innerHTML=n,o.content.children[0]})(`<p aria-live="assertive" aria-atomic="true" id="${this.id}" style="${this.style}"></p>`);return document.body.appendChild(t),t}announce(t,n=0){return new Promise(o=>{setTimeout(()=>{this.region.textContent===t&&(t=`${t}.`),this.region.textContent="",this.region.textContent=t,o()},n)})}}function m(s){let t;if(t=typeof s=="string"?document.querySelector(s):s,!(t instanceof HTMLElement))return;const n=t.getAttribute("tabindex");t.setAttribute("tabindex","-1"),t.focus({preventScroll:!0}),n!==null&&t.setAttribute("tabindex",n)}class S extends g{constructor(t={}){super(),this.name="SwupA11yPlugin",this.requires={swup:">=4"},this.defaults={headingSelector:"h1",respectReducedMotion:!0,autofocus:!1,announcements:{visit:"Navigated to: {title}",url:"New page at {url}"}},this.options=void 0,this.announcer=void 0,this.announcementDelay=100,this.rootSelector="body",this.handleAnchorScroll=(n,{hash:o})=>{const r=this.swup.getAnchorElement(o);r instanceof HTMLElement&&m(r)},this.options=h({},this.defaults,t),this.announcer=new w}mount(){this.swup.hooks.create("content:announce"),this.swup.hooks.create("content:focus"),this.before("visit:start",this.prepareVisit),this.on("visit:start",this.markAsBusy),this.on("visit:end",this.unmarkAsBusy),this.on("visit:end",this.focusContent),this.on("visit:end",this.announceContent),this.on("scroll:anchor",this.handleAnchorScroll),this.before("visit:start",this.disableAnimations),this.before("link:self",this.disableAnimations),this.before("link:anchor",this.disableAnimations),this.swup.announce=this.announce.bind(this)}unmount(){this.swup.announce=void 0}async announce(t){await this.announcer.announce(t)}markAsBusy(){document.documentElement.setAttribute("aria-busy","true")}unmarkAsBusy(){document.documentElement.removeAttribute("aria-busy")}prepareVisit(t){t.a11y={announce:void 0,focus:this.rootSelector}}announceContent(t){this.swup.hooks.callSync("content:announce",t,void 0,n=>{n.a11y.announce===void 0&&(n.a11y.announce=this.getPageAnnouncement()),n.a11y.announce&&this.announcer.announce(n.a11y.announce,this.announcementDelay)})}focusContent(t){this.swup.hooks.callSync("content:focus",t,void 0,n=>{n.a11y.focus&&(this.options.autofocus&&(function(){const o=(function(){const r=document.querySelector("body [autofocus]");if(r&&!r.closest('[inert], [aria-disabled], [aria-hidden="true"]'))return r})();return!!o&&(o!==document.activeElement&&o.focus(),!0)})()===!0||m(n.a11y.focus))})}getPageAnnouncement(){const{headingSelector:t,announcements:n}=this.options;return(function({headingSelector:o="h1",announcements:r={}}){var i,e;const u=document.documentElement.lang||"*",{href:a,url:d,pathname:f}=y.fromUrl(window.location.href),l=(i=(e=r[u])!=null?e:r["*"])!=null?i:r;if(typeof l!="object")return;const c=document.querySelector(o);c||console.warn(`SwupA11yPlugin: No main heading (${o}) found on new page`);const v=c?.getAttribute("aria-label")||c?.textContent||document.title||p(l.url,{href:a,url:d,path:f});return p(l.visit,{title:v,href:a,url:d,path:f})})({headingSelector:t,announcements:n})}disableAnimations(t){this.options.respectReducedMotion&&window.matchMedia("(prefers-reduced-motion: reduce)").matches&&(t.animation.animate=!1,t.scroll.animate=!1)}}export{S as default};
|