number-flow 0.5.5 → 0.5.7

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/group.d.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { ServerSafeHTMLElement } from './ssr';
2
- import NumberFlow from '.';
3
2
  export default class NumberFlowGroup extends ServerSafeHTMLElement {
4
3
  private _mutationObserver?;
5
4
  connectedCallback(): void;
6
5
  private _flows;
7
- _addDescendant: (flow: NumberFlow) => void;
8
- _removeDescendant: (flow: NumberFlow) => void;
6
+ private _addDescendant;
7
+ private _removeDescendant;
9
8
  private _onDescendantConnected;
10
9
  private _updating;
11
10
  private _onDescendantUpdate;
package/dist/group.js CHANGED
@@ -1 +1 @@
1
- "use strict";const n=require("./lite-B614HEDD.js"),t=require("./index.js");class i extends n.ServerSafeHTMLElement{constructor(){super(...arguments),this._flows=new Set,this._addDescendant=e=>{e.batched=!0,this._flows.add(e)},this._removeDescendant=e=>{e.batched=!1,this._flows.delete(e)},this._onDescendantConnected=e=>{this._addDescendant(e.target)},this._updating=!1,this._onDescendantUpdate=()=>{this._updating||(this._updating=!0,this._flows.forEach(e=>{e.created&&(e.willUpdate(),queueMicrotask(()=>{e.connected&&e.didUpdate()}))}),queueMicrotask(()=>{this._updating=!1}))}}connectedCallback(){this.querySelectorAll("number-flow").forEach(e=>{this._addDescendant(e)}),this.addEventListener(t.CONNECT_EVENT,this._onDescendantConnected),this.addEventListener(t.UPDATE_EVENT,this._onDescendantUpdate),this._mutationObserver??(this._mutationObserver=new MutationObserver(e=>{e.forEach(r=>{r.removedNodes.forEach(s=>{s instanceof t.default&&this._removeDescendant(s)})})})),this._mutationObserver.observe(this,{childList:!0,subtree:!0})}disconnectedCallback(){var e;this.removeEventListener(t.CONNECT_EVENT,this._onDescendantConnected),this.removeEventListener(t.UPDATE_EVENT,this._onDescendantUpdate),(e=this._mutationObserver)==null||e.disconnect()}}n.define("number-flow-group",i);module.exports=i;
1
+ "use strict";const n=require("./lite-B8p0lZvR.js"),t=require("./index.js");class i extends n.ServerSafeHTMLElement{constructor(){super(...arguments),this._flows=new Set,this._addDescendant=e=>{e.batched=!0,this._flows.add(e)},this._removeDescendant=e=>{e.batched=!1,this._flows.delete(e)},this._onDescendantConnected=e=>{this._addDescendant(e.target)},this._updating=!1,this._onDescendantUpdate=()=>{this._updating||(this._updating=!0,this._flows.forEach(e=>{e.created&&(e.willUpdate(),queueMicrotask(()=>{e.connected&&e.didUpdate()}))}),queueMicrotask(()=>{this._updating=!1}))}}connectedCallback(){this.querySelectorAll("number-flow").forEach(e=>{this._addDescendant(e)}),this.addEventListener(t.CONNECT_EVENT,this._onDescendantConnected),this.addEventListener(t.UPDATE_EVENT,this._onDescendantUpdate),this._mutationObserver??(this._mutationObserver=new MutationObserver(e=>{e.forEach(r=>{r.removedNodes.forEach(s=>{s instanceof t.default&&this._removeDescendant(s)})})})),this._mutationObserver.observe(this,{childList:!0,subtree:!0})}disconnectedCallback(){var e;this.removeEventListener(t.CONNECT_EVENT,this._onDescendantConnected),this.removeEventListener(t.UPDATE_EVENT,this._onDescendantUpdate),(e=this._mutationObserver)==null||e.disconnect()}}n.define("number-flow-group",i);module.exports=i;
package/dist/group.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { d as a, S as r } from "./lite-D-JHjRWt.mjs";
1
+ import { d as a, S as r } from "./lite-CzDvSY_4.mjs";
2
2
  import d, { CONNECT_EVENT as s, UPDATE_EVENT as n } from "./index.mjs";
3
3
  class o extends r {
4
4
  constructor() {
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./lite-B614HEDD.js"),l=require("./plugins.js"),n="number-flow-connect",s="number-flow-update",f=(r,{locales:t,format:o,numberPrefix:a,numberSuffix:u}={})=>{const c=e.formatToData(r,new Intl.NumberFormat(t,o),a,u);return e.renderInnerHTML(c)};class i extends e.NumberFlowLite{constructor(){super(...arguments),this.connected=!1}connectedCallback(){this.connected=!0,this.dispatchEvent(new Event(n,{bubbles:!0}))}disconnectedCallback(){this.connected=!1}get value(){return this._value}update(t){(!this._formatter||this._prevFormat!==this.format||this._prevLocales!==this.locales)&&(this._formatter=new Intl.NumberFormat(this.locales,this.format),this._prevFormat=this.format,this._prevLocales=this.locales),t!=null&&(this._value=t),this.dispatchEvent(new Event(s,{bubbles:!0})),this.data=e.formatToData(this._value,this._formatter,this.numberPrefix,this.numberSuffix)}}e.define("number-flow",i);exports.Digit=e.Digit;exports.canAnimate=e.canAnimate;exports.define=e.define;exports.formatToData=e.formatToData;exports.prefersReducedMotion=e.prefersReducedMotion;exports.continuous=l.continuous;exports.CONNECT_EVENT=n;exports.UPDATE_EVENT=s;exports.default=i;exports.renderInnerHTML=f;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./lite-B8p0lZvR.js"),l=require("./plugins.js"),n="number-flow-connect",s="number-flow-update",f=(r,{locales:t,format:o,numberPrefix:a,numberSuffix:u}={})=>{const c=e.formatToData(r,new Intl.NumberFormat(t,o),a,u);return e.renderInnerHTML(c)};class i extends e.NumberFlowLite{constructor(){super(...arguments),this.connected=!1}connectedCallback(){this.connected=!0,this.dispatchEvent(new Event(n,{bubbles:!0}))}disconnectedCallback(){this.connected=!1}get value(){return this._value}update(t){(!this._formatter||this._prevFormat!==this.format||this._prevLocales!==this.locales)&&(this._formatter=new Intl.NumberFormat(this.locales,this.format),this._prevFormat=this.format,this._prevLocales=this.locales),t!=null&&(this._value=t),this.dispatchEvent(new Event(s,{bubbles:!0})),this.data=e.formatToData(this._value,this._formatter,this.numberPrefix,this.numberSuffix)}}e.define("number-flow",i);exports.Digit=e.Digit;exports.canAnimate=e.canAnimate;exports.define=e.define;exports.formatToData=e.formatToData;exports.prefersReducedMotion=e.prefersReducedMotion;exports.continuous=l.continuous;exports.CONNECT_EVENT=n;exports.UPDATE_EVENT=s;exports.default=i;exports.renderInnerHTML=f;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { d as i, N as u, f as r, r as c } from "./lite-D-JHjRWt.mjs";
2
- import { D as _, c as v, p as E } from "./lite-D-JHjRWt.mjs";
1
+ import { d as i, N as u, f as r, r as c } from "./lite-CzDvSY_4.mjs";
2
+ import { D as _, c as v, p as E } from "./lite-CzDvSY_4.mjs";
3
3
  import { continuous as N } from "./plugins.mjs";
4
4
  const l = "number-flow-connect", m = "number-flow-update", d = (t, { locales: e, format: s, numberPrefix: n, numberSuffix: a } = {}) => {
5
5
  const o = r(t, new Intl.NumberFormat(e, s), n, a);
@@ -0,0 +1,12 @@
1
+ "use strict";const _=require("esm-env");require("./plugins.js");const u=(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},I=(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},X=n=>n.offsetWidth>0&&n.offsetHeight>0,z=(n,t)=>{_.BROWSER&&customElements.get(n)!==t&&customElements.define(n,t)};function q(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=[],c=[],d={},p=h=>`${h}:${d[h]=(d[h]??-1)+1}`;let x="",g=!1,y=!1;for(const h of s){x+=h.value;const l=h.type==="minusSign"||h.type==="plusSign"?"sign":h.type;l==="integer"?(g=!0,r.push(...h.value.split("").map(R=>({type:l,value:parseInt(R)})))):l==="group"?r.push({type:l,value:h.value}):l==="decimal"?(y=!0,o.push({type:l,value:h.value,key:p(l)})):l==="fraction"?o.push(...h.value.split("").map(R=>({type:l,value:parseInt(R),key:p(l),pos:-1-d[l]}))):(g||y?c:a).push({type:l,value:h.value,key:p(l)})}const P=[];for(let h=r.length-1;h>=0;h--){const l=r[h];P.unshift(l.type==="integer"?{...l,key:p(l.type),pos:d[l.type]}:{...l,key:p(l.type)})}return{pre:a,integer:P,fraction:o,post:c,valueAsString:x,value:typeof n=="string"?parseFloat(n):n}}const G=String.raw,V=String.raw,K=_.BROWSER&&(()=>{try{document.createElement("div").animate({opacity:0},{easing:"linear(0, 1)"})}catch{return!1}return!0})(),Z=_.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("line-height","mod(1,1)"),$=_.BROWSER&&typeof matchMedia<"u"?matchMedia("(prefers-reduced-motion: reduce)"):null,S="--_number-flow-d-opacity",U="--_number-flow-d-width",E="--_number-flow-dx",j="--_number-flow-d",J=(()=>{try{return CSS.registerProperty({name:S,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:E,syntax:"<length>",inherits:!0,initialValue:"0px"}),CSS.registerProperty({name:U,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:j,syntax:"<number>",inherits:!0,initialValue:"0"}),!0}catch{return!1}})(),T="var(--number-flow-char-height, 1em)",f="var(--number-flow-mask-height, 0.25em)",C=`calc(${f} / 2)`,A="var(--number-flow-mask-width, 0.5em)",m=`calc(${A} / var(--scale-x))`,v="#000 0, transparent 71%",L=V`:host{display:inline-block;direction:ltr;white-space:nowrap;isolation:isolate;line-height:${T} !important}.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(${U}) / var(--width));transform:translateX(var(${E})) scaleX(var(--scale-x));margin:0 calc(-1 * ${A});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${m},#000 calc(100% - ${m}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${f},#000 calc(100% - ${f}),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% - ${f} * 2),calc(100% - ${m} * 2) 100%,${m} ${f},${m} ${f},${m} ${f},${m} ${f};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${C} ${A};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${E})))}:host > :not(.number){z-index:5}.section,.symbol{display:inline-block;position:relative;isolation:isolate}.section::after{content:'\200b';display:inline-block}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section > [inert],.symbol > [inert]{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${j})}.digit__num,.number .section::after{padding:${C} 0}.digit__num{display:inline-block;--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[inert]{position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num[inert]{display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol > [inert]{left:0}.section--justify-right .symbol > [inert]{right:0}.animate-presence{opacity:calc(1 + var(${S}))}`,B=_.BROWSER?HTMLElement:class{},Q=V`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${T} !important}span{display:inline-block}:host([data-will-change]) span{will-change:transform}.number,.digit{padding:${C} 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`:`symbol ${n.type}`}">${n.value}</span>`,w=(n,t)=>`<span part="${t}">${n.reduce((e,i)=>e+tt(i),"")}</span>`,et=n=>G`<template shadowroot="open" shadowrootmode="open"
2
+ ><style>
3
+ ${Q}</style
4
+ ><span role="img" aria-label="${n.valueAsString}"
5
+ >${w(n.pre,"left")}<span part="number" class="number"
6
+ >${w(n.integer,"integer")}${w(n.fraction,"fraction")}</span
7
+ >${w(n.post,"right")}</span
8
+ ></template
9
+ ><span
10
+ style="font-kerning: none; display: inline-block; line-height: ${T} !important; padding: ${f} 0;"
11
+ >${n.valueAsString}</span
12
+ >`,D=Z&&K&&J;let b;class H extends B{constructor(){super(),this.created=!1,this.batched=!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){var o;if(t==null)return;const{pre:e,integer:i,fraction:s,post:a,value:r}=t;if(this.created){const c=this._data;this._data=t,this.computedTrend=typeof this.trend=="function"?this.trend(c.value,r):this.trend,this.computedAnimated=D&&this._animated&&(!this.respectMotionPreference||!($!=null&&$.matches))&&X(this),(o=this.plugins)==null||o.forEach(d=>{var p;return(p=d.onUpdate)==null?void 0:p.call(d,t,c,this)}),this.batched||this.willUpdate(),this._pre.update(e),this._num.update({integer:i,fraction:s}),this._post.update(a),this.batched||this.didUpdate()}else{this._data=t,this.attachShadow({mode:"open"});try{this._internals??(this._internals=this.attachInternals()),this._internals.role="img"}catch{}if(typeof CSSStyleSheet<"u"&&this.shadowRoot.adoptedStyleSheets)b||(b=new CSSStyleSheet,b.replaceSync(L)),this.shadowRoot.adoptedStyleSheets=[b];else{const c=document.createElement("style");c.textContent=L,this.shadowRoot.appendChild(c)}this._pre=new N(this,e,{justify:"right",part:"left"}),this.shadowRoot.appendChild(this._pre.el),this._num=new it(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}try{this._internals.ariaLabel=t.valueAsString}catch{}}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}}H.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),respectMotionPreference:!0,plugins:void 0,digits:void 0};class it{constructor(t,e,i,{className:s,...a}={}){this.flow=t,this._integer=new W(t,e,{justify:"right",part:"integer"}),this._fraction=new W(t,i,{justify:"left",part:"fraction"}),this._inner=u("span",{className:"number__inner"},[this._integer.el,this._fraction.el]),this.el=u("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({[E]:[`${e}px`,"0px"],[U]:[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=c=>()=>{this.children.delete(c)},this.justify=i;const o=e.map(c=>this.addChar(c).el);this.el=u("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 M(this,t.type,e?0:t.value,t.pos,{...i,onRemove:this.onCharRemove(t.key)}):new st(this,t.type,t.value,{...i,onRemove:this.onCharRemove(t.key)});return this.children.set(t.key,s),s}unpop(t){t.el.removeAttribute("inert"),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]=`${I(e.el,this.justify)}px`}),t.forEach(e=>{e.el.setAttribute("inert",""),e.present=!1})}addNewAndUpdateExisting(t){const e=new Map,i=new Map,s=this.justify==="left",a=s?"prepend":"append";if(q(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 W 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 M&&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({[S]:[-.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,t?this.el.removeAttribute("inert"):this.el.setAttribute("inert",""),!this.flow.computedAnimated){t||this._remove();return}this.el.style.setProperty("--_number-flow-d-opacity",t?"0":"-.999"),this.el.animate({[S]: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 M extends F{constructor(t,e,i,s,a){var d,p;const r=(((p=(d=t.flow.digits)==null?void 0:d[s])==null?void 0:p.max)??9)+1,o=Array.from({length:r}).map((x,g)=>{const y=u("span",{className:"digit__num"},[document.createTextNode(String(g))]);return g!==i&&y.setAttribute("inert",""),y.style.setProperty("--n",String(g)),y}),c=u("span",{part:`digit ${e}-digit`,className:"digit"},o);c.style.setProperty("--current",String(i)),c.style.setProperty("--length",String(r)),super(t,i,c,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){this.el.style.setProperty("--current",String(t)),this._numbers.forEach((e,i)=>i===t?e.removeAttribute("inert"):e.setAttribute("inert","")),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.getDelta();o&&(this.el.classList.add("is-spinning"),this.el.animate({[j]:[-o,0]},{...this.flow.spinTiming??this.flow.transformTiming,composite:"accumulate"}),this.flow.addEventListener("animationsfinish",this._onAnimationsFinish,{once:!0}))}getDelta(){var i;if(this.flow.plugins)for(const s of this.flow.plugins){const a=(i=s.getDelta)==null?void 0:i.call(s,this.value,this._prevValue,this);if(a!=null)return a}const t=this.value-this._prevValue,e=this.flow.computedTrend||Math.sign(t);return e<0&&this.value>this._prevValue?this.value-this.length-this._prevValue:e>0&&this.value<this._prevValue?this.length-this._prevValue+this.value:t}}class st extends F{constructor(t,e,i,s){const a=u("span",{className:"symbol__value",textContent:i});super(t,i,u("span",{part:`symbol ${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);e&&(e.present=!1);const i=this._children.get(t);if(i)i.present=!0;else{const s=u("span",{className:"symbol__value",textContent:t});this.el.appendChild(s),this._children.set(t,new k(this.flow,s,{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.Digit=M;exports.NumberFlowLite=H;exports.ServerSafeHTMLElement=B;exports.canAnimate=D;exports.define=z;exports.formatToData=Y;exports.prefersReducedMotion=$;exports.renderInnerHTML=et;
@@ -1,8 +1,8 @@
1
1
  import { BROWSER as _ } from "esm-env";
2
2
  import "./plugins.mjs";
3
- const u = (n, t, e) => {
3
+ const m = (n, t, e) => {
4
4
  const i = document.createElement(n), [s, a] = Array.isArray(t) ? [void 0, t] : [t, e];
5
- return s && Object.assign(i, s), a == null || a.forEach((o) => i.appendChild(o)), i;
5
+ return s && Object.assign(i, s), a == null || a.forEach((r) => i.appendChild(r)), i;
6
6
  }, F = (n, t) => {
7
7
  var e;
8
8
  return t === "left" ? n.offsetLeft : (((e = n.offsetParent instanceof HTMLElement ? n.offsetParent : null) == null ? void 0 : e.offsetWidth) ?? 0) - n.offsetWidth - n.offsetLeft;
@@ -17,14 +17,14 @@ function X(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 = [], o = [], r = [], c = [], d = {}, p = (h) => `${h}:${d[h] = (d[h] ?? -1) + 1}`;
21
- let C = "", g = !1, y = !1;
20
+ const a = [], r = [], o = [], c = [], d = {}, p = (h) => `${h}:${d[h] = (d[h] ?? -1) + 1}`;
21
+ let x = "", g = !1, y = !1;
22
22
  for (const h of s) {
23
- C += h.value;
23
+ x += h.value;
24
24
  const l = h.type === "minusSign" || h.type === "plusSign" ? "sign" : h.type;
25
- l === "integer" ? (g = !0, o.push(...h.value.split("").map((x) => ({ type: l, value: parseInt(x) })))) : l === "group" ? o.push({ type: l, value: h.value }) : l === "decimal" ? (y = !0, r.push({ type: l, value: h.value, key: p(l) })) : l === "fraction" ? r.push(...h.value.split("").map((x) => ({
25
+ l === "integer" ? (g = !0, r.push(...h.value.split("").map((C) => ({ type: l, value: parseInt(C) })))) : l === "group" ? r.push({ type: l, value: h.value }) : l === "decimal" ? (y = !0, o.push({ type: l, value: h.value, key: p(l) })) : l === "fraction" ? o.push(...h.value.split("").map((C) => ({
26
26
  type: l,
27
- value: parseInt(x),
27
+ value: parseInt(C),
28
28
  key: p(l),
29
29
  pos: -1 - d[l]
30
30
  }))) : (g || y ? c : a).push({
@@ -34,8 +34,8 @@ function nt(n, t, e, i) {
34
34
  });
35
35
  }
36
36
  const T = [];
37
- for (let h = o.length - 1; h >= 0; h--) {
38
- const l = o[h];
37
+ for (let h = r.length - 1; h >= 0; h--) {
38
+ const l = r[h];
39
39
  T.unshift(l.type === "integer" ? {
40
40
  ...l,
41
41
  key: p(l.type),
@@ -48,13 +48,20 @@ function nt(n, t, e, i) {
48
48
  return {
49
49
  pre: a,
50
50
  integer: T,
51
- fraction: r,
51
+ fraction: o,
52
52
  post: c,
53
- valueAsString: C,
53
+ valueAsString: x,
54
54
  value: typeof n == "string" ? parseFloat(n) : n
55
55
  };
56
56
  }
57
- const I = String.raw, V = String.raw, O = _ && typeof CSS < "u" && CSS.supports && CSS.supports("animation-timing-function", "linear(1,2)"), z = _ && typeof CSS < "u" && CSS.supports && CSS.supports("line-height", "mod(1,1)"), A = _ && typeof matchMedia < "u" ? matchMedia("(prefers-reduced-motion: reduce)") : null, $ = "--_number-flow-d-opacity", U = "--_number-flow-d-width", S = "--_number-flow-dx", j = "--_number-flow-d", Y = (() => {
57
+ const I = String.raw, V = String.raw, O = _ && (() => {
58
+ try {
59
+ document.createElement("div").animate({ opacity: 0 }, { easing: "linear(0, 1)" });
60
+ } catch {
61
+ return !1;
62
+ }
63
+ return !0;
64
+ })(), z = _ && typeof CSS < "u" && CSS.supports && CSS.supports("line-height", "mod(1,1)"), A = _ && typeof matchMedia < "u" ? matchMedia("(prefers-reduced-motion: reduce)") : null, $ = "--_number-flow-d-opacity", U = "--_number-flow-d-width", S = "--_number-flow-dx", j = "--_number-flow-d", Y = (() => {
58
65
  try {
59
66
  return CSS.registerProperty({
60
67
  name: $,
@@ -80,7 +87,7 @@ const I = String.raw, V = String.raw, O = _ && typeof CSS < "u" && CSS.supports
80
87
  } catch {
81
88
  return !1;
82
89
  }
83
- })(), P = "var(--number-flow-char-height, 1em)", f = "var(--number-flow-mask-height, 0.25em)", k = `calc(${f} / 2)`, E = "var(--number-flow-mask-width, 0.5em)", m = `calc(${E} / var(--scale-x))`, w = "#000 0, transparent 71%", M = V`:host{display:inline-block;direction:ltr;white-space:nowrap;isolation:isolate;line-height:${P} !important}.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(${U}) / var(--width));transform:translateX(var(${S})) scaleX(var(--scale-x));margin:0 calc(-1 * ${E});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${m},#000 calc(100% - ${m}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${f},#000 calc(100% - ${f}),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% - ${f} * 2),calc(100% - ${m} * 2) 100%,${m} ${f},${m} ${f},${m} ${f},${m} ${f};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${k} ${E};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${S})))}:host > :not(.number){z-index:5}.section,.symbol{display:inline-block;position:relative;isolation:isolate}.section::after{content:'\200b';display:inline-block}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section > [inert],.symbol > [inert]{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${j})}.digit__num,.number .section::after{padding:${k} 0}.digit__num{display:inline-block;--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[inert]{position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num[inert]{display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol > [inert]{left:0}.section--justify-right .symbol > [inert]{right:0}.animate-presence{opacity:calc(1 + var(${$}))}`, G = _ ? HTMLElement : class {
90
+ })(), P = "var(--number-flow-char-height, 1em)", f = "var(--number-flow-mask-height, 0.25em)", k = `calc(${f} / 2)`, E = "var(--number-flow-mask-width, 0.5em)", u = `calc(${E} / var(--scale-x))`, w = "#000 0, transparent 71%", M = V`:host{display:inline-block;direction:ltr;white-space:nowrap;isolation:isolate;line-height:${P} !important}.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(${U}) / var(--width));transform:translateX(var(${S})) scaleX(var(--scale-x));margin:0 calc(-1 * ${E});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${u},#000 calc(100% - ${u}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${f},#000 calc(100% - ${f}),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% - ${f} * 2),calc(100% - ${u} * 2) 100%,${u} ${f},${u} ${f},${u} ${f},${u} ${f};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${k} ${E};transform:scaleX(calc(1 / var(--scale-x))) translateX(calc(-1 * var(${S})))}:host > :not(.number){z-index:5}.section,.symbol{display:inline-block;position:relative;isolation:isolate}.section::after{content:'\200b';display:inline-block}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section > [inert],.symbol > [inert]{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${j})}.digit__num,.number .section::after{padding:${k} 0}.digit__num{display:inline-block;--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[inert]{position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num[inert]{display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol > [inert]{left:0}.section--justify-right .symbol > [inert]{right:0}.animate-presence{opacity:calc(1 + var(${$}))}`, G = _ ? HTMLElement : class {
84
91
  }, 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:${k} 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` : `symbol ${n.type}`}">${n.value}</span>`, v = (n, t) => `<span part="${t}">${n.reduce((e, i) => e + Z(i), "")}</span>`, at = (n) => (
85
92
  // shadowroot="open" non-standard attribute for old Chrome:
86
93
  I`<template shadowroot="open" shadowrootmode="open"
@@ -114,19 +121,24 @@ class J extends G {
114
121
  * @internal
115
122
  */
116
123
  set data(t) {
117
- var r;
124
+ var o;
118
125
  if (t == null)
119
126
  return;
120
- const { pre: e, integer: i, fraction: s, post: a, value: o } = t;
127
+ const { pre: e, integer: i, fraction: s, post: a, value: r } = t;
121
128
  if (this.created) {
122
129
  const c = this._data;
123
- this._data = t, this.computedTrend = typeof this.trend == "function" ? this.trend(c.value, o) : this.trend, this.computedAnimated = q && this._animated && (!this.respectMotionPreference || !(A != null && A.matches)) && // https://github.com/barvian/number-flow/issues/9
124
- H(this), (r = this.plugins) == null || r.forEach((d) => {
130
+ this._data = t, this.computedTrend = typeof this.trend == "function" ? this.trend(c.value, r) : this.trend, this.computedAnimated = q && this._animated && (!this.respectMotionPreference || !(A != null && A.matches)) && // https://github.com/barvian/number-flow/issues/9
131
+ H(this), (o = this.plugins) == null || o.forEach((d) => {
125
132
  var p;
126
133
  return (p = d.onUpdate) == null ? void 0 : p.call(d, t, c, this);
127
134
  }), this.batched || this.willUpdate(), this._pre.update(e), this._num.update({ integer: i, fraction: s }), this._post.update(a), this.batched || this.didUpdate();
128
135
  } else {
129
- if (this._data = t, this.attachShadow({ mode: "open" }), this._internals ?? (this._internals = this.attachInternals()), this._internals.role = "img", typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
136
+ this._data = t, this.attachShadow({ mode: "open" });
137
+ try {
138
+ this._internals ?? (this._internals = this.attachInternals()), this._internals.role = "img";
139
+ } catch {
140
+ }
141
+ if (typeof CSSStyleSheet < "u" && this.shadowRoot.adoptedStyleSheets)
130
142
  b || (b = new CSSStyleSheet(), b.replaceSync(M)), this.shadowRoot.adoptedStyleSheets = [b];
131
143
  else {
132
144
  const c = document.createElement("style");
@@ -140,7 +152,10 @@ class J extends G {
140
152
  part: "right"
141
153
  }), this.shadowRoot.appendChild(this._post.el), this.created = !0;
142
154
  }
143
- this._internals.ariaLabel = t.valueAsString;
155
+ try {
156
+ this._internals.ariaLabel = t.valueAsString;
157
+ } catch {
158
+ }
144
159
  }
145
160
  /**
146
161
  * @internal
@@ -183,9 +198,9 @@ class Q {
183
198
  }), this._fraction = new L(t, i, {
184
199
  justify: "left",
185
200
  part: "fraction"
186
- }), this._inner = u("span", {
201
+ }), this._inner = m("span", {
187
202
  className: "number__inner"
188
- }, [this._integer.el, this._fraction.el]), this.el = u("span", {
203
+ }, [this._integer.el, this._fraction.el]), this.el = m("span", {
189
204
  ...a,
190
205
  part: "number",
191
206
  className: `number ${s ?? ""}`
@@ -211,15 +226,15 @@ class Q {
211
226
  }
212
227
  }
213
228
  class W {
214
- constructor(t, e, { justify: i, className: s, ...a }, o) {
229
+ constructor(t, e, { justify: i, className: s, ...a }, r) {
215
230
  this.flow = t, this.children = /* @__PURE__ */ new Map(), this.onCharRemove = (c) => () => {
216
231
  this.children.delete(c);
217
232
  }, this.justify = i;
218
- const r = e.map((c) => this.addChar(c).el);
219
- this.el = u("span", {
233
+ const o = e.map((c) => this.addChar(c).el);
234
+ this.el = m("span", {
220
235
  ...a,
221
236
  className: `section section--justify-${i} ${s ?? ""}`
222
- }, o ? o(r) : r);
237
+ }, r ? r(o) : o);
223
238
  }
224
239
  addChar(t, { startDigitsAtZero: e = !1, ...i } = {}) {
225
240
  const s = t.type === "integer" || t.type === "fraction" ? new D(this, t.type, e ? 0 : t.value, t.pos, {
@@ -243,19 +258,19 @@ class W {
243
258
  }
244
259
  addNewAndUpdateExisting(t) {
245
260
  const e = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), s = this.justify === "left", a = s ? "prepend" : "append";
246
- if (X(t, (o) => {
247
- let r;
248
- this.children.has(o.key) ? (r = this.children.get(o.key), i.set(o, r), this.unpop(r), r.present = !0) : (r = this.addChar(o, { startDigitsAtZero: !0, animateIn: !0 }), e.set(o, r)), this.el[a](r.el);
261
+ if (X(t, (r) => {
262
+ let o;
263
+ 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);
249
264
  }, { reverse: s }), this.flow.computedAnimated) {
250
- const o = this.el.getBoundingClientRect();
251
- e.forEach((r) => {
252
- r.willUpdate(o);
265
+ const r = this.el.getBoundingClientRect();
266
+ e.forEach((o) => {
267
+ o.willUpdate(r);
253
268
  });
254
269
  }
255
- e.forEach((o, r) => {
256
- o.update(r.value);
257
- }), i.forEach((o, r) => {
258
- o.update(r.value);
270
+ e.forEach((r, o) => {
271
+ r.update(o.value);
272
+ }), i.forEach((r, o) => {
273
+ r.update(o.value);
259
274
  });
260
275
  }
261
276
  willUpdate() {
@@ -332,18 +347,18 @@ class B extends R {
332
347
  class D extends B {
333
348
  constructor(t, e, i, s, a) {
334
349
  var d, p;
335
- const o = (((p = (d = t.flow.digits) == null ? void 0 : d[s]) == null ? void 0 : p.max) ?? 9) + 1, r = Array.from({ length: o }).map((C, g) => {
336
- const y = u("span", { className: "digit__num" }, [
350
+ const r = (((p = (d = t.flow.digits) == null ? void 0 : d[s]) == null ? void 0 : p.max) ?? 9) + 1, o = Array.from({ length: r }).map((x, g) => {
351
+ const y = m("span", { className: "digit__num" }, [
337
352
  document.createTextNode(String(g))
338
353
  ]);
339
354
  return g !== i && y.setAttribute("inert", ""), y.style.setProperty("--n", String(g)), y;
340
- }), c = u("span", {
355
+ }), c = m("span", {
341
356
  part: `digit ${e}-digit`,
342
357
  className: "digit"
343
- }, r);
344
- c.style.setProperty("--current", String(i)), c.style.setProperty("--length", String(o)), super(t, i, c, a), this.pos = s, this._onAnimationsFinish = () => {
358
+ }, o);
359
+ c.style.setProperty("--current", String(i)), c.style.setProperty("--length", String(r)), super(t, i, c, a), this.pos = s, this._onAnimationsFinish = () => {
345
360
  this.el.classList.remove("is-spinning");
346
- }, this._numbers = r, this.length = o;
361
+ }, this._numbers = o, this.length = r;
347
362
  }
348
363
  willUpdate(t) {
349
364
  const e = this.el.getBoundingClientRect();
@@ -355,16 +370,16 @@ class D extends B {
355
370
  this.el.style.setProperty("--current", String(t)), this._numbers.forEach((e, i) => i === t ? e.removeAttribute("inert") : e.setAttribute("inert", "")), this.value = t;
356
371
  }
357
372
  didUpdate(t) {
358
- 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, o = this._prevCenter - a;
359
- o && this.el.animate({
360
- transform: [`translateX(${o}px)`, "none"]
373
+ 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;
374
+ r && this.el.animate({
375
+ transform: [`translateX(${r}px)`, "none"]
361
376
  }, {
362
377
  ...this.flow.transformTiming,
363
378
  composite: "accumulate"
364
379
  });
365
- const r = this.getDelta();
366
- r && (this.el.classList.add("is-spinning"), this.el.animate({
367
- [j]: [-r, 0]
380
+ const o = this.getDelta();
381
+ o && (this.el.classList.add("is-spinning"), this.el.animate({
382
+ [j]: [-o, 0]
368
383
  }, {
369
384
  ...this.flow.spinTiming ?? this.flow.transformTiming,
370
385
  composite: "accumulate"
@@ -384,15 +399,15 @@ class D extends B {
384
399
  }
385
400
  class tt extends B {
386
401
  constructor(t, e, i, s) {
387
- const a = u("span", {
402
+ const a = m("span", {
388
403
  className: "symbol__value",
389
404
  textContent: i
390
405
  });
391
- super(t, i, u("span", {
406
+ super(t, i, m("span", {
392
407
  part: `symbol ${e}`,
393
408
  className: "symbol"
394
- }, [a]), s), this.type = e, this._children = /* @__PURE__ */ new Map(), this._onChildRemove = (o) => () => {
395
- this._children.delete(o);
409
+ }, [a]), s), this.type = e, this._children = /* @__PURE__ */ new Map(), this._onChildRemove = (r) => () => {
410
+ this._children.delete(r);
396
411
  }, this._children.set(i, new R(this.flow, a, {
397
412
  onRemove: this._onChildRemove(i)
398
413
  }));
@@ -411,7 +426,7 @@ class tt extends B {
411
426
  if (i)
412
427
  i.present = !0;
413
428
  else {
414
- const s = u("span", {
429
+ const s = m("span", {
415
430
  className: "symbol__value",
416
431
  textContent: t
417
432
  });
package/dist/lite.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./lite-B614HEDD.js"),t=require("./plugins.js");exports.Digit=e.Digit;exports.canAnimate=e.canAnimate;exports.default=e.NumberFlowLite;exports.define=e.define;exports.formatToData=e.formatToData;exports.prefersReducedMotion=e.prefersReducedMotion;exports.renderInnerHTML=e.renderInnerHTML;exports.continuous=t.continuous;
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const e=require("./lite-B8p0lZvR.js"),t=require("./plugins.js");exports.Digit=e.Digit;exports.canAnimate=e.canAnimate;exports.default=e.NumberFlowLite;exports.define=e.define;exports.formatToData=e.formatToData;exports.prefersReducedMotion=e.prefersReducedMotion;exports.renderInnerHTML=e.renderInnerHTML;exports.continuous=t.continuous;
package/dist/lite.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { D as r, c as o, N as n, d as s, f as t, p as f, r as d } from "./lite-D-JHjRWt.mjs";
1
+ import { D as r, c as o, N as n, d as s, f as t, p as f, r as d } from "./lite-CzDvSY_4.mjs";
2
2
  import { continuous as c } from "./plugins.mjs";
3
3
  export {
4
4
  r as Digit,
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.5.5",
6
+ "version": "0.5.7",
7
7
  "author": {
8
8
  "name": "Maxwell Barvian",
9
9
  "email": "max@barvian.me",
@@ -11,7 +11,7 @@
11
11
  },
12
12
  "description": "A component to transition and format numbers.",
13
13
  "license": "MIT",
14
- "homepage": "https://number-flow.barvian.me/",
14
+ "homepage": "https://number-flow.barvian.me/vanilla",
15
15
  "repository": {
16
16
  "type": "git",
17
17
  "url": "https://github.com/barvian/number-flow",
@@ -1,12 +0,0 @@
1
- "use strict";const _=require("esm-env");require("./plugins.js");const u=(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(o=>i.appendChild(o)),i},I=(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},X=n=>n.offsetWidth>0&&n.offsetHeight>0,z=(n,t)=>{_.BROWSER&&customElements.get(n)!==t&&customElements.define(n,t)};function q(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=[],o=[],r=[],c=[],d={},p=h=>`${h}:${d[h]=(d[h]??-1)+1}`;let E="",g=!1,y=!1;for(const h of s){E+=h.value;const l=h.type==="minusSign"||h.type==="plusSign"?"sign":h.type;l==="integer"?(g=!0,o.push(...h.value.split("").map(x=>({type:l,value:parseInt(x)})))):l==="group"?o.push({type:l,value:h.value}):l==="decimal"?(y=!0,r.push({type:l,value:h.value,key:p(l)})):l==="fraction"?r.push(...h.value.split("").map(x=>({type:l,value:parseInt(x),key:p(l),pos:-1-d[l]}))):(g||y?c:a).push({type:l,value:h.value,key:p(l)})}const P=[];for(let h=o.length-1;h>=0;h--){const l=o[h];P.unshift(l.type==="integer"?{...l,key:p(l.type),pos:d[l.type]}:{...l,key:p(l.type)})}return{pre:a,integer:P,fraction:r,post:c,valueAsString:E,value:typeof n=="string"?parseFloat(n):n}}const G=String.raw,V=String.raw,K=_.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("animation-timing-function","linear(1,2)"),Z=_.BROWSER&&typeof CSS<"u"&&CSS.supports&&CSS.supports("line-height","mod(1,1)"),S=_.BROWSER&&typeof matchMedia<"u"?matchMedia("(prefers-reduced-motion: reduce)"):null,$="--_number-flow-d-opacity",U="--_number-flow-d-width",C="--_number-flow-dx",j="--_number-flow-d",J=(()=>{try{return CSS.registerProperty({name:$,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:C,syntax:"<length>",inherits:!0,initialValue:"0px"}),CSS.registerProperty({name:U,syntax:"<number>",inherits:!1,initialValue:"0"}),CSS.registerProperty({name:j,syntax:"<number>",inherits:!0,initialValue:"0"}),!0}catch{return!1}})(),T="var(--number-flow-char-height, 1em)",f="var(--number-flow-mask-height, 0.25em)",R=`calc(${f} / 2)`,A="var(--number-flow-mask-width, 0.5em)",m=`calc(${A} / var(--scale-x))`,w="#000 0, transparent 71%",L=V`:host{display:inline-block;direction:ltr;white-space:nowrap;isolation:isolate;line-height:${T} !important}.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(${U}) / var(--width));transform:translateX(var(${C})) scaleX(var(--scale-x));margin:0 calc(-1 * ${A});position:relative;-webkit-mask-image:linear-gradient(to right,transparent 0,#000 ${m},#000 calc(100% - ${m}),transparent ),linear-gradient(to bottom,transparent 0,#000 ${f},#000 calc(100% - ${f}),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% - ${f} * 2),calc(100% - ${m} * 2) 100%,${m} ${f},${m} ${f},${m} ${f},${m} ${f};-webkit-mask-position:center,center,top left,top right,bottom right,bottom left;-webkit-mask-repeat:no-repeat}.number__inner{padding:${R} ${A};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}.section--justify-left{transform-origin:center left}.section--justify-right{transform-origin:center right}.section > [inert],.symbol > [inert]{margin:0 !important;position:absolute !important;z-index:-1}.digit{display:inline-block;position:relative;--c:var(--current) + var(${j})}.digit__num,.number .section::after{padding:${R} 0}.digit__num{display:inline-block;--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[inert]{position:absolute;top:0;left:50%;transform:translateX(-50%) translateY(var(--y))}.digit:not(.is-spinning) .digit__num[inert]{display:none}.symbol__value{display:inline-block;mix-blend-mode:plus-lighter;white-space:pre}.section--justify-left .symbol > [inert]{left:0}.section--justify-right .symbol > [inert]{right:0}.animate-presence{opacity:calc(1 + var(${$}))}`,B=_.BROWSER?HTMLElement:class{},Q=V`:host{display:inline-block;direction:ltr;white-space:nowrap;line-height:${T} !important}span{display:inline-block}:host([data-will-change]) span{will-change:transform}.number,.digit{padding:${R} 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`:`symbol ${n.type}`}">${n.value}</span>`,v=(n,t)=>`<span part="${t}">${n.reduce((e,i)=>e+tt(i),"")}</span>`,et=n=>G`<template shadowroot="open" shadowrootmode="open"
2
- ><style>
3
- ${Q}</style
4
- ><span role="img" aria-label="${n.valueAsString}"
5
- >${v(n.pre,"left")}<span part="number" class="number"
6
- >${v(n.integer,"integer")}${v(n.fraction,"fraction")}</span
7
- >${v(n.post,"right")}</span
8
- ></template
9
- ><span
10
- style="font-kerning: none; display: inline-block; line-height: ${T} !important; padding: ${f} 0;"
11
- >${n.valueAsString}</span
12
- >`,D=Z&&K&&J;let b;class H extends B{constructor(){super(),this.created=!1,this.batched=!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){var r;if(t==null)return;const{pre:e,integer:i,fraction:s,post:a,value:o}=t;if(this.created){const c=this._data;this._data=t,this.computedTrend=typeof this.trend=="function"?this.trend(c.value,o):this.trend,this.computedAnimated=D&&this._animated&&(!this.respectMotionPreference||!(S!=null&&S.matches))&&X(this),(r=this.plugins)==null||r.forEach(d=>{var p;return(p=d.onUpdate)==null?void 0:p.call(d,t,c,this)}),this.batched||this.willUpdate(),this._pre.update(e),this._num.update({integer:i,fraction:s}),this._post.update(a),this.batched||this.didUpdate()}else{if(this._data=t,this.attachShadow({mode:"open"}),this._internals??(this._internals=this.attachInternals()),this._internals.role="img",typeof CSSStyleSheet<"u"&&this.shadowRoot.adoptedStyleSheets)b||(b=new CSSStyleSheet,b.replaceSync(L)),this.shadowRoot.adoptedStyleSheets=[b];else{const c=document.createElement("style");c.textContent=L,this.shadowRoot.appendChild(c)}this._pre=new N(this,e,{justify:"right",part:"left"}),this.shadowRoot.appendChild(this._pre.el),this._num=new it(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}this._internals.ariaLabel=t.valueAsString}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}}H.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),respectMotionPreference:!0,plugins:void 0,digits:void 0};class it{constructor(t,e,i,{className:s,...a}={}){this.flow=t,this._integer=new W(t,e,{justify:"right",part:"integer"}),this._fraction=new W(t,i,{justify:"left",part:"fraction"}),this._inner=u("span",{className:"number__inner"},[this._integer.el,this._fraction.el]),this.el=u("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"],[U]:[s,0]},{...this.flow.transformTiming,composite:"accumulate"})}}class O{constructor(t,e,{justify:i,className:s,...a},o){this.flow=t,this.children=new Map,this.onCharRemove=c=>()=>{this.children.delete(c)},this.justify=i;const r=e.map(c=>this.addChar(c).el);this.el=u("span",{...a,className:`section section--justify-${i} ${s??""}`},o?o(r):r)}addChar(t,{startDigitsAtZero:e=!1,...i}={}){const s=t.type==="integer"||t.type==="fraction"?new M(this,t.type,e?0:t.value,t.pos,{...i,onRemove:this.onCharRemove(t.key)}):new st(this,t.type,t.value,{...i,onRemove:this.onCharRemove(t.key)});return this.children.set(t.key,s),s}unpop(t){t.el.removeAttribute("inert"),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]=`${I(e.el,this.justify)}px`}),t.forEach(e=>{e.el.setAttribute("inert",""),e.present=!1})}addNewAndUpdateExisting(t){const e=new Map,i=new Map,s=this.justify==="left",a=s?"prepend":"append";if(q(t,o=>{let r;this.children.has(o.key)?(r=this.children.get(o.key),i.set(o,r),this.unpop(r),r.present=!0):(r=this.addChar(o,{startDigitsAtZero:!0,animateIn:!0}),e.set(o,r)),this.el[a](r.el)},{reverse:s}),this.flow.computedAnimated){const o=this.el.getBoundingClientRect();e.forEach(r=>{r.willUpdate(o)})}e.forEach((o,r)=>{o.update(r.value)}),i.forEach((o,r)=>{o.update(r.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 W 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 M&&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({[$]:[-.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,t?this.el.removeAttribute("inert"):this.el.setAttribute("inert",""),!this.flow.computedAnimated){t||this._remove();return}this.el.style.setProperty("--_number-flow-d-opacity",t?"0":"-.999"),this.el.animate({[$]: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 M extends F{constructor(t,e,i,s,a){var d,p;const o=(((p=(d=t.flow.digits)==null?void 0:d[s])==null?void 0:p.max)??9)+1,r=Array.from({length:o}).map((E,g)=>{const y=u("span",{className:"digit__num"},[document.createTextNode(String(g))]);return g!==i&&y.setAttribute("inert",""),y.style.setProperty("--n",String(g)),y}),c=u("span",{part:`digit ${e}-digit`,className:"digit"},r);c.style.setProperty("--current",String(i)),c.style.setProperty("--length",String(o)),super(t,i,c,a),this.pos=s,this._onAnimationsFinish=()=>{this.el.classList.remove("is-spinning")},this._numbers=r,this.length=o}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){this.el.style.setProperty("--current",String(t)),this._numbers.forEach((e,i)=>i===t?e.removeAttribute("inert"):e.setAttribute("inert","")),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,o=this._prevCenter-a;o&&this.el.animate({transform:[`translateX(${o}px)`,"none"]},{...this.flow.transformTiming,composite:"accumulate"});const r=this.getDelta();r&&(this.el.classList.add("is-spinning"),this.el.animate({[j]:[-r,0]},{...this.flow.spinTiming??this.flow.transformTiming,composite:"accumulate"}),this.flow.addEventListener("animationsfinish",this._onAnimationsFinish,{once:!0}))}getDelta(){var i;if(this.flow.plugins)for(const s of this.flow.plugins){const a=(i=s.getDelta)==null?void 0:i.call(s,this.value,this._prevValue,this);if(a!=null)return a}const t=this.value-this._prevValue,e=this.flow.computedTrend||Math.sign(t);return e<0&&this.value>this._prevValue?this.value-this.length-this._prevValue:e>0&&this.value<this._prevValue?this.length-this._prevValue+this.value:t}}class st extends F{constructor(t,e,i,s){const a=u("span",{className:"symbol__value",textContent:i});super(t,i,u("span",{part:`symbol ${e}`,className:"symbol"},[a]),s),this.type=e,this._children=new Map,this._onChildRemove=o=>()=>{this._children.delete(o)},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);e&&(e.present=!1);const i=this._children.get(t);if(i)i.present=!0;else{const s=u("span",{className:"symbol__value",textContent:t});this.el.appendChild(s),this._children.set(t,new k(this.flow,s,{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.Digit=M;exports.NumberFlowLite=H;exports.ServerSafeHTMLElement=B;exports.canAnimate=D;exports.define=z;exports.formatToData=Y;exports.prefersReducedMotion=S;exports.renderInnerHTML=et;