UNPKG

swiper

Version:

Most modern mobile touch slider and framework with hardware accelerated transitions

2 lines 6.53 kB
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:n})=>{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 i,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 p(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("role",t)})}function g(e,t){makeElementsArray(e).forEach(e=>{e.setAttribute("aria-roledescription",t)})}function u(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 n=l(),i=e.params.pagination,a=t.target;if(!e.pagination||!e.pagination.el||a!==e.pagination.el&&!e.pagination.el.contains(a)||a.matches(classesToSelector(i?.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(n.lastSlideMessage):o(n.nextSlideMessage)),t.includes(a)&&(e.isBeginning&&!e.params.loop||e.slidePrev(),e.isBeginning?o(n.firstSlideMessage):o(n.prevSlideMessage))}e.pagination&&a.matches(classesToSelector(i?.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,n)=>{d(e),"BUTTON"!==e.tagName&&(p(e,"button"),e.addEventListener("keydown",f)),u(e,n)},A=t=>{i&&i!==t.target&&!i.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()},x=t=>{const n=l();if(e.a11y.clicked||!n.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;i=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 p=t.sourceCapabilities;p&&p.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)}))},M=()=>{const t=l();t.itemRoleDescriptionMessage&&g(e.slides,t.itemRoleDescriptionMessage),t.slideRole&&p(e.slides,t.slideRole);const n=e.slides.length,i=t.slideLabelMessage;i&&e.slides.forEach((t,a)=>{const s=e.params.loop?parseInt(t.getAttribute("data-swiper-slide-index")||"0",10):a;u(t,i.replace(/\{\{index\}\}/,String(s+1)).replace(/\{\{slidesLength\}\}/,String(n)))})},L=()=>{const t=l();a&&e.el.append(a);const n=e.el;t.containerRoleDescriptionMessage&&g(n,t.containerRoleDescriptionMessage),t.containerMessage&&u(n,t.containerMessage),t.containerRole&&p(n,t.containerRole);const i=e.wrapperEl,s=String(t.id||i.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(i).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)})}(i,e.params.autoplay&&t?.enabled?"off":"polite")}M();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",x,!0),e.el.addEventListener("pointerdown",A,!0),e.el.addEventListener("pointerup",y,!0)};n("beforeInit",()=>{a=createElement("span",l().notificationClass),a.setAttribute("aria-live","assertive"),a.setAttribute("aria-atomic","true")}),n("afterInit",()=>{l().enabled&&L()}),n("slidesLengthChange snapGridLengthChange slidesGridLengthChange",()=>{l().enabled&&M()}),n("fromEdge toEdge afterInit lock unlock",()=>{l().enabled&&function(){if(e.params.loop||e.params.rewind||!e.navigation)return;const{nextEl:t,prevEl:n}=e.navigation;n&&(e.isBeginning?(m(n),c(n)):(E(n),d(n))),t&&(e.isEnd?(m(t),c(t)):(E(t),d(t)))}()}),n("paginationUpdate",()=>{l().enabled&&function(){const t=l();if(!v())return;const n=e.params.pagination;e.pagination.bullets.forEach(e=>{n.clickable&&(d(e),n.renderBullet||(p(e,"button"),u(e,t.paginationBulletMessage.replace(/\{\{index\}\}/,String((elementIndex(e)??0)+1))))),e.matches(classesToSelector(n.bulletActiveClass))?e.setAttribute("aria-current","true"):e.removeAttribute("aria-current")})}()}),n("destroy",()=>{l().enabled&&function(){a&&a.remove();const t=e.navigation?e.navigation:{nextEl:void 0,prevEl:void 0},n=makeElementsArray(t.nextEl),i=makeElementsArray(t.prevEl);n&&n.forEach(e=>e.removeEventListener("keydown",f)),i&&i.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",x,!0),e.el.removeEventListener("pointerdown",A,!0),e.el.removeEventListener("pointerup",y,!0))}()})};export{A11y as default}; //# sourceMappingURL=a11y.min.mjs.map