number-flow 0.3.9 → 0.4.0

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.
@@ -17,7 +17,7 @@ type KeyedPart = {
17
17
  key: NumberPartKey;
18
18
  };
19
19
  export type KeyedDigitPart = DigitPart & KeyedPart & {
20
- place: number;
20
+ pos: number;
21
21
  };
22
22
  export type KeyedSymbolPart = SymbolPart & KeyedPart;
23
23
  export type KeyedNumberPart = KeyedDigitPart | KeyedSymbolPart;
package/dist/index.d.ts CHANGED
@@ -5,39 +5,54 @@ export { prefersReducedMotion } from './styles';
5
5
  export { renderInnerHTML } from './ssr';
6
6
  export * from './formatter';
7
7
  export declare const canAnimate: boolean;
8
- type RawTrend = boolean | 'increasing' | 'decreasing';
9
- export { type RawTrend as Trend };
10
- declare enum Trend {
11
- UP = 1,
12
- DOWN = -1,
13
- NONE = 0
14
- }
8
+ export type Trend = number | ((oldValue: number, value: number) => number);
9
+ export type DigitOptions = {
10
+ max?: number;
11
+ };
12
+ export type Digits = Record<number, DigitOptions>;
15
13
  export interface Props {
16
14
  transformTiming: EffectTiming;
17
15
  spinTiming: EffectTiming | undefined;
18
16
  opacityTiming: EffectTiming;
19
17
  animated: boolean;
20
18
  respectMotionPreference: boolean;
21
- trend: RawTrend;
19
+ trend: Trend;
22
20
  continuous: boolean;
21
+ digits: Digits | undefined;
23
22
  }
