number-flow 0.4.0 → 0.4.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.
package/dist/index.d.ts CHANGED
@@ -20,6 +20,8 @@ export interface Props {
20
20
  continuous: boolean;
21
21
  digits: Digits | undefined;
22
22
  }
23
+ export interface NumberFlowLite extends Props {
24
+ }
23
25
  export declare class NumberFlowLite extends ServerSafeHTMLElement implements Props {
24
26
  /**
25
27
  * Use `private _private` properties instead of `#private` to avoid # polyfill and
@@ -28,27 +30,17 @@ export declare class NumberFlowLite extends ServerSafeHTMLElement implements Pro
28
30
  * supports e.g. https://github.com/microsoft/TypeScript/issues/37487
29
31
  */
30
32
  static defaultProps: Props;
31
- transformTiming: EffectTiming;
32
- spinTiming: EffectTiming | undefined;
33
- opacityTiming: EffectTiming;
34
- respectMotionPreference: boolean;
35
- trend: Trend;
36
- continuous: boolean;
37
- digits: Digits | undefined;
33
+ constructor();
38
34
  private _animated;
39
35
  get animated(): boolean;
40
36
  set animated(val: boolean);
41
- private _created;
42
- get created(): boolean;
37
+ readonly created: boolean;
43
38
  private _pre?;
44
39
  private _num?;
45
40
  private _post?;
46
- private _computedTrend?;
47
- get computedTrend(): number | undefined;
48
- private _startingPos?;
49
- get startingPos(): number | null | undefined;
50
- private _computedAnimated;
51
- get computedAnimated(): boolean;
41
+ readonly computedTrend?: number;
42
+ readonly startingPos?: number | null;
43
+ readonly computedAnimated: boolean;
52
44
  private _data?;
53
45
  manual: boolean;
54
46
  set data(data: Data | undefined);
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
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"
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,r]=Array.isArray(t)?[void 0,t]:[t,e];return s&&Object.assign(i,s),r==null||r.forEach(a=>i.appendChild(a)),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 r=[],a=[],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,a.push(...c.value.split("").map(E=>({type:l,value:parseInt(E)})))):l==="group"?a.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:r).push({type:l,value:c.value,key:p(l)})}const T=[];for(let c=a.length-1;c>=0;c--){const l=a[c];T.unshift(l.type==="integer"?{...l,key:p(l.type),pos:u[l.type]}:{...l,key:p(l.type)})}return{pre:r,integer:T,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",A="--_number-flow-d-width",C="--_number-flow-dx",U="--_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:A,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:U,syntax:"<number>",inherits:!0,initialValue:"0"}),!0}catch{return!1}})(),P="var(--number-flow-char-height, 1em)",m="var(--number-flow-mask-height, 0.25em)",x=`calc(${m} / 2)`,k="var(--number-flow-mask-width, 0.5em)",_=`calc(${k} / var(--scale-x))`,w="#000 0, transparent 71%",L=W`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${P} !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(${A}) / var(--width));transform:translateX(var(${C})) scaleX(var(--scale-x));margin:0 calc(-1 * ${k});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} ${k};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(${U})}.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:${P} !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
3
  ${Q}</style
4
4
  >${b(n.pre,"left")}<span part="number" class="number"
5
5
  >${b(n.integer,"integer")}${b(n.fraction,"fraction")}</span
6
6
  >${b(n.post,"right")}</template
7
7
  ><span
8
- style="font-kerning: none; display: inline-block; line-height: ${U} !important; padding: ${m} 0;"
8
+ style="font-kerning: none; display: inline-block; line-height: ${P} !important; padding: ${m} 0;"
9
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;
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(),this.created=!1,this.manual=!1;const{animated:t,...e}=this.constructor.defaultProps;this._animated=this.computedAnimated=t,Object.assign(this,e)}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()))}set data(t){if(t==null)return;const{pre:e,integer:i,fraction:s,post:r,value:a}=t;if(this.created){const o=this._data;if(this._data=t,this.computedTrend=typeof this.trend=="function"?this.trend(o.value,a):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(r),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,r,{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,...r}={}){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",{...r,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"],[A]:[s,0]},{...this.flow.transformTiming,composite:"accumulate"})}}class O{constructor(t,e,{justify:i,className:s,...r},a){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",{...r,className:`section section--justify-${i} ${s??""}`},a?a(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",r=s?"prepend":"append";if(z(t,a=>{let o;this.children.has(a.key)?(o=this.children.get(a.key),i.set(a,o),this.unpop(o),o.present=!0):(o=this.addChar(a,{startDigitsAtZero:!0,animateIn:!0}),e.set(a,o)),this.el[r](o.el)},{reverse:s}),this.flow.computedAnimated){const a=this.el.getBoundingClientRect();e.forEach(o=>{o.willUpdate(a)})}e.forEach((a,o)=>{a.update(o.value)}),i.forEach((a,o)=>{a.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(r=>r.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(r=>r.key===s)||e.set(s,i)}),this.pop(e),this.addNewAndUpdateExisting(t)}}class j{constructor(t,e,{onRemove:i,animateIn:s=!1}={}){this.flow=t,this.el=e,this._present=!0,this._remove=()=>{var r;this.el.remove(),(r=this._onRemove)==null||r.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 j{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,r){var u,p;const a=(((p=(u=t.flow.digits)==null?void 0:u[s])==null?void 0:p.max)??9)+1,o=Array.from({length:a}).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(a)),super(t,i,d,r),this.pos=s,this._onAnimationsFinish=()=>{this.el.classList.remove("is-spinning")},this._numbers=o,this._length=a}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,r=this.section.justify==="left"?i+s:i-s,a=this._prevCenter-r;a&&this.el.animate({transform:[`translateX(${a}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"});const o=this.diff;o&&(this.el.classList.add("is-spinning"),this.el.animate({[U]:[-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 r=g("span",{className:"symbol__value",textContent:i});super(t,i,g("span",{part:e,className:"symbol"},[r]),s),this.type=e,this._children=new Map,this._onChildRemove=a=>()=>{this._children.delete(a)},this._children.set(i,new j(this.flow,r,{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 j(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,7 +1,7 @@
1
1
  import { BROWSER as w } from "esm-env";
2
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;
3
+ const i = document.createElement(n), [s, r] = Array.isArray(t) ? [void 0, t] : [t, e];
4
+ return s && Object.assign(i, s), r == null || r.forEach((a) => i.appendChild(a)), i;
5
5
  }, H = (n, t) => {
6
6
  var e;
7
7
  return t === "left" ? n.offsetLeft : (((e = n.offsetParent instanceof HTMLElement ? n.offsetParent : null) == null ? void 0 : e.offsetWidth) ?? 0) - n.offsetWidth - n.offsetLeft;
@@ -17,26 +17,26 @@ function D(n, t, { reverse: e = !1 } = {}) {
17
17
  function nt(n, t, e, i) {
18
18
  const s = t.formatToParts(n);
19
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}`;
20
+ const r = [], a = [], o = [], d = [], u = {}, p = (c) => `${c}:${u[c] = (u[c] ?? -1) + 1}`;
21
21
  let y = "", h = !1, f = !1;
22
22
  for (const c of s) {
23
23
  y += c.value;
24
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) => ({
25
+ l === "integer" ? (h = !0, a.push(...c.value.split("").map((k) => ({ type: l, value: parseInt(k) })))) : l === "group" ? a.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
26
  type: l,
27
27
  value: parseInt(k),
28
28
  key: p(l),
29
29
  pos: -1 - u[l]
30
- }))) : (h || f ? d : a).push({
30
+ }))) : (h || f ? d : r).push({
31
31
  type: l,
32
32
  value: c.value,
33
33
  key: p(l)
34
34
  });
35
35
  }
36
- const j = [];
37
- for (let c = r.length - 1; c >= 0; c--) {
38
- const l = r[c];
39
- j.unshift(l.type === "integer" ? {
36
+ const L = [];
37
+ for (let c = a.length - 1; c >= 0; c--) {
38
+ const l = a[c];
39
+ L.unshift(l.type === "integer" ? {
40
40
  ...l,
41
41
  key: p(l.type),
42
42
  pos: u[l.type]
@@ -46,15 +46,15 @@ function nt(n, t, e, i) {
46
46
  });
47
47
  }
48
48
  return {
49
- pre: a,
50
- integer: j,
49
+ pre: r,
50
+ integer: L,
51
51
  fraction: o,
52
52
  post: d,
53
53
  valueAsString: y,
54
54
  value: typeof n == "string" ? parseFloat(n) : n
55
55
  };
56
56
  }
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 = (() => {
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)"), E = w && typeof matchMedia < "u" ? matchMedia("(prefers-reduced-motion: reduce)") : null, x = "--_number-flow-d-opacity", j = "--_number-flow-d-width", C = "--_number-flow-dx", A = "--_number-flow-d", Y = (() => {
58
58
  try {
59
59
  return CSS.registerProperty({
60
60
  name: x,
@@ -67,7 +67,7 @@ const I = String.raw, V = String.raw, O = w && typeof CSS < "u" && CSS.supports
67
67
  inherits: !0,
68
68
  initialValue: "0px"
69
69
  }), CSS.registerProperty({
70
- name: T,
70
+ name: j,
71
71
  syntax: "<number>",
72
72
  inherits: !1,
73
73
  initialValue: "0"
@@ -80,8 +80,8 @@ const I = String.raw, V = String.raw, O = w && typeof CSS < "u" && CSS.supports
80
80
  } catch {
81
81
  return !1;
82
82
  }
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) => (
83
+ })(), P = "var(--number-flow-char-height, 1em)", m = "var(--number-flow-mask-height, 0.25em)", S = `calc(${m} / 2)`, R = "var(--number-flow-mask-width, 0.5em)", _ = `calc(${R} / var(--scale-x))`, v = "#000 0, transparent 71%", T = V`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${P} !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(${j}) / var(--width));transform:translateX(var(${C})) scaleX(var(--scale-x));margin:0 calc(-1 * ${R});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} ${R};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:${P} !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) => (
85
85
  // shadowroot="open" non-standard attribute for old Chrome:
86
86
  I`<template shadowroot="open" shadowrootmode="open"
87
87
  ><style>
@@ -90,14 +90,16 @@ const I = String.raw, V = String.raw, O = w && typeof CSS < "u" && CSS.supports
90
90
  >${b(n.integer, "integer")}${b(n.fraction, "fraction")}</span
91
91
  >${b(n.post, "right")}</template
92
92
  ><span
93
- style="font-kerning: none; display: inline-block; line-height: ${U} !important; padding: ${m} 0;"
93
+ style="font-kerning: none; display: inline-block; line-height: ${P} !important; padding: ${m} 0;"
94
94
  >${n.valueAsString}</span
95
95
  >`
96
96
  ), q = (n, t) => n == null ? t : t == null ? n : Math.max(n, t), J = z && O && Y;
