coverflow-carousel 0.2.0-dev.0 → 0.2.0-dev.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -21,6 +21,8 @@ export declare class CoverflowCarouselElement extends HTMLElement {
21
21
  private pendingAnimToken;
22
22
  private animFallbackTimerId;
23
23
  private cleanup;
24
+ private lightDomObserver;
25
+ private refreshScheduled;
24
26
  private reflectGuard;
25
27
  connectedCallback(): void;
26
28
  disconnectedCallback(): void;
@@ -35,6 +37,8 @@ export declare class CoverflowCarouselElement extends HTMLElement {
35
37
  private readAttributes;
36
38
  private render;
37
39
  private bindEvents;
40
+ private setupLightDomObserver;
41
+ private scheduleRefreshFromMutations;
38
42
  private rebuildCardsFromLightDom;
39
43
  private getVisibleSet;
40
44
  private buildDots;
package/dist/index.cjs.js CHANGED
@@ -1,4 +1,4 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const b=`:host {
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const x=`:host {
2
2
  display: block;
3
3
  width: 100%;
4
4
  height: 100%;
@@ -164,4 +164,4 @@
164
164
  white-space: nowrap;
165
165
  border: 0;
166
166
  }
167
- `,A=b;function v(a){return"adoptedStyleSheets"in a}function S(a){try{const t=new CSSStyleSheet;return t.replaceSync(a),t}catch{return null}}const I=S(b),C=b;function x(a){return a.split(",").map(t=>t.trim()).filter(Boolean)}function g(a,t){const e=a.trim();if(!e)return t;if(e.endsWith("ms")){const n=Number(e.slice(0,-2).trim());return Number.isFinite(n)?n:t}if(e.endsWith("s")){const n=Number(e.slice(0,-1).trim());return Number.isFinite(n)?n*1e3:t}const i=Number(e);return Number.isFinite(i)?i:t}function _(a,t){const e=x(a.transitionProperty),i=x(a.transitionDuration),n=x(a.transitionDelay);if(!e.length)return 0;const r=(m=>{const d=e.indexOf(m);if(d>=0)return d;const E=e.indexOf("all");return E>=0?E:-1})(t);if(r<0)return 0;const c=i[Math.min(r,i.length-1)]??"0s",o=n[Math.min(r,n.length-1)]??"0s",l=g(c,0),y=g(o,0);return Math.max(0,l+y)}function k(){return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches??!1}function u(a,t,e){if(!a.hasAttribute(t))return e;const i=a.getAttribute(t);return i==null||i===""?!0:i!=="false"}function L(a,t,e){const i=a.getAttribute(t);if(i==null)return e;const n=Number(i.trim());return Number.isFinite(n)?Math.trunc(n):e}function w(a,t){const e=a.getAttribute(t);if(e==null)return null;const i=e.trim();return i||null}function f(a,t){return t<=0?0:(a%t+t)%t}function T(a,t,e){const i=t-a,n=e/2;return i>n?i-e:i<-n?i+e:i}const p=1;let N=0;class h extends HTMLElement{static defaultStylesheet=I;static observedAttributes=["start-index","index","show-dots","show-arrows","announce-changes"];shadow=this.attachShadow({mode:"open"});instanceId=`cfc-${++N}`;rootEl;trackEl;dotsEl;prevBtn;nextBtn;liveRegion;styleEl=null;cards=[];dots=[];currentIndex=0;isAnimating=!1;hasAppliedInitialLayout=!1;lastLayoutIndex=null;lastVisibleSet=new Set;pendingAnimToken=0;animFallbackTimerId=null;cleanup=[];reflectGuard=!1;connectedCallback(){this.render(),this.readAttributes({isInit:!0}),this.refresh()}disconnectedCallback(){this.destroy()}attributeChangedCallback(t,e,i){if(!this.isConnected||this.reflectGuard)return;this.readAttributes({isInit:!1});const n=w(this,"index");if(n!=null){const s=f(Number(n),this.cards.length);if(Number.isFinite(s)&&s!==this.currentIndex){this.goTo(s);return}}this.applyLayoutAndA11y({announce:!0,emitChange:!1})}next(){this.goTo(this.currentIndex+1)}prev(){this.goTo(this.currentIndex-1)}goTo(t){if(this.isAnimating)return;const e=f(t,this.cards.length);e!==this.currentIndex&&(this.isAnimating=!0,this.currentIndex=e,this.reflectIndexAttr(),this.applyLayoutAndA11y({announce:!0,emitChange:!0}),this.lockUntilTransitionEnd())}refresh(){this.rebuildCardsFromLightDom();const t=L(this,"start-index",0),e=w(this,"index"),i=e!=null?Number(e):t,n=Number.isFinite(i)?i:0;this.currentIndex=f(n,this.cards.length),this.reflectIndexAttr(),this.buildDots(),this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set,this.applyLayoutAndA11y({announce:!0,emitChange:!1}),this.dispatchReady()}destroy(){this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null),this.cleanup.forEach(t=>{t()}),this.cleanup=[],this.isAnimating=!1}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}readAttributes(t){this.rootEl||this.render();const e=u(this,"show-arrows",!1),i=u(this,"show-dots",!1);this.prevBtn.style.display=e?"":"none",this.nextBtn.style.display=e?"":"none",this.dotsEl.style.display=i?"":"none";const n=this.getAttribute("aria-label");n&&this.rootEl.setAttribute("aria-label",n),t.isInit&&this.setAttribute("aria-roledescription","carousel")}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="cfc",this.trackEl=document.createElement("div"),this.trackEl.className="cfc__track",this.prevBtn=document.createElement("button"),this.prevBtn.type="button",this.prevBtn.className="cfc__arrow cfc__arrow--left",this.prevBtn.setAttribute("aria-label","Previous"),this.prevBtn.textContent="‹",this.nextBtn=document.createElement("button"),this.nextBtn.type="button",this.nextBtn.className="cfc__arrow cfc__arrow--right",this.nextBtn.setAttribute("aria-label","Next"),this.nextBtn.textContent="›",this.dotsEl=document.createElement("div"),this.dotsEl.className="cfc__dots",this.liveRegion=document.createElement("div"),this.liveRegion.className="cfc__sr",this.liveRegion.setAttribute("aria-live","polite"),this.liveRegion.setAttribute("aria-atomic","true"),this.rootEl.append(this.trackEl,this.prevBtn,this.nextBtn,this.dotsEl,this.liveRegion),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.bindEvents()}bindEvents(){this.cleanup.forEach(s=>{s()}),this.cleanup=[];const t=()=>this.prev(),e=()=>this.next();this.prevBtn.addEventListener("click",t),this.nextBtn.addEventListener("click",e),this.cleanup.push(()=>this.prevBtn.removeEventListener("click",t)),this.cleanup.push(()=>this.nextBtn.removeEventListener("click",e));const i=s=>{if(!this.isAnimating||s.propertyName!=="transform"||!(s.target instanceof HTMLElement))return;const r=this.cards[this.currentIndex];r&&s.target===r&&this.unlockAnimation()};this.rootEl.addEventListener("transitionend",i),this.cleanup.push(()=>this.rootEl.removeEventListener("transitionend",i));const n=s=>{const r=s.target;if(!(r instanceof HTMLElement)||r.tagName!=="SCRATCH-REVEAL")return;const c=s.composedPath?.();if(!c?.length)return;const o=c.find(d=>d instanceof HTMLElement&&d.classList.contains("cfc__card")&&d.dataset.cfcIndex!=null);if(!o)return;const l=Number(o.dataset.cfcIndex);if(!Number.isFinite(l))return;const m=s.detail?.percent??100;this.dispatchEvent(new CustomEvent("coverflow-carousel:scratch-complete",{detail:{index:l,length:this.cards.length,percent:m},bubbles:!0,composed:!0}))};this.rootEl.addEventListener("complete",n),this.cleanup.push(()=>this.rootEl.removeEventListener("complete",n))}rebuildCardsFromLightDom(){const t=Array.from(this.trackEl.children).filter(r=>r instanceof HTMLElement&&r.classList.contains("cfc__card")),e=[];t.forEach(r=>{const c=r.firstElementChild;c instanceof HTMLElement&&e.push(c)});const i=Array.from(this.children).filter(r=>r instanceof HTMLElement),n=[...e,...i],s=[];for(let r=0;r<n.length;r++){const c=n[r],o=t[r]??document.createElement("div");t[r]||(o.className="cfc__card",this.trackEl.append(o)),o.firstElementChild!==c&&o.replaceChildren(c),s.push(o)}for(let r=n.length;r<t.length;r++)t[r]?.remove();this.cards=s,this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set}getVisibleSet(){const t=this.cards.length,e=new Set;if(t<=0)return e;const i=Math.floor(t/2);if(p>=i){for(let n=0;n<t;n++)e.add(n);return e}for(let n=-p;n<=p;n++)e.add(f(this.currentIndex+n,t));return e}buildDots(){const t=Array.from(this.dotsEl.children).filter(s=>s instanceof HTMLElement&&s.classList.contains("cfc__dot"));if(!u(this,"show-dots",!1)){t.forEach(s=>{s.remove()}),this.dots=[];return}const i=[],n=this.cards.length;for(let s=0;s<n;s++){const r=t[s]??document.createElement("span");t[s]||this.dotsEl.append(r),r.className="cfc__dot",r.setAttribute("aria-hidden","true"),i.push(r)}for(let s=n;s<t.length;s++)t[s]?.remove();this.dots=i,this.updateDotsVisualState()}computeCardState(t){const e=this.cards.length,i=T(this.currentIndex,t,e),n=Math.abs(i);return{index:t,delta:i,abs:n,isVisible:n<=p,isActive:t===this.currentIndex}}applyCardState(t,e){t.setAttribute("aria-hidden",e.isVisible?"false":"true"),t.dataset.active=e.isActive?"true":"false",t.dataset.cfcIndex=String(e.index),t.setAttribute("role","group"),t.setAttribute("aria-roledescription","slide"),t.setAttribute("aria-setsize",String(this.cards.length)),t.setAttribute("aria-posinset",String(e.index+1));const i=`${this.instanceId}-slide-${e.index}`;t.id=i;const n=String(e.delta),s=String(e.abs),r=t.dataset.cfcDelta,c=t.dataset.cfcAbs;r!==n&&(t.style.setProperty("--cfc-delta",n),t.dataset.cfcDelta=n),c!==s&&(t.style.setProperty("--cfc-abs",s),t.dataset.cfcAbs=s)}applyLayoutAndA11y(t){if(this.cards.length){if(this.hasAppliedInitialLayout){const e=this.getVisibleSet(),i=new Set;this.lastVisibleSet.forEach(n=>{i.add(n)}),e.forEach(n=>{i.add(n)}),this.lastLayoutIndex!=null&&i.add(this.lastLayoutIndex),i.add(this.currentIndex),i.forEach(n=>{const s=this.cards[n];if(!s)return;const r=this.computeCardState(n);this.applyCardState(s,r)}),this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=e}else{for(let e=0;e<this.cards.length;e++){const i=this.cards[e];if(!i)continue;const n=this.computeCardState(e);this.applyCardState(i,n)}this.hasAppliedInitialLayout=!0,this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=this.getVisibleSet()}this.updateDotsVisualState(),t.announce&&u(this,"announce-changes",!0)&&this.announce(`Slide ${this.currentIndex+1} of ${this.cards.length}`),t.emitChange&&this.emitChange()}}updateDotsVisualState(){if(this.dots.length)for(let t=0;t<this.dots.length;t++){const e=this.dots[t],i=t===this.currentIndex;e.dataset.active=i?"true":"false"}}lockUntilTransitionEnd(){this.pendingAnimToken++;const t=this.pendingAnimToken;if(k()){this.unlockAnimation();return}const e=this.cards[this.currentIndex];if(!e){this.unlockAnimation();return}const i=getComputedStyle(e),n=_(i,"transform");if(n<=0){this.unlockAnimation();return}const s=g(i.getPropertyValue("--cfc-transition-ms").trim(),400),c=Math.max(n,s)+60;this.animFallbackTimerId!==null&&window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=window.setTimeout(()=>{this.pendingAnimToken===t&&this.unlockAnimation()},c)}unlockAnimation(){this.isAnimating=!1,this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null)}emitChange(){this.dispatchEvent(new CustomEvent("coverflow-carousel:change",{detail:{index:this.currentIndex,length:this.cards.length}}))}dispatchReady(){this.dispatchEvent(new CustomEvent("coverflow-carousel:ready",{detail:{index:this.currentIndex,length:this.cards.length}}))}announce(t){this.liveRegion.textContent="",queueMicrotask(()=>{this.liveRegion.textContent=t})}reflectIndexAttr(){this.reflectGuard=!0;try{this.setAttribute("index",String(this.currentIndex))}finally{this.reflectGuard=!1}}applyStyles(t){if(typeof t=="string"){if(v(this.shadow)){const e=S(t);if(e){this.shadow.adoptedStyleSheets=[e],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&v(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(h.defaultStylesheet&&v(this.shadow)){this.shadow.adoptedStyleSheets=[h.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=C}}function F(a={}){const{selector:t="coverflow-carousel",onReady:e,onChange:i,onScratchComplete:n,stylesheet:s}=a,r=Array.from(document.querySelectorAll(t));return(e||i||n)&&r.forEach(c=>{e&&c.addEventListener("coverflow-carousel:ready",o=>{e(c,o.detail)}),i&&c.addEventListener("coverflow-carousel:change",o=>{i(c,o.detail)}),n&&c.addEventListener("coverflow-carousel:scratch-complete",o=>{n(c,o.detail)})}),s&&r.forEach(c=>{const o=c;typeof s=="string"?o.adoptStyles(s):o.adoptStylesheet(s)}),r}function D(a="coverflow-carousel"){typeof window>"u"||!("customElements"in window)||customElements.get(a)||customElements.define(a,h)}exports.CoverflowCarouselElement=h;exports.coverflowCarouselCssText=A;exports.initCoverflowCarousels=F;exports.registerCoverflowCarouselElement=D;
167
+ `,A=x;function v(a){return"adoptedStyleSheets"in a}function S(a){try{const t=new CSSStyleSheet;return t.replaceSync(a),t}catch{return null}}const I=S(x),_=x;function b(a){return a.split(",").map(t=>t.trim()).filter(Boolean)}function g(a,t){const e=a.trim();if(!e)return t;if(e.endsWith("ms")){const n=Number(e.slice(0,-2).trim());return Number.isFinite(n)?n:t}if(e.endsWith("s")){const n=Number(e.slice(0,-1).trim());return Number.isFinite(n)?n*1e3:t}const s=Number(e);return Number.isFinite(s)?s:t}function C(a,t){const e=b(a.transitionProperty),s=b(a.transitionDuration),n=b(a.transitionDelay);if(!e.length)return 0;const r=(m=>{const h=e.indexOf(m);if(h>=0)return h;const E=e.indexOf("all");return E>=0?E:-1})(t);if(r<0)return 0;const c=s[Math.min(r,s.length-1)]??"0s",o=n[Math.min(r,n.length-1)]??"0s",l=g(c,0),y=g(o,0);return Math.max(0,l+y)}function k(){return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches??!1}function u(a,t,e){if(!a.hasAttribute(t))return e;const s=a.getAttribute(t);return s==null||s===""?!0:s!=="false"}function L(a,t,e){const s=a.getAttribute(t);if(s==null)return e;const n=Number(s.trim());return Number.isFinite(n)?Math.trunc(n):e}function w(a,t){const e=a.getAttribute(t);if(e==null)return null;const s=e.trim();return s||null}function f(a,t){return t<=0?0:(a%t+t)%t}function T(a,t,e){const s=t-a,n=e/2;return s>n?s-e:s<-n?s+e:s}const p=1;let D=0;class d extends HTMLElement{static defaultStylesheet=I;static observedAttributes=["start-index","index","show-dots","show-arrows","announce-changes"];shadow=this.attachShadow({mode:"open"});instanceId=`cfc-${++D}`;rootEl;trackEl;dotsEl;prevBtn;nextBtn;liveRegion;styleEl=null;cards=[];dots=[];currentIndex=0;isAnimating=!1;hasAppliedInitialLayout=!1;lastLayoutIndex=null;lastVisibleSet=new Set;pendingAnimToken=0;animFallbackTimerId=null;cleanup=[];lightDomObserver=null;refreshScheduled=!1;reflectGuard=!1;connectedCallback(){this.render(),this.readAttributes({isInit:!0}),this.refresh(),this.setupLightDomObserver()}disconnectedCallback(){this.destroy()}attributeChangedCallback(t,e,s){if(!this.isConnected||this.reflectGuard)return;this.readAttributes({isInit:!1});const n=w(this,"index");if(n!=null){const i=f(Number(n),this.cards.length);if(Number.isFinite(i)&&i!==this.currentIndex){this.goTo(i);return}}this.applyLayoutAndA11y({announce:!0,emitChange:!1})}next(){this.goTo(this.currentIndex+1)}prev(){this.goTo(this.currentIndex-1)}goTo(t){if(this.isAnimating)return;const e=f(t,this.cards.length);e!==this.currentIndex&&(this.isAnimating=!0,this.currentIndex=e,this.reflectIndexAttr(),this.applyLayoutAndA11y({announce:!0,emitChange:!0}),this.lockUntilTransitionEnd())}refresh(){this.rebuildCardsFromLightDom();const t=L(this,"start-index",0),e=w(this,"index"),s=e!=null?Number(e):t,n=Number.isFinite(s)?s:0;this.currentIndex=f(n,this.cards.length),this.reflectIndexAttr(),this.buildDots(),this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set,this.applyLayoutAndA11y({announce:!0,emitChange:!1}),this.dispatchReady()}destroy(){this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null),this.cleanup.forEach(t=>{t()}),this.cleanup=[],this.isAnimating=!1}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}readAttributes(t){this.rootEl||this.render();const e=u(this,"show-arrows",!1),s=u(this,"show-dots",!1);this.prevBtn.style.display=e?"":"none",this.nextBtn.style.display=e?"":"none",this.dotsEl.style.display=s?"":"none";const n=this.getAttribute("aria-label");n&&this.rootEl.setAttribute("aria-label",n),t.isInit&&this.setAttribute("aria-roledescription","carousel")}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="cfc",this.trackEl=document.createElement("div"),this.trackEl.className="cfc__track",this.prevBtn=document.createElement("button"),this.prevBtn.type="button",this.prevBtn.className="cfc__arrow cfc__arrow--left",this.prevBtn.setAttribute("aria-label","Previous"),this.prevBtn.textContent="‹",this.nextBtn=document.createElement("button"),this.nextBtn.type="button",this.nextBtn.className="cfc__arrow cfc__arrow--right",this.nextBtn.setAttribute("aria-label","Next"),this.nextBtn.textContent="›",this.dotsEl=document.createElement("div"),this.dotsEl.className="cfc__dots",this.liveRegion=document.createElement("div"),this.liveRegion.className="cfc__sr",this.liveRegion.setAttribute("aria-live","polite"),this.liveRegion.setAttribute("aria-atomic","true"),this.rootEl.append(this.trackEl,this.prevBtn,this.nextBtn,this.dotsEl,this.liveRegion),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.bindEvents()}bindEvents(){this.cleanup.forEach(i=>{i()}),this.cleanup=[];const t=()=>this.prev(),e=()=>this.next();this.prevBtn.addEventListener("click",t),this.nextBtn.addEventListener("click",e),this.cleanup.push(()=>this.prevBtn.removeEventListener("click",t)),this.cleanup.push(()=>this.nextBtn.removeEventListener("click",e));const s=i=>{if(!this.isAnimating||i.propertyName!=="transform"||!(i.target instanceof HTMLElement))return;const r=this.cards[this.currentIndex];r&&i.target===r&&this.unlockAnimation()};this.rootEl.addEventListener("transitionend",s),this.cleanup.push(()=>this.rootEl.removeEventListener("transitionend",s));const n=i=>{const r=i.target;if(!(r instanceof HTMLElement)||r.tagName!=="SCRATCH-REVEAL")return;const c=i.composedPath?.();if(!c?.length)return;const o=c.find(h=>h instanceof HTMLElement&&h.classList.contains("cfc__card")&&h.dataset.cfcIndex!=null);if(!o)return;const l=Number(o.dataset.cfcIndex);if(!Number.isFinite(l))return;const m=i.detail?.percent??100;this.dispatchEvent(new CustomEvent("coverflow-carousel:scratch-complete",{detail:{index:l,length:this.cards.length,percent:m},bubbles:!0,composed:!0}))};this.rootEl.addEventListener("complete",n),this.cleanup.push(()=>this.rootEl.removeEventListener("complete",n))}setupLightDomObserver(){this.lightDomObserver||(this.lightDomObserver=new MutationObserver(t=>{t.some(s=>!(s.type==="attributes"&&s.attributeName==="slot"))&&this.scheduleRefreshFromMutations()}),this.lightDomObserver.observe(this,{childList:!0,attributes:!0,subtree:!0}),this.cleanup.push(()=>{this.lightDomObserver?.disconnect(),this.lightDomObserver=null}))}scheduleRefreshFromMutations(){this.refreshScheduled||(this.refreshScheduled=!0,queueMicrotask(()=>{this.refreshScheduled=!1,this.isConnected&&this.refresh()}))}rebuildCardsFromLightDom(){const t=Array.from(this.trackEl.children).filter(n=>n instanceof HTMLElement&&n.classList.contains("cfc__card")),e=[],s=Array.from(this.children).filter(n=>n instanceof HTMLElement);for(let n=0;n<s.length;n++){const i=s[n],r=t[n]??document.createElement("div"),c=`${this.instanceId}-slot-${n}`;t[n]||(r.className="cfc__card",this.trackEl.append(r)),i.getAttribute("slot")!==c&&i.setAttribute("slot",c);const o=r.querySelector("slot");let l;o instanceof HTMLSlotElement?l=o:(l=document.createElement("slot"),r.replaceChildren(l)),l.name!==c&&(l.name=c),e.push(r)}for(let n=s.length;n<t.length;n++)t[n]?.remove();this.cards=e,this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set}getVisibleSet(){const t=this.cards.length,e=new Set;if(t<=0)return e;const s=Math.floor(t/2);if(p>=s){for(let n=0;n<t;n++)e.add(n);return e}for(let n=-p;n<=p;n++)e.add(f(this.currentIndex+n,t));return e}buildDots(){const t=Array.from(this.dotsEl.children).filter(i=>i instanceof HTMLElement&&i.classList.contains("cfc__dot"));if(!u(this,"show-dots",!1)){t.forEach(i=>{i.remove()}),this.dots=[];return}const s=[],n=this.cards.length;for(let i=0;i<n;i++){const r=t[i]??document.createElement("span");t[i]||this.dotsEl.append(r),r.className="cfc__dot",r.setAttribute("aria-hidden","true"),s.push(r)}for(let i=n;i<t.length;i++)t[i]?.remove();this.dots=s,this.updateDotsVisualState()}computeCardState(t){const e=this.cards.length,s=T(this.currentIndex,t,e),n=Math.abs(s);return{index:t,delta:s,abs:n,isVisible:n<=p,isActive:t===this.currentIndex}}applyCardState(t,e){t.setAttribute("aria-hidden",e.isVisible?"false":"true"),t.dataset.active=e.isActive?"true":"false",t.dataset.cfcIndex=String(e.index),t.setAttribute("role","group"),t.setAttribute("aria-roledescription","slide"),t.setAttribute("aria-setsize",String(this.cards.length)),t.setAttribute("aria-posinset",String(e.index+1));const s=`${this.instanceId}-slide-${e.index}`;t.id=s;const n=String(e.delta),i=String(e.abs),r=t.dataset.cfcDelta,c=t.dataset.cfcAbs;r!==n&&(t.style.setProperty("--cfc-delta",n),t.dataset.cfcDelta=n),c!==i&&(t.style.setProperty("--cfc-abs",i),t.dataset.cfcAbs=i)}applyLayoutAndA11y(t){if(this.cards.length){if(this.hasAppliedInitialLayout){const e=this.getVisibleSet(),s=new Set;this.lastVisibleSet.forEach(n=>{s.add(n)}),e.forEach(n=>{s.add(n)}),this.lastLayoutIndex!=null&&s.add(this.lastLayoutIndex),s.add(this.currentIndex),s.forEach(n=>{const i=this.cards[n];if(!i)return;const r=this.computeCardState(n);this.applyCardState(i,r)}),this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=e}else{for(let e=0;e<this.cards.length;e++){const s=this.cards[e];if(!s)continue;const n=this.computeCardState(e);this.applyCardState(s,n)}this.hasAppliedInitialLayout=!0,this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=this.getVisibleSet()}this.updateDotsVisualState(),t.announce&&u(this,"announce-changes",!0)&&this.announce(`Slide ${this.currentIndex+1} of ${this.cards.length}`),t.emitChange&&this.emitChange()}}updateDotsVisualState(){if(this.dots.length)for(let t=0;t<this.dots.length;t++){const e=this.dots[t],s=t===this.currentIndex;e.dataset.active=s?"true":"false"}}lockUntilTransitionEnd(){this.pendingAnimToken++;const t=this.pendingAnimToken;if(k()){this.unlockAnimation();return}const e=this.cards[this.currentIndex];if(!e){this.unlockAnimation();return}const s=getComputedStyle(e),n=C(s,"transform");if(n<=0){this.unlockAnimation();return}const i=g(s.getPropertyValue("--cfc-transition-ms").trim(),400),c=Math.max(n,i)+60;this.animFallbackTimerId!==null&&window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=window.setTimeout(()=>{this.pendingAnimToken===t&&this.unlockAnimation()},c)}unlockAnimation(){this.isAnimating=!1,this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null)}emitChange(){this.dispatchEvent(new CustomEvent("coverflow-carousel:change",{detail:{index:this.currentIndex,length:this.cards.length}}))}dispatchReady(){this.dispatchEvent(new CustomEvent("coverflow-carousel:ready",{detail:{index:this.currentIndex,length:this.cards.length}}))}announce(t){this.liveRegion.textContent="",queueMicrotask(()=>{this.liveRegion.textContent=t})}reflectIndexAttr(){this.reflectGuard=!0;try{this.setAttribute("index",String(this.currentIndex))}finally{this.reflectGuard=!1}}applyStyles(t){if(typeof t=="string"){if(v(this.shadow)){const e=S(t);if(e){this.shadow.adoptedStyleSheets=[e],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&v(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(d.defaultStylesheet&&v(this.shadow)){this.shadow.adoptedStyleSheets=[d.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=_}}function N(a={}){const{selector:t="coverflow-carousel",onReady:e,onChange:s,onScratchComplete:n,stylesheet:i}=a,r=Array.from(document.querySelectorAll(t));return(e||s||n)&&r.forEach(c=>{e&&c.addEventListener("coverflow-carousel:ready",o=>{e(c,o.detail)}),s&&c.addEventListener("coverflow-carousel:change",o=>{s(c,o.detail)}),n&&c.addEventListener("coverflow-carousel:scratch-complete",o=>{n(c,o.detail)})}),i&&r.forEach(c=>{const o=c;typeof i=="string"?o.adoptStyles(i):o.adoptStylesheet(i)}),r}function F(a="coverflow-carousel"){typeof window>"u"||!("customElements"in window)||customElements.get(a)||customElements.define(a,d)}exports.CoverflowCarouselElement=d;exports.coverflowCarouselCssText=A;exports.initCoverflowCarousels=N;exports.registerCoverflowCarouselElement=F;
package/dist/index.es.js CHANGED
@@ -1,4 +1,4 @@
1
- const b = `:host {
1
+ const x = `:host {
2
2
  display: block;
3
3
  width: 100%;
4
4
  height: 100%;
@@ -164,7 +164,7 @@ const b = `:host {
164
164
  white-space: nowrap;
165
165
  border: 0;
166
166
  }
167
- `, N = b;
167
+ `, D = x;
168
168
  function v(a) {
169
169
  return "adoptedStyleSheets" in a;
170
170
  }
@@ -176,8 +176,8 @@ function S(a) {
176
176
  return null;
177
177
  }
178
178
  }
179
- const A = S(b), I = b;
180
- function x(a) {
179
+ const A = S(x), I = x;
180
+ function b(a) {
181
181
  return a.split(",").map((t) => t.trim()).filter(Boolean);
182
182
  }
183
183
  function g(a, t) {
@@ -191,48 +191,48 @@ function g(a, t) {
191
191
  const n = Number(e.slice(0, -1).trim());
192
192
  return Number.isFinite(n) ? n * 1e3 : t;
193
193
  }
194
- const i = Number(e);
195
- return Number.isFinite(i) ? i : t;
194
+ const s = Number(e);
195
+ return Number.isFinite(s) ? s : t;
196
196
  }
197
197
  function _(a, t) {
198
- const e = x(a.transitionProperty), i = x(a.transitionDuration), n = x(a.transitionDelay);
198
+ const e = b(a.transitionProperty), s = b(a.transitionDuration), n = b(a.transitionDelay);
199
199
  if (!e.length) return 0;
200
200
  const r = ((m) => {
201
- const d = e.indexOf(m);
202
- if (d >= 0) return d;
201
+ const h = e.indexOf(m);
202
+ if (h >= 0) return h;
203
203
  const E = e.indexOf("all");
204
204
  return E >= 0 ? E : -1;
205
205
  })(t);
206
206
  if (r < 0) return 0;
207
- const c = i[Math.min(r, i.length - 1)] ?? "0s", o = n[Math.min(r, n.length - 1)] ?? "0s", l = g(c, 0), y = g(o, 0);
207
+ const c = s[Math.min(r, s.length - 1)] ?? "0s", o = n[Math.min(r, n.length - 1)] ?? "0s", l = g(c, 0), y = g(o, 0);
208
208
  return Math.max(0, l + y);
209
209
  }
210
210
  function k() {
211
211
  return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches ?? !1;
212
212
  }
213
- function h(a, t, e) {
213
+ function d(a, t, e) {
214
214
  if (!a.hasAttribute(t)) return e;
215
- const i = a.getAttribute(t);
216
- return i == null || i === "" ? !0 : i !== "false";
215
+ const s = a.getAttribute(t);
216
+ return s == null || s === "" ? !0 : s !== "false";
217
217
  }
218
218
  function C(a, t, e) {
219
- const i = a.getAttribute(t);
220
- if (i == null) return e;
221
- const n = Number(i.trim());
219
+ const s = a.getAttribute(t);
220
+ if (s == null) return e;
221
+ const n = Number(s.trim());
222
222
  return Number.isFinite(n) ? Math.trunc(n) : e;
223
223
  }
224
224
  function w(a, t) {
225
225
  const e = a.getAttribute(t);
226
226
  if (e == null) return null;
227
- const i = e.trim();
228
- return i || null;
227
+ const s = e.trim();
228
+ return s || null;
229
229
  }
230
230
  function u(a, t) {
231
231
  return t <= 0 ? 0 : (a % t + t) % t;
232
232
  }
233
233
  function L(a, t, e) {
234
- const i = t - a, n = e / 2;
235
- return i > n ? i - e : i < -n ? i + e : i;
234
+ const s = t - a, n = e / 2;
235
+ return s > n ? s - e : s < -n ? s + e : s;
236
236
  }
237
237
  const f = 1;
238
238
  let T = 0;
@@ -264,21 +264,23 @@ class p extends HTMLElement {
264
264
  pendingAnimToken = 0;
265
265
  animFallbackTimerId = null;
266
266
  cleanup = [];
267
+ lightDomObserver = null;
268
+ refreshScheduled = !1;
267
269
  reflectGuard = !1;
268
270
  connectedCallback() {
269
- this.render(), this.readAttributes({ isInit: !0 }), this.refresh();
271
+ this.render(), this.readAttributes({ isInit: !0 }), this.refresh(), this.setupLightDomObserver();
270
272
  }
271
273
  disconnectedCallback() {
272
274
  this.destroy();
273
275
  }
274
- attributeChangedCallback(t, e, i) {
276
+ attributeChangedCallback(t, e, s) {
275
277
  if (!this.isConnected || this.reflectGuard) return;
276
278
  this.readAttributes({ isInit: !1 });
277
279
  const n = w(this, "index");
278
280
  if (n != null) {
279
- const s = u(Number(n), this.cards.length);
280
- if (Number.isFinite(s) && s !== this.currentIndex) {
281
- this.goTo(s);
281
+ const i = u(Number(n), this.cards.length);
282
+ if (Number.isFinite(i) && i !== this.currentIndex) {
283
+ this.goTo(i);
282
284
  return;
283
285
  }
284
286
  }
@@ -297,7 +299,7 @@ class p extends HTMLElement {
297
299
  }
298
300
  refresh() {
299
301
  this.rebuildCardsFromLightDom();
300
- const t = C(this, "start-index", 0), e = w(this, "index"), i = e != null ? Number(e) : t, n = Number.isFinite(i) ? i : 0;
302
+ const t = C(this, "start-index", 0), e = w(this, "index"), s = e != null ? Number(e) : t, n = Number.isFinite(s) ? s : 0;
301
303
  this.currentIndex = u(n, this.cards.length), this.reflectIndexAttr(), this.buildDots(), this.hasAppliedInitialLayout = !1, this.lastLayoutIndex = null, this.lastVisibleSet = /* @__PURE__ */ new Set(), this.applyLayoutAndA11y({ announce: !0, emitChange: !1 }), this.dispatchReady();
302
304
  }
303
305
  destroy() {
@@ -313,8 +315,8 @@ class p extends HTMLElement {
313
315
  }
314
316
  readAttributes(t) {
315
317
  this.rootEl || this.render();
316
- const e = h(this, "show-arrows", !1), i = h(this, "show-dots", !1);
317
- this.prevBtn.style.display = e ? "" : "none", this.nextBtn.style.display = e ? "" : "none", this.dotsEl.style.display = i ? "" : "none";
318
+ const e = d(this, "show-arrows", !1), s = d(this, "show-dots", !1);
319
+ this.prevBtn.style.display = e ? "" : "none", this.nextBtn.style.display = e ? "" : "none", this.dotsEl.style.display = s ? "" : "none";
318
320
  const n = this.getAttribute("aria-label");
319
321
  n && this.rootEl.setAttribute("aria-label", n), t.isInit && this.setAttribute("aria-roledescription", "carousel");
320
322
  }
@@ -322,29 +324,29 @@ class p extends HTMLElement {
322
324
  this.applyStyles(null), this.rootEl = document.createElement("div"), this.rootEl.className = "cfc", this.trackEl = document.createElement("div"), this.trackEl.className = "cfc__track", this.prevBtn = document.createElement("button"), this.prevBtn.type = "button", this.prevBtn.className = "cfc__arrow cfc__arrow--left", this.prevBtn.setAttribute("aria-label", "Previous"), this.prevBtn.textContent = "‹", this.nextBtn = document.createElement("button"), this.nextBtn.type = "button", this.nextBtn.className = "cfc__arrow cfc__arrow--right", this.nextBtn.setAttribute("aria-label", "Next"), this.nextBtn.textContent = "›", this.dotsEl = document.createElement("div"), this.dotsEl.className = "cfc__dots", this.liveRegion = document.createElement("div"), this.liveRegion.className = "cfc__sr", this.liveRegion.setAttribute("aria-live", "polite"), this.liveRegion.setAttribute("aria-atomic", "true"), this.rootEl.append(this.trackEl, this.prevBtn, this.nextBtn, this.dotsEl, this.liveRegion), this.shadow.innerHTML = "", this.styleEl && this.shadow.append(this.styleEl), this.shadow.append(this.rootEl), this.bindEvents();
323
325
  }
324
326
  bindEvents() {
325
- this.cleanup.forEach((s) => {
326
- s();
327
+ this.cleanup.forEach((i) => {
328
+ i();
327
329
  }), this.cleanup = [];
328
330
  const t = () => this.prev(), e = () => this.next();
329
331
  this.prevBtn.addEventListener("click", t), this.nextBtn.addEventListener("click", e), this.cleanup.push(() => this.prevBtn.removeEventListener("click", t)), this.cleanup.push(() => this.nextBtn.removeEventListener("click", e));
330
- const i = (s) => {
331
- if (!this.isAnimating || s.propertyName !== "transform" || !(s.target instanceof HTMLElement)) return;
332
+ const s = (i) => {
333
+ if (!this.isAnimating || i.propertyName !== "transform" || !(i.target instanceof HTMLElement)) return;
332
334
  const r = this.cards[this.currentIndex];
333
- r && s.target === r && this.unlockAnimation();
335
+ r && i.target === r && this.unlockAnimation();
334
336
  };
335
- this.rootEl.addEventListener("transitionend", i), this.cleanup.push(() => this.rootEl.removeEventListener("transitionend", i));
336
- const n = (s) => {
337
- const r = s.target;
337
+ this.rootEl.addEventListener("transitionend", s), this.cleanup.push(() => this.rootEl.removeEventListener("transitionend", s));
338
+ const n = (i) => {
339
+ const r = i.target;
338
340
  if (!(r instanceof HTMLElement) || r.tagName !== "SCRATCH-REVEAL") return;
339
- const c = s.composedPath?.();
341
+ const c = i.composedPath?.();
340
342
  if (!c?.length) return;
341
343
  const o = c.find(
342
- (d) => d instanceof HTMLElement && d.classList.contains("cfc__card") && d.dataset.cfcIndex != null
344
+ (h) => h instanceof HTMLElement && h.classList.contains("cfc__card") && h.dataset.cfcIndex != null
343
345
  );
344
346
  if (!o) return;
345
347
  const l = Number(o.dataset.cfcIndex);
346
348
  if (!Number.isFinite(l)) return;
347
- const m = s.detail?.percent ?? 100;
349
+ const m = i.detail?.percent ?? 100;
348
350
  this.dispatchEvent(
349
351
  new CustomEvent("coverflow-carousel:scratch-complete", {
350
352
  detail: { index: l, length: this.cards.length, percent: m },
@@ -357,30 +359,44 @@ class p extends HTMLElement {
357
359
  () => this.rootEl.removeEventListener("complete", n)
358
360
  );
359
361
  }
362
+ setupLightDomObserver() {
363
+ this.lightDomObserver || (this.lightDomObserver = new MutationObserver((t) => {
364
+ t.some((s) => !(s.type === "attributes" && s.attributeName === "slot")) && this.scheduleRefreshFromMutations();
365
+ }), this.lightDomObserver.observe(this, {
366
+ childList: !0,
367
+ attributes: !0,
368
+ subtree: !0
369
+ }), this.cleanup.push(() => {
370
+ this.lightDomObserver?.disconnect(), this.lightDomObserver = null;
371
+ }));
372
+ }
373
+ scheduleRefreshFromMutations() {
374
+ this.refreshScheduled || (this.refreshScheduled = !0, queueMicrotask(() => {
375
+ this.refreshScheduled = !1, this.isConnected && this.refresh();
376
+ }));
377
+ }
360
378
  rebuildCardsFromLightDom() {
361
379
  const t = Array.from(this.trackEl.children).filter(
362
- (r) => r instanceof HTMLElement && r.classList.contains("cfc__card")
363
- ), e = [];
364
- t.forEach((r) => {
365
- const c = r.firstElementChild;
366
- c instanceof HTMLElement && e.push(c);
367
- });
368
- const i = Array.from(this.children).filter(
369
- (r) => r instanceof HTMLElement
370
- ), n = [...e, ...i], s = [];
371
- for (let r = 0; r < n.length; r++) {
372
- const c = n[r], o = t[r] ?? document.createElement("div");
373
- t[r] || (o.className = "cfc__card", this.trackEl.append(o)), o.firstElementChild !== c && o.replaceChildren(c), s.push(o);
380
+ (n) => n instanceof HTMLElement && n.classList.contains("cfc__card")
381
+ ), e = [], s = Array.from(this.children).filter(
382
+ (n) => n instanceof HTMLElement
383
+ );
384
+ for (let n = 0; n < s.length; n++) {
385
+ const i = s[n], r = t[n] ?? document.createElement("div"), c = `${this.instanceId}-slot-${n}`;
386
+ t[n] || (r.className = "cfc__card", this.trackEl.append(r)), i.getAttribute("slot") !== c && i.setAttribute("slot", c);
387
+ const o = r.querySelector("slot");
388
+ let l;
389
+ o instanceof HTMLSlotElement ? l = o : (l = document.createElement("slot"), r.replaceChildren(l)), l.name !== c && (l.name = c), e.push(r);
374
390
  }
375
- for (let r = n.length; r < t.length; r++)
376
- t[r]?.remove();
377
- this.cards = s, this.hasAppliedInitialLayout = !1, this.lastLayoutIndex = null, this.lastVisibleSet = /* @__PURE__ */ new Set();
391
+ for (let n = s.length; n < t.length; n++)
392
+ t[n]?.remove();
393
+ this.cards = e, this.hasAppliedInitialLayout = !1, this.lastLayoutIndex = null, this.lastVisibleSet = /* @__PURE__ */ new Set();
378
394
  }
379
395
  getVisibleSet() {
380
396
  const t = this.cards.length, e = /* @__PURE__ */ new Set();
381
397
  if (t <= 0) return e;
382
- const i = Math.floor(t / 2);
383
- if (f >= i) {
398
+ const s = Math.floor(t / 2);
399
+ if (f >= s) {
384
400
  for (let n = 0; n < t; n++) e.add(n);
385
401
  return e;
386
402
  }
@@ -390,28 +406,28 @@ class p extends HTMLElement {
390
406
  }
391
407
  buildDots() {
392
408
  const t = Array.from(this.dotsEl.children).filter(
393
- (s) => s instanceof HTMLElement && s.classList.contains("cfc__dot")
409
+ (i) => i instanceof HTMLElement && i.classList.contains("cfc__dot")
394
410
  );
395
- if (!h(this, "show-dots", !1)) {
396
- t.forEach((s) => {
397
- s.remove();
411
+ if (!d(this, "show-dots", !1)) {
412
+ t.forEach((i) => {
413
+ i.remove();
398
414
  }), this.dots = [];
399
415
  return;
400
416
  }
401
- const i = [], n = this.cards.length;
402
- for (let s = 0; s < n; s++) {
403
- const r = t[s] ?? document.createElement("span");
404
- t[s] || this.dotsEl.append(r), r.className = "cfc__dot", r.setAttribute("aria-hidden", "true"), i.push(r);
417
+ const s = [], n = this.cards.length;
418
+ for (let i = 0; i < n; i++) {
419
+ const r = t[i] ?? document.createElement("span");
420
+ t[i] || this.dotsEl.append(r), r.className = "cfc__dot", r.setAttribute("aria-hidden", "true"), s.push(r);
405
421
  }
406
- for (let s = n; s < t.length; s++)
407
- t[s]?.remove();
408
- this.dots = i, this.updateDotsVisualState();
422
+ for (let i = n; i < t.length; i++)
423
+ t[i]?.remove();
424
+ this.dots = s, this.updateDotsVisualState();
409
425
  }
410
426
  computeCardState(t) {
411
- const e = this.cards.length, i = L(this.currentIndex, t, e), n = Math.abs(i);
427
+ const e = this.cards.length, s = L(this.currentIndex, t, e), n = Math.abs(s);
412
428
  return {
413
429
  index: t,
414
- delta: i,
430
+ delta: s,
415
431
  abs: n,
416
432
  isVisible: n <= f,
417
433
  isActive: t === this.currentIndex
@@ -419,42 +435,42 @@ class p extends HTMLElement {
419
435
  }
420
436
  applyCardState(t, e) {
421
437
  t.setAttribute("aria-hidden", e.isVisible ? "false" : "true"), t.dataset.active = e.isActive ? "true" : "false", t.dataset.cfcIndex = String(e.index), t.setAttribute("role", "group"), t.setAttribute("aria-roledescription", "slide"), t.setAttribute("aria-setsize", String(this.cards.length)), t.setAttribute("aria-posinset", String(e.index + 1));
422
- const i = `${this.instanceId}-slide-${e.index}`;
423
- t.id = i;
424
- const n = String(e.delta), s = String(e.abs), r = t.dataset.cfcDelta, c = t.dataset.cfcAbs;
425
- r !== n && (t.style.setProperty("--cfc-delta", n), t.dataset.cfcDelta = n), c !== s && (t.style.setProperty("--cfc-abs", s), t.dataset.cfcAbs = s);
438
+ const s = `${this.instanceId}-slide-${e.index}`;
439
+ t.id = s;
440
+ const n = String(e.delta), i = String(e.abs), r = t.dataset.cfcDelta, c = t.dataset.cfcAbs;
441
+ r !== n && (t.style.setProperty("--cfc-delta", n), t.dataset.cfcDelta = n), c !== i && (t.style.setProperty("--cfc-abs", i), t.dataset.cfcAbs = i);
426
442
  }
427
443
  applyLayoutAndA11y(t) {
428
444
  if (this.cards.length) {
429
445
  if (this.hasAppliedInitialLayout) {
430
- const e = this.getVisibleSet(), i = /* @__PURE__ */ new Set();
446
+ const e = this.getVisibleSet(), s = /* @__PURE__ */ new Set();
431
447
  this.lastVisibleSet.forEach((n) => {
432
- i.add(n);
448
+ s.add(n);
433
449
  }), e.forEach((n) => {
434
- i.add(n);
435
- }), this.lastLayoutIndex != null && i.add(this.lastLayoutIndex), i.add(this.currentIndex), i.forEach((n) => {
436
- const s = this.cards[n];
437
- if (!s) return;
450
+ s.add(n);
451
+ }), this.lastLayoutIndex != null && s.add(this.lastLayoutIndex), s.add(this.currentIndex), s.forEach((n) => {
452
+ const i = this.cards[n];
453
+ if (!i) return;
438
454
  const r = this.computeCardState(n);
439
- this.applyCardState(s, r);
455
+ this.applyCardState(i, r);
440
456
  }), this.lastLayoutIndex = this.currentIndex, this.lastVisibleSet = e;
441
457
  } else {
442
458
  for (let e = 0; e < this.cards.length; e++) {
443
- const i = this.cards[e];
444
- if (!i) continue;
459
+ const s = this.cards[e];
460
+ if (!s) continue;
445
461
  const n = this.computeCardState(e);
446
- this.applyCardState(i, n);
462
+ this.applyCardState(s, n);
447
463
  }
448
464
  this.hasAppliedInitialLayout = !0, this.lastLayoutIndex = this.currentIndex, this.lastVisibleSet = this.getVisibleSet();
449
465
  }
450
- this.updateDotsVisualState(), t.announce && h(this, "announce-changes", !0) && this.announce(`Slide ${this.currentIndex + 1} of ${this.cards.length}`), t.emitChange && this.emitChange();
466
+ this.updateDotsVisualState(), t.announce && d(this, "announce-changes", !0) && this.announce(`Slide ${this.currentIndex + 1} of ${this.cards.length}`), t.emitChange && this.emitChange();
451
467
  }
452
468
  }
453
469
  updateDotsVisualState() {
454
470
  if (this.dots.length)
455
471
  for (let t = 0; t < this.dots.length; t++) {
456
- const e = this.dots[t], i = t === this.currentIndex;
457
- e.dataset.active = i ? "true" : "false";
472
+ const e = this.dots[t], s = t === this.currentIndex;
473
+ e.dataset.active = s ? "true" : "false";
458
474
  }
459
475
  }
460
476
  lockUntilTransitionEnd() {
@@ -469,12 +485,12 @@ class p extends HTMLElement {
469
485
  this.unlockAnimation();
470
486
  return;
471
487
  }
472
- const i = getComputedStyle(e), n = _(i, "transform");
488
+ const s = getComputedStyle(e), n = _(s, "transform");
473
489
  if (n <= 0) {
474
490
  this.unlockAnimation();
475
491
  return;
476
492
  }
477
- const s = g(i.getPropertyValue("--cfc-transition-ms").trim(), 400), c = Math.max(n, s) + 60;
493
+ const i = g(s.getPropertyValue("--cfc-transition-ms").trim(), 400), c = Math.max(n, i) + 60;
478
494
  this.animFallbackTimerId !== null && window.clearTimeout(this.animFallbackTimerId), this.animFallbackTimerId = window.setTimeout(() => {
479
495
  this.pendingAnimToken === t && this.unlockAnimation();
480
496
  }, c);
@@ -532,33 +548,33 @@ class p extends HTMLElement {
532
548
  this.styleEl || (this.styleEl = document.createElement("style")), this.styleEl.textContent = I;
533
549
  }
534
550
  }
535
- function F(a = {}) {
551
+ function N(a = {}) {
536
552
  const {
537
553
  selector: t = "coverflow-carousel",
538
554
  onReady: e,
539
- onChange: i,
555
+ onChange: s,
540
556
  onScratchComplete: n,
541
- stylesheet: s
557
+ stylesheet: i
542
558
  } = a, r = Array.from(document.querySelectorAll(t));
543
- return (e || i || n) && r.forEach((c) => {
559
+ return (e || s || n) && r.forEach((c) => {
544
560
  e && c.addEventListener("coverflow-carousel:ready", (o) => {
545
561
  e(c, o.detail);
546
- }), i && c.addEventListener("coverflow-carousel:change", (o) => {
547
- i(c, o.detail);
562
+ }), s && c.addEventListener("coverflow-carousel:change", (o) => {
563
+ s(c, o.detail);
548
564
  }), n && c.addEventListener("coverflow-carousel:scratch-complete", (o) => {
549
565
  n(c, o.detail);
550
566
  });
551
- }), s && r.forEach((c) => {
567
+ }), i && r.forEach((c) => {
552
568
  const o = c;
553
- typeof s == "string" ? o.adoptStyles(s) : o.adoptStylesheet(s);
569
+ typeof i == "string" ? o.adoptStyles(i) : o.adoptStylesheet(i);
554
570
  }), r;
555
571
  }
556
- function D(a = "coverflow-carousel") {
572
+ function F(a = "coverflow-carousel") {
557
573
  typeof window > "u" || !("customElements" in window) || customElements.get(a) || customElements.define(a, p);
558
574
  }
559
575
  export {
560
576
  p as CoverflowCarouselElement,
561
- N as coverflowCarouselCssText,
562
- F as initCoverflowCarousels,
563
- D as registerCoverflowCarouselElement
577
+ D as coverflowCarouselCssText,
578
+ N as initCoverflowCarousels,
579
+ F as registerCoverflowCarouselElement
564
580
  };
package/dist/index.umd.js CHANGED
@@ -1,4 +1,4 @@
1
- (function(l,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(l=typeof globalThis<"u"?globalThis:l||self,d(l.CoverflowCarousel={}))})(this,(function(l){"use strict";const d=`:host {
1
+ (function(d,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(d=typeof globalThis<"u"?globalThis:d||self,h(d.CoverflowCarousel={}))})(this,(function(d){"use strict";const h=`:host {
2
2
  display: block;
3
3
  width: 100%;
4
4
  height: 100%;
@@ -164,4 +164,4 @@
164
164
  white-space: nowrap;
165
165
  border: 0;
166
166
  }
167
- `,I=d;function x(a){return"adoptedStyleSheets"in a}function E(a){try{const t=new CSSStyleSheet;return t.replaceSync(a),t}catch{return null}}const C=E(d),_=d;function g(a){return a.split(",").map(t=>t.trim()).filter(Boolean)}function b(a,t){const e=a.trim();if(!e)return t;if(e.endsWith("ms")){const n=Number(e.slice(0,-2).trim());return Number.isFinite(n)?n:t}if(e.endsWith("s")){const n=Number(e.slice(0,-1).trim());return Number.isFinite(n)?n*1e3:t}const i=Number(e);return Number.isFinite(i)?i:t}function k(a,t){const e=g(a.transitionProperty),i=g(a.transitionDuration),n=g(a.transitionDelay);if(!e.length)return 0;const r=(y=>{const u=e.indexOf(y);if(u>=0)return u;const A=e.indexOf("all");return A>=0?A:-1})(t);if(r<0)return 0;const c=i[Math.min(r,i.length-1)]??"0s",o=n[Math.min(r,n.length-1)]??"0s",h=b(c,0),S=b(o,0);return Math.max(0,h+S)}function T(){return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches??!1}function p(a,t,e){if(!a.hasAttribute(t))return e;const i=a.getAttribute(t);return i==null||i===""?!0:i!=="false"}function L(a,t,e){const i=a.getAttribute(t);if(i==null)return e;const n=Number(i.trim());return Number.isFinite(n)?Math.trunc(n):e}function w(a,t){const e=a.getAttribute(t);if(e==null)return null;const i=e.trim();return i||null}function m(a,t){return t<=0?0:(a%t+t)%t}function N(a,t,e){const i=t-a,n=e/2;return i>n?i-e:i<-n?i+e:i}const v=1;let F=0;class f extends HTMLElement{static defaultStylesheet=C;static observedAttributes=["start-index","index","show-dots","show-arrows","announce-changes"];shadow=this.attachShadow({mode:"open"});instanceId=`cfc-${++F}`;rootEl;trackEl;dotsEl;prevBtn;nextBtn;liveRegion;styleEl=null;cards=[];dots=[];currentIndex=0;isAnimating=!1;hasAppliedInitialLayout=!1;lastLayoutIndex=null;lastVisibleSet=new Set;pendingAnimToken=0;animFallbackTimerId=null;cleanup=[];reflectGuard=!1;connectedCallback(){this.render(),this.readAttributes({isInit:!0}),this.refresh()}disconnectedCallback(){this.destroy()}attributeChangedCallback(t,e,i){if(!this.isConnected||this.reflectGuard)return;this.readAttributes({isInit:!1});const n=w(this,"index");if(n!=null){const s=m(Number(n),this.cards.length);if(Number.isFinite(s)&&s!==this.currentIndex){this.goTo(s);return}}this.applyLayoutAndA11y({announce:!0,emitChange:!1})}next(){this.goTo(this.currentIndex+1)}prev(){this.goTo(this.currentIndex-1)}goTo(t){if(this.isAnimating)return;const e=m(t,this.cards.length);e!==this.currentIndex&&(this.isAnimating=!0,this.currentIndex=e,this.reflectIndexAttr(),this.applyLayoutAndA11y({announce:!0,emitChange:!0}),this.lockUntilTransitionEnd())}refresh(){this.rebuildCardsFromLightDom();const t=L(this,"start-index",0),e=w(this,"index"),i=e!=null?Number(e):t,n=Number.isFinite(i)?i:0;this.currentIndex=m(n,this.cards.length),this.reflectIndexAttr(),this.buildDots(),this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set,this.applyLayoutAndA11y({announce:!0,emitChange:!1}),this.dispatchReady()}destroy(){this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null),this.cleanup.forEach(t=>{t()}),this.cleanup=[],this.isAnimating=!1}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}readAttributes(t){this.rootEl||this.render();const e=p(this,"show-arrows",!1),i=p(this,"show-dots",!1);this.prevBtn.style.display=e?"":"none",this.nextBtn.style.display=e?"":"none",this.dotsEl.style.display=i?"":"none";const n=this.getAttribute("aria-label");n&&this.rootEl.setAttribute("aria-label",n),t.isInit&&this.setAttribute("aria-roledescription","carousel")}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="cfc",this.trackEl=document.createElement("div"),this.trackEl.className="cfc__track",this.prevBtn=document.createElement("button"),this.prevBtn.type="button",this.prevBtn.className="cfc__arrow cfc__arrow--left",this.prevBtn.setAttribute("aria-label","Previous"),this.prevBtn.textContent="‹",this.nextBtn=document.createElement("button"),this.nextBtn.type="button",this.nextBtn.className="cfc__arrow cfc__arrow--right",this.nextBtn.setAttribute("aria-label","Next"),this.nextBtn.textContent="›",this.dotsEl=document.createElement("div"),this.dotsEl.className="cfc__dots",this.liveRegion=document.createElement("div"),this.liveRegion.className="cfc__sr",this.liveRegion.setAttribute("aria-live","polite"),this.liveRegion.setAttribute("aria-atomic","true"),this.rootEl.append(this.trackEl,this.prevBtn,this.nextBtn,this.dotsEl,this.liveRegion),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.bindEvents()}bindEvents(){this.cleanup.forEach(s=>{s()}),this.cleanup=[];const t=()=>this.prev(),e=()=>this.next();this.prevBtn.addEventListener("click",t),this.nextBtn.addEventListener("click",e),this.cleanup.push(()=>this.prevBtn.removeEventListener("click",t)),this.cleanup.push(()=>this.nextBtn.removeEventListener("click",e));const i=s=>{if(!this.isAnimating||s.propertyName!=="transform"||!(s.target instanceof HTMLElement))return;const r=this.cards[this.currentIndex];r&&s.target===r&&this.unlockAnimation()};this.rootEl.addEventListener("transitionend",i),this.cleanup.push(()=>this.rootEl.removeEventListener("transitionend",i));const n=s=>{const r=s.target;if(!(r instanceof HTMLElement)||r.tagName!=="SCRATCH-REVEAL")return;const c=s.composedPath?.();if(!c?.length)return;const o=c.find(u=>u instanceof HTMLElement&&u.classList.contains("cfc__card")&&u.dataset.cfcIndex!=null);if(!o)return;const h=Number(o.dataset.cfcIndex);if(!Number.isFinite(h))return;const y=s.detail?.percent??100;this.dispatchEvent(new CustomEvent("coverflow-carousel:scratch-complete",{detail:{index:h,length:this.cards.length,percent:y},bubbles:!0,composed:!0}))};this.rootEl.addEventListener("complete",n),this.cleanup.push(()=>this.rootEl.removeEventListener("complete",n))}rebuildCardsFromLightDom(){const t=Array.from(this.trackEl.children).filter(r=>r instanceof HTMLElement&&r.classList.contains("cfc__card")),e=[];t.forEach(r=>{const c=r.firstElementChild;c instanceof HTMLElement&&e.push(c)});const i=Array.from(this.children).filter(r=>r instanceof HTMLElement),n=[...e,...i],s=[];for(let r=0;r<n.length;r++){const c=n[r],o=t[r]??document.createElement("div");t[r]||(o.className="cfc__card",this.trackEl.append(o)),o.firstElementChild!==c&&o.replaceChildren(c),s.push(o)}for(let r=n.length;r<t.length;r++)t[r]?.remove();this.cards=s,this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set}getVisibleSet(){const t=this.cards.length,e=new Set;if(t<=0)return e;const i=Math.floor(t/2);if(v>=i){for(let n=0;n<t;n++)e.add(n);return e}for(let n=-v;n<=v;n++)e.add(m(this.currentIndex+n,t));return e}buildDots(){const t=Array.from(this.dotsEl.children).filter(s=>s instanceof HTMLElement&&s.classList.contains("cfc__dot"));if(!p(this,"show-dots",!1)){t.forEach(s=>{s.remove()}),this.dots=[];return}const i=[],n=this.cards.length;for(let s=0;s<n;s++){const r=t[s]??document.createElement("span");t[s]||this.dotsEl.append(r),r.className="cfc__dot",r.setAttribute("aria-hidden","true"),i.push(r)}for(let s=n;s<t.length;s++)t[s]?.remove();this.dots=i,this.updateDotsVisualState()}computeCardState(t){const e=this.cards.length,i=N(this.currentIndex,t,e),n=Math.abs(i);return{index:t,delta:i,abs:n,isVisible:n<=v,isActive:t===this.currentIndex}}applyCardState(t,e){t.setAttribute("aria-hidden",e.isVisible?"false":"true"),t.dataset.active=e.isActive?"true":"false",t.dataset.cfcIndex=String(e.index),t.setAttribute("role","group"),t.setAttribute("aria-roledescription","slide"),t.setAttribute("aria-setsize",String(this.cards.length)),t.setAttribute("aria-posinset",String(e.index+1));const i=`${this.instanceId}-slide-${e.index}`;t.id=i;const n=String(e.delta),s=String(e.abs),r=t.dataset.cfcDelta,c=t.dataset.cfcAbs;r!==n&&(t.style.setProperty("--cfc-delta",n),t.dataset.cfcDelta=n),c!==s&&(t.style.setProperty("--cfc-abs",s),t.dataset.cfcAbs=s)}applyLayoutAndA11y(t){if(this.cards.length){if(this.hasAppliedInitialLayout){const e=this.getVisibleSet(),i=new Set;this.lastVisibleSet.forEach(n=>{i.add(n)}),e.forEach(n=>{i.add(n)}),this.lastLayoutIndex!=null&&i.add(this.lastLayoutIndex),i.add(this.currentIndex),i.forEach(n=>{const s=this.cards[n];if(!s)return;const r=this.computeCardState(n);this.applyCardState(s,r)}),this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=e}else{for(let e=0;e<this.cards.length;e++){const i=this.cards[e];if(!i)continue;const n=this.computeCardState(e);this.applyCardState(i,n)}this.hasAppliedInitialLayout=!0,this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=this.getVisibleSet()}this.updateDotsVisualState(),t.announce&&p(this,"announce-changes",!0)&&this.announce(`Slide ${this.currentIndex+1} of ${this.cards.length}`),t.emitChange&&this.emitChange()}}updateDotsVisualState(){if(this.dots.length)for(let t=0;t<this.dots.length;t++){const e=this.dots[t],i=t===this.currentIndex;e.dataset.active=i?"true":"false"}}lockUntilTransitionEnd(){this.pendingAnimToken++;const t=this.pendingAnimToken;if(T()){this.unlockAnimation();return}const e=this.cards[this.currentIndex];if(!e){this.unlockAnimation();return}const i=getComputedStyle(e),n=k(i,"transform");if(n<=0){this.unlockAnimation();return}const s=b(i.getPropertyValue("--cfc-transition-ms").trim(),400),c=Math.max(n,s)+60;this.animFallbackTimerId!==null&&window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=window.setTimeout(()=>{this.pendingAnimToken===t&&this.unlockAnimation()},c)}unlockAnimation(){this.isAnimating=!1,this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null)}emitChange(){this.dispatchEvent(new CustomEvent("coverflow-carousel:change",{detail:{index:this.currentIndex,length:this.cards.length}}))}dispatchReady(){this.dispatchEvent(new CustomEvent("coverflow-carousel:ready",{detail:{index:this.currentIndex,length:this.cards.length}}))}announce(t){this.liveRegion.textContent="",queueMicrotask(()=>{this.liveRegion.textContent=t})}reflectIndexAttr(){this.reflectGuard=!0;try{this.setAttribute("index",String(this.currentIndex))}finally{this.reflectGuard=!1}}applyStyles(t){if(typeof t=="string"){if(x(this.shadow)){const e=E(t);if(e){this.shadow.adoptedStyleSheets=[e],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&x(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(f.defaultStylesheet&&x(this.shadow)){this.shadow.adoptedStyleSheets=[f.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=_}}function D(a={}){const{selector:t="coverflow-carousel",onReady:e,onChange:i,onScratchComplete:n,stylesheet:s}=a,r=Array.from(document.querySelectorAll(t));return(e||i||n)&&r.forEach(c=>{e&&c.addEventListener("coverflow-carousel:ready",o=>{e(c,o.detail)}),i&&c.addEventListener("coverflow-carousel:change",o=>{i(c,o.detail)}),n&&c.addEventListener("coverflow-carousel:scratch-complete",o=>{n(c,o.detail)})}),s&&r.forEach(c=>{const o=c;typeof s=="string"?o.adoptStyles(s):o.adoptStylesheet(s)}),r}function B(a="coverflow-carousel"){typeof window>"u"||!("customElements"in window)||customElements.get(a)||customElements.define(a,f)}l.CoverflowCarouselElement=f,l.coverflowCarouselCssText=I,l.initCoverflowCarousels=D,l.registerCoverflowCarouselElement=B,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
167
+ `,I=h;function b(a){return"adoptedStyleSheets"in a}function E(a){try{const t=new CSSStyleSheet;return t.replaceSync(a),t}catch{return null}}const C=E(h),_=h;function g(a){return a.split(",").map(t=>t.trim()).filter(Boolean)}function x(a,t){const e=a.trim();if(!e)return t;if(e.endsWith("ms")){const n=Number(e.slice(0,-2).trim());return Number.isFinite(n)?n:t}if(e.endsWith("s")){const n=Number(e.slice(0,-1).trim());return Number.isFinite(n)?n*1e3:t}const s=Number(e);return Number.isFinite(s)?s:t}function k(a,t){const e=g(a.transitionProperty),s=g(a.transitionDuration),n=g(a.transitionDelay);if(!e.length)return 0;const r=(y=>{const u=e.indexOf(y);if(u>=0)return u;const A=e.indexOf("all");return A>=0?A:-1})(t);if(r<0)return 0;const c=s[Math.min(r,s.length-1)]??"0s",o=n[Math.min(r,n.length-1)]??"0s",l=x(c,0),S=x(o,0);return Math.max(0,l+S)}function L(){return window.matchMedia?.("(prefers-reduced-motion: reduce)")?.matches??!1}function p(a,t,e){if(!a.hasAttribute(t))return e;const s=a.getAttribute(t);return s==null||s===""?!0:s!=="false"}function T(a,t,e){const s=a.getAttribute(t);if(s==null)return e;const n=Number(s.trim());return Number.isFinite(n)?Math.trunc(n):e}function w(a,t){const e=a.getAttribute(t);if(e==null)return null;const s=e.trim();return s||null}function m(a,t){return t<=0?0:(a%t+t)%t}function D(a,t,e){const s=t-a,n=e/2;return s>n?s-e:s<-n?s+e:s}const v=1;let N=0;class f extends HTMLElement{static defaultStylesheet=C;static observedAttributes=["start-index","index","show-dots","show-arrows","announce-changes"];shadow=this.attachShadow({mode:"open"});instanceId=`cfc-${++N}`;rootEl;trackEl;dotsEl;prevBtn;nextBtn;liveRegion;styleEl=null;cards=[];dots=[];currentIndex=0;isAnimating=!1;hasAppliedInitialLayout=!1;lastLayoutIndex=null;lastVisibleSet=new Set;pendingAnimToken=0;animFallbackTimerId=null;cleanup=[];lightDomObserver=null;refreshScheduled=!1;reflectGuard=!1;connectedCallback(){this.render(),this.readAttributes({isInit:!0}),this.refresh(),this.setupLightDomObserver()}disconnectedCallback(){this.destroy()}attributeChangedCallback(t,e,s){if(!this.isConnected||this.reflectGuard)return;this.readAttributes({isInit:!1});const n=w(this,"index");if(n!=null){const i=m(Number(n),this.cards.length);if(Number.isFinite(i)&&i!==this.currentIndex){this.goTo(i);return}}this.applyLayoutAndA11y({announce:!0,emitChange:!1})}next(){this.goTo(this.currentIndex+1)}prev(){this.goTo(this.currentIndex-1)}goTo(t){if(this.isAnimating)return;const e=m(t,this.cards.length);e!==this.currentIndex&&(this.isAnimating=!0,this.currentIndex=e,this.reflectIndexAttr(),this.applyLayoutAndA11y({announce:!0,emitChange:!0}),this.lockUntilTransitionEnd())}refresh(){this.rebuildCardsFromLightDom();const t=T(this,"start-index",0),e=w(this,"index"),s=e!=null?Number(e):t,n=Number.isFinite(s)?s:0;this.currentIndex=m(n,this.cards.length),this.reflectIndexAttr(),this.buildDots(),this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set,this.applyLayoutAndA11y({announce:!0,emitChange:!1}),this.dispatchReady()}destroy(){this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null),this.cleanup.forEach(t=>{t()}),this.cleanup=[],this.isAnimating=!1}adoptStylesheet(t){this.applyStyles(t)}adoptStyles(t){this.applyStyles(t)}readAttributes(t){this.rootEl||this.render();const e=p(this,"show-arrows",!1),s=p(this,"show-dots",!1);this.prevBtn.style.display=e?"":"none",this.nextBtn.style.display=e?"":"none",this.dotsEl.style.display=s?"":"none";const n=this.getAttribute("aria-label");n&&this.rootEl.setAttribute("aria-label",n),t.isInit&&this.setAttribute("aria-roledescription","carousel")}render(){this.applyStyles(null),this.rootEl=document.createElement("div"),this.rootEl.className="cfc",this.trackEl=document.createElement("div"),this.trackEl.className="cfc__track",this.prevBtn=document.createElement("button"),this.prevBtn.type="button",this.prevBtn.className="cfc__arrow cfc__arrow--left",this.prevBtn.setAttribute("aria-label","Previous"),this.prevBtn.textContent="‹",this.nextBtn=document.createElement("button"),this.nextBtn.type="button",this.nextBtn.className="cfc__arrow cfc__arrow--right",this.nextBtn.setAttribute("aria-label","Next"),this.nextBtn.textContent="›",this.dotsEl=document.createElement("div"),this.dotsEl.className="cfc__dots",this.liveRegion=document.createElement("div"),this.liveRegion.className="cfc__sr",this.liveRegion.setAttribute("aria-live","polite"),this.liveRegion.setAttribute("aria-atomic","true"),this.rootEl.append(this.trackEl,this.prevBtn,this.nextBtn,this.dotsEl,this.liveRegion),this.shadow.innerHTML="",this.styleEl&&this.shadow.append(this.styleEl),this.shadow.append(this.rootEl),this.bindEvents()}bindEvents(){this.cleanup.forEach(i=>{i()}),this.cleanup=[];const t=()=>this.prev(),e=()=>this.next();this.prevBtn.addEventListener("click",t),this.nextBtn.addEventListener("click",e),this.cleanup.push(()=>this.prevBtn.removeEventListener("click",t)),this.cleanup.push(()=>this.nextBtn.removeEventListener("click",e));const s=i=>{if(!this.isAnimating||i.propertyName!=="transform"||!(i.target instanceof HTMLElement))return;const r=this.cards[this.currentIndex];r&&i.target===r&&this.unlockAnimation()};this.rootEl.addEventListener("transitionend",s),this.cleanup.push(()=>this.rootEl.removeEventListener("transitionend",s));const n=i=>{const r=i.target;if(!(r instanceof HTMLElement)||r.tagName!=="SCRATCH-REVEAL")return;const c=i.composedPath?.();if(!c?.length)return;const o=c.find(u=>u instanceof HTMLElement&&u.classList.contains("cfc__card")&&u.dataset.cfcIndex!=null);if(!o)return;const l=Number(o.dataset.cfcIndex);if(!Number.isFinite(l))return;const y=i.detail?.percent??100;this.dispatchEvent(new CustomEvent("coverflow-carousel:scratch-complete",{detail:{index:l,length:this.cards.length,percent:y},bubbles:!0,composed:!0}))};this.rootEl.addEventListener("complete",n),this.cleanup.push(()=>this.rootEl.removeEventListener("complete",n))}setupLightDomObserver(){this.lightDomObserver||(this.lightDomObserver=new MutationObserver(t=>{t.some(s=>!(s.type==="attributes"&&s.attributeName==="slot"))&&this.scheduleRefreshFromMutations()}),this.lightDomObserver.observe(this,{childList:!0,attributes:!0,subtree:!0}),this.cleanup.push(()=>{this.lightDomObserver?.disconnect(),this.lightDomObserver=null}))}scheduleRefreshFromMutations(){this.refreshScheduled||(this.refreshScheduled=!0,queueMicrotask(()=>{this.refreshScheduled=!1,this.isConnected&&this.refresh()}))}rebuildCardsFromLightDom(){const t=Array.from(this.trackEl.children).filter(n=>n instanceof HTMLElement&&n.classList.contains("cfc__card")),e=[],s=Array.from(this.children).filter(n=>n instanceof HTMLElement);for(let n=0;n<s.length;n++){const i=s[n],r=t[n]??document.createElement("div"),c=`${this.instanceId}-slot-${n}`;t[n]||(r.className="cfc__card",this.trackEl.append(r)),i.getAttribute("slot")!==c&&i.setAttribute("slot",c);const o=r.querySelector("slot");let l;o instanceof HTMLSlotElement?l=o:(l=document.createElement("slot"),r.replaceChildren(l)),l.name!==c&&(l.name=c),e.push(r)}for(let n=s.length;n<t.length;n++)t[n]?.remove();this.cards=e,this.hasAppliedInitialLayout=!1,this.lastLayoutIndex=null,this.lastVisibleSet=new Set}getVisibleSet(){const t=this.cards.length,e=new Set;if(t<=0)return e;const s=Math.floor(t/2);if(v>=s){for(let n=0;n<t;n++)e.add(n);return e}for(let n=-v;n<=v;n++)e.add(m(this.currentIndex+n,t));return e}buildDots(){const t=Array.from(this.dotsEl.children).filter(i=>i instanceof HTMLElement&&i.classList.contains("cfc__dot"));if(!p(this,"show-dots",!1)){t.forEach(i=>{i.remove()}),this.dots=[];return}const s=[],n=this.cards.length;for(let i=0;i<n;i++){const r=t[i]??document.createElement("span");t[i]||this.dotsEl.append(r),r.className="cfc__dot",r.setAttribute("aria-hidden","true"),s.push(r)}for(let i=n;i<t.length;i++)t[i]?.remove();this.dots=s,this.updateDotsVisualState()}computeCardState(t){const e=this.cards.length,s=D(this.currentIndex,t,e),n=Math.abs(s);return{index:t,delta:s,abs:n,isVisible:n<=v,isActive:t===this.currentIndex}}applyCardState(t,e){t.setAttribute("aria-hidden",e.isVisible?"false":"true"),t.dataset.active=e.isActive?"true":"false",t.dataset.cfcIndex=String(e.index),t.setAttribute("role","group"),t.setAttribute("aria-roledescription","slide"),t.setAttribute("aria-setsize",String(this.cards.length)),t.setAttribute("aria-posinset",String(e.index+1));const s=`${this.instanceId}-slide-${e.index}`;t.id=s;const n=String(e.delta),i=String(e.abs),r=t.dataset.cfcDelta,c=t.dataset.cfcAbs;r!==n&&(t.style.setProperty("--cfc-delta",n),t.dataset.cfcDelta=n),c!==i&&(t.style.setProperty("--cfc-abs",i),t.dataset.cfcAbs=i)}applyLayoutAndA11y(t){if(this.cards.length){if(this.hasAppliedInitialLayout){const e=this.getVisibleSet(),s=new Set;this.lastVisibleSet.forEach(n=>{s.add(n)}),e.forEach(n=>{s.add(n)}),this.lastLayoutIndex!=null&&s.add(this.lastLayoutIndex),s.add(this.currentIndex),s.forEach(n=>{const i=this.cards[n];if(!i)return;const r=this.computeCardState(n);this.applyCardState(i,r)}),this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=e}else{for(let e=0;e<this.cards.length;e++){const s=this.cards[e];if(!s)continue;const n=this.computeCardState(e);this.applyCardState(s,n)}this.hasAppliedInitialLayout=!0,this.lastLayoutIndex=this.currentIndex,this.lastVisibleSet=this.getVisibleSet()}this.updateDotsVisualState(),t.announce&&p(this,"announce-changes",!0)&&this.announce(`Slide ${this.currentIndex+1} of ${this.cards.length}`),t.emitChange&&this.emitChange()}}updateDotsVisualState(){if(this.dots.length)for(let t=0;t<this.dots.length;t++){const e=this.dots[t],s=t===this.currentIndex;e.dataset.active=s?"true":"false"}}lockUntilTransitionEnd(){this.pendingAnimToken++;const t=this.pendingAnimToken;if(L()){this.unlockAnimation();return}const e=this.cards[this.currentIndex];if(!e){this.unlockAnimation();return}const s=getComputedStyle(e),n=k(s,"transform");if(n<=0){this.unlockAnimation();return}const i=x(s.getPropertyValue("--cfc-transition-ms").trim(),400),c=Math.max(n,i)+60;this.animFallbackTimerId!==null&&window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=window.setTimeout(()=>{this.pendingAnimToken===t&&this.unlockAnimation()},c)}unlockAnimation(){this.isAnimating=!1,this.animFallbackTimerId!==null&&(window.clearTimeout(this.animFallbackTimerId),this.animFallbackTimerId=null)}emitChange(){this.dispatchEvent(new CustomEvent("coverflow-carousel:change",{detail:{index:this.currentIndex,length:this.cards.length}}))}dispatchReady(){this.dispatchEvent(new CustomEvent("coverflow-carousel:ready",{detail:{index:this.currentIndex,length:this.cards.length}}))}announce(t){this.liveRegion.textContent="",queueMicrotask(()=>{this.liveRegion.textContent=t})}reflectIndexAttr(){this.reflectGuard=!0;try{this.setAttribute("index",String(this.currentIndex))}finally{this.reflectGuard=!1}}applyStyles(t){if(typeof t=="string"){if(b(this.shadow)){const e=E(t);if(e){this.shadow.adoptedStyleSheets=[e],this.styleEl=null;return}}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=t;return}if(t&&b(this.shadow)){this.shadow.adoptedStyleSheets=[t],this.styleEl=null;return}if(f.defaultStylesheet&&b(this.shadow)){this.shadow.adoptedStyleSheets=[f.defaultStylesheet],this.styleEl=null;return}this.styleEl||(this.styleEl=document.createElement("style")),this.styleEl.textContent=_}}function F(a={}){const{selector:t="coverflow-carousel",onReady:e,onChange:s,onScratchComplete:n,stylesheet:i}=a,r=Array.from(document.querySelectorAll(t));return(e||s||n)&&r.forEach(c=>{e&&c.addEventListener("coverflow-carousel:ready",o=>{e(c,o.detail)}),s&&c.addEventListener("coverflow-carousel:change",o=>{s(c,o.detail)}),n&&c.addEventListener("coverflow-carousel:scratch-complete",o=>{n(c,o.detail)})}),i&&r.forEach(c=>{const o=c;typeof i=="string"?o.adoptStyles(i):o.adoptStylesheet(i)}),r}function M(a="coverflow-carousel"){typeof window>"u"||!("customElements"in window)||customElements.get(a)||customElements.define(a,f)}d.CoverflowCarouselElement=f,d.coverflowCarouselCssText=I,d.initCoverflowCarousels=F,d.registerCoverflowCarouselElement=M,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "coverflow-carousel",
3
- "version": "0.2.0-dev.0",
3
+ "version": "0.2.0-dev.1",
4
4
  "description": "Tiny coverflow carousel Web Component. Exposes API via attributes + events (explicit element registration).",
5
5
  "author": "ux-ui.pro",
6
6
  "license": "MIT",