24
23
  export declare class NumberFlowLite extends ServerSafeHTMLElement implements Props {
25
- #private;
24
+ /**
25
+ * Use `private _private` properties instead of `#private` to avoid # polyfill and
26
+ * reduce bundle size. Also, use `readonly` properties instead of getters to save on bundle
27
+ * size, even though you have to do gross stuff like `(this as Mutable<...>)` until TS
28
+ * supports e.g. https://github.com/microsoft/TypeScript/issues/37487
29
+ */
26
30
  static defaultProps: Props;
27
31
  transformTiming: EffectTiming;
28
32
  spinTiming: EffectTiming | undefined;
29
33
  opacityTiming: EffectTiming;
30
34
  respectMotionPreference: boolean;
31
- trend: RawTrend;
35
+ trend: Trend;
32
36
  continuous: boolean;
37
+ digits: Digits | undefined;
38
+ private _animated;
33
39
  get animated(): boolean;
34
40
  set animated(val: boolean);
41
+ private _created;
35
42
  get created(): boolean;
36
- get computedTrend(): Trend | undefined;
37
- get startingPlace(): number | null | undefined;
43
+ private _pre?;
44
+ private _num?;
45
+ private _post?;
46
+ private _computedTrend?;
47
+ get computedTrend(): number | undefined;
48
+ private _startingPos?;
49
+ get startingPos(): number | null | undefined;
50
+ private _computedAnimated;
38
51
  get computedAnimated(): boolean;
52
+ private _data?;
39
53
  manual: boolean;
40
54
  set data(data: Data | undefined);
41
55
  willUpdate(): void;
56
+ private _abortAnimationsFinish?;
42
57
  didUpdate(): void;
43
58
  }
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O=require("esm-env");function n(s,t,e,i){if(e==="a"&&!i)throw new TypeError("Private accessor was defined without a getter");if(typeof t=="function"?s!==t||!i:!t.has(s))throw new TypeError("Cannot read private member from an object whose class did not declare it");return e==="m"?i:e==="a"?i.call(s):i?i.value:t.get(s)}function c(s,t,e,i,a){if(i==="m")throw new TypeError("Private method is not writable");if(i==="a"&&!a)throw new TypeError("Private accessor was defined without a setter");if(typeof t=="function"?s!==t||!a:!t.has(s))throw new TypeError("Cannot write private member to an object whose class did not declare it");return i==="a"?a.call(s,e):a?a.value=e:t.set(s,e),e}const S=(s,t,e)=>{const i=document.createElement(s),[a,l]=Array.isArray(t)?[void 0,t]:[t,e];return a&&Object.assign(i,a),l==null||l.forEach(r=>i.appendChild(r)),i},yt=(s,t)=>{var e;return t==="left"?s.offsetLeft:(((e=s.offsetParent instanceof HTMLElement?s.offsetParent:null)==null?void 0:e.offsetWidth)??0)-s.offsetWidth-s.offsetLeft},vt=s=>s.offsetWidth>0&&s.offsetHeight>0,_t=(s,t)=>{if(!(!O.BROWSER||customElements.get(s)===t))return customElements.define(s,t)};function bt(s,t,{reverse:e=!1}={}){const i=s.length;for(let a=e?i-1:0;e?a>=0:a<i;e?a--:a++)t(s[a],a)}function St(s,t,e,i){const a=t.formatToParts(s);e&&a.unshift({type:"prefix",value:e}),i&&a.push({type:"suffix",value:i});const l=[],r=[],o=[],m=[],u={},p=f=>`${f}:${u[f]=(u[f]??-1)+1}`;let N="",d=!1,w=!1;for(const f of a){N+=f.value;const h=f.type==="minusSign"||f.type==="plusSign"?"sign":f.type;h==="integer"?(d=!0,r.push(...f.value.split("").map(et=>({type:h,value:parseInt(et)})))):h==="group"?r.push({type:h,value:f.value}):h==="decimal"?(w=!0,o.push({type:h,value:f.value,key:p(h)})):h==="fraction"?o.push(...f.value.split("").map(et=>({type:h,value:parseInt(et),key:p(h),place:-1-u[h]}))):(d||w?m:l).push({type:h,value:f.value,key:p(h)})}const lt=[];for(let f=r.length-1;f>=0;f--){const h=r[f];lt.unshift(h.type==="integer"?{...h,key:p(h.type),place:u[h.type]}:{...h,key:p(h.type)})}return{pre:l,integer:lt,fraction:o,post:m,valueAsString:N,value:typeof s=="string"?parseFloat(s):s}}const $t=String.raw,dt=String.raw,kt=O.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("animation-timing-function","linear(1,2)"),Et=O.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("line-height","mod(1,1)"),H=O.BROWSER&&typeof matchMedia<"u"?matchMedia("(prefers-reduced-motion: reduce)"):null,Q="--_number-flow-d-opacity",at="--_number-flow-d-width",tt="--_number-flow-dx",ot="--_number-flow-d",Mt=(()=>{try{return CSS.registerProperty({name:Q,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:tt,syntax:"<length>",inherits:!0,initialValue:"0px"}),CSS.registerProperty({name:at,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:ot,syntax:"<number>",inherits:!0,initialValue:"0"}),!0}catch{return!1}})(),rt="var(--number-flow-char-height, 1em)",g="var(--number-flow-mask-height, 0.25em)",X=`calc(${g} / 2)`,it="var(--number-flow-mask-width, 0.5em)",$=`calc(${it} / var(--scale-x))`,D="#000 0, transparent 71%",ct=dt`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${rt} !important;isolation:isolate;}.number,.number__inner{display:inline-block;transform-origin:left top;}:host([data-will-change]) :is(.number,.number__inner,.section,.digit,.digit__num,.symbol){will-change:transform;}.number{--scale-x:calc(1 + var(${at}) / var(--width));transform:translateX(var(${tt})) scaleX(var(--scale-x));margin:0 calc(-1 * ${it});position:relative;z-index:-1;-webkit-mask-image:linear-gradient( to right,transparent 0,#000 ${$},#000 calc(100% - ${$}),transparent ),linear-gradient( to bottom,transparent 0,#000 ${g},#000 calc(100% - ${g}),transparent 100% ),radial-gradient(at bottom right,${D}),radial-gradient(at bottom left,${D}),radial-gradient(at top left,${D}),radial-gradient(at top right,${D});-webkit-mask-size:100% calc(100% - ${g} * 2),calc(100% - ${$} * 2) 100%,${$} ${g},${$} ${g},${$} ${g},${$} ${g};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat;}.number__inner{padding:${X} ${it};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${tt})));}.section{display:inline-block;position:relative;isolation:isolate;}.section::after{content:'\200b';display:inline-block;padding:${X} 0;}.section--justify-left{transform-origin:center left;}.section--justify-right{transform-origin:center right;}.section__exiting,.symbol__exiting{margin:0 !important;position:absolute !important;z-index:-1;}.digit{display:inline-block;position:relative;--c:var(--current) + var(${ot});}.digit__num{display:inline-block;padding:${X} 0;--offset-raw:mod(10 + var(--n) - mod(var(--c),10),10);--offset:calc(var(--offset-raw) - 10 * round(down,var(--offset-raw) / 5,1));--y:clamp(-100%,var(--offset) * 100%,100%);transform:translateY(var(--y));}.digit__num:not(.is-current){position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y));}.digit:not(.is-spinning) .digit__num:not(.is-current){display:none;}.symbol{display:inline-block;position:relative;isolation:isolate;}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre;}.section--justify-left .symbol__exiting{left:0;}.section--justify-right .symbol__exiting{right:0;}.animate-presence{opacity:calc(1 + var(${Q}));}`,xt=O.BROWSER?HTMLElement:class{},Wt=dt`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${rt} !important;}span{display:inline-block;}:host([data-will-change]) span{will-change:transform;}.number,.digit{padding:${X} 0;}.symbol{white-space:pre;}`,Nt=s=>`<span class="${s.type==="integer"||s.type==="fraction"?"digit":"symbol"}" part="${s.type==="integer"||s.type==="fraction"?`digit ${s.type}-digit`:s.type}">${s.value}</span>`,B=(s,t)=>`<span part="${t}">${s.reduce((e,i)=>e+Nt(i),"")}</span>`,Pt=s=>$t`<template shadowroot="open" shadowrootmode="open"
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const v=require("esm-env"),g=(n,t,e)=>{const i=document.createElement(n),[s,a]=Array.isArray(t)?[void 0,t]:[t,e];return s&&Object.assign(i,s),a==null||a.forEach(r=>i.appendChild(r)),i},X=(n,t)=>{var e;return t==="left"?n.offsetLeft:(((e=n.offsetParent instanceof HTMLElement?n.offsetParent:null)==null?void 0:e.offsetWidth)??0)-n.offsetWidth-n.offsetLeft},D=n=>n.offsetWidth>0&&n.offsetHeight>0,I=(n,t)=>{if(!(!v.BROWSER||customElements.get(n)===t))return customElements.define(n,t)};function z(n,t,{reverse:e=!1}={}){const i=n.length;for(let s=e?i-1:0;e?s>=0:s<i;e?s--:s++)t(n[s],s)}function Y(n,t,e,i){const s=t.formatToParts(n);e&&s.unshift({type:"prefix",value:e}),i&&s.push({type:"suffix",value:i});const a=[],r=[],o=[],d=[],u={},p=c=>`${c}:${u[c]=(u[c]??-1)+1}`;let y="",h=!1,f=!1;for(const c of s){y+=c.value;const l=c.type==="minusSign"||c.type==="plusSign"?"sign":c.type;l==="integer"?(h=!0,r.push(...c.value.split("").map(E=>({type:l,value:parseInt(E)})))):l==="group"?r.push({type:l,value:c.value}):l==="decimal"?(f=!0,o.push({type:l,value:c.value,key:p(l)})):l==="fraction"?o.push(...c.value.split("").map(E=>({type:l,value:parseInt(E),key:p(l),pos:-1-u[l]}))):(h||f?d:a).push({type:l,value:c.value,key:p(l)})}const j=[];for(let c=r.length-1;c>=0;c--){const l=r[c];j.unshift(l.type==="integer"?{...l,key:p(l.type),pos:u[l.type]}:{...l,key:p(l.type)})}return{pre:a,integer:j,fraction:o,post:d,valueAsString:y,value:typeof n=="string"?parseFloat(n):n}}const q=String.raw,W=String.raw,G=v.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("animation-timing-function","linear(1,2)"),K=v.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("line-height","mod(1,1)"),$=v.BROWSER&&typeof matchMedia<"u"?matchMedia("(prefers-reduced-motion: reduce)"):null,R="--_number-flow-d-opacity",T="--_number-flow-d-width",C="--_number-flow-dx",A="--_number-flow-d",Z=(()=>{try{return CSS.registerProperty({name:R,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:C,syntax:"<length>",inherits:!0,initialValue:"0px"}),CSS.registerProperty({name:T,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:A,syntax:"<number>",inherits:!0,initialValue:"0"}),!0}catch{return!1}})(),U="var(--number-flow-char-height, 1em)",m="var(--number-flow-mask-height, 0.25em)",x=`calc(${m} / 2)`,P="var(--number-flow-mask-width, 0.5em)",_=`calc(${P} / var(--scale-x))`,w="#000 0, transparent 71%",L=W`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${U} !important;isolation:isolate}.number,.number__inner{display:inline-block;transform-origin:left top}:host([data-will-change]) :is(.number,.number__inner,.section,.digit,.digit__num,.symbol){will-change:transform}.number{--scale-x:calc(1 + var(${T}) / var(--width));transform:translateX(var(${C})) scaleX(var(--scale-x));margin:0 calc(-1 * ${P});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${_},#000 calc(100% - ${_}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${m},#000 calc(100% - ${m}),transparent 100% ),radial-gradient(at bottom right,${w}),radial-gradient(at bottom left,${w}),radial-gradient(at top left,${w}),radial-gradient(at top right,${w});-webkit-mask-size:100% calc(100% - ${m} * 2),calc(100% - ${_} * 2) 100%,${_} ${m},${_} ${m},${_} ${m},${_} ${m};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${x} ${P};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${C})))}:host > :not(.number){z-index:5}.section,.symbol{display:inline-block;position:relative;isolation:isolate}.section::after{content:'\200b';display:inline-block;padding:${x} 0}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section__exiting,.symbol__exiting{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${A})}.digit__num{display:inline-block;padding:${x} 0;--offset-raw:mod(var(--length) + var(--n) - mod(var(--c),var(--length)),var(--length));--offset:calc(var(--offset-raw) - var(--length) * round(down,var(--offset-raw) / (var(--length)/2),1));--y:clamp(-100%,var(--offset) * 100%,100%);transform:translateY(var(--y))}.digit__num:not(.is-current){position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num:not(.is-current){display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol__exiting{left:0}.section--justify-right .symbol__exiting{right:0}.animate-presence{opacity:calc(1 + var(${R}))}`,J=v.BROWSER?HTMLElement:class{},Q=W`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${U} !important}span{display:inline-block}:host([data-will-change]) span{will-change:transform}.number,.digit{padding:${x} 0}.symbol{white-space:pre}`,tt=n=>`<span class="${n.type==="integer"||n.type==="fraction"?"digit":"symbol"}" part="${n.type==="integer"||n.type==="fraction"?`digit ${n.type}-digit`:n.type}">${n.value}</span>`,b=(n,t)=>`<span part="${t}">${n.reduce((e,i)=>e+tt(i),"")}</span>`,et=n=>q`<template shadowroot="open" shadowrootmode="open"
2
2
  ><style>
3
- ${Wt}</style
4
- >${B(s.pre,"left")}<span part="number" class="number"
5
- >${B(s.integer,"integer")}${B(s.fraction,"fraction")}</span
6
- >${B(s.post,"right")}</template
3
+ ${Q}</style
4
+ >${b(n.pre,"left")}<span part="number" class="number"
5
+ >${b(n.integer,"integer")}${b(n.fraction,"fraction")}</span
6
+ >${b(n.post,"right")}</template
7
7
  ><span
8
- style="font-kerning: none; display: inline-block; line-height: ${rt} !important; padding: ${g} 0;"
9
- >${s.valueAsString}</span
10
- >`,Ct=(s,t)=>s!=null&&t==null?s:s==null&&t!=null?t:s!=null&&t!=null?Math.max(s,t):null;var P,R,k,E,M,y,L,T,U,C,I,x,W,z,G,Y,j,q,A,F,v,K,st,_,Z,J;const pt=Et&&kt&&Mt;var b;(function(s){s[s.UP=1]="UP",s[s.DOWN=-1]="DOWN",s[s.NONE=0]="NONE"})(b||(b={}));let V;class ut extends xt{constructor(){super(...arguments),this.transformTiming=this.constructor.defaultProps.transformTiming,this.spinTiming=this.constructor.defaultProps.spinTiming,this.opacityTiming=this.constructor.defaultProps.opacityTiming,this.respectMotionPreference=this.constructor.defaultProps.respectMotionPreference,this.trend=this.constructor.defaultProps.trend,this.continuous=this.constructor.defaultProps.continuous,P.set(this,this.constructor.defaultProps.animated),R.set(this,!1),k.set(this,void 0),E.set(this,void 0),M.set(this,void 0),y.set(this,void 0),L.set(this,void 0),T.set(this,n(this,P,"f")),U.set(this,void 0),this.manual=!1,C.set(this,void 0)}get animated(){return n(this,P,"f")}set animated(t){var e;this.animated!==t&&(c(this,P,t,"f"),(e=this.shadowRoot)==null||e.getAnimations().forEach(i=>i.finish()))}get created(){return n(this,R,"f")}get computedTrend(){return n(this,y,"f")}get startingPlace(){return n(this,L,"f")}get computedAnimated(){return n(this,T,"f")}set data(t){if(t==null)return;const{pre:e,integer:i,fraction:a,post:l,value:r}=t;if(n(this,R,"f")){const o=n(this,U,"f");if(c(this,U,t,"f"),this.trend===!0?c(this,y,Math.sign(r-o.value),"f"):this.trend==="increasing"?c(this,y,b.UP,"f"):this.trend==="decreasing"?c(this,y,b.DOWN,"f"):c(this,y,b.NONE,"f"),c(this,L,void 0,"f"),n(this,y,"f")!==b.NONE&&this.continuous){const m=o.integer.concat(o.fraction).filter(d=>d.type==="integer"||d.type==="fraction"),u=t.integer.concat(t.fraction).filter(d=>d.type==="integer"||d.type==="fraction"),p=m.find(d=>!u.find(w=>w.place===d.place&&w.value===d.value)),N=u.find(d=>!m.find(w=>d.place===w.place&&d.value===w.value));c(this,L,Ct(p==null?void 0:p.place,N==null?void 0:N.place),"f")}c(this,T,pt&&n(this,P,"f")&&(!this.respectMotionPreference||!(H!=null&&H.matches))&&vt(this),"f"),this.manual||this.willUpdate(),n(this,k,"f").update(e),n(this,E,"f").update({integer:i,fraction:a}),n(this,M,"f").update(l),this.manual||this.didUpdate()}else{if(c(this,U,t,"f"),this.attachShadow({mode:"open"}),typeof CSSStyleSheet<"u"&&this.shadowRoot.adoptedStyleSheets)V||(V=new CSSStyleSheet,V.replaceSync(ct)),this.shadowRoot.adoptedStyleSheets=[V];else{const o=document.createElement("style");o.textContent=ct,this.shadowRoot.appendChild(o)}c(this,k,new ft(this,e,{justify:"right",part:"left"}),"f"),this.shadowRoot.appendChild(n(this,k,"f").el),c(this,E,new Rt(this,i,a),"f"),this.shadowRoot.appendChild(n(this,E,"f").el),c(this,M,new ft(this,l,{justify:"left",part:"right"}),"f"),this.shadowRoot.appendChild(n(this,M,"f").el)}c(this,R,!0,"f")}willUpdate(){n(this,k,"f").willUpdate(),n(this,E,"f").willUpdate(),n(this,M,"f").willUpdate()}didUpdate(){if(!n(this,T,"f"))return;n(this,C,"f")?n(this,C,"f").abort():this.dispatchEvent(new Event("animationsstart")),n(this,k,"f").didUpdate(),n(this,E,"f").didUpdate(),n(this,M,"f").didUpdate();const t=new AbortController;Promise.all(this.shadowRoot.getAnimations().map(e=>e.finished)).then(()=>{t.signal.aborted||(this.dispatchEvent(new Event("animationsfinish")),c(this,C,void 0,"f"))}),c(this,C,t,"f")}}P=new WeakMap,R=new WeakMap,k=new WeakMap,E=new WeakMap,M=new WeakMap,y=new WeakMap,L=new WeakMap,T=new WeakMap,U=new WeakMap,C=new WeakMap;ut.defaultProps={transformTiming:{duration:900,easing:"linear(0,.005,.019,.039,.066,.096,.129,.165,.202,.24,.278,.316,.354,.39,.426,.461,.494,.526,.557,.586,.614,.64,.665,.689,.711,.731,.751,.769,.786,.802,.817,.831,.844,.856,.867,.877,.887,.896,.904,.912,.919,.925,.931,.937,.942,.947,.951,.955,.959,.962,.965,.968,.971,.973,.976,.978,.98,.981,.983,.984,.986,.987,.988,.989,.99,.991,.992,.992,.993,.994,.994,.995,.995,.996,.996,.9963,.9967,.9969,.9972,.9975,.9977,.9979,.9981,.9982,.9984,.9985,.9987,.9988,.9989,1)"},spinTiming:void 0,opacityTiming:{duration:450,easing:"ease-out"},animated:!0,trend:!0,continuous:!1,respectMotionPreference:!0};class Rt{constructor(t,e,i,{className:a,...l}={}){this.flow=t,I.set(this,void 0),x.set(this,void 0),W.set(this,void 0),z.set(this,void 0),G.set(this,void 0),c(this,x,new ht(t,e,{justify:"right",part:"integer"}),"f"),c(this,W,new ht(t,i,{justify:"left",part:"fraction"}),"f"),c(this,I,S("span",{className:"number__inner"},[n(this,x,"f").el,n(this,W,"f").el]),"f"),this.el=S("span",{...l,part:"number",className:`number ${a??""}`},[n(this,I,"f")])}willUpdate(){c(this,z,this.el.offsetWidth,"f"),c(this,G,this.el.getBoundingClientRect().left,"f"),n(this,x,"f").willUpdate(),n(this,W,"f").willUpdate()}update({integer:t,fraction:e}){n(this,x,"f").update(t),n(this,W,"f").update(e)}didUpdate(){const t=this.el.getBoundingClientRect();n(this,x,"f").didUpdate(),n(this,W,"f").didUpdate();const e=n(this,G,"f")-t.left,i=this.el.offsetWidth,a=n(this,z,"f")-i;this.el.style.setProperty("--width",String(i)),this.el.animate({[tt]:[`${e}px`,"0px"],[at]:[a,0]},{...this.flow.transformTiming,composite:"accumulate"})}}I=new WeakMap,x=new WeakMap,W=new WeakMap,z=new WeakMap,G=new WeakMap;class mt{constructor(t,e,{justify:i,className:a,...l},r){this.flow=t,this.children=new Map,this.onCharRemove=m=>()=>{this.children.delete(m)},Y.set(this,void 0),this.justify=i;const o=e.map(m=>this.addChar(m).el);this.el=S("span",{...l,className:`section section--justify-${i} ${a??""}`},r?r(o):o)}addChar(t,{startDigitsAtZero:e=!1,...i}={}){const a=t.type==="integer"||t.type==="fraction"?new wt(this,t.type,e?0:t.value,t.place,{...i,onRemove:this.onCharRemove(t.key)}):new Lt(this,t.type,t.value,{...i,onRemove:this.onCharRemove(t.key)});return this.children.set(t.key,a),a}unpop(t){t.el.classList.remove("section__exiting"),t.el.style.top="",t.el.style[this.justify]=""}pop(t){t.forEach(e=>{e.el.style.top=`${e.el.offsetTop}px`,e.el.style[this.justify]=`${yt(e.el,this.justify)}px`}),t.forEach(e=>{e.el.classList.add("section__exiting"),e.present=!1})}addNewAndUpdateExisting(t){const e=new Map,i=new Map,a=this.justify==="left",l=a?"prepend":"append";if(bt(t,r=>{let o;this.children.has(r.key)?(o=this.children.get(r.key),i.set(r,o),this.unpop(o),o.present=!0):(o=this.addChar(r,{startDigitsAtZero:!0,animateIn:!0}),e.set(r,o)),this.el[l](o.el)},{reverse:a}),this.flow.computedAnimated){const r=this.el.getBoundingClientRect();e.forEach(o=>{o.willUpdate(r)})}e.forEach((r,o)=>{r.update(o.value)}),i.forEach((r,o)=>{r.update(o.value)})}willUpdate(){const t=this.el.getBoundingClientRect();c(this,Y,t[this.justify],"f"),this.children.forEach(e=>e.willUpdate(t))}didUpdate(){const t=this.el.getBoundingClientRect();this.children.forEach(a=>a.didUpdate(t));const e=t[this.justify],i=n(this,Y,"f")-e;i&&this.children.size&&this.el.animate({transform:[`translateX(${i}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"})}}Y=new WeakMap;class ht extends mt{update(t){const e=new Map;this.children.forEach((i,a)=>{t.find(l=>l.key===a)||e.set(a,i),this.unpop(i)}),this.addNewAndUpdateExisting(t),e.forEach(i=>{i instanceof wt&&i.update(0)}),this.pop(e)}}class ft extends mt{update(t){const e=new Map;this.children.forEach((i,a)=>{t.find(l=>l.key===a)||e.set(a,i)}),this.pop(e),this.addNewAndUpdateExisting(t)}}class nt{constructor(t,e,{onRemove:i,animateIn:a=!1}={}){this.flow=t,this.el=e,j.set(this,!0),q.set(this,void 0),A.set(this,()=>{var l;this.el.remove(),(l=n(this,q,"f"))==null||l.call(this)}),this.el.classList.add("animate-presence"),this.flow.computedAnimated&&a&&this.el.animate({[Q]:[-.9999,0]},{...this.flow.opacityTiming,composite:"accumulate"}),c(this,q,i,"f")}get present(){return n(this,j,"f")}set present(t){if(n(this,j,"f")!==t){if(c(this,j,t,"f"),!this.flow.computedAnimated){t||n(this,A,"f").call(this);return}this.el.style.setProperty("--_number-flow-d-opacity",t?"0":"-.999"),this.el.animate({[Q]:t?[-.9999,0]:[.999,0]},{...this.flow.opacityTiming,composite:"accumulate"}),t?this.flow.removeEventListener("animationsfinish",n(this,A,"f")):this.flow.addEventListener("animationsfinish",n(this,A,"f"),{once:!0})}}}j=new WeakMap,q=new WeakMap,A=new WeakMap;class gt extends nt{constructor(t,e,i,a){super(t.flow,i,a),this.section=t,this.value=e,this.el=i}}class wt extends gt{constructor(t,e,i,a,l){const r=Array.from({length:10}).map((m,u)=>{const p=S("span",{className:`digit__num${u===i?" is-current":""}`},[document.createTextNode(String(u))]);return p.style.setProperty("--n",String(u)),p}),o=S("span",{part:`digit ${e}-digit`,className:"digit"},r);o.style.setProperty("--current",String(i)),super(t,i,o,l),this.place=a,F.set(this,void 0),v.set(this,void 0),K.set(this,void 0),st.set(this,()=>{this.el.classList.remove("is-spinning")}),c(this,F,r,"f")}willUpdate(t){const e=this.el.getBoundingClientRect();c(this,v,this.value,"f");const i=e[this.section.justify]-t[this.section.justify],a=e.width/2;c(this,K,this.section.justify==="left"?i+a:i-a,"f")}update(t){var e,i;(e=n(this,F,"f")[this.value])==null||e.classList.remove("is-current"),this.el.style.setProperty("--current",String(t)),(i=n(this,F,"f")[t])==null||i.classList.add("is-current"),this.value=t}didUpdate(t){const e=this.el.getBoundingClientRect(),i=e[this.section.justify]-t[this.section.justify],a=e.width/2,l=this.section.justify==="left"?i+a:i-a,r=n(this,K,"f")-l;r&&this.el.animate({transform:[`translateX(${r}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"});const o=this.diff;o&&(this.el.classList.add("is-spinning"),this.el.animate({[ot]:[-o,0]},{...this.flow.spinTiming??this.flow.transformTiming,composite:"accumulate"}),this.flow.addEventListener("animationsfinish",n(this,st,"f"),{once:!0}))}get diff(){let t=this.flow.computedTrend;const e=this.value-n(this,v,"f");return!e&&this.flow.startingPlace!=null&&this.flow.startingPlace>=this.place?10*t:(t||(t=Math.sign(e)),t===b.DOWN&&this.value>n(this,v,"f")?this.value-10-n(this,v,"f"):t===b.UP&&this.value<n(this,v,"f")?10-n(this,v,"f")+this.value:e)}}F=new WeakMap,v=new WeakMap,K=new WeakMap,st=new WeakMap;class Lt extends gt{constructor(t,e,i,a){const l=S("span",{className:"symbol__value",textContent:i});super(t,i,S("span",{part:e,className:"symbol"},[l]),a),this.type=e,_.set(this,new Map),Z.set(this,void 0),J.set(this,r=>()=>{n(this,_,"f").delete(r)}),n(this,_,"f").set(i,new nt(this.flow,l,{onRemove:n(this,J,"f").call(this,i)}))}willUpdate(t){if(this.type==="decimal")return;const e=this.el.getBoundingClientRect();c(this,Z,e[this.section.justify]-t[this.section.justify],"f")}update(t){if(this.value!==t){const e=n(this,_,"f").get(this.value);if(e.present=!1,e.el.classList.add("symbol__exiting"),n(this,_,"f").has(t)){const i=n(this,_,"f").get(t);i.present=!0,i.el.classList.remove("symbol__exiting")}else{const i=S("span",{className:"symbol__value",textContent:t});this.el.appendChild(i),n(this,_,"f").set(t,new nt(this.flow,i,{animateIn:!0,onRemove:n(this,J,"f").call(this,t)}))}}this.value=t}didUpdate(t){if(this.type==="decimal")return;const i=this.el.getBoundingClientRect()[this.section.justify]-t[this.section.justify],a=n(this,Z,"f")-i;a&&this.el.animate({transform:[`translateX(${a}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"})}}_=new WeakMap,Z=new WeakMap,J=new WeakMap;exports.NumberFlowLite=ut;exports.canAnimate=pt;exports.define=_t;exports.formatToData=St;exports.prefersReducedMotion=H;exports.renderInnerHTML=Pt;
8
+ style="font-kerning: none; display: inline-block; line-height: ${U} !important; padding: ${m} 0;"
9
+ >${n.valueAsString}</span
10
+ >`,it=(n,t)=>n==null?t:t==null?n:Math.max(n,t),V=K&&G&&Z;let S;class B extends J{constructor(){super(...arguments),this.transformTiming=this.constructor.defaultProps.transformTiming,this.spinTiming=this.constructor.defaultProps.spinTiming,this.opacityTiming=this.constructor.defaultProps.opacityTiming,this.respectMotionPreference=this.constructor.defaultProps.respectMotionPreference,this.trend=this.constructor.defaultProps.trend,this.continuous=this.constructor.defaultProps.continuous,this.digits=this.constructor.defaultProps.digits,this._animated=this.constructor.defaultProps.animated,this._created=!1,this._computedAnimated=this._animated,this.manual=!1}get animated(){return this._animated}set animated(t){var e;this.animated!==t&&(this._animated=t,(e=this.shadowRoot)==null||e.getAnimations().forEach(i=>i.finish()))}get created(){return this._created}get computedTrend(){return this._computedTrend}get startingPos(){return this._startingPos}get computedAnimated(){return this._computedAnimated}set data(t){if(t==null)return;const{pre:e,integer:i,fraction:s,post:a,value:r}=t;if(this._created){const o=this._data;if(this._data=t,this._computedTrend=typeof this.trend=="function"?this.trend(o.value,r):this.trend,this._startingPos=void 0,this._computedTrend&&this.continuous){const d=o.integer.concat(o.fraction).filter(h=>h.type==="integer"||h.type==="fraction"),u=t.integer.concat(t.fraction).filter(h=>h.type==="integer"||h.type==="fraction"),p=d.find(h=>!u.find(f=>f.pos===h.pos&&f.value===h.value)),y=u.find(h=>!d.find(f=>h.pos===f.pos&&h.value===f.value));this._startingPos=it(p==null?void 0:p.pos,y==null?void 0:y.pos)}this._computedAnimated=V&&this._animated&&(!this.respectMotionPreference||!($!=null&&$.matches))&&D(this),this.manual||this.willUpdate(),this._pre.update(e),this._num.update({integer:i,fraction:s}),this._post.update(a),this.manual||this.didUpdate()}else{if(this._data=t,this.attachShadow({mode:"open"}),typeof CSSStyleSheet<"u"&&this.shadowRoot.adoptedStyleSheets)S||(S=new CSSStyleSheet,S.replaceSync(L)),this.shadowRoot.adoptedStyleSheets=[S];else{const o=document.createElement("style");o.textContent=L,this.shadowRoot.appendChild(o)}this._pre=new N(this,e,{justify:"right",part:"left"}),this.shadowRoot.appendChild(this._pre.el),this._num=new st(this,i,s),this.shadowRoot.appendChild(this._num.el),this._post=new N(this,a,{justify:"left",part:"right"}),this.shadowRoot.appendChild(this._post.el)}this._created=!0}willUpdate(){this._pre.willUpdate(),this._num.willUpdate(),this._post.willUpdate()}didUpdate(){if(!this._computedAnimated)return;this._abortAnimationsFinish?this._abortAnimationsFinish.abort():this.dispatchEvent(new Event("animationsstart")),this._pre.didUpdate(),this._num.didUpdate(),this._post.didUpdate();const t=new AbortController;Promise.all(this.shadowRoot.getAnimations().map(e=>e.finished)).then(()=>{t.signal.aborted||(this.dispatchEvent(new Event("animationsfinish")),this._abortAnimationsFinish=void 0)}),this._abortAnimationsFinish=t}}B.defaultProps={transformTiming:{duration:900,easing:"linear(0,.005,.019,.039,.066,.096,.129,.165,.202,.24,.278,.316,.354,.39,.426,.461,.494,.526,.557,.586,.614,.64,.665,.689,.711,.731,.751,.769,.786,.802,.817,.831,.844,.856,.867,.877,.887,.896,.904,.912,.919,.925,.931,.937,.942,.947,.951,.955,.959,.962,.965,.968,.971,.973,.976,.978,.98,.981,.983,.984,.986,.987,.988,.989,.99,.991,.992,.992,.993,.994,.994,.995,.995,.996,.996,.9963,.9967,.9969,.9972,.9975,.9977,.9979,.9981,.9982,.9984,.9985,.9987,.9988,.9989,1)"},spinTiming:void 0,opacityTiming:{duration:450,easing:"ease-out"},animated:!0,trend:(n,t)=>Math.sign(t-n),continuous:!1,respectMotionPreference:!0,digits:void 0};class st{constructor(t,e,i,{className:s,...a}={}){this.flow=t,this._integer=new M(t,e,{justify:"right",part:"integer"}),this._fraction=new M(t,i,{justify:"left",part:"fraction"}),this._inner=g("span",{className:"number__inner"},[this._integer.el,this._fraction.el]),this.el=g("span",{...a,part:"number",className:`number ${s??""}`},[this._inner])}willUpdate(){this._prevWidth=this.el.offsetWidth,this._prevLeft=this.el.getBoundingClientRect().left,this._integer.willUpdate(),this._fraction.willUpdate()}update({integer:t,fraction:e}){this._integer.update(t),this._fraction.update(e)}didUpdate(){const t=this.el.getBoundingClientRect();this._integer.didUpdate(),this._fraction.didUpdate();const e=this._prevLeft-t.left,i=this.el.offsetWidth,s=this._prevWidth-i;this.el.style.setProperty("--width",String(i)),this.el.animate({[C]:[`${e}px`,"0px"],[T]:[s,0]},{...this.flow.transformTiming,composite:"accumulate"})}}class O{constructor(t,e,{justify:i,className:s,...a},r){this.flow=t,this.children=new Map,this.onCharRemove=d=>()=>{this.children.delete(d)},this.justify=i;const o=e.map(d=>this.addChar(d).el);this.el=g("span",{...a,className:`section section--justify-${i} ${s??""}`},r?r(o):o)}addChar(t,{startDigitsAtZero:e=!1,...i}={}){const s=t.type==="integer"||t.type==="fraction"?new H(this,t.type,e?0:t.value,t.pos,{...i,onRemove:this.onCharRemove(t.key)}):new nt(this,t.type,t.value,{...i,onRemove:this.onCharRemove(t.key)});return this.children.set(t.key,s),s}unpop(t){t.el.classList.remove("section__exiting"),t.el.style.top="",t.el.style[this.justify]=""}pop(t){t.forEach(e=>{e.el.style.top=`${e.el.offsetTop}px`,e.el.style[this.justify]=`${X(e.el,this.justify)}px`}),t.forEach(e=>{e.el.classList.add("section__exiting"),e.present=!1})}addNewAndUpdateExisting(t){const e=new Map,i=new Map,s=this.justify==="left",a=s?"prepend":"append";if(z(t,r=>{let o;this.children.has(r.key)?(o=this.children.get(r.key),i.set(r,o),this.unpop(o),o.present=!0):(o=this.addChar(r,{startDigitsAtZero:!0,animateIn:!0}),e.set(r,o)),this.el[a](o.el)},{reverse:s}),this.flow.computedAnimated){const r=this.el.getBoundingClientRect();e.forEach(o=>{o.willUpdate(r)})}e.forEach((r,o)=>{r.update(o.value)}),i.forEach((r,o)=>{r.update(o.value)})}willUpdate(){const t=this.el.getBoundingClientRect();this._prevOffset=t[this.justify],this.children.forEach(e=>e.willUpdate(t))}didUpdate(){const t=this.el.getBoundingClientRect();this.children.forEach(s=>s.didUpdate(t));const e=t[this.justify],i=this._prevOffset-e;i&&this.children.size&&this.el.animate({transform:[`translateX(${i}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"})}}class M extends O{update(t){const e=new Map;this.children.forEach((i,s)=>{t.find(a=>a.key===s)||e.set(s,i),this.unpop(i)}),this.addNewAndUpdateExisting(t),e.forEach(i=>{i instanceof H&&i.update(0)}),this.pop(e)}}class N extends O{update(t){const e=new Map;this.children.forEach((i,s)=>{t.find(a=>a.key===s)||e.set(s,i)}),this.pop(e),this.addNewAndUpdateExisting(t)}}class k{constructor(t,e,{onRemove:i,animateIn:s=!1}={}){this.flow=t,this.el=e,this._present=!0,this._remove=()=>{var a;this.el.remove(),(a=this._onRemove)==null||a.call(this)},this.el.classList.add("animate-presence"),this.flow.computedAnimated&&s&&this.el.animate({[R]:[-.9999,0]},{...this.flow.opacityTiming,composite:"accumulate"}),this._onRemove=i}get present(){return this._present}set present(t){if(this._present!==t){if(this._present=t,!this.flow.computedAnimated){t||this._remove();return}this.el.style.setProperty("--_number-flow-d-opacity",t?"0":"-.999"),this.el.animate({[R]:t?[-.9999,0]:[.999,0]},{...this.flow.opacityTiming,composite:"accumulate"}),t?this.flow.removeEventListener("animationsfinish",this._remove):this.flow.addEventListener("animationsfinish",this._remove,{once:!0})}}}class F extends k{constructor(t,e,i,s){super(t.flow,i,s),this.section=t,this.value=e,this.el=i}}class H extends F{constructor(t,e,i,s,a){var u,p;const r=(((p=(u=t.flow.digits)==null?void 0:u[s])==null?void 0:p.max)??9)+1,o=Array.from({length:r}).map((y,h)=>{const f=g("span",{className:`digit__num${h===i?" is-current":""}`},[document.createTextNode(String(h))]);return f.style.setProperty("--n",String(h)),f}),d=g("span",{part:`digit ${e}-digit`,className:"digit"},o);d.style.setProperty("--current",String(i)),d.style.setProperty("--length",String(r)),super(t,i,d,a),this.pos=s,this._onAnimationsFinish=()=>{this.el.classList.remove("is-spinning")},this._numbers=o,this._length=r}willUpdate(t){const e=this.el.getBoundingClientRect();this._prevValue=this.value;const i=e[this.section.justify]-t[this.section.justify],s=e.width/2;this._prevCenter=this.section.justify==="left"?i+s:i-s}update(t){var e,i;(e=this._numbers[this.value])==null||e.classList.remove("is-current"),this.el.style.setProperty("--current",String(t)),(i=this._numbers[t])==null||i.classList.add("is-current"),this.value=t}didUpdate(t){const e=this.el.getBoundingClientRect(),i=e[this.section.justify]-t[this.section.justify],s=e.width/2,a=this.section.justify==="left"?i+s:i-s,r=this._prevCenter-a;r&&this.el.animate({transform:[`translateX(${r}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"});const o=this.diff;o&&(this.el.classList.add("is-spinning"),this.el.animate({[A]:[-o,0]},{...this.flow.spinTiming??this.flow.transformTiming,composite:"accumulate"}),this.flow.addEventListener("animationsfinish",this._onAnimationsFinish,{once:!0}))}get diff(){let t=this.flow.computedTrend;const e=this.value-this._prevValue;return!e&&this.flow.startingPos!=null&&this.flow.startingPos>=this.pos?this._length*t:(t||(t=Math.sign(e)),t<0&&this.value>this._prevValue?this.value-this._length-this._prevValue:t>0&&this.value<this._prevValue?this._length-this._prevValue+this.value:e)}}class nt extends F{constructor(t,e,i,s){const a=g("span",{className:"symbol__value",textContent:i});super(t,i,g("span",{part:e,className:"symbol"},[a]),s),this.type=e,this._children=new Map,this._onChildRemove=r=>()=>{this._children.delete(r)},this._children.set(i,new k(this.flow,a,{onRemove:this._onChildRemove(i)}))}willUpdate(t){if(this.type==="decimal")return;const e=this.el.getBoundingClientRect();this._prevOffset=e[this.section.justify]-t[this.section.justify]}update(t){if(this.value!==t){const e=this._children.get(this.value);if(e.present=!1,e.el.classList.add("symbol__exiting"),this._children.has(t)){const i=this._children.get(t);i.present=!0,i.el.classList.remove("symbol__exiting")}else{const i=g("span",{className:"symbol__value",textContent:t});this.el.appendChild(i),this._children.set(t,new k(this.flow,i,{animateIn:!0,onRemove:this._onChildRemove(t)}))}}this.value=t}didUpdate(t){if(this.type==="decimal")return;const i=this.el.getBoundingClientRect()[this.section.justify]-t[this.section.justify],s=this._prevOffset-i;s&&this.el.animate({transform:[`translateX(${s}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"})}}exports.NumberFlowLite=B;exports.canAnimate=V;exports.define=I;exports.formatToData=Y;exports.prefersReducedMotion=$;exports.renderInnerHTML=et;
package/dist/index.mjs CHANGED
@@ -1,89 +1,78 @@
1
- import { BROWSER as D } from "esm-env";
2
- function n(s, t, e, i) {
3
- if (e === "a" && !i) throw new TypeError("Private accessor was defined without a getter");
4
- if (typeof t == "function" ? s !== t || !i : !t.has(s)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
- return e === "m" ? i : e === "a" ? i.call(s) : i ? i.value : t.get(s);
6
- }
7
- function h(s, t, e, i, a) {
8
- if (i === "m") throw new TypeError("Private method is not writable");
9
- if (i === "a" && !a) throw new TypeError("Private accessor was defined without a setter");
10
- if (typeof t == "function" ? s !== t || !a : !t.has(s)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
11
- return i === "a" ? a.call(s, e) : a ? a.value = e : t.set(s, e), e;
12
- }
13
- const S = (s, t, e) => {
14
- const i = document.createElement(s), [a, l] = Array.isArray(t) ? [void 0, t] : [t, e];
15
- return a && Object.assign(i, a), l == null || l.forEach((r) => i.appendChild(r)), i;
16
- }, gt = (s, t) => {
1
+ import { BROWSER as w } from "esm-env";
2
+ const g = (n, t, e) => {
3
+ const i = document.createElement(n), [s, a] = Array.isArray(t) ? [void 0, t] : [t, e];
4
+ return s && Object.assign(i, s), a == null || a.forEach((r) => i.appendChild(r)), i;
5
+ }, H = (n, t) => {
17
6
  var e;
18
- return t === "left" ? s.offsetLeft : (((e = s.offsetParent instanceof HTMLElement ? s.offsetParent : null) == null ? void 0 : e.offsetWidth) ?? 0) - s.offsetWidth - s.offsetLeft;
19
- }, wt = (s) => s.offsetWidth > 0 && s.offsetHeight > 0, Ut = (s, t) => {
20
- if (!(!D || customElements.get(s) === t))
21
- return customElements.define(s, t);
7
+ return t === "left" ? n.offsetLeft : (((e = n.offsetParent instanceof HTMLElement ? n.offsetParent : null) == null ? void 0 : e.offsetWidth) ?? 0) - n.offsetWidth - n.offsetLeft;
8
+ }, X = (n) => n.offsetWidth > 0 && n.offsetHeight > 0, st = (n, t) => {
9
+ if (!(!w || customElements.get(n) === t))
10
+ return customElements.define(n, t);
22
11
  };
23
- function yt(s, t, { reverse: e = !1 } = {}) {
24
- const i = s.length;
25
- for (let a = e ? i - 1 : 0; e ? a >= 0 : a < i; e ? a-- : a++)
26
- t(s[a], a);
12
+ function D(n, t, { reverse: e = !1 } = {}) {
13
+ const i = n.length;
14
+ for (let s = e ? i - 1 : 0; e ? s >= 0 : s < i; e ? s-- : s++)
15
+ t(n[s], s);
27
16
  }
28
- function Lt(s, t, e, i) {
29
- const a = t.formatToParts(s);
30
- e && a.unshift({ type: "prefix", value: e }), i && a.push({ type: "suffix", value: i });
31
- const l = [], r = [], o = [], m = [], u = {}, p = (f) => `${f}:${u[f] = (u[f] ?? -1) + 1}`;
32
- let P = "", d = !1, w = !1;
33
- for (const f of a) {
34
- P += f.value;
35
- const c = f.type === "minusSign" || f.type === "plusSign" ? "sign" : f.type;
36
- c === "integer" ? (d = !0, r.push(...f.value.split("").map((tt) => ({ type: c, value: parseInt(tt) })))) : c === "group" ? r.push({ type: c, value: f.value }) : c === "decimal" ? (w = !0, o.push({ type: c, value: f.value, key: p(c) })) : c === "fraction" ? o.push(...f.value.split("").map((tt) => ({
37
- type: c,
38
- value: parseInt(tt),
39
- key: p(c),
40
- place: -1 - u[c]
41
- }))) : (d || w ? m : l).push({
42
- type: c,
43
- value: f.value,
44
- key: p(c)
17
+ function nt(n, t, e, i) {
18
+ const s = t.formatToParts(n);
19
+ e && s.unshift({ type: "prefix", value: e }), i && s.push({ type: "suffix", value: i });
20
+ const a = [], r = [], o = [], d = [], u = {}, p = (c) => `${c}:${u[c] = (u[c] ?? -1) + 1}`;
21
+ let y = "", h = !1, f = !1;
22
+ for (const c of s) {
23
+ y += c.value;
24
+ const l = c.type === "minusSign" || c.type === "plusSign" ? "sign" : c.type;
25
+ l === "integer" ? (h = !0, r.push(...c.value.split("").map((k) => ({ type: l, value: parseInt(k) })))) : l === "group" ? r.push({ type: l, value: c.value }) : l === "decimal" ? (f = !0, o.push({ type: l, value: c.value, key: p(l) })) : l === "fraction" ? o.push(...c.value.split("").map((k) => ({
26
+ type: l,
27
+ value: parseInt(k),
28
+ key: p(l),
29
+ pos: -1 - u[l]
30
+ }))) : (h || f ? d : a).push({
31
+ type: l,
32
+ value: c.value,
33
+ key: p(l)
45
34
  });
46
35
  }
47
- const lt = [];
48
- for (let f = r.length - 1; f >= 0; f--) {
49
- const c = r[f];
50
- lt.unshift(c.type === "integer" ? {
51
- ...c,
52
- key: p(c.type),
53
- place: u[c.type]
36
+ const j = [];
37
+ for (let c = r.length - 1; c >= 0; c--) {
38
+ const l = r[c];
39
+ j.unshift(l.type === "integer" ? {
40
+ ...l,
41
+ key: p(l.type),
42
+ pos: u[l.type]
54
43
  } : {
55
- ...c,
56
- key: p(c.type)
44
+ ...l,
45
+ key: p(l.type)
57
46
  });
58
47
  }
59
48
  return {
60
- pre: l,
61
- integer: lt,
49
+ pre: a,
50
+ integer: j,
62
51
  fraction: o,
63
- post: m,
64
- valueAsString: P,
65
- value: typeof s == "string" ? parseFloat(s) : s
52
+ post: d,
53
+ valueAsString: y,
54
+ value: typeof n == "string" ? parseFloat(n) : n
66
55
  };
67
56
  }
68
- const vt = String.raw, dt = String.raw, _t = D && typeof CSS < "u" && CSS.supports && CSS.supports("animation-timing-function", "linear(1,2)"), bt = D && typeof CSS < "u" && CSS.supports && CSS.supports("line-height", "mod(1,1)"), et = D && typeof matchMedia < "u" ? matchMedia("(prefers-reduced-motion: reduce)") : null, J = "--_number-flow-d-opacity", at = "--_number-flow-d-width", Q = "--_number-flow-dx", ot = "--_number-flow-d", St = (() => {
57
+ const I = String.raw, V = String.raw, O = w && typeof CSS < "u" && CSS.supports && CSS.supports("animation-timing-function", "linear(1,2)"), z = w && typeof CSS < "u" && CSS.supports && CSS.supports("line-height", "mod(1,1)"), P = w && typeof matchMedia < "u" ? matchMedia("(prefers-reduced-motion: reduce)") : null, x = "--_number-flow-d-opacity", T = "--_number-flow-d-width", C = "--_number-flow-dx", A = "--_number-flow-d", Y = (() => {
69
58
  try {
70
59
  return CSS.registerProperty({
71
- name: J,
60
+ name: x,
72
61
  syntax: "<number>",
73
62
  inherits: !1,
74
63
  initialValue: "0"
75
64
  }), CSS.registerProperty({
76
- name: Q,
65
+ name: C,
77
66
  syntax: "<length>",
78
67
  inherits: !0,
79
68
  initialValue: "0px"
80
69
  }), CSS.registerProperty({
81
- name: at,
70
+ name: T,
82
71
  syntax: "<number>",
83
72
  inherits: !1,
84
73
  initialValue: "0"
85
74
  }), CSS.registerProperty({
86
- name: ot,
75
+ name: A,
87
76
  syntax: "<number>",
88
77
  inherits: !0,
89
78
  initialValue: "0"
@@ -91,94 +80,87 @@ const vt = String.raw, dt = String.raw, _t = D && typeof CSS < "u" && CSS.suppor
91
80
  } catch {
92
81
  return !1;
93
82
  }
94
- })(), rt = "var(--number-flow-char-height, 1em)", g = "var(--number-flow-mask-height, 0.25em)", H = `calc(${g} / 2)`, it = "var(--number-flow-mask-width, 0.5em)", $ = `calc(${it} / var(--scale-x))`, O = "#000 0, transparent 71%", ht = dt`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${rt} !important;isolation:isolate;}.number,.number__inner{display:inline-block;transform-origin:left top;}:host([data-will-change]) :is(.number,.number__inner,.section,.digit,.digit__num,.symbol){will-change:transform;}.number{--scale-x:calc(1 + var(${at}) / var(--width));transform:translateX(var(${Q})) scaleX(var(--scale-x));margin:0 calc(-1 * ${it});position:relative;z-index:-1;-webkit-mask-image:linear-gradient( to right,transparent 0,#000 ${$},#000 calc(100% - ${$}),transparent ),linear-gradient( to bottom,transparent 0,#000 ${g},#000 calc(100% - ${g}),transparent 100% ),radial-gradient(at bottom right,${O}),radial-gradient(at bottom left,${O}),radial-gradient(at top left,${O}),radial-gradient(at top right,${O});-webkit-mask-size:100% calc(100% - ${g} * 2),calc(100% - ${$} * 2) 100%,${$} ${g},${$} ${g},${$} ${g},${$} ${g};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat;}.number__inner{padding:${H} ${it};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${Q})));}.section{display:inline-block;position:relative;isolation:isolate;}.section::after{content:'\200b';display:inline-block;padding:${H} 0;}.section--justify-left{transform-origin:center left;}.section--justify-right{transform-origin:center right;}.section__exiting,.symbol__exiting{margin:0 !important;position:absolute !important;z-index:-1;}.digit{display:inline-block;position:relative;--c:var(--current) + var(${ot});}.digit__num{display:inline-block;padding:${H} 0;--offset-raw:mod(10 + var(--n) - mod(var(--c),10),10);--offset:calc(var(--offset-raw) - 10 * round(down,var(--offset-raw) / 5,1));--y:clamp(-100%,var(--offset) * 100%,100%);transform:translateY(var(--y));}.digit__num:not(.is-current){position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y));}.digit:not(.is-spinning) .digit__num:not(.is-current){display:none;}.symbol{display:inline-block;position:relative;isolation:isolate;}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre;}.section--justify-left .symbol__exiting{left:0;}.section--justify-right .symbol__exiting{right:0;}.animate-presence{opacity:calc(1 + var(${J}));}`, $t = D ? HTMLElement : class {
95
- }, kt = dt`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${rt} !important;}span{display:inline-block;}:host([data-will-change]) span{will-change:transform;}.number,.digit{padding:${H} 0;}.symbol{white-space:pre;}`, xt = (s) => `<span class="${s.type === "integer" || s.type === "fraction" ? "digit" : "symbol"}" part="${s.type === "integer" || s.type === "fraction" ? `digit ${s.type}-digit` : s.type}">${s.value}</span>`, B = (s, t) => `<span part="${t}">${s.reduce((e, i) => e + xt(i), "")}</span>`, Rt = (s) => (
83
+ })(), U = "var(--number-flow-char-height, 1em)", m = "var(--number-flow-mask-height, 0.25em)", S = `calc(${m} / 2)`, E = "var(--number-flow-mask-width, 0.5em)", _ = `calc(${E} / var(--scale-x))`, v = "#000 0, transparent 71%", L = V`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${U} !important;isolation:isolate}.number,.number__inner{display:inline-block;transform-origin:left top}:host([data-will-change]) :is(.number,.number__inner,.section,.digit,.digit__num,.symbol){will-change:transform}.number{--scale-x:calc(1 + var(${T}) / var(--width));transform:translateX(var(${C})) scaleX(var(--scale-x));margin:0 calc(-1 * ${E});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${_},#000 calc(100% - ${_}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${m},#000 calc(100% - ${m}),transparent 100% ),radial-gradient(at bottom right,${v}),radial-gradient(at bottom left,${v}),radial-gradient(at top left,${v}),radial-gradient(at top right,${v});-webkit-mask-size:100% calc(100% - ${m} * 2),calc(100% - ${_} * 2) 100%,${_} ${m},${_} ${m},${_} ${m},${_} ${m};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${S} ${E};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${C})))}:host > :not(.number){z-index:5}.section,.symbol{display:inline-block;position:relative;isolation:isolate}.section::after{content:'\200b';display:inline-block;padding:${S} 0}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section__exiting,.symbol__exiting{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${A})}.digit__num{display:inline-block;padding:${S} 0;--offset-raw:mod(var(--length) + var(--n) - mod(var(--c),var(--length)),var(--length));--offset:calc(var(--offset-raw) - var(--length) * round(down,var(--offset-raw) / (var(--length)/2),1));--y:clamp(-100%,var(--offset) * 100%,100%);transform:translateY(var(--y))}.digit__num:not(.is-current){position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num:not(.is-current){display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol__exiting{left:0}.section--justify-right .symbol__exiting{right:0}.animate-presence{opacity:calc(1 + var(${x}))}`, G = w ? HTMLElement : class {
84
+ }, K = V`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${U} !important}span{display:inline-block}:host([data-will-change]) span{will-change:transform}.number,.digit{padding:${S} 0}.symbol{white-space:pre}`, Z = (n) => `<span class="${n.type === "integer" || n.type === "fraction" ? "digit" : "symbol"}" part="${n.type === "integer" || n.type === "fraction" ? `digit ${n.type}-digit` : n.type}">${n.value}</span>`, b = (n, t) => `<span part="${t}">${n.reduce((e, i) => e + Z(i), "")}</span>`, ot = (n) => (
96
85
  // shadowroot="open" non-standard attribute for old Chrome:
97
- vt`<template shadowroot="open" shadowrootmode="open"
86
+ I`<template shadowroot="open" shadowrootmode="open"
98
87
  ><style>
99
- ${kt}</style
100
- >${B(s.pre, "left")}<span part="number" class="number"
101
- >${B(s.integer, "integer")}${B(s.fraction, "fraction")}</span
102
- >${B(s.post, "right")}</template
88
+ ${K}</style
89
+ >${b(n.pre, "left")}<span part="number" class="number"
90
+ >${b(n.integer, "integer")}${b(n.fraction, "fraction")}</span
91
+ >${b(n.post, "right")}</template
103
92
  ><span
104
- style="font-kerning: none; display: inline-block; line-height: ${rt} !important; padding: ${g} 0;"
105
- >${s.valueAsString}</span
93
+ style="font-kerning: none; display: inline-block; line-height: ${U} !important; padding: ${m} 0;"
94
+ >${n.valueAsString}</span
106
95
  >`
107
- ), Et = (s, t) => s != null && t == null ? s : s == null && t != null ? t : s != null && t != null ? Math.max(s, t) : null;
108
- var W, U, k, x, E, y, L, R, T, C, X, M, N, I, z, G, j, Y, A, F, v, K, st, _, Z, q;
109
- const Mt = bt && _t && St;
110
- var b;
111
- (function(s) {
112
- s[s.UP = 1] = "UP", s[s.DOWN = -1] = "DOWN", s[s.NONE = 0] = "NONE";
113
- })(b || (b = {}));
114
- let V;
115
- class Nt extends $t {
96
+ ), q = (n, t) => n == null ? t : t == null ? n : Math.max(n, t), J = z && O && Y;
97
+ let $;
98
+ class Q extends G {
116
99
  constructor() {
117
- super(...arguments), this.transformTiming = this.constructor.defaultProps.transformTiming, this.spinTiming = this.constructor.defaultProps.spinTiming, this.opacityTiming = this.constructor.defaultProps.opacityTiming, this.respectMotionPreference = this.constructor.defaultProps.respectMotionPreference, this.trend = this.constructor.defaultProps.trend, this.continuous = this.constructor.defaultProps.continuous, W.set(this, this.constructor.defaultProps.animated), U.set(this, !1), k.set(this, void 0), x.set(this, void 0), E.set(this, void 0), y.set(this, void 0), L.set(this, void 0), R.set(this, n(this, W, "f")), T.set(this, void 0), this.manual = !1, C.set(this, void 0);
100
+ super(...arguments), this.transformTiming = this.constructor.defaultProps.transformTiming, this.spinTiming = this.constructor.defaultProps.spinTiming, this.opacityTiming = this.constructor.defaultProps.opacityTiming, this.respectMotionPreference = this.constructor.defaultProps.respectMotionPreference, this.trend = this.constructor.defaultProps.trend, this.continuous = this.constructor.defaultProps.continuous, this.digits = this.constructor.defaultProps.digits, this._animated = this.constructor.defaultProps.animated, this._created = !1, this._computedAnimated = this._animated, this.manual = !1;
118
101
  }
119
102
  get animated() {
120
- return n(this, W, "f");
103
+ return this._animated;
121
104
  }
122
105
  set animated(t) {
123
106
  var e;
124
- this.animated !== t && (h(this, W, t, "f"), (e = this.shadowRoot) == null || e.getAnimations().forEach((i) => i.finish()));
107
+ this.animated !== t && (this._animated = t, (e = this.shadowRoot) == null || e.getAnimations().forEach((i) => i.finish()));
125
108
  }
126
109
  get created() {
127
- return n(this, U, "f");
110
+ return this._created;
128
111
  }
129
112
  get computedTrend() {
130
- return n(this, y, "f");
113
+ return this._computedTrend;
131
114
  }
132
- get startingPlace() {
133
- return n(this, L, "f");
115
+ get startingPos() {
116
+ return this._startingPos;
134
117
  }
135
118
  get computedAnimated() {
136
- return n(this, R, "f");
119
+ return this._computedAnimated;
137
120
  }
138
121
  set data(t) {
139
122
  if (t == null)
140
123
  return;
141
- const { pre: e, integer: i, fraction: a, post: l, value: r } = t;
142
- if (n(this, U, "f")) {
143
- const o = n(this, T, "f");
144
- if (h(this, T, t, "f"), this.trend === !0 ? h(this, y, Math.sign(r - o.value), "f") : this.trend === "increasing" ? h(this, y, b.UP, "f") : this.trend === "decreasing" ? h(this, y, b.DOWN, "f") : h(this, y, b.NONE, "f"), h(this, L, void 0, "f"), n(this, y, "f") !== b.NONE && this.continuous) {
145
- const m = o.integer.concat(o.fraction).filter((d) => d.type === "integer" || d.type === "fraction"), u = t.integer.concat(t.fraction).filter((d) => d.type === "integer" || d.type === "fraction"), p = m.find((d) => !u.find((w) => w.place === d.place && w.value === d.value)), P = u.find((d) => !m.find((w) => d.place === w.place && d.value === w.value));
146
- h(this, L, Et(p == null ? void 0 : p.place, P == null ? void 0 : P.place), "f");
124
+ const { pre: e, integer: i, fraction: s, post: a, value: r } = t;
125
+ if (this._created) {
126
+ const o = this._data;
127
+ if (this._data = t, this._computedTrend = typeof this.trend == "function" ? this.trend(o.value, r) : this.trend, this._startingPos = void 0, this._computedTrend && this.continuous) {
128
+ const d = o.integer.concat(o.fraction).filter((h) => h.type === "integer" || h.type === "fraction"), u = t.integer.concat(t.fraction).filter((h) => h.type === "integer" || h.type === "fraction"), p = d.find((h) => !u.find((f) => f.pos === h.pos && f.value === h.value)), y = u.find((h) => !d.find((f) => h.pos === f.pos && h.value === f.value));
129
+ this._startingPos = q(p == null ? void 0 : p.pos, y == null ? void 0 : y.pos);
147
130
  }
148
- h(this, R, Mt && n(this, W, "f") && (!this.respectMotionPreference || !(et != null && et.matches)) && // https://github.com/barvian/number-flow/issues/9
149
- wt(this), "f"), this.manual || this.willUpdate(), n(this, k, "f").update(e), n(this, x, "f").update({ integer: i, fraction: a }), n(this, E, "f").update(l), this.manual || this.didUpdate();
131
+ this._computedAnimated = J && this._animated && (!this.respectMotionPreference || !(P != null && P.matches)) && // https://github.com/barvian/number-flow/issues/9
132
+ X(this), this.manual || this.willUpdate(), this._pre.update(e), this._num.update({ integer: i, fraction: s }), this._post.update(a), this.manual || this.didUpdate();
150
133
  } else {
151
- if (h(this, T, t, "f"), this.attachShadow({ mode: "open" }), typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
152
- V || (V = new CSSStyleSheet(), V.replaceSync(ht)), this.shadowRoot.adoptedStyleSheets = [V];
134
+ if (this._data = t, this.attachShadow({ mode: "open" }), typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
135
+ $ || ($ = new CSSStyleSheet(), $.replaceSync(L)), this.shadowRoot.adoptedStyleSheets = [$];
153
136
  else {
154
137
  const o = document.createElement("style");
155
- o.textContent = ht, this.shadowRoot.appendChild(o);
138
+ o.textContent = L, this.shadowRoot.appendChild(o);
156
139
  }
157
- h(this, k, new ft(this, e, {
140
+ this._pre = new N(this, e, {
158
141
  justify: "right",
159
142
  part: "left"
160
- }), "f"), this.shadowRoot.appendChild(n(this, k, "f").el), h(this, x, new Pt(this, i, a), "f"), this.shadowRoot.appendChild(n(this, x, "f").el), h(this, E, new ft(this, l, {
143
+ }), this.shadowRoot.appendChild(this._pre.el), this._num = new tt(this, i, s), this.shadowRoot.appendChild(this._num.el), this._post = new N(this, a, {
161
144
  justify: "left",
162
145
  part: "right"
163
- }), "f"), this.shadowRoot.appendChild(n(this, E, "f").el);
146
+ }), this.shadowRoot.appendChild(this._post.el);
164
147
  }
165
- h(this, U, !0, "f");
148
+ this._created = !0;
166
149
  }
167
150
  willUpdate() {
168
- n(this, k, "f").willUpdate(), n(this, x, "f").willUpdate(), n(this, E, "f").willUpdate();
151
+ this._pre.willUpdate(), this._num.willUpdate(), this._post.willUpdate();
169
152
  }
170
153
  didUpdate() {
171
- if (!n(this, R, "f"))
154
+ if (!this._computedAnimated)
172
155
  return;
173
- n(this, C, "f") ? n(this, C, "f").abort() : this.dispatchEvent(new Event("animationsstart")), n(this, k, "f").didUpdate(), n(this, x, "f").didUpdate(), n(this, E, "f").didUpdate();
156
+ this._abortAnimationsFinish ? this._abortAnimationsFinish.abort() : this.dispatchEvent(new Event("animationsstart")), this._pre.didUpdate(), this._num.didUpdate(), this._post.didUpdate();
174
157
  const t = new AbortController();
175
158
  Promise.all(this.shadowRoot.getAnimations().map((e) => e.finished)).then(() => {
176
- t.signal.aborted || (this.dispatchEvent(new Event("animationsfinish")), h(this, C, void 0, "f"));
177
- }), h(this, C, t, "f");
159
+ t.signal.aborted || (this.dispatchEvent(new Event("animationsfinish")), this._abortAnimationsFinish = void 0);
160
+ }), this._abortAnimationsFinish = t;
178
161
  }
179
162
  }
180
- W = /* @__PURE__ */ new WeakMap(), U = /* @__PURE__ */ new WeakMap(), k = /* @__PURE__ */ new WeakMap(), x = /* @__PURE__ */ new WeakMap(), E = /* @__PURE__ */ new WeakMap(), y = /* @__PURE__ */ new WeakMap(), L = /* @__PURE__ */ new WeakMap(), R = /* @__PURE__ */ new WeakMap(), T = /* @__PURE__ */ new WeakMap(), C = /* @__PURE__ */ new WeakMap();
181
- Nt.defaultProps = {
163
+ Q.defaultProps = {
182
164
  transformTiming: {
183
165
  duration: 900,
184
166
  // Make sure to keep this minified:
@@ -187,83 +169,83 @@ Nt.defaultProps = {
187
169
  spinTiming: void 0,
188
170
  opacityTiming: { duration: 450, easing: "ease-out" },
189
171
  animated: !0,
190
- trend: !0,
172
+ trend: (n, t) => Math.sign(t - n),
191
173
  continuous: !1,
192
- respectMotionPreference: !0
174
+ respectMotionPreference: !0,
175
+ digits: void 0
193
176
  };
194
- class Pt {
195
- constructor(t, e, i, { className: a, ...l } = {}) {
196
- this.flow = t, X.set(this, void 0), M.set(this, void 0), N.set(this, void 0), I.set(this, void 0), z.set(this, void 0), h(this, M, new ct(t, e, {
177
+ class tt {
178
+ constructor(t, e, i, { className: s, ...a } = {}) {
179
+ this.flow = t, this._integer = new M(t, e, {
197
180
  justify: "right",
198
181
  part: "integer"
199
- }), "f"), h(this, N, new ct(t, i, {
182
+ }), this._fraction = new M(t, i, {
200
183
  justify: "left",
201
184
  part: "fraction"
202
- }), "f"), h(this, X, S("span", {
185
+ }), this._inner = g("span", {
203
186
  className: "number__inner"
204
- }, [n(this, M, "f").el, n(this, N, "f").el]), "f"), this.el = S("span", {
205
- ...l,
187
+ }, [this._integer.el, this._fraction.el]), this.el = g("span", {
188
+ ...a,
206
189
  part: "number",
207
- className: `number ${a ?? ""}`
208
- }, [n(this, X, "f")]);
190
+ className: `number ${s ?? ""}`
191
+ }, [this._inner]);
209
192
  }
210
193
  willUpdate() {
211
- h(this, I, this.el.offsetWidth, "f"), h(this, z, this.el.getBoundingClientRect().left, "f"), n(this, M, "f").willUpdate(), n(this, N, "f").willUpdate();
194
+ this._prevWidth = this.el.offsetWidth, this._prevLeft = this.el.getBoundingClientRect().left, this._integer.willUpdate(), this._fraction.willUpdate();
212
195
  }
213
196
  update({ integer: t, fraction: e }) {
214
- n(this, M, "f").update(t), n(this, N, "f").update(e);
197
+ this._integer.update(t), this._fraction.update(e);
215
198
  }
216
199
  didUpdate() {
217
200
  const t = this.el.getBoundingClientRect();
218
- n(this, M, "f").didUpdate(), n(this, N, "f").didUpdate();
219
- const e = n(this, z, "f") - t.left, i = this.el.offsetWidth, a = n(this, I, "f") - i;
201
+ this._integer.didUpdate(), this._fraction.didUpdate();
202
+ const e = this._prevLeft - t.left, i = this.el.offsetWidth, s = this._prevWidth - i;
220
203
  this.el.style.setProperty("--width", String(i)), this.el.animate({
221
- [Q]: [`${e}px`, "0px"],
222
- [at]: [a, 0]
204
+ [C]: [`${e}px`, "0px"],
205
+ [T]: [s, 0]
223
206
  }, {
224
207
  ...this.flow.transformTiming,
225
208
  composite: "accumulate"
226
209
  });
227
210
  }
228
211
  }
229
- X = /* @__PURE__ */ new WeakMap(), M = /* @__PURE__ */ new WeakMap(), N = /* @__PURE__ */ new WeakMap(), I = /* @__PURE__ */ new WeakMap(), z = /* @__PURE__ */ new WeakMap();
230
- class pt {
231
- constructor(t, e, { justify: i, className: a, ...l }, r) {
232
- this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (m) => () => {
233
- this.children.delete(m);
234
- }, G.set(this, void 0), this.justify = i;
235
- const o = e.map((m) => this.addChar(m).el);
236
- this.el = S("span", {
237
- ...l,
238
- className: `section section--justify-${i} ${a ?? ""}`
212
+ class W {
213
+ constructor(t, e, { justify: i, className: s, ...a }, r) {
214
+ this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (d) => () => {
215
+ this.children.delete(d);
216
+ }, this.justify = i;
217
+ const o = e.map((d) => this.addChar(d).el);
218
+ this.el = g("span", {
219
+ ...a,
220
+ className: `section section--justify-${i} ${s ?? ""}`
239
221
  }, r ? r(o) : o);
240
222
  }
241
223
  addChar(t, { startDigitsAtZero: e = !1, ...i } = {}) {
242
- const a = t.type === "integer" || t.type === "fraction" ? new mt(this, t.type, e ? 0 : t.value, t.place, {
224
+ const s = t.type === "integer" || t.type === "fraction" ? new F(this, t.type, e ? 0 : t.value, t.pos, {
243
225
  ...i,
244
226
  onRemove: this.onCharRemove(t.key)
245
- }) : new Wt(this, t.type, t.value, {
227
+ }) : new et(this, t.type, t.value, {
246
228
  ...i,
247
229
  onRemove: this.onCharRemove(t.key)
248
230
  });
249
- return this.children.set(t.key, a), a;
231
+ return this.children.set(t.key, s), s;
250
232
  }
251
233
  unpop(t) {
252
234
  t.el.classList.remove("section__exiting"), t.el.style.top = "", t.el.style[this.justify] = "";
253
235
  }
254
236
  pop(t) {
255
237
  t.forEach((e) => {
256
- e.el.style.top = `${e.el.offsetTop}px`, e.el.style[this.justify] = `${gt(e.el, this.justify)}px`;
238
+ e.el.style.top = `${e.el.offsetTop}px`, e.el.style[this.justify] = `${H(e.el, this.justify)}px`;
257
239
  }), t.forEach((e) => {
258
240
  e.el.classList.add("section__exiting"), e.present = !1;
259
241
  });
260
242
  }
261
243
  addNewAndUpdateExisting(t) {
262
- const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = this.justify === "left", l = a ? "prepend" : "append";
263
- if (yt(t, (r) => {
244
+ const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), s = this.justify === "left", a = s ? "prepend" : "append";
245
+ if (D(t, (r) => {
264
246
  let o;
265
- this.children.has(r.key) ? (o = this.children.get(r.key), i.set(r, o), this.unpop(o), o.present = !0) : (o = this.addChar(r, { startDigitsAtZero: !0, animateIn: !0 }), e.set(r, o)), this.el[l](o.el);
266
- }, { reverse: a }), this.flow.computedAnimated) {
247
+ this.children.has(r.key) ? (o = this.children.get(r.key), i.set(r, o), this.unpop(o), o.present = !0) : (o = this.addChar(r, { startDigitsAtZero: !0, animateIn: !0 }), e.set(r, o)), this.el[a](o.el);
248
+ }, { reverse: s }), this.flow.computedAnimated) {
267
249
  const r = this.el.getBoundingClientRect();
268
250
  e.forEach((o) => {
269
251
  o.willUpdate(r);
@@ -277,12 +259,12 @@ class pt {
277
259
  }
278
260
  willUpdate() {
279
261
  const t = this.el.getBoundingClientRect();
280
- h(this, G, t[this.justify], "f"), this.children.forEach((e) => e.willUpdate(t));
262
+ this._prevOffset = t[this.justify], this.children.forEach((e) => e.willUpdate(t));
281
263
  }
282
264
  didUpdate() {
283
265
  const t = this.el.getBoundingClientRect();
284
- this.children.forEach((a) => a.didUpdate(t));
285
- const e = t[this.justify], i = n(this, G, "f") - e;
266
+ this.children.forEach((s) => s.didUpdate(t));
267
+ const e = t[this.justify], i = this._prevOffset - e;
286
268
  i && this.children.size && this.el.animate({
287
269
  transform: [`translateX(${i}px)`, "none"]
288
270
  }, {
@@ -291,88 +273,87 @@ class pt {
291
273
  });
292
274
  }
293
275
  }
294
- G = /* @__PURE__ */ new WeakMap();
295
- class ct extends pt {
276
+ class M extends W {
296
277
  update(t) {
297
278
  const e = /* @__PURE__ */ new Map();
298
- this.children.forEach((i, a) => {
299
- t.find((l) => l.key === a) || e.set(a, i), this.unpop(i);
279
+ this.children.forEach((i, s) => {
280
+ t.find((a) => a.key === s) || e.set(s, i), this.unpop(i);
300
281
  }), this.addNewAndUpdateExisting(t), e.forEach((i) => {
301
- i instanceof mt && i.update(0);
282
+ i instanceof F && i.update(0);
302
283
  }), this.pop(e);
303
284
  }
304
285
  }
305
- class ft extends pt {
286
+ class N extends W {
306
287
  update(t) {
307
288
  const e = /* @__PURE__ */ new Map();
308
- this.children.forEach((i, a) => {
309
- t.find((l) => l.key === a) || e.set(a, i);
289
+ this.children.forEach((i, s) => {
290
+ t.find((a) => a.key === s) || e.set(s, i);
310
291
  }), this.pop(e), this.addNewAndUpdateExisting(t);
311
292
  }
312
293
  }
313
- class nt {
314
- constructor(t, e, { onRemove: i, animateIn: a = !1 } = {}) {
315
- this.flow = t, this.el = e, j.set(this, !0), Y.set(this, void 0), A.set(this, () => {
316
- var l;
317
- this.el.remove(), (l = n(this, Y, "f")) == null || l.call(this);
318
- }), this.el.classList.add("animate-presence"), this.flow.computedAnimated && a && this.el.animate({
319
- [J]: [-0.9999, 0]
294
+ class R {
295
+ constructor(t, e, { onRemove: i, animateIn: s = !1 } = {}) {
296
+ this.flow = t, this.el = e, this._present = !0, this._remove = () => {
297
+ var a;
298
+ this.el.remove(), (a = this._onRemove) == null || a.call(this);
299
+ }, this.el.classList.add("animate-presence"), this.flow.computedAnimated && s && this.el.animate({
300
+ [x]: [-0.9999, 0]
320
301
  }, {
321
302
  ...this.flow.opacityTiming,
322
303
  composite: "accumulate"
323
- }), h(this, Y, i, "f");
304
+ }), this._onRemove = i;
324
305
  }
325
306
  get present() {
326
- return n(this, j, "f");
307
+ return this._present;
327
308
  }
328
309
  set present(t) {
329
- if (n(this, j, "f") !== t) {
330
- if (h(this, j, t, "f"), !this.flow.computedAnimated) {
331
- t || n(this, A, "f").call(this);
310
+ if (this._present !== t) {
311
+ if (this._present = t, !this.flow.computedAnimated) {
312
+ t || this._remove();
332
313
  return;
333
314
  }
334
315
  this.el.style.setProperty("--_number-flow-d-opacity", t ? "0" : "-.999"), this.el.animate({
335
- [J]: t ? [-0.9999, 0] : [0.999, 0]
316
+ [x]: t ? [-0.9999, 0] : [0.999, 0]
336
317
  }, {
337
318
  ...this.flow.opacityTiming,
338
319
  composite: "accumulate"
339
- }), t ? this.flow.removeEventListener("animationsfinish", n(this, A, "f")) : this.flow.addEventListener("animationsfinish", n(this, A, "f"), {
320
+ }), t ? this.flow.removeEventListener("animationsfinish", this._remove) : this.flow.addEventListener("animationsfinish", this._remove, {
340
321
  once: !0
341
322
  });
342
323
  }
343
324
  }
344
325
  }
345
- j = /* @__PURE__ */ new WeakMap(), Y = /* @__PURE__ */ new WeakMap(), A = /* @__PURE__ */ new WeakMap();
346
- class ut extends nt {
347
- constructor(t, e, i, a) {
348
- super(t.flow, i, a), this.section = t, this.value = e, this.el = i;
326
+ class B extends R {
327
+ constructor(t, e, i, s) {
328
+ super(t.flow, i, s), this.section = t, this.value = e, this.el = i;
349
329
  }
350
330
  }
351
- class mt extends ut {
352
- constructor(t, e, i, a, l) {
353
- const r = Array.from({ length: 10 }).map((m, u) => {
354
- const p = S("span", { className: `digit__num${u === i ? " is-current" : ""}` }, [document.createTextNode(String(u))]);
355
- return p.style.setProperty("--n", String(u)), p;
356
- }), o = S("span", {
331
+ class F extends B {
332
+ constructor(t, e, i, s, a) {
333
+ var u, p;
334
+ const r = (((p = (u = t.flow.digits) == null ? void 0 : u[s]) == null ? void 0 : p.max) ?? 9) + 1, o = Array.from({ length: r }).map((y, h) => {
335
+ const f = g("span", { className: `digit__num${h === i ? " is-current" : ""}` }, [document.createTextNode(String(h))]);
336
+ return f.style.setProperty("--n", String(h)), f;
337
+ }), d = g("span", {
357
338
  part: `digit ${e}-digit`,
358
339
  className: "digit"
359
- }, r);
360
- o.style.setProperty("--current", String(i)), super(t, i, o, l), this.place = a, F.set(this, void 0), v.set(this, void 0), K.set(this, void 0), st.set(this, () => {
340
+ }, o);
341
+ d.style.setProperty("--current", String(i)), d.style.setProperty("--length", String(r)), super(t, i, d, a), this.pos = s, this._onAnimationsFinish = () => {
361
342
  this.el.classList.remove("is-spinning");
362
- }), h(this, F, r, "f");
343
+ }, this._numbers = o, this._length = r;
363
344
  }
364
345
  willUpdate(t) {
365
346
  const e = this.el.getBoundingClientRect();
366
- h(this, v, this.value, "f");
367
- const i = e[this.section.justify] - t[this.section.justify], a = e.width / 2;
368
- h(this, K, this.section.justify === "left" ? i + a : i - a, "f");
347
+ this._prevValue = this.value;
348
+ const i = e[this.section.justify] - t[this.section.justify], s = e.width / 2;
349
+ this._prevCenter = this.section.justify === "left" ? i + s : i - s;
369
350
  }
370
351
  update(t) {
371
352
  var e, i;
372
- (e = n(this, F, "f")[this.value]) == null || e.classList.remove("is-current"), this.el.style.setProperty("--current", String(t)), (i = n(this, F, "f")[t]) == null || i.classList.add("is-current"), this.value = t;
353
+ (e = this._numbers[this.value]) == null || e.classList.remove("is-current"), this.el.style.setProperty("--current", String(t)), (i = this._numbers[t]) == null || i.classList.add("is-current"), this.value = t;
373
354
  }
374
355
  didUpdate(t) {
375
- const e = this.el.getBoundingClientRect(), i = e[this.section.justify] - t[this.section.justify], a = e.width / 2, l = this.section.justify === "left" ? i + a : i - a, r = n(this, K, "f") - l;
356
+ const e = this.el.getBoundingClientRect(), i = e[this.section.justify] - t[this.section.justify], s = e.width / 2, a = this.section.justify === "left" ? i + s : i - s, r = this._prevCenter - a;
376
357
  r && this.el.animate({
377
358
  transform: [`translateX(${r}px)`, "none"]
378
359
  }, {
@@ -381,54 +362,53 @@ class mt extends ut {
381
362
  });
382
363
  const o = this.diff;
383
364
  o && (this.el.classList.add("is-spinning"), this.el.animate({
384
- [ot]: [-o, 0]
365
+ [A]: [-o, 0]
385
366
  }, {
386
367
  ...this.flow.spinTiming ?? this.flow.transformTiming,
387
368
  composite: "accumulate"
388
- }), this.flow.addEventListener("animationsfinish", n(this, st, "f"), { once: !0 }));
369
+ }), this.flow.addEventListener("animationsfinish", this._onAnimationsFinish, { once: !0 }));
389
370
  }
390
371
  get diff() {
391
372
  let t = this.flow.computedTrend;
392
- const e = this.value - n(this, v, "f");
393
- return !e && this.flow.startingPlace != null && this.flow.startingPlace >= this.place ? 10 * t : (t || (t = Math.sign(e)), t === b.DOWN && this.value > n(this, v, "f") ? this.value - 10 - n(this, v, "f") : t === b.UP && this.value < n(this, v, "f") ? 10 - n(this, v, "f") + this.value : e);
373
+ const e = this.value - this._prevValue;
374
+ return !e && this.flow.startingPos != null && this.flow.startingPos >= this.pos ? this._length * t : (t || (t = Math.sign(e)), t < 0 && this.value > this._prevValue ? this.value - this._length - this._prevValue : t > 0 && this.value < this._prevValue ? this._length - this._prevValue + this.value : e);
394
375
  }
395
376
  }
396
- F = /* @__PURE__ */ new WeakMap(), v = /* @__PURE__ */ new WeakMap(), K = /* @__PURE__ */ new WeakMap(), st = /* @__PURE__ */ new WeakMap();
397
- class Wt extends ut {
398
- constructor(t, e, i, a) {
399
- const l = S("span", {
377
+ class et extends B {
378
+ constructor(t, e, i, s) {
379
+ const a = g("span", {
400
380
  className: "symbol__value",
401
381
  textContent: i
402
382
  });
403
- super(t, i, S("span", {
383
+ super(t, i, g("span", {
404
384
  part: e,
405
385
  className: "symbol"
406
- }, [l]), a), this.type = e, _.set(this, /* @__PURE__ */ new Map()), Z.set(this, void 0), q.set(this, (r) => () => {
407
- n(this, _, "f").delete(r);
408
- }), n(this, _, "f").set(i, new nt(this.flow, l, {
409
- onRemove: n(this, q, "f").call(this, i)
386
+ }, [a]), s), this.type = e, this._children = /* @__PURE__ */ new Map(), this._onChildRemove = (r) => () => {
387
+ this._children.delete(r);
388
+ }, this._children.set(i, new R(this.flow, a, {
389
+ onRemove: this._onChildRemove(i)
410
390
  }));
411
391
  }
412
392
  willUpdate(t) {
413
393
  if (this.type === "decimal")
414
394
  return;
415
395
  const e = this.el.getBoundingClientRect();
416
- h(this, Z, e[this.section.justify] - t[this.section.justify], "f");
396
+ this._prevOffset = e[this.section.justify] - t[this.section.justify];
417
397
  }
418
398
  update(t) {
419
399
  if (this.value !== t) {
420
- const e = n(this, _, "f").get(this.value);
421
- if (e.present = !1, e.el.classList.add("symbol__exiting"), n(this, _, "f").has(t)) {
422
- const i = n(this, _, "f").get(t);
400
+ const e = this._children.get(this.value);
401
+ if (e.present = !1, e.el.classList.add("symbol__exiting"), this._children.has(t)) {
402
+ const i = this._children.get(t);
423
403
  i.present = !0, i.el.classList.remove("symbol__exiting");
424
404
  } else {
425
- const i = S("span", {
405
+ const i = g("span", {
426
406
  className: "symbol__value",
427
407
  textContent: t
428
408
  });
429
- this.el.appendChild(i), n(this, _, "f").set(t, new nt(this.flow, i, {
409
+ this.el.appendChild(i), this._children.set(t, new R(this.flow, i, {
430
410
  animateIn: !0,
431
- onRemove: n(this, q, "f").call(this, t)
411
+ onRemove: this._onChildRemove(t)
432
412
  }));
433
413
  }
434
414
  }
@@ -437,18 +417,17 @@ class Wt extends ut {
437
417
  didUpdate(t) {
438
418
  if (this.type === "decimal")
439
419
  return;
440
- const i = this.el.getBoundingClientRect()[this.section.justify] - t[this.section.justify], a = n(this, Z, "f") - i;
441
- a && this.el.animate({
442
- transform: [`translateX(${a}px)`, "none"]
420
+ const i = this.el.getBoundingClientRect()[this.section.justify] - t[this.section.justify], s = this._prevOffset - i;
421
+ s && this.el.animate({
422
+ transform: [`translateX(${s}px)`, "none"]
443
423
  }, { ...this.flow.transformTiming, composite: "accumulate" });
444
424
  }
445
425
  }
446
- _ = /* @__PURE__ */ new WeakMap(), Z = /* @__PURE__ */ new WeakMap(), q = /* @__PURE__ */ new WeakMap();
447
426
  export {
448
- Nt as NumberFlowLite,
449
- Mt as canAnimate,
450
- Ut as define,
451
- Lt as formatToData,
452
- et as prefersReducedMotion,
453
- Rt as renderInnerHTML
427
+ Q as NumberFlowLite,
428
+ J as canAnimate,
429
+ st as define,
430
+ nt as formatToData,
431
+ P as prefersReducedMotion,
432
+ ot as renderInnerHTML
454
433
  };
package/dist/styles.d.ts CHANGED
@@ -9,6 +9,5 @@ export declare const supportsAtProperty: boolean;
9
9
  export declare const charHeight = "var(--number-flow-char-height, 1em)";
10
10
  export declare const maskHeight = "var(--number-flow-mask-height, 0.25em)";
11
11
  export declare const halfMaskHeight = "calc(var(--number-flow-mask-height, 0.25em) / 2)";
12
- export declare const SlottedTag = "span";
13
12
  declare const styles: string;
14
13
  export default styles;
@@ -1 +1 @@
1
- export declare const max: (n1?: number, n2?: number) => number | null;
1
+ export declare const max: (n1?: number, n2?: number) => number | undefined;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.3.9",
6
+ "version": "0.4.0",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",