97
97
  let $;
98
98
  class Q extends G {
99
99
  constructor() {
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;
100
+ super(), this.created = !1, this.manual = !1;
101
+ const { animated: t, ...e } = this.constructor.defaultProps;
102
+ this._animated = this.computedAnimated = t, Object.assign(this, e);
101
103
  }
102
104
  get animated() {
103
105
  return this._animated;
@@ -106,52 +108,40 @@ class Q extends G {
106
108
  var e;
107
109
  this.animated !== t && (this._animated = t, (e = this.shadowRoot) == null || e.getAnimations().forEach((i) => i.finish()));
108
110
  }
109
- get created() {
110
- return this._created;
111
- }
112
- get computedTrend() {
113
- return this._computedTrend;
114
- }
115
- get startingPos() {
116
- return this._startingPos;
117
- }
118
- get computedAnimated() {
119
- return this._computedAnimated;
120
- }
121
111
  set data(t) {
122
112
  if (t == null)
123
113
  return;
124
- const { pre: e, integer: i, fraction: s, post: a, value: r } = t;
125
- if (this._created) {
114
+ const { pre: e, integer: i, fraction: s, post: r, value: a } = t;
115
+ if (this.created) {
126
116
  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) {
117
+ if (this._data = t, this.computedTrend = typeof this.trend == "function" ? this.trend(o.value, a) : this.trend, this.startingPos = void 0, this.computedTrend && this.continuous) {
128
118
  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);
119
+ this.startingPos = q(p == null ? void 0 : p.pos, y == null ? void 0 : y.pos);
130
120
  }
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();
121
+ this.computedAnimated = J && this._animated && (!this.respectMotionPreference || !(E != null && E.matches)) && // https://github.com/barvian/number-flow/issues/9
122
+ X(this), this.manual || this.willUpdate(), this._pre.update(e), this._num.update({ integer: i, fraction: s }), this._post.update(r), this.manual || this.didUpdate();
133
123
  } else {
134
124
  if (this._data = t, this.attachShadow({ mode: "open" }), typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
135
- $ || ($ = new CSSStyleSheet(), $.replaceSync(L)), this.shadowRoot.adoptedStyleSheets = [$];
125
+ $ || ($ = new CSSStyleSheet(), $.replaceSync(T)), this.shadowRoot.adoptedStyleSheets = [$];
136
126
  else {
137
127
  const o = document.createElement("style");
138
- o.textContent = L, this.shadowRoot.appendChild(o);
128
+ o.textContent = T, this.shadowRoot.appendChild(o);
139
129
  }
140
130
  this._pre = new N(this, e, {
141
131
  justify: "right",
142
132
  part: "left"
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, {
133
+ }), this.shadowRoot.appendChild(this._pre.el), this._num = new tt(this, i, s), this.shadowRoot.appendChild(this._num.el), this._post = new N(this, r, {
144
134
  justify: "left",
145
135
  part: "right"
146
136
  }), this.shadowRoot.appendChild(this._post.el);
147
137
  }
148
- this._created = !0;
138
+ this.created = !0;
149
139
  }
150
140
  willUpdate() {
151
141
  this._pre.willUpdate(), this._num.willUpdate(), this._post.willUpdate();
152
142
  }
153
143
  didUpdate() {
154
- if (!this._computedAnimated)
144
+ if (!this.computedAnimated)
155
145
  return;
156
146
  this._abortAnimationsFinish ? this._abortAnimationsFinish.abort() : this.dispatchEvent(new Event("animationsstart")), this._pre.didUpdate(), this._num.didUpdate(), this._post.didUpdate();
157
147
  const t = new AbortController();
@@ -175,7 +165,7 @@ Q.defaultProps = {
175
165
  digits: void 0
176
166
  };
177
167
  class tt {
178
- constructor(t, e, i, { className: s, ...a } = {}) {
168
+ constructor(t, e, i, { className: s, ...r } = {}) {
179
169
  this.flow = t, this._integer = new M(t, e, {
180
170
  justify: "right",
181
171
  part: "integer"
@@ -185,7 +175,7 @@ class tt {
185
175
  }), this._inner = g("span", {
186
176
  className: "number__inner"
187
177
  }, [this._integer.el, this._fraction.el]), this.el = g("span", {
188
- ...a,
178
+ ...r,
189
179
  part: "number",
190
180
  className: `number ${s ?? ""}`
191
181
  }, [this._inner]);
@@ -202,7 +192,7 @@ class tt {
202
192
  const e = this._prevLeft - t.left, i = this.el.offsetWidth, s = this._prevWidth - i;
203
193
  this.el.style.setProperty("--width", String(i)), this.el.animate({
204
194
  [C]: [`${e}px`, "0px"],
205
- [T]: [s, 0]
195
+ [j]: [s, 0]
206
196
  }, {
207
197
  ...this.flow.transformTiming,
208
198
  composite: "accumulate"
@@ -210,15 +200,15 @@ class tt {
210
200
  }
211
201
  }
212
202
  class W {
213
- constructor(t, e, { justify: i, className: s, ...a }, r) {
203
+ constructor(t, e, { justify: i, className: s, ...r }, a) {
214
204
  this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (d) => () => {
215
205
  this.children.delete(d);
216
206
  }, this.justify = i;
217
207
  const o = e.map((d) => this.addChar(d).el);
218
208
  this.el = g("span", {
219
- ...a,
209
+ ...r,
220
210
  className: `section section--justify-${i} ${s ?? ""}`
221
- }, r ? r(o) : o);
211
+ }, a ? a(o) : o);
222
212
  }
223
213
  addChar(t, { startDigitsAtZero: e = !1, ...i } = {}) {
224
214
  const s = t.type === "integer" || t.type === "fraction" ? new F(this, t.type, e ? 0 : t.value, t.pos, {
@@ -241,20 +231,20 @@ class W {
241
231
  });
242
232
  }
243
233
  addNewAndUpdateExisting(t) {
244
- const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), s = this.justify === "left", a = s ? "prepend" : "append";
245
- if (D(t, (r) => {
234
+ const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), s = this.justify === "left", r = s ? "prepend" : "append";
235
+ if (D(t, (a) => {
246
236
  let o;
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);
237
+ this.children.has(a.key) ? (o = this.children.get(a.key), i.set(a, o), this.unpop(o), o.present = !0) : (o = this.addChar(a, { startDigitsAtZero: !0, animateIn: !0 }), e.set(a, o)), this.el[r](o.el);
248
238
  }, { reverse: s }), this.flow.computedAnimated) {
249
- const r = this.el.getBoundingClientRect();
239
+ const a = this.el.getBoundingClientRect();
250
240
  e.forEach((o) => {
251
- o.willUpdate(r);
241
+ o.willUpdate(a);
252
242
  });
253
243
  }
254
- e.forEach((r, o) => {
255
- r.update(o.value);
256
- }), i.forEach((r, o) => {
257
- r.update(o.value);
244
+ e.forEach((a, o) => {
245
+ a.update(o.value);
246
+ }), i.forEach((a, o) => {
247
+ a.update(o.value);
258
248
  });
259
249
  }
260
250
  willUpdate() {
@@ -277,7 +267,7 @@ class M extends W {
277
267
  update(t) {
278
268
  const e = /* @__PURE__ */ new Map();
279
269
  this.children.forEach((i, s) => {
280
- t.find((a) => a.key === s) || e.set(s, i), this.unpop(i);
270
+ t.find((r) => r.key === s) || e.set(s, i), this.unpop(i);
281
271
  }), this.addNewAndUpdateExisting(t), e.forEach((i) => {
282
272
  i instanceof F && i.update(0);
283
273
  }), this.pop(e);
@@ -287,15 +277,15 @@ class N extends W {
287
277
  update(t) {
288
278
  const e = /* @__PURE__ */ new Map();
289
279
  this.children.forEach((i, s) => {
290
- t.find((a) => a.key === s) || e.set(s, i);
280
+ t.find((r) => r.key === s) || e.set(s, i);
291
281
  }), this.pop(e), this.addNewAndUpdateExisting(t);
292
282
  }
293
283
  }
294
- class R {
284
+ class U {
295
285
  constructor(t, e, { onRemove: i, animateIn: s = !1 } = {}) {
296
286
  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);
287
+ var r;
288
+ this.el.remove(), (r = this._onRemove) == null || r.call(this);
299
289
  }, this.el.classList.add("animate-presence"), this.flow.computedAnimated && s && this.el.animate({
300
290
  [x]: [-0.9999, 0]
301
291
  }, {
@@ -323,24 +313,24 @@ class R {
323
313
  }
324
314
  }
325
315
  }
326
- class B extends R {
316
+ class B extends U {
327
317
  constructor(t, e, i, s) {
328
318
  super(t.flow, i, s), this.section = t, this.value = e, this.el = i;
329
319
  }
330
320
  }
331
321
  class F extends B {
332
- constructor(t, e, i, s, a) {
322
+ constructor(t, e, i, s, r) {
333
323
  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) => {
324
+ const a = (((p = (u = t.flow.digits) == null ? void 0 : u[s]) == null ? void 0 : p.max) ?? 9) + 1, o = Array.from({ length: a }).map((y, h) => {
335
325
  const f = g("span", { className: `digit__num${h === i ? " is-current" : ""}` }, [document.createTextNode(String(h))]);
336
326
  return f.style.setProperty("--n", String(h)), f;
337
327
  }), d = g("span", {
338
328
  part: `digit ${e}-digit`,
339
329
  className: "digit"
340
330
  }, o);
341
- d.style.setProperty("--current", String(i)), d.style.setProperty("--length", String(r)), super(t, i, d, a), this.pos = s, this._onAnimationsFinish = () => {
331
+ d.style.setProperty("--current", String(i)), d.style.setProperty("--length", String(a)), super(t, i, d, r), this.pos = s, this._onAnimationsFinish = () => {
342
332
  this.el.classList.remove("is-spinning");
343
- }, this._numbers = o, this._length = r;
333
+ }, this._numbers = o, this._length = a;
344
334
  }
345
335
  willUpdate(t) {
346
336
  const e = this.el.getBoundingClientRect();
@@ -353,9 +343,9 @@ class F extends B {
353
343
  (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;
354
344
  }
355
345
  didUpdate(t) {
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;
357
- r && this.el.animate({
358
- transform: [`translateX(${r}px)`, "none"]
346
+ const e = this.el.getBoundingClientRect(), i = e[this.section.justify] - t[this.section.justify], s = e.width / 2, r = this.section.justify === "left" ? i + s : i - s, a = this._prevCenter - r;
347
+ a && this.el.animate({
348
+ transform: [`translateX(${a}px)`, "none"]
359
349
  }, {
360
350
  ...this.flow.transformTiming,
361
351
  composite: "accumulate"
@@ -376,16 +366,16 @@ class F extends B {
376
366
  }
377
367
  class et extends B {
378
368
  constructor(t, e, i, s) {
379
- const a = g("span", {
369
+ const r = g("span", {
380
370
  className: "symbol__value",
381
371
  textContent: i
382
372
  });
383
373
  super(t, i, g("span", {
384
374
  part: e,
385
375
  className: "symbol"
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, {
376
+ }, [r]), s), this.type = e, this._children = /* @__PURE__ */ new Map(), this._onChildRemove = (a) => () => {
377
+ this._children.delete(a);
378
+ }, this._children.set(i, new U(this.flow, r, {
389
379
  onRemove: this._onChildRemove(i)
390
380
  }));
391
381
  }
@@ -406,7 +396,7 @@ class et extends B {
406
396
  className: "symbol__value",
407
397
  textContent: t
408
398
  });
409
- this.el.appendChild(i), this._children.set(t, new R(this.flow, i, {
399
+ this.el.appendChild(i), this._children.set(t, new U(this.flow, i, {
410
400
  animateIn: !0,
411
401
  onRemove: this._onChildRemove(t)
412
402
  }));
@@ -428,6 +418,6 @@ export {
428
418
  J as canAnimate,
429
419
  st as define,
430
420
  nt as formatToData,
431
- P as prefersReducedMotion,
421
+ E as prefersReducedMotion,
432
422
  ot as renderInnerHTML
433
423
  };
@@ -0,0 +1,3 @@
1
+ export type Mutable<T> = {
2
+ -readonly [P in keyof T]: T[P];
3
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.4.0",
6
+ "version": "0.4.1",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",