swiper
Version:
Most modern mobile touch slider and framework with hardware accelerated transitions
2 lines • 7.16 kB
JavaScript
import{c as classesToSelector}from"../shared/classes-to-selector.min.mjs";import{a as createElement,s as makeElementsArray,b as elementIndex,w as setInnerHTML}from"../shared/utils.min.mjs";const isVirtualEnabled=e=>!!e.virtual&&!!e.params.virtual?.enabled,A11y=({swiper:e,extendParams:t,on:i})=>{t({a11y:{enabled:!0,notificationClass:"swiper-notification",prevSlideMessage:"Previous slide",nextSlideMessage:"Next slide",firstSlideMessage:"This is the first slide",lastSlideMessage:"This is the last slide",paginationBulletMessage:"Go to slide {{index}}",slideLabelMessage:"{{index}} / {{slidesLength}}",containerMessage:null,containerRoleDescriptionMessage:null,containerRole:null,itemRoleDescriptionMessage:null,slideRole:"group",id:null,scrollOnFocus:!0,wrapperLiveRegion:!0}}),e.a11y={clicked:!1};let n,a=null,s=!1,r=(new Date).getTime();function l(){return e.params.a11y}function o(e){a&&e&&setInnerHTML(a,e)}function d(e){makeElementsArray(e).forEach(e=>{e.setAttribute("tabIndex","0")})}function c(e){makeElementsArray(e).forEach(e=>{e.setAttribute("tabIndex","-1")})}function u(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("role",t)})}function p(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("aria-roledescription",t)})}function g(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("aria-label",t)})}function m(e){makeElementsArray(e).forEach(e=>{e.setAttribute("aria-disabled","true")})}function E(e){makeElementsArray(e).forEach(e=>{e.removeAttribute("aria-disabled")})}function f(t){if(13!==t.keyCode&&32!==t.keyCode)return;const i=l(),n=e.params.pagination,a=t.target;if(!e.pagination||!e.pagination.el||a!==e.pagination.el&&!e.pagination.el.contains(a)||a.matches(classesToSelector(n?.bulletClass))){if(e.navigation&&e.navigation.prevEl&&e.navigation.nextEl){const t=makeElementsArray(e.navigation.prevEl);makeElementsArray(e.navigation.nextEl).includes(a)&&(e.isEnd&&!e.params.loop||e.slideNext(),e.isEnd?o(i.lastSlideMessage):o(i.nextSlideMessage)),t.includes(a)&&(e.isBeginning&&!e.params.loop||e.slidePrev(),e.isBeginning?o(i.firstSlideMessage):o(i.prevSlideMessage))}e.pagination&&a.matches(classesToSelector(n?.bulletClass))&&a.click()}}function v(){return!!(e.pagination&&e.pagination.bullets&&e.pagination.bullets.length)}function b(){const t=e.params.pagination;return v()&&!!t?.clickable}const h=(e,t,i)=>{d(e),"BUTTON"!==e.tagName&&(u(e,"button"),e.addEventListener("keydown",f)),g(e,i)},A=t=>{n&&n!==t.target&&!n.contains(t.target)&&(s=!0),e.a11y.clicked=!0},y=()=>{s=!1,requestAnimationFrame(()=>{requestAnimationFrame(()=>{e.destroyed||(e.a11y.clicked=!1)})})},k=e=>{r=(new Date).getTime()};let x=[];const w=()=>{x.forEach(e=>e.removeAttribute("inert")),x=[]},L=t=>{if("Tab"!==t.key||!e.params.loop||e.destroyed)return;const i=t.target,n=i?.closest?.(`.${e.params.slideClass}, swiper-slide`);if(!n||!e.slides.includes(n))return;const a=parseInt(n.getAttribute("data-swiper-slide-index")||"",10);if(Number.isNaN(a))return;const s=isVirtualEnabled(e)?e.virtual.slides.length:e.slides.length;(t.shiftKey?0===a:a===s-1)&&(w(),e.slides.forEach(e=>{e===n||e.hasAttribute("inert")||(e.setAttribute("inert",""),x.push(e))}),setTimeout(w,0))},M=t=>{const i=l();if(e.a11y.clicked||!i.scrollOnFocus)return;if((new Date).getTime()-r<100)return;const a=t.target.closest(`.${e.params.slideClass}, swiper-slide`);if(!a||!e.slides.includes(a))return;n=a;const o=isVirtualEnabled(e),d=(o?parseInt(a.getAttribute("data-swiper-slide-index")||"0",10):e.slides.indexOf(a))===e.activeIndex,c=e.params.watchSlidesProgress&&e.visibleSlides&&e.visibleSlides.includes(a);if(d||c)return;const u=t.sourceCapabilities;u&&u.firesTouchEvents||(e.isHorizontal()?e.el.scrollLeft=0:e.el.scrollTop=0,requestAnimationFrame(()=>{s||(e.params.loop?e.slideToLoop(e.getSlideIndexWhenGrid(parseInt(a.getAttribute("data-swiper-slide-index")||"0",10)),0):o?e.slideTo(e.getSlideIndexWhenGrid(parseInt(a.getAttribute("data-swiper-slide-index")||"0",10)),0):e.slideTo(e.getSlideIndexWhenGrid(e.slides.indexOf(a)),0),s=!1)}))},S=()=>{const t=l();t.itemRoleDescriptionMessage&&p(e.slides,t.itemRoleDescriptionMessage),t.slideRole&&u(e.slides,t.slideRole);const i=e.slides.length,n=t.slideLabelMessage;n&&e.slides.forEach((t,a)=>{const s=e.params.loop?parseInt(t.getAttribute("data-swiper-slide-index")||"0",10):a;g(t,n.replace(/\{\{index\}\}/,String(s+1)).replace(/\{\{slidesLength\}\}/,String(i)))})},T=()=>{const t=l();a&&e.el.append(a);const i=e.el;t.containerRoleDescriptionMessage&&p(i,t.containerRoleDescriptionMessage),t.containerMessage&&g(i,t.containerMessage),t.containerRole&&u(i,t.containerRole);const n=e.wrapperEl,s=String(t.id||n.getAttribute("id")||`swiper-wrapper-${function(e=16){return"x".repeat(e).replace(/x/g,()=>Math.round(16*Math.random()).toString(16))}(16)}`);var r;if(r=s,makeElementsArray(n).forEach(e=>{e.setAttribute("id",r)}),t.wrapperLiveRegion){const t=e.params.autoplay;!function(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("aria-live",t)})}(n,e.params.autoplay&&t?.enabled?"off":"polite")}S();const o=e.navigation?e.navigation:{nextEl:void 0,prevEl:void 0},d=makeElementsArray(o.nextEl),c=makeElementsArray(o.prevEl);if(d&&d.forEach(e=>h(e,0,t.nextSlideMessage)),c&&c.forEach(e=>h(e,0,t.prevSlideMessage)),b()){makeElementsArray(e.pagination.el).forEach(e=>{e.addEventListener("keydown",f)})}document.addEventListener("visibilitychange",k),e.el.addEventListener("focus",M,!0),e.el.addEventListener("keydown",L,!0),e.el.addEventListener("pointerdown",A,!0),e.el.addEventListener("pointerup",y,!0)};i("beforeInit",()=>{a=createElement("span",l().notificationClass),a.setAttribute("aria-live","assertive"),a.setAttribute("aria-atomic","true")}),i("afterInit",()=>{l().enabled&&T()}),i("slidesLengthChange snapGridLengthChange slidesGridLengthChange",()=>{l().enabled&&S()}),i("fromEdge toEdge afterInit lock unlock",()=>{l().enabled&&function(){if(e.params.loop||e.params.rewind||!e.navigation)return;const{nextEl:t,prevEl:i}=e.navigation;i&&(e.isBeginning?(m(i),c(i)):(E(i),d(i))),t&&(e.isEnd?(m(t),c(t)):(E(t),d(t)))}()}),i("paginationUpdate",()=>{l().enabled&&function(){const t=l();if(!v())return;const i=e.params.pagination;e.pagination.bullets.forEach(e=>{i.clickable&&(d(e),i.renderBullet||(u(e,"button"),g(e,t.paginationBulletMessage.replace(/\{\{index\}\}/,String((elementIndex(e)??0)+1))))),e.matches(classesToSelector(i.bulletActiveClass))?e.setAttribute("aria-current","true"):e.removeAttribute("aria-current")})}()}),i("destroy",()=>{l().enabled&&function(){a&&a.remove();const t=e.navigation?e.navigation:{nextEl:void 0,prevEl:void 0},i=makeElementsArray(t.nextEl),n=makeElementsArray(t.prevEl);i&&i.forEach(e=>e.removeEventListener("keydown",f)),n&&n.forEach(e=>e.removeEventListener("keydown",f)),b()&&makeElementsArray(e.pagination.el).forEach(e=>{e.removeEventListener("keydown",f)});document.removeEventListener("visibilitychange",k),e.el&&"string"!=typeof e.el&&(e.el.removeEventListener("focus",M,!0),e.el.removeEventListener("keydown",L,!0),e.el.removeEventListener("pointerdown",A,!0),e.el.removeEventListener("pointerup",y,!0)),w()}()})};export{A11y as default};
//# sourceMappingURL=a11y.min.mjs.map