number-flow 0.3.10 → 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/formatter.d.ts +1 -1
- package/dist/index.d.ts +15 -23
- package/dist/index.js +8 -8
- package/dist/index.mjs +155 -167
- package/dist/util/math.d.ts +1 -1
- package/dist/util/types.d.ts +3 -0
- package/package.json +1 -1
package/dist/formatter.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ type KeyedPart = {
|
|
|
17
17
|
key: NumberPartKey;
|
|
18
18
|
};
|
|
19
19
|
export type KeyedDigitPart = DigitPart & KeyedPart & {
|
|
20
|
-
|
|
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,50 +5,42 @@ export { prefersReducedMotion } from './styles';
|
|
|
5
5
|
export { renderInnerHTML } from './ssr';
|
|
6
6
|
export * from './formatter';
|
|
7
7
|
export declare const canAnimate: boolean;
|
|
8
|
-
type
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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:
|
|
19
|
+
trend: Trend;
|
|
22
20
|
continuous: boolean;
|
|
21
|
+
digits: Digits | undefined;
|
|
22
|
+
}
|
|
23
|
+
export interface NumberFlowLite extends Props {
|
|
23
24
|
}
|
|
24
25
|
export declare class NumberFlowLite extends ServerSafeHTMLElement implements Props {
|
|
25
26
|
/**
|
|
26
|
-
* Use `private _private` properties instead of `
|
|
27
|
+
* Use `private _private` properties instead of `#private` to avoid # polyfill and
|
|
27
28
|
* reduce bundle size. Also, use `readonly` properties instead of getters to save on bundle
|
|
28
29
|
* size, even though you have to do gross stuff like `(this as Mutable<...>)` until TS
|
|
29
30
|
* supports e.g. https://github.com/microsoft/TypeScript/issues/37487
|
|
30
31
|
*/
|
|
31
32
|
static defaultProps: Props;
|
|
32
|
-
|
|
33
|
-
spinTiming: EffectTiming | undefined;
|
|
34
|
-
opacityTiming: EffectTiming;
|
|
35
|
-
respectMotionPreference: boolean;
|
|
36
|
-
trend: RawTrend;
|
|
37
|
-
continuous: boolean;
|
|
33
|
+
constructor();
|
|
38
34
|
private _animated;
|
|
39
35
|
get animated(): boolean;
|
|
40
36
|
set animated(val: boolean);
|
|
41
|
-
|
|
42
|
-
get created(): boolean;
|
|
37
|
+
readonly created: boolean;
|
|
43
38
|
private _pre?;
|
|
44
39
|
private _num?;
|
|
45
40
|
private _post?;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
get startingPlace(): 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"),
|
|
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
|
-
${
|
|
4
|
-
>${
|
|
5
|
-
>${
|
|
6
|
-
>${
|
|
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: ${
|
|
9
|
-
>${
|
|
10
|
-
>`,st=(s,t)=>s!=null&&t==null?s:s==null&&t!=null?t:s!=null&&t!=null?Math.max(s,t):null,V=Z&&K&&J;var g;(function(s){s[s.UP=1]="UP",s[s.DOWN=-1]="DOWN",s[s.NONE=0]="NONE"})(g||(g={}));let $;class B extends Q{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._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 startingPlace(){return this._startingPlace}get computedAnimated(){return this._computedAnimated}set data(t){if(t==null)return;const{pre:e,integer:i,fraction:n,post:r,value:o}=t;if(this._created){const a=this._data;if(this._data=t,this.trend===!0?this._computedTrend=Math.sign(o-a.value):this.trend==="increasing"?this._computedTrend=g.UP:this.trend==="decreasing"?this._computedTrend=g.DOWN:this._computedTrend=g.NONE,this._startingPlace=void 0,this._computedTrend!==g.NONE&&this.continuous){const u=a.integer.concat(a.fraction).filter(h=>h.type==="integer"||h.type==="fraction"),p=t.integer.concat(t.fraction).filter(h=>h.type==="integer"||h.type==="fraction"),d=u.find(h=>!p.find(m=>m.place===h.place&&m.value===h.value)),w=p.find(h=>!u.find(m=>h.place===m.place&&h.value===m.value));this._startingPlace=st(d==null?void 0:d.place,w==null?void 0:w.place)}this._computedAnimated=V&&this._animated&&(!this.respectMotionPreference||!(x!=null&&x.matches))&&I(this),this.manual||this.willUpdate(),this._pre.update(e),this._num.update({integer:i,fraction:n}),this._post.update(r),this.manual||this.didUpdate()}else{if(this._data=t,this.attachShadow({mode:"open"}),typeof CSSStyleSheet<"u"&&this.shadowRoot.adoptedStyleSheets)$||($=new CSSStyleSheet,$.replaceSync(L)),this.shadowRoot.adoptedStyleSheets=[$];else{const a=document.createElement("style");a.textContent=L,this.shadowRoot.appendChild(a)}this._pre=new W(this,e,{justify:"right",part:"left"}),this.shadowRoot.appendChild(this._pre.el),this._num=new nt(this,i,n),this.shadowRoot.appendChild(this._num.el),this._post=new W(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:!0,continuous:!1,respectMotionPreference:!0};class nt{constructor(t,e,i,{className:n,...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=y("span",{className:"number__inner"},[this._integer.el,this._fraction.el]),this.el=y("span",{...r,part:"number",className:`number ${n??""}`},[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,n=this._prevWidth-i;this.el.style.setProperty("--width",String(i)),this.el.animate({[C]:[`${e}px`,"0px"],[U]:[n,0]},{...this.flow.transformTiming,composite:"accumulate"})}}class D{constructor(t,e,{justify:i,className:n,...r},o){this.flow=t,this.children=new Map,this.onCharRemove=u=>()=>{this.children.delete(u)},this.justify=i;const a=e.map(u=>this.addChar(u).el);this.el=y("span",{...r,className:`section section--justify-${i} ${n??""}`},o?o(a):a)}addChar(t,{startDigitsAtZero:e=!1,...i}={}){const n=t.type==="integer"||t.type==="fraction"?new H(this,t.type,e?0:t.value,t.place,{...i,onRemove:this.onCharRemove(t.key)}):new at(this,t.type,t.value,{...i,onRemove:this.onCharRemove(t.key)});return this.children.set(t.key,n),n}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,n=this.justify==="left",r=n?"prepend":"append";if(Y(t,o=>{let a;this.children.has(o.key)?(a=this.children.get(o.key),i.set(o,a),this.unpop(a),a.present=!0):(a=this.addChar(o,{startDigitsAtZero:!0,animateIn:!0}),e.set(o,a)),this.el[r](a.el)},{reverse:n}),this.flow.computedAnimated){const o=this.el.getBoundingClientRect();e.forEach(a=>{a.willUpdate(o)})}e.forEach((o,a)=>{o.update(a.value)}),i.forEach((o,a)=>{o.update(a.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(n=>n.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 D{update(t){const e=new Map;this.children.forEach((i,n)=>{t.find(r=>r.key===n)||e.set(n,i),this.unpop(i)}),this.addNewAndUpdateExisting(t),e.forEach(i=>{i instanceof H&&i.update(0)}),this.pop(e)}}class W extends D{update(t){const e=new Map;this.children.forEach((i,n)=>{t.find(r=>r.key===n)||e.set(n,i)}),this.pop(e),this.addNewAndUpdateExisting(t)}}class T{constructor(t,e,{onRemove:i,animateIn:n=!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&&n&&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 T{constructor(t,e,i,n){super(t.flow,i,n),this.section=t,this.value=e,this.el=i}}class H extends F{constructor(t,e,i,n,r){const o=Array.from({length:10}).map((u,p)=>{const d=y("span",{className:`digit__num${p===i?" is-current":""}`},[document.createTextNode(String(p))]);return d.style.setProperty("--n",String(p)),d}),a=y("span",{part:`digit ${e}-digit`,className:"digit"},o);a.style.setProperty("--current",String(i)),super(t,i,a,r),this.place=n,this._onAnimationsFinish=()=>{this.el.classList.remove("is-spinning")},this._numbers=o}willUpdate(t){const e=this.el.getBoundingClientRect();this._prevValue=this.value;const i=e[this.section.justify]-t[this.section.justify],n=e.width/2;this._prevCenter=this.section.justify==="left"?i+n:i-n}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],n=e.width/2,r=this.section.justify==="left"?i+n:i-n,o=this._prevCenter-r;o&&this.el.animate({transform:[`translateX(${o}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"});const a=this.diff;a&&(this.el.classList.add("is-spinning"),this.el.animate({[A]:[-a,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.startingPlace!=null&&this.flow.startingPlace>=this.place?10*t:(t||(t=Math.sign(e)),t===g.DOWN&&this.value>this._prevValue?this.value-10-this._prevValue:t===g.UP&&this.value<this._prevValue?10-this._prevValue+this.value:e)}}class at extends F{constructor(t,e,i,n){const r=y("span",{className:"symbol__value",textContent:i});super(t,i,y("span",{part:e,className:"symbol"},[r]),n),this.type=e,this._children=new Map,this._onChildRemove=o=>()=>{this._children.delete(o)},this._children.set(i,new T(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=y("span",{className:"symbol__value",textContent:t});this.el.appendChild(i),this._children.set(t,new T(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],n=this._prevOffset-i;n&&this.el.animate({transform:[`translateX(${n}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"})}}exports.NumberFlowLite=B;exports.canAnimate=V;exports.define=z;exports.formatToData=q;exports.prefersReducedMotion=x;exports.renderInnerHTML=it;
|
|
8
|
+
style="font-kerning: none; display: inline-block; line-height: ${P} !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(),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,78 +1,78 @@
|
|
|
1
|
-
import { BROWSER as
|
|
2
|
-
const
|
|
3
|
-
const i = document.createElement(
|
|
4
|
-
return
|
|
5
|
-
},
|
|
1
|
+
import { BROWSER as w } from "esm-env";
|
|
2
|
+
const g = (n, t, e) => {
|
|
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
|
+
}, H = (n, t) => {
|
|
6
6
|
var e;
|
|
7
|
-
return t === "left" ?
|
|
8
|
-
},
|
|
9
|
-
if (!(!
|
|
10
|
-
return customElements.define(
|
|
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);
|
|
11
11
|
};
|
|
12
|
-
function
|
|
13
|
-
const i =
|
|
14
|
-
for (let
|
|
15
|
-
t(s
|
|
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);
|
|
16
16
|
}
|
|
17
|
-
function
|
|
18
|
-
const
|
|
19
|
-
e &&
|
|
20
|
-
const r = [],
|
|
21
|
-
let
|
|
22
|
-
for (const c of
|
|
23
|
-
|
|
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 r = [], a = [], 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
24
|
const l = c.type === "minusSign" || c.type === "plusSign" ? "sign" : c.type;
|
|
25
|
-
l === "integer" ? (h = !0,
|
|
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
|
-
value: parseInt(
|
|
28
|
-
key:
|
|
29
|
-
|
|
30
|
-
}))) : (h ||
|
|
27
|
+
value: parseInt(k),
|
|
28
|
+
key: p(l),
|
|
29
|
+
pos: -1 - u[l]
|
|
30
|
+
}))) : (h || f ? d : r).push({
|
|
31
31
|
type: l,
|
|
32
32
|
value: c.value,
|
|
33
|
-
key:
|
|
33
|
+
key: p(l)
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
-
const
|
|
37
|
-
for (let c =
|
|
38
|
-
const l =
|
|
39
|
-
|
|
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
|
-
key:
|
|
42
|
-
|
|
41
|
+
key: p(l.type),
|
|
42
|
+
pos: u[l.type]
|
|
43
43
|
} : {
|
|
44
44
|
...l,
|
|
45
|
-
key:
|
|
45
|
+
key: p(l.type)
|
|
46
46
|
});
|
|
47
47
|
}
|
|
48
48
|
return {
|
|
49
49
|
pre: r,
|
|
50
|
-
integer:
|
|
51
|
-
fraction:
|
|
52
|
-
post:
|
|
53
|
-
valueAsString:
|
|
54
|
-
value: typeof
|
|
50
|
+
integer: L,
|
|
51
|
+
fraction: o,
|
|
52
|
+
post: d,
|
|
53
|
+
valueAsString: y,
|
|
54
|
+
value: typeof n == "string" ? parseFloat(n) : n
|
|
55
55
|
};
|
|
56
56
|
}
|
|
57
|
-
const I = String.raw, V = String.raw,
|
|
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
|
-
name:
|
|
60
|
+
name: x,
|
|
61
61
|
syntax: "<number>",
|
|
62
62
|
inherits: !1,
|
|
63
63
|
initialValue: "0"
|
|
64
64
|
}), CSS.registerProperty({
|
|
65
|
-
name:
|
|
65
|
+
name: C,
|
|
66
66
|
syntax: "<length>",
|
|
67
67
|
inherits: !0,
|
|
68
68
|
initialValue: "0px"
|
|
69
69
|
}), CSS.registerProperty({
|
|
70
|
-
name:
|
|
70
|
+
name: j,
|
|
71
71
|
syntax: "<number>",
|
|
72
72
|
inherits: !1,
|
|
73
73
|
initialValue: "0"
|
|
74
74
|
}), CSS.registerProperty({
|
|
75
|
-
name:
|
|
75
|
+
name: A,
|
|
76
76
|
syntax: "<number>",
|
|
77
77
|
inherits: !0,
|
|
78
78
|
initialValue: "0"
|
|
@@ -80,28 +80,26 @@ const I = String.raw, V = String.raw, z = v && typeof CSS < "u" && CSS.supports
|
|
|
80
80
|
} catch {
|
|
81
81
|
return !1;
|
|
82
82
|
}
|
|
83
|
-
})(),
|
|
84
|
-
},
|
|
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>
|
|
88
|
-
${
|
|
89
|
-
>${
|
|
90
|
-
>${
|
|
91
|
-
>${
|
|
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
|
|
92
92
|
><span
|
|
93
|
-
style="font-kerning: none; display: inline-block; line-height: ${
|
|
94
|
-
>${
|
|
93
|
+
style="font-kerning: none; display: inline-block; line-height: ${P} !important; padding: ${m} 0;"
|
|
94
|
+
>${n.valueAsString}</span
|
|
95
95
|
>`
|
|
96
|
-
),
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
s[s.UP = 1] = "UP", s[s.DOWN = -1] = "DOWN", s[s.NONE = 0] = "NONE";
|
|
100
|
-
})(g || (g = {}));
|
|
101
|
-
let S;
|
|
102
|
-
class tt extends K {
|
|
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 {
|
|
103
99
|
constructor() {
|
|
104
|
-
super(
|
|
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);
|
|
105
103
|
}
|
|
106
104
|
get animated() {
|
|
107
105
|
return this._animated;
|
|
@@ -110,52 +108,40 @@ class tt extends K {
|
|
|
110
108
|
var e;
|
|
111
109
|
this.animated !== t && (this._animated = t, (e = this.shadowRoot) == null || e.getAnimations().forEach((i) => i.finish()));
|
|
112
110
|
}
|
|
113
|
-
get created() {
|
|
114
|
-
return this._created;
|
|
115
|
-
}
|
|
116
|
-
get computedTrend() {
|
|
117
|
-
return this._computedTrend;
|
|
118
|
-
}
|
|
119
|
-
get startingPlace() {
|
|
120
|
-
return this._startingPlace;
|
|
121
|
-
}
|
|
122
|
-
get computedAnimated() {
|
|
123
|
-
return this._computedAnimated;
|
|
124
|
-
}
|
|
125
111
|
set data(t) {
|
|
126
112
|
if (t == null)
|
|
127
113
|
return;
|
|
128
|
-
const { pre: e, integer: i, fraction:
|
|
129
|
-
if (this.
|
|
130
|
-
const
|
|
131
|
-
if (this._data = t, this.
|
|
132
|
-
const
|
|
133
|
-
this.
|
|
114
|
+
const { pre: e, integer: i, fraction: s, post: r, value: a } = t;
|
|
115
|
+
if (this.created) {
|
|
116
|
+
const o = this._data;
|
|
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) {
|
|
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));
|
|
119
|
+
this.startingPos = q(p == null ? void 0 : p.pos, y == null ? void 0 : y.pos);
|
|
134
120
|
}
|
|
135
|
-
this.
|
|
136
|
-
|
|
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();
|
|
137
123
|
} else {
|
|
138
124
|
if (this._data = t, this.attachShadow({ mode: "open" }), typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
|
|
139
|
-
|
|
125
|
+
$ || ($ = new CSSStyleSheet(), $.replaceSync(T)), this.shadowRoot.adoptedStyleSheets = [$];
|
|
140
126
|
else {
|
|
141
|
-
const
|
|
142
|
-
|
|
127
|
+
const o = document.createElement("style");
|
|
128
|
+
o.textContent = T, this.shadowRoot.appendChild(o);
|
|
143
129
|
}
|
|
144
|
-
this._pre = new
|
|
130
|
+
this._pre = new N(this, e, {
|
|
145
131
|
justify: "right",
|
|
146
132
|
part: "left"
|
|
147
|
-
}), this.shadowRoot.appendChild(this._pre.el), this._num = new
|
|
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, {
|
|
148
134
|
justify: "left",
|
|
149
135
|
part: "right"
|
|
150
136
|
}), this.shadowRoot.appendChild(this._post.el);
|
|
151
137
|
}
|
|
152
|
-
this.
|
|
138
|
+
this.created = !0;
|
|
153
139
|
}
|
|
154
140
|
willUpdate() {
|
|
155
141
|
this._pre.willUpdate(), this._num.willUpdate(), this._post.willUpdate();
|
|
156
142
|
}
|
|
157
143
|
didUpdate() {
|
|
158
|
-
if (!this.
|
|
144
|
+
if (!this.computedAnimated)
|
|
159
145
|
return;
|
|
160
146
|
this._abortAnimationsFinish ? this._abortAnimationsFinish.abort() : this.dispatchEvent(new Event("animationsstart")), this._pre.didUpdate(), this._num.didUpdate(), this._post.didUpdate();
|
|
161
147
|
const t = new AbortController();
|
|
@@ -164,7 +150,7 @@ class tt extends K {
|
|
|
164
150
|
}), this._abortAnimationsFinish = t;
|
|
165
151
|
}
|
|
166
152
|
}
|
|
167
|
-
|
|
153
|
+
Q.defaultProps = {
|
|
168
154
|
transformTiming: {
|
|
169
155
|
duration: 900,
|
|
170
156
|
// Make sure to keep this minified:
|
|
@@ -173,24 +159,25 @@ tt.defaultProps = {
|
|
|
173
159
|
spinTiming: void 0,
|
|
174
160
|
opacityTiming: { duration: 450, easing: "ease-out" },
|
|
175
161
|
animated: !0,
|
|
176
|
-
trend:
|
|
162
|
+
trend: (n, t) => Math.sign(t - n),
|
|
177
163
|
continuous: !1,
|
|
178
|
-
respectMotionPreference: !0
|
|
164
|
+
respectMotionPreference: !0,
|
|
165
|
+
digits: void 0
|
|
179
166
|
};
|
|
180
|
-
class
|
|
181
|
-
constructor(t, e, i, { className:
|
|
167
|
+
class tt {
|
|
168
|
+
constructor(t, e, i, { className: s, ...r } = {}) {
|
|
182
169
|
this.flow = t, this._integer = new M(t, e, {
|
|
183
170
|
justify: "right",
|
|
184
171
|
part: "integer"
|
|
185
172
|
}), this._fraction = new M(t, i, {
|
|
186
173
|
justify: "left",
|
|
187
174
|
part: "fraction"
|
|
188
|
-
}), this._inner =
|
|
175
|
+
}), this._inner = g("span", {
|
|
189
176
|
className: "number__inner"
|
|
190
|
-
}, [this._integer.el, this._fraction.el]), this.el =
|
|
177
|
+
}, [this._integer.el, this._fraction.el]), this.el = g("span", {
|
|
191
178
|
...r,
|
|
192
179
|
part: "number",
|
|
193
|
-
className: `number ${
|
|
180
|
+
className: `number ${s ?? ""}`
|
|
194
181
|
}, [this._inner]);
|
|
195
182
|
}
|
|
196
183
|
willUpdate() {
|
|
@@ -202,62 +189,62 @@ class et {
|
|
|
202
189
|
didUpdate() {
|
|
203
190
|
const t = this.el.getBoundingClientRect();
|
|
204
191
|
this._integer.didUpdate(), this._fraction.didUpdate();
|
|
205
|
-
const e = this._prevLeft - t.left, i = this.el.offsetWidth,
|
|
192
|
+
const e = this._prevLeft - t.left, i = this.el.offsetWidth, s = this._prevWidth - i;
|
|
206
193
|
this.el.style.setProperty("--width", String(i)), this.el.animate({
|
|
207
|
-
[
|
|
208
|
-
[
|
|
194
|
+
[C]: [`${e}px`, "0px"],
|
|
195
|
+
[j]: [s, 0]
|
|
209
196
|
}, {
|
|
210
197
|
...this.flow.transformTiming,
|
|
211
198
|
composite: "accumulate"
|
|
212
199
|
});
|
|
213
200
|
}
|
|
214
201
|
}
|
|
215
|
-
class
|
|
216
|
-
constructor(t, e, { justify: i, className:
|
|
217
|
-
this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (
|
|
218
|
-
this.children.delete(
|
|
202
|
+
class W {
|
|
203
|
+
constructor(t, e, { justify: i, className: s, ...r }, a) {
|
|
204
|
+
this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (d) => () => {
|
|
205
|
+
this.children.delete(d);
|
|
219
206
|
}, this.justify = i;
|
|
220
|
-
const
|
|
221
|
-
this.el =
|
|
207
|
+
const o = e.map((d) => this.addChar(d).el);
|
|
208
|
+
this.el = g("span", {
|
|
222
209
|
...r,
|
|
223
|
-
className: `section section--justify-${i} ${
|
|
224
|
-
},
|
|
210
|
+
className: `section section--justify-${i} ${s ?? ""}`
|
|
211
|
+
}, a ? a(o) : o);
|
|
225
212
|
}
|
|
226
213
|
addChar(t, { startDigitsAtZero: e = !1, ...i } = {}) {
|
|
227
|
-
const
|
|
214
|
+
const s = t.type === "integer" || t.type === "fraction" ? new F(this, t.type, e ? 0 : t.value, t.pos, {
|
|
228
215
|
...i,
|
|
229
216
|
onRemove: this.onCharRemove(t.key)
|
|
230
|
-
}) : new
|
|
217
|
+
}) : new et(this, t.type, t.value, {
|
|
231
218
|
...i,
|
|
232
219
|
onRemove: this.onCharRemove(t.key)
|
|
233
220
|
});
|
|
234
|
-
return this.children.set(t.key,
|
|
221
|
+
return this.children.set(t.key, s), s;
|
|
235
222
|
}
|
|
236
223
|
unpop(t) {
|
|
237
224
|
t.el.classList.remove("section__exiting"), t.el.style.top = "", t.el.style[this.justify] = "";
|
|
238
225
|
}
|
|
239
226
|
pop(t) {
|
|
240
227
|
t.forEach((e) => {
|
|
241
|
-
e.el.style.top = `${e.el.offsetTop}px`, e.el.style[this.justify] = `${
|
|
228
|
+
e.el.style.top = `${e.el.offsetTop}px`, e.el.style[this.justify] = `${H(e.el, this.justify)}px`;
|
|
242
229
|
}), t.forEach((e) => {
|
|
243
230
|
e.el.classList.add("section__exiting"), e.present = !1;
|
|
244
231
|
});
|
|
245
232
|
}
|
|
246
233
|
addNewAndUpdateExisting(t) {
|
|
247
|
-
const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(),
|
|
248
|
-
if (
|
|
249
|
-
let
|
|
250
|
-
this.children.has(
|
|
251
|
-
}, { reverse:
|
|
252
|
-
const
|
|
253
|
-
e.forEach((
|
|
254
|
-
|
|
234
|
+
const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), s = this.justify === "left", r = s ? "prepend" : "append";
|
|
235
|
+
if (D(t, (a) => {
|
|
236
|
+
let o;
|
|
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);
|
|
238
|
+
}, { reverse: s }), this.flow.computedAnimated) {
|
|
239
|
+
const a = this.el.getBoundingClientRect();
|
|
240
|
+
e.forEach((o) => {
|
|
241
|
+
o.willUpdate(a);
|
|
255
242
|
});
|
|
256
243
|
}
|
|
257
|
-
e.forEach((
|
|
258
|
-
|
|
259
|
-
}), i.forEach((
|
|
260
|
-
|
|
244
|
+
e.forEach((a, o) => {
|
|
245
|
+
a.update(o.value);
|
|
246
|
+
}), i.forEach((a, o) => {
|
|
247
|
+
a.update(o.value);
|
|
261
248
|
});
|
|
262
249
|
}
|
|
263
250
|
willUpdate() {
|
|
@@ -266,7 +253,7 @@ class O {
|
|
|
266
253
|
}
|
|
267
254
|
didUpdate() {
|
|
268
255
|
const t = this.el.getBoundingClientRect();
|
|
269
|
-
this.children.forEach((
|
|
256
|
+
this.children.forEach((s) => s.didUpdate(t));
|
|
270
257
|
const e = t[this.justify], i = this._prevOffset - e;
|
|
271
258
|
i && this.children.size && this.el.animate({
|
|
272
259
|
transform: [`translateX(${i}px)`, "none"]
|
|
@@ -276,31 +263,31 @@ class O {
|
|
|
276
263
|
});
|
|
277
264
|
}
|
|
278
265
|
}
|
|
279
|
-
class M extends
|
|
266
|
+
class M extends W {
|
|
280
267
|
update(t) {
|
|
281
268
|
const e = /* @__PURE__ */ new Map();
|
|
282
|
-
this.children.forEach((i,
|
|
283
|
-
t.find((r) => r.key ===
|
|
269
|
+
this.children.forEach((i, s) => {
|
|
270
|
+
t.find((r) => r.key === s) || e.set(s, i), this.unpop(i);
|
|
284
271
|
}), this.addNewAndUpdateExisting(t), e.forEach((i) => {
|
|
285
|
-
i instanceof
|
|
272
|
+
i instanceof F && i.update(0);
|
|
286
273
|
}), this.pop(e);
|
|
287
274
|
}
|
|
288
275
|
}
|
|
289
|
-
class
|
|
276
|
+
class N extends W {
|
|
290
277
|
update(t) {
|
|
291
278
|
const e = /* @__PURE__ */ new Map();
|
|
292
|
-
this.children.forEach((i,
|
|
293
|
-
t.find((r) => r.key ===
|
|
279
|
+
this.children.forEach((i, s) => {
|
|
280
|
+
t.find((r) => r.key === s) || e.set(s, i);
|
|
294
281
|
}), this.pop(e), this.addNewAndUpdateExisting(t);
|
|
295
282
|
}
|
|
296
283
|
}
|
|
297
284
|
class U {
|
|
298
|
-
constructor(t, e, { onRemove: i, animateIn:
|
|
285
|
+
constructor(t, e, { onRemove: i, animateIn: s = !1 } = {}) {
|
|
299
286
|
this.flow = t, this.el = e, this._present = !0, this._remove = () => {
|
|
300
287
|
var r;
|
|
301
288
|
this.el.remove(), (r = this._onRemove) == null || r.call(this);
|
|
302
|
-
}, this.el.classList.add("animate-presence"), this.flow.computedAnimated &&
|
|
303
|
-
[
|
|
289
|
+
}, this.el.classList.add("animate-presence"), this.flow.computedAnimated && s && this.el.animate({
|
|
290
|
+
[x]: [-0.9999, 0]
|
|
304
291
|
}, {
|
|
305
292
|
...this.flow.opacityTiming,
|
|
306
293
|
composite: "accumulate"
|
|
@@ -316,7 +303,7 @@ class U {
|
|
|
316
303
|
return;
|
|
317
304
|
}
|
|
318
305
|
this.el.style.setProperty("--_number-flow-d-opacity", t ? "0" : "-.999"), this.el.animate({
|
|
319
|
-
[
|
|
306
|
+
[x]: t ? [-0.9999, 0] : [0.999, 0]
|
|
320
307
|
}, {
|
|
321
308
|
...this.flow.opacityTiming,
|
|
322
309
|
composite: "accumulate"
|
|
@@ -327,44 +314,45 @@ class U {
|
|
|
327
314
|
}
|
|
328
315
|
}
|
|
329
316
|
class B extends U {
|
|
330
|
-
constructor(t, e, i,
|
|
331
|
-
super(t.flow, i,
|
|
317
|
+
constructor(t, e, i, s) {
|
|
318
|
+
super(t.flow, i, s), this.section = t, this.value = e, this.el = i;
|
|
332
319
|
}
|
|
333
320
|
}
|
|
334
|
-
class
|
|
335
|
-
constructor(t, e, i,
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
321
|
+
class F extends B {
|
|
322
|
+
constructor(t, e, i, s, r) {
|
|
323
|
+
var u, p;
|
|
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) => {
|
|
325
|
+
const f = g("span", { className: `digit__num${h === i ? " is-current" : ""}` }, [document.createTextNode(String(h))]);
|
|
326
|
+
return f.style.setProperty("--n", String(h)), f;
|
|
327
|
+
}), d = g("span", {
|
|
340
328
|
part: `digit ${e}-digit`,
|
|
341
329
|
className: "digit"
|
|
342
330
|
}, o);
|
|
343
|
-
|
|
331
|
+
d.style.setProperty("--current", String(i)), d.style.setProperty("--length", String(a)), super(t, i, d, r), this.pos = s, this._onAnimationsFinish = () => {
|
|
344
332
|
this.el.classList.remove("is-spinning");
|
|
345
|
-
}, this._numbers = o;
|
|
333
|
+
}, this._numbers = o, this._length = a;
|
|
346
334
|
}
|
|
347
335
|
willUpdate(t) {
|
|
348
336
|
const e = this.el.getBoundingClientRect();
|
|
349
337
|
this._prevValue = this.value;
|
|
350
|
-
const i = e[this.section.justify] - t[this.section.justify],
|
|
351
|
-
this._prevCenter = this.section.justify === "left" ? i +
|
|
338
|
+
const i = e[this.section.justify] - t[this.section.justify], s = e.width / 2;
|
|
339
|
+
this._prevCenter = this.section.justify === "left" ? i + s : i - s;
|
|
352
340
|
}
|
|
353
341
|
update(t) {
|
|
354
342
|
var e, i;
|
|
355
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;
|
|
356
344
|
}
|
|
357
345
|
didUpdate(t) {
|
|
358
|
-
const e = this.el.getBoundingClientRect(), i = e[this.section.justify] - t[this.section.justify],
|
|
359
|
-
|
|
360
|
-
transform: [`translateX(${
|
|
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"]
|
|
361
349
|
}, {
|
|
362
350
|
...this.flow.transformTiming,
|
|
363
351
|
composite: "accumulate"
|
|
364
352
|
});
|
|
365
|
-
const
|
|
366
|
-
|
|
367
|
-
[
|
|
353
|
+
const o = this.diff;
|
|
354
|
+
o && (this.el.classList.add("is-spinning"), this.el.animate({
|
|
355
|
+
[A]: [-o, 0]
|
|
368
356
|
}, {
|
|
369
357
|
...this.flow.spinTiming ?? this.flow.transformTiming,
|
|
370
358
|
composite: "accumulate"
|
|
@@ -373,20 +361,20 @@ class D extends B {
|
|
|
373
361
|
get diff() {
|
|
374
362
|
let t = this.flow.computedTrend;
|
|
375
363
|
const e = this.value - this._prevValue;
|
|
376
|
-
return !e && this.flow.
|
|
364
|
+
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);
|
|
377
365
|
}
|
|
378
366
|
}
|
|
379
|
-
class
|
|
380
|
-
constructor(t, e, i,
|
|
381
|
-
const r =
|
|
367
|
+
class et extends B {
|
|
368
|
+
constructor(t, e, i, s) {
|
|
369
|
+
const r = g("span", {
|
|
382
370
|
className: "symbol__value",
|
|
383
371
|
textContent: i
|
|
384
372
|
});
|
|
385
|
-
super(t, i,
|
|
373
|
+
super(t, i, g("span", {
|
|
386
374
|
part: e,
|
|
387
375
|
className: "symbol"
|
|
388
|
-
}, [r]),
|
|
389
|
-
this._children.delete(
|
|
376
|
+
}, [r]), s), this.type = e, this._children = /* @__PURE__ */ new Map(), this._onChildRemove = (a) => () => {
|
|
377
|
+
this._children.delete(a);
|
|
390
378
|
}, this._children.set(i, new U(this.flow, r, {
|
|
391
379
|
onRemove: this._onChildRemove(i)
|
|
392
380
|
}));
|
|
@@ -404,7 +392,7 @@ class it extends B {
|
|
|
404
392
|
const i = this._children.get(t);
|
|
405
393
|
i.present = !0, i.el.classList.remove("symbol__exiting");
|
|
406
394
|
} else {
|
|
407
|
-
const i =
|
|
395
|
+
const i = g("span", {
|
|
408
396
|
className: "symbol__value",
|
|
409
397
|
textContent: t
|
|
410
398
|
});
|
|
@@ -419,17 +407,17 @@ class it extends B {
|
|
|
419
407
|
didUpdate(t) {
|
|
420
408
|
if (this.type === "decimal")
|
|
421
409
|
return;
|
|
422
|
-
const i = this.el.getBoundingClientRect()[this.section.justify] - t[this.section.justify],
|
|
423
|
-
|
|
424
|
-
transform: [`translateX(${
|
|
410
|
+
const i = this.el.getBoundingClientRect()[this.section.justify] - t[this.section.justify], s = this._prevOffset - i;
|
|
411
|
+
s && this.el.animate({
|
|
412
|
+
transform: [`translateX(${s}px)`, "none"]
|
|
425
413
|
}, { ...this.flow.transformTiming, composite: "accumulate" });
|
|
426
414
|
}
|
|
427
415
|
}
|
|
428
416
|
export {
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
417
|
+
Q as NumberFlowLite,
|
|
418
|
+
J as canAnimate,
|
|
419
|
+
st as define,
|
|
420
|
+
nt as formatToData,
|
|
421
|
+
E as prefersReducedMotion,
|
|
434
422
|
ot as renderInnerHTML
|
|
435
423
|
};
|
package/dist/util/math.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const max: (n1?: number, n2?: number) => number |
|
|
1
|
+
export declare const max: (n1?: number, n2?: number) => number | undefined;
|