torph 0.0.6 → 0.0.8

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.
@@ -1,4 +1,22 @@
1
- import { TextMorphOptions } from '../../lib/text-morph/types';
1
+ interface SpringParams {
2
+ stiffness?: number;
3
+ damping?: number;
4
+ mass?: number;
5
+ precision?: number;
6
+ }
7
+
8
+ interface TextMorphOptions {
9
+ debug?: boolean;
10
+ element: HTMLElement;
11
+ locale?: Intl.LocalesArgument;
12
+ scale?: boolean;
13
+ duration?: number;
14
+ ease?: string | SpringParams;
15
+ disabled?: boolean;
16
+ respectReducedMotion?: boolean;
17
+ onAnimationStart?: () => void;
18
+ onAnimationComplete?: () => void;
19
+ }
2
20
 
3
21
  interface TextMorphProps extends Omit<TextMorphOptions, "element"> {
4
22
  text: string;
@@ -0,0 +1,28 @@
1
+ interface SpringParams {
2
+ stiffness?: number;
3
+ damping?: number;
4
+ mass?: number;
5
+ precision?: number;
6
+ }
7
+
8
+ interface TextMorphOptions {
9
+ debug?: boolean;
10
+ element: HTMLElement;
11
+ locale?: Intl.LocalesArgument;
12
+ scale?: boolean;
13
+ duration?: number;
14
+ ease?: string | SpringParams;
15
+ disabled?: boolean;
16
+ respectReducedMotion?: boolean;
17
+ onAnimationStart?: () => void;
18
+ onAnimationComplete?: () => void;
19
+ }
20
+
21
+ interface TextMorphProps extends Omit<TextMorphOptions, "element"> {
22
+ text: string;
23
+ class?: string;
24
+ style?: string;
25
+ as?: string;
26
+ }
27
+
28
+ export type { TextMorphProps };
@@ -1,2 +1,23 @@
1
- 'use strict';require('svelte/internal/disclose-version');var e=require('svelte/internal/client'),svelte=require('svelte'),textMorph=require('../lib/text-morph');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var e__namespace=/*#__PURE__*/_interopNamespace(e);function d(l,t){e__namespace.push(t,true);let s=e__namespace.prop(t,"locale",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.locale),p=e__namespace.prop(t,"duration",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.duration),c=e__namespace.prop(t,"ease",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.ease),u=e__namespace.prop(t,"scale",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.scale),m=e__namespace.prop(t,"debug",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.debug),f=e__namespace.prop(t,"disabled",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.disabled),g=e__namespace.prop(t,"respectReducedMotion",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),b=e__namespace.prop(t,"onAnimationStart",19,()=>{}),M=e__namespace.prop(t,"onAnimationComplete",19,()=>{}),$=e__namespace.prop(t,"as",3,textMorph.DEFAULT_AS),A=e__namespace.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),n=e__namespace.state(void 0),a=e__namespace.state(null);svelte.onMount(()=>(e__namespace.get(n)&&(e__namespace.set(a,new textMorph.TextMorph({element:e__namespace.get(n),locale:s(),duration:p(),ease:c(),debug:m(),scale:u(),disabled:f(),respectReducedMotion:g(),onAnimationStart:b(),onAnimationComplete:M()}),true),e__namespace.get(a).update(t.text)),()=>{e__namespace.get(a)?.destroy();})),e__namespace.user_effect(()=>{e__namespace.get(a)&&e__namespace.get(a).update(t.text);});var i=e__namespace.comment(),h=e__namespace.first_child(i);e__namespace.element(h,$,false,(r,v)=>{e__namespace.bind_this(r,x=>e__namespace.set(n,x,true),()=>e__namespace.get(n)),e__namespace.attribute_effect(r,()=>({...A}));}),e__namespace.append(l,i),e__namespace.pop();}exports.TextMorph=d;//# sourceMappingURL=index.js.map
2
- //# sourceMappingURL=index.js.map
1
+ 'use strict';require('svelte/internal/disclose-version');var a=require('svelte/internal/client');function _interopNamespace(e){if(e&&e.__esModule)return e;var n=Object.create(null);if(e){Object.keys(e).forEach(function(k){if(k!=='default'){var d=Object.getOwnPropertyDescriptor(e,k);Object.defineProperty(n,k,d.get?d:{enumerable:true,get:function(){return e[k]}});}})}n.default=e;return Object.freeze(n)}var a__namespace=/*#__PURE__*/_interopNamespace(a);function H(e,t,n){if(n<1){let u=t*Math.sqrt(1-n*n);return 1-Math.exp(-n*t*e)*(Math.cos(u*e)+n*t/u*Math.sin(u*e))}let o=Math.sqrt(n*n-1),r=-t*(n+o),i=-t*(n-o),s=-r/(i-r);return 1-(1-s)*Math.exp(r*e)-s*Math.exp(i*e)}function Z(e,t,n){let i=0;for(let s=0;s<10;s+=.001)if(Math.abs(H(s,e,t)-1)>n)i=0;else if(i+=.001,i>.1)return Math.ceil((s-i+.001)*1e3);return Math.ceil(10*1e3)}var C=new Map;function N(e){let{stiffness:t=100,damping:n=10,mass:o=1,precision:r=.001}=e??{},i=`${t}:${n}:${o}:${r}`,s=C.get(i);if(s)return s;let c=Math.sqrt(t/o),u=n/(2*Math.sqrt(t*o)),p=Z(c,u,r),d=Math.min(100,Math.max(32,Math.round(p/15))),m=[];for(let f=0;f<d;f++){let T=f/(d-1)*(p/1e3),M=f===d-1?1:H(T,c,u);m.push(Math.round(M*1e4)/1e4+"");}for(;m.length>2&&m[m.length-2]==="1";)m.splice(m.length-2,1);let l={easing:`linear(${m.join(", ")})`,duration:p};return C.set(i,l),l}function k(e,t){let n=e.includes(" ");if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(t,{granularity:n?"word":"grapheme"}).segment(e)[Symbol.iterator]();return tt(r)}return nt(e,n)}function tt(e){return Array.from(e).reduce((t,n)=>n.segment===" "?[...t,{id:`space-${n.index}`,string:"\xA0"}]:t.find(r=>r.string===n.segment)?[...t,{id:`${n.segment}-${n.index}`,string:n.segment}]:[...t,{id:n.segment,string:n.segment}],[])}function et(e,t,n){let o=e.find(r=>r.string===t);e.push(o?{id:`${t}-${n}`,string:t}:{id:t,string:t});}function nt(e,t){let n=t?e.split(" "):e.split(""),o=[];return n.forEach((r,i)=>{t&&i>0&&o.push({id:`space-${i}`,string:"\xA0"}),et(o,r,i);}),o}var b="torph-root",A="torph-item",h="torph-id",y="torph-exiting",S="torph-debug";function v(e){let t=Array.from(e.children),n={},o=e.getBoundingClientRect();return t.forEach((r,i)=>{if(r.hasAttribute(y))return;let s=r.getAttribute(h)||`child-${i}`,c=r.getBoundingClientRect();n[s]={x:c.left-o.left,y:c.top-o.top};}),n}function $(e,t,n){let o=e[n],r=t[n];return !o||!r?{dx:0,dy:0}:{dx:o.x-r.x,dy:o.y-r.y}}function L(e,t,n,o="backward-first"){let[r,i]=o==="backward-first"?["backward","forward"]:["forward","backward"],s=c=>{if(c==="backward"){for(let u=e-1;u>=0;u--)if(n.has(t[u]))return t[u]}else for(let u=e+1;u<t.length;u++)if(n.has(t[u]))return t[u];return null};return s(r)??s(i)}function F(e,t,n,o){let r=new Set(n.filter((s,c)=>o.has(s)&&!t.has(e[c]))),i=new Map;for(let s=0;s<e.length;s++){let c=e[s];t.has(c)&&i.set(c,L(s,n,r,"forward-first"));}return i}function P(e,t){return Math.min(e*t,150)}function I(e){let t=getComputedStyle(e).transform;if(!t||t==="none")return {tx:0,ty:0};let n=t.match(/matrix\(([^)]+)\)/);if(!n)return {tx:0,ty:0};let o=n[1].split(",").map(Number);return {tx:o[4]||0,ty:o[5]||0}}function rt(e){let{tx:t,ty:n}=I(e),o=Number(getComputedStyle(e).opacity)||1;return e.getAnimations().forEach(r=>r.cancel()),{tx:t,ty:n,opacity:o}}function X(e,t){let{dx:n,dy:o,duration:r,ease:i,scale:s}=t;e.animate({transform:s?`translate(${n}px, ${o}px) scale(0.95)`:`translate(${n}px, ${o}px)`,offset:1},{duration:r,easing:i,fill:"both"});let c=e.animate({opacity:0,offset:1},{duration:P(r,.25),easing:"linear",fill:"both"});c.onfinish=()=>e.remove();}function B(e,t){let{deltaX:n,deltaY:o,isNew:r,duration:i,ease:s}=t,c=rt(e),u=n+c.tx,p=o+c.ty;e.animate({transform:`translate(${u}px, ${p}px) scale(${r?.95:1})`,offset:0},{duration:i,easing:s,fill:"both"});let d=r&&c.opacity>=1?0:c.opacity;d<1&&e.animate([{opacity:d},{opacity:1}],{duration:P(i,r?.5:.25),easing:"linear",fill:"both"});}var E=null;function G(e,t,n,o,r){if(E&&(E(),E=null),t===0||n===0)return;e.style.width="auto",e.style.height="auto",e.offsetWidth;let i=e.offsetWidth,s=e.offsetHeight;e.style.width=`${t}px`,e.style.height=`${n}px`,e.offsetWidth,e.style.width=`${i}px`,e.style.height=`${s}px`;function c(){e.removeEventListener("transitionend",u),clearTimeout(p),E=null,e.style.width="auto",e.style.height="auto",r?.();}function u(d){d.target===e&&(d.propertyName!=="width"&&d.propertyName!=="height"||c());}e.addEventListener("transitionend",u);let p=setTimeout(c,o+50);E=()=>{e.removeEventListener("transitionend",u),clearTimeout(p),E=null;};}function U(e){let n=e[0]?.parentElement?.getBoundingClientRect(),o=e.map(r=>{let{tx:i,ty:s}=I(r),c=Number(getComputedStyle(r).opacity)||1;r.getAnimations().forEach(p=>p.cancel());let u=r.getBoundingClientRect();return {left:(n?u.left-n.left:u.left)+i,top:(n?u.top-n.top:u.top)+s,width:u.width,height:u.height,opacity:c}});e.forEach((r,i)=>{let s=o[i];r.setAttribute(y,""),r.style.position="absolute",r.style.pointerEvents="none",r.style.left=`${s.left}px`,r.style.top=`${s.top}px`,r.style.width=`${s.width}px`,r.style.height=`${s.height}px`,r.style.opacity=String(s.opacity);});}function K(e,t,n,o){let r=new Map;t.forEach(i=>{let s=i.getAttribute(h);n.has(s)&&!i.hasAttribute(y)&&(r.set(s,i),i.remove());}),Array.from(e.childNodes).forEach(i=>{i.nodeType===Node.TEXT_NODE&&i.remove();}),o.forEach(i=>{let s=r.get(i.id);if(s)s.textContent=i.string,e.appendChild(s);else {let c=document.createElement("span");c.setAttribute(A,""),c.setAttribute(h,i.id),c.textContent=i.string,e.appendChild(c);}});}var it=`
2
+ [${b}] {
3
+ display: inline-block;
4
+ position: relative;
5
+ will-change: width, height;
6
+ transition-property: width, height;
7
+ white-space: nowrap;
8
+ }
9
+
10
+ [${A}] {
11
+ display: inline-block;
12
+ will-change: opacity, transform;
13
+ transform: none;
14
+ opacity: 1;
15
+ }
16
+
17
+ [${b}][${S}] {
18
+ outline: 2px solid magenta;
19
+ [${A}] {
20
+ outline: 2px solid cyan;
21
+ outline-offset: -4px;
22
+ }
23
+ }`,x=null,O=0;function W(){O++,!x&&(x=document.createElement("style"),x.dataset.torph="true",x.textContent=it,document.head.appendChild(x));}function j(){O--,!(O>0||!x)&&(x.remove(),x=null);}function q(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let e=window.matchMedia("(prefers-reduced-motion: reduce)"),t={prefersReducedMotion:e.matches,destroy:o};function n(r){t.prefersReducedMotion=r.matches;}function o(){e.removeEventListener("change",n);}return e.addEventListener("change",n),t}var R=class e{instance=null;lastText="";configKey="";attach(t,n){this.instance?.destroy(),this.instance=new w({element:t,...n}),this.configKey=e.serializeConfig(n),this.lastText&&this.instance.update(this.lastText);}update(t){this.lastText=t,this.instance?.update(t);}needsRecreate(t){return e.serializeConfig(t)!==this.configKey}destroy(){this.instance?.destroy(),this.instance=null;}static serializeConfig(t){return JSON.stringify({ease:t.ease,duration:t.duration,locale:t.locale,scale:t.scale,debug:t.debug,disabled:t.disabled,respectReducedMotion:t.respectReducedMotion})}};var Y="span",g={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},w=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(t){let{ease:n,...o}={...g,...t},r,i;if(typeof n=="object"){let s=N(n);r=s.easing,i=s.duration;}else r=n,i=o.duration;this.options={...o,ease:r,duration:i},this.element=t.element,this.options.respectReducedMotion&&(this.reducedMotion=q()),this.isDisabled()||(this.element.setAttribute(b,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,t.debug&&this.element.setAttribute(S,"")),this.data="",this.isDisabled()||W();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(t=>t.cancel()),this.element.removeAttribute(b),this.element.removeAttribute(S),j();}isDisabled(){return !!(this.options.disabled||this.reducedMotion?.prefersReducedMotion)}update(t){if(t!==this.data){if(this.data=t,this.isDisabled()){typeof t=="string"&&(this.element.textContent=t);return}if(this.data instanceof HTMLElement)throw new Error("HTMLElement not yet supported");this.options.onAnimationStart&&!this.isInitialRender&&this.options.onAnimationStart(),this.createTextGroup(this.data,this.element);}}createTextGroup(t,n){let o=n.offsetWidth,r=n.offsetHeight,i=k(t,this.options.locale);this.prevMeasures=v(this.element);let s=Array.from(n.children),c=new Set(i.map(l=>l.id)),u=s.filter(l=>!c.has(l.getAttribute(h))&&!l.hasAttribute(y)),p=new Set(u),d=s.map(l=>l.getAttribute(h)),m=F(s,p,d,c);if(U(u),K(n,s,c,i),this.currentMeasures=v(this.element),this.updateStyles(i),u.forEach(l=>{if(this.isInitialRender){l.remove();return}let f=m.get(l),{dx:T,dy:M}=f?$(this.currentMeasures,this.prevMeasures,f):{dx:0,dy:0};X(l,{dx:T,dy:M,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,n.style.width="auto",n.style.height="auto";return}G(n,o,r,this.options.duration,this.options.onAnimationComplete);}updateStyles(t){if(this.isInitialRender)return;let n=Array.from(this.element.children),o=t.map(i=>i.id),r=new Set(o.filter(i=>this.prevMeasures[i]));n.forEach((i,s)=>{if(i.hasAttribute(y))return;let c=i.getAttribute(h)||`child-${s}`,u=!this.prevMeasures[c],p=u?L(t.findIndex(l=>l.id===c),o,r):c,{dx:d,dy:m}=p?$(this.prevMeasures,this.currentMeasures,p):{dx:0,dy:0};B(i,{deltaX:d,deltaY:m,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};function Q(e,t){a__namespace.push(t,true);let n=a__namespace.prop(t,"locale",19,()=>g.locale),o=a__namespace.prop(t,"duration",19,()=>g.duration),r=a__namespace.prop(t,"ease",19,()=>g.ease),i=a__namespace.prop(t,"scale",19,()=>g.scale),s=a__namespace.prop(t,"debug",19,()=>g.debug),c=a__namespace.prop(t,"disabled",19,()=>g.disabled),u=a__namespace.prop(t,"respectReducedMotion",19,()=>g.respectReducedMotion),p=a__namespace.prop(t,"onAnimationStart",19,()=>{}),d=a__namespace.prop(t,"onAnimationComplete",19,()=>{}),m=a__namespace.prop(t,"as",3,Y),l=a__namespace.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),f=a__namespace.state(void 0),T=new R,M=a__namespace.derived(()=>({locale:n(),duration:o(),ease:r(),debug:s(),scale:i(),disabled:c(),respectReducedMotion:u(),onAnimationStart:p(),onAnimationComplete:d()})),J=a__namespace.derived(()=>R.serializeConfig(a__namespace.get(M)));a__namespace.user_effect(()=>{if(a__namespace.get(J),a__namespace.get(f))return T.attach(a__namespace.get(f),a__namespace.get(M)),()=>{T.destroy();}}),a__namespace.user_effect(()=>{T.update(t.text);});var _=a__namespace.comment(),z=a__namespace.first_child(_);a__namespace.element(z,m,false,(D,st)=>{a__namespace.bind_this(D,V=>a__namespace.set(f,V,true),()=>a__namespace.get(f)),a__namespace.attribute_effect(D,()=>({...l}));}),a__namespace.append(e,_),a__namespace.pop();}exports.TextMorph=Q;
@@ -1,2 +1,23 @@
1
- import'svelte/internal/disclose-version';import*as e from'svelte/internal/client';import {onMount}from'svelte';import {DEFAULT_TEXT_MORPH_OPTIONS,DEFAULT_AS,TextMorph}from'../lib/text-morph';function d(l,t){e.push(t,true);let s=e.prop(t,"locale",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.locale),p=e.prop(t,"duration",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.duration),c=e.prop(t,"ease",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.ease),u=e.prop(t,"scale",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.scale),m=e.prop(t,"debug",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.debug),f=e.prop(t,"disabled",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.disabled),g=e.prop(t,"respectReducedMotion",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),b=e.prop(t,"onAnimationStart",19,()=>{}),M=e.prop(t,"onAnimationComplete",19,()=>{}),$=e.prop(t,"as",3,DEFAULT_AS),A=e.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),n=e.state(void 0),a=e.state(null);onMount(()=>(e.get(n)&&(e.set(a,new TextMorph({element:e.get(n),locale:s(),duration:p(),ease:c(),debug:m(),scale:u(),disabled:f(),respectReducedMotion:g(),onAnimationStart:b(),onAnimationComplete:M()}),true),e.get(a).update(t.text)),()=>{e.get(a)?.destroy();})),e.user_effect(()=>{e.get(a)&&e.get(a).update(t.text);});var i=e.comment(),h=e.first_child(i);e.element(h,$,false,(r,v)=>{e.bind_this(r,x=>e.set(n,x,true),()=>e.get(n)),e.attribute_effect(r,()=>({...A}));}),e.append(l,i),e.pop();}export{d as TextMorph};//# sourceMappingURL=index.mjs.map
2
- //# sourceMappingURL=index.mjs.map
1
+ import'svelte/internal/disclose-version';import*as a from'svelte/internal/client';function H(e,t,n){if(n<1){let u=t*Math.sqrt(1-n*n);return 1-Math.exp(-n*t*e)*(Math.cos(u*e)+n*t/u*Math.sin(u*e))}let o=Math.sqrt(n*n-1),r=-t*(n+o),i=-t*(n-o),s=-r/(i-r);return 1-(1-s)*Math.exp(r*e)-s*Math.exp(i*e)}function Z(e,t,n){let i=0;for(let s=0;s<10;s+=.001)if(Math.abs(H(s,e,t)-1)>n)i=0;else if(i+=.001,i>.1)return Math.ceil((s-i+.001)*1e3);return Math.ceil(10*1e3)}var C=new Map;function N(e){let{stiffness:t=100,damping:n=10,mass:o=1,precision:r=.001}=e??{},i=`${t}:${n}:${o}:${r}`,s=C.get(i);if(s)return s;let c=Math.sqrt(t/o),u=n/(2*Math.sqrt(t*o)),p=Z(c,u,r),d=Math.min(100,Math.max(32,Math.round(p/15))),m=[];for(let f=0;f<d;f++){let T=f/(d-1)*(p/1e3),M=f===d-1?1:H(T,c,u);m.push(Math.round(M*1e4)/1e4+"");}for(;m.length>2&&m[m.length-2]==="1";)m.splice(m.length-2,1);let l={easing:`linear(${m.join(", ")})`,duration:p};return C.set(i,l),l}function k(e,t){let n=e.includes(" ");if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(t,{granularity:n?"word":"grapheme"}).segment(e)[Symbol.iterator]();return tt(r)}return nt(e,n)}function tt(e){return Array.from(e).reduce((t,n)=>n.segment===" "?[...t,{id:`space-${n.index}`,string:"\xA0"}]:t.find(r=>r.string===n.segment)?[...t,{id:`${n.segment}-${n.index}`,string:n.segment}]:[...t,{id:n.segment,string:n.segment}],[])}function et(e,t,n){let o=e.find(r=>r.string===t);e.push(o?{id:`${t}-${n}`,string:t}:{id:t,string:t});}function nt(e,t){let n=t?e.split(" "):e.split(""),o=[];return n.forEach((r,i)=>{t&&i>0&&o.push({id:`space-${i}`,string:"\xA0"}),et(o,r,i);}),o}var b="torph-root",A="torph-item",h="torph-id",y="torph-exiting",S="torph-debug";function v(e){let t=Array.from(e.children),n={},o=e.getBoundingClientRect();return t.forEach((r,i)=>{if(r.hasAttribute(y))return;let s=r.getAttribute(h)||`child-${i}`,c=r.getBoundingClientRect();n[s]={x:c.left-o.left,y:c.top-o.top};}),n}function $(e,t,n){let o=e[n],r=t[n];return !o||!r?{dx:0,dy:0}:{dx:o.x-r.x,dy:o.y-r.y}}function L(e,t,n,o="backward-first"){let[r,i]=o==="backward-first"?["backward","forward"]:["forward","backward"],s=c=>{if(c==="backward"){for(let u=e-1;u>=0;u--)if(n.has(t[u]))return t[u]}else for(let u=e+1;u<t.length;u++)if(n.has(t[u]))return t[u];return null};return s(r)??s(i)}function F(e,t,n,o){let r=new Set(n.filter((s,c)=>o.has(s)&&!t.has(e[c]))),i=new Map;for(let s=0;s<e.length;s++){let c=e[s];t.has(c)&&i.set(c,L(s,n,r,"forward-first"));}return i}function P(e,t){return Math.min(e*t,150)}function I(e){let t=getComputedStyle(e).transform;if(!t||t==="none")return {tx:0,ty:0};let n=t.match(/matrix\(([^)]+)\)/);if(!n)return {tx:0,ty:0};let o=n[1].split(",").map(Number);return {tx:o[4]||0,ty:o[5]||0}}function rt(e){let{tx:t,ty:n}=I(e),o=Number(getComputedStyle(e).opacity)||1;return e.getAnimations().forEach(r=>r.cancel()),{tx:t,ty:n,opacity:o}}function X(e,t){let{dx:n,dy:o,duration:r,ease:i,scale:s}=t;e.animate({transform:s?`translate(${n}px, ${o}px) scale(0.95)`:`translate(${n}px, ${o}px)`,offset:1},{duration:r,easing:i,fill:"both"});let c=e.animate({opacity:0,offset:1},{duration:P(r,.25),easing:"linear",fill:"both"});c.onfinish=()=>e.remove();}function B(e,t){let{deltaX:n,deltaY:o,isNew:r,duration:i,ease:s}=t,c=rt(e),u=n+c.tx,p=o+c.ty;e.animate({transform:`translate(${u}px, ${p}px) scale(${r?.95:1})`,offset:0},{duration:i,easing:s,fill:"both"});let d=r&&c.opacity>=1?0:c.opacity;d<1&&e.animate([{opacity:d},{opacity:1}],{duration:P(i,r?.5:.25),easing:"linear",fill:"both"});}var E=null;function G(e,t,n,o,r){if(E&&(E(),E=null),t===0||n===0)return;e.style.width="auto",e.style.height="auto",e.offsetWidth;let i=e.offsetWidth,s=e.offsetHeight;e.style.width=`${t}px`,e.style.height=`${n}px`,e.offsetWidth,e.style.width=`${i}px`,e.style.height=`${s}px`;function c(){e.removeEventListener("transitionend",u),clearTimeout(p),E=null,e.style.width="auto",e.style.height="auto",r?.();}function u(d){d.target===e&&(d.propertyName!=="width"&&d.propertyName!=="height"||c());}e.addEventListener("transitionend",u);let p=setTimeout(c,o+50);E=()=>{e.removeEventListener("transitionend",u),clearTimeout(p),E=null;};}function U(e){let n=e[0]?.parentElement?.getBoundingClientRect(),o=e.map(r=>{let{tx:i,ty:s}=I(r),c=Number(getComputedStyle(r).opacity)||1;r.getAnimations().forEach(p=>p.cancel());let u=r.getBoundingClientRect();return {left:(n?u.left-n.left:u.left)+i,top:(n?u.top-n.top:u.top)+s,width:u.width,height:u.height,opacity:c}});e.forEach((r,i)=>{let s=o[i];r.setAttribute(y,""),r.style.position="absolute",r.style.pointerEvents="none",r.style.left=`${s.left}px`,r.style.top=`${s.top}px`,r.style.width=`${s.width}px`,r.style.height=`${s.height}px`,r.style.opacity=String(s.opacity);});}function K(e,t,n,o){let r=new Map;t.forEach(i=>{let s=i.getAttribute(h);n.has(s)&&!i.hasAttribute(y)&&(r.set(s,i),i.remove());}),Array.from(e.childNodes).forEach(i=>{i.nodeType===Node.TEXT_NODE&&i.remove();}),o.forEach(i=>{let s=r.get(i.id);if(s)s.textContent=i.string,e.appendChild(s);else {let c=document.createElement("span");c.setAttribute(A,""),c.setAttribute(h,i.id),c.textContent=i.string,e.appendChild(c);}});}var it=`
2
+ [${b}] {
3
+ display: inline-block;
4
+ position: relative;
5
+ will-change: width, height;
6
+ transition-property: width, height;
7
+ white-space: nowrap;
8
+ }
9
+
10
+ [${A}] {
11
+ display: inline-block;
12
+ will-change: opacity, transform;
13
+ transform: none;
14
+ opacity: 1;
15
+ }
16
+
17
+ [${b}][${S}] {
18
+ outline: 2px solid magenta;
19
+ [${A}] {
20
+ outline: 2px solid cyan;
21
+ outline-offset: -4px;
22
+ }
23
+ }`,x=null,O=0;function W(){O++,!x&&(x=document.createElement("style"),x.dataset.torph="true",x.textContent=it,document.head.appendChild(x));}function j(){O--,!(O>0||!x)&&(x.remove(),x=null);}function q(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let e=window.matchMedia("(prefers-reduced-motion: reduce)"),t={prefersReducedMotion:e.matches,destroy:o};function n(r){t.prefersReducedMotion=r.matches;}function o(){e.removeEventListener("change",n);}return e.addEventListener("change",n),t}var R=class e{instance=null;lastText="";configKey="";attach(t,n){this.instance?.destroy(),this.instance=new w({element:t,...n}),this.configKey=e.serializeConfig(n),this.lastText&&this.instance.update(this.lastText);}update(t){this.lastText=t,this.instance?.update(t);}needsRecreate(t){return e.serializeConfig(t)!==this.configKey}destroy(){this.instance?.destroy(),this.instance=null;}static serializeConfig(t){return JSON.stringify({ease:t.ease,duration:t.duration,locale:t.locale,scale:t.scale,debug:t.debug,disabled:t.disabled,respectReducedMotion:t.respectReducedMotion})}};var Y="span",g={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},w=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(t){let{ease:n,...o}={...g,...t},r,i;if(typeof n=="object"){let s=N(n);r=s.easing,i=s.duration;}else r=n,i=o.duration;this.options={...o,ease:r,duration:i},this.element=t.element,this.options.respectReducedMotion&&(this.reducedMotion=q()),this.isDisabled()||(this.element.setAttribute(b,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,t.debug&&this.element.setAttribute(S,"")),this.data="",this.isDisabled()||W();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(t=>t.cancel()),this.element.removeAttribute(b),this.element.removeAttribute(S),j();}isDisabled(){return !!(this.options.disabled||this.reducedMotion?.prefersReducedMotion)}update(t){if(t!==this.data){if(this.data=t,this.isDisabled()){typeof t=="string"&&(this.element.textContent=t);return}if(this.data instanceof HTMLElement)throw new Error("HTMLElement not yet supported");this.options.onAnimationStart&&!this.isInitialRender&&this.options.onAnimationStart(),this.createTextGroup(this.data,this.element);}}createTextGroup(t,n){let o=n.offsetWidth,r=n.offsetHeight,i=k(t,this.options.locale);this.prevMeasures=v(this.element);let s=Array.from(n.children),c=new Set(i.map(l=>l.id)),u=s.filter(l=>!c.has(l.getAttribute(h))&&!l.hasAttribute(y)),p=new Set(u),d=s.map(l=>l.getAttribute(h)),m=F(s,p,d,c);if(U(u),K(n,s,c,i),this.currentMeasures=v(this.element),this.updateStyles(i),u.forEach(l=>{if(this.isInitialRender){l.remove();return}let f=m.get(l),{dx:T,dy:M}=f?$(this.currentMeasures,this.prevMeasures,f):{dx:0,dy:0};X(l,{dx:T,dy:M,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,n.style.width="auto",n.style.height="auto";return}G(n,o,r,this.options.duration,this.options.onAnimationComplete);}updateStyles(t){if(this.isInitialRender)return;let n=Array.from(this.element.children),o=t.map(i=>i.id),r=new Set(o.filter(i=>this.prevMeasures[i]));n.forEach((i,s)=>{if(i.hasAttribute(y))return;let c=i.getAttribute(h)||`child-${s}`,u=!this.prevMeasures[c],p=u?L(t.findIndex(l=>l.id===c),o,r):c,{dx:d,dy:m}=p?$(this.prevMeasures,this.currentMeasures,p):{dx:0,dy:0};B(i,{deltaX:d,deltaY:m,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};function Q(e,t){a.push(t,true);let n=a.prop(t,"locale",19,()=>g.locale),o=a.prop(t,"duration",19,()=>g.duration),r=a.prop(t,"ease",19,()=>g.ease),i=a.prop(t,"scale",19,()=>g.scale),s=a.prop(t,"debug",19,()=>g.debug),c=a.prop(t,"disabled",19,()=>g.disabled),u=a.prop(t,"respectReducedMotion",19,()=>g.respectReducedMotion),p=a.prop(t,"onAnimationStart",19,()=>{}),d=a.prop(t,"onAnimationComplete",19,()=>{}),m=a.prop(t,"as",3,Y),l=a.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),f=a.state(void 0),T=new R,M=a.derived(()=>({locale:n(),duration:o(),ease:r(),debug:s(),scale:i(),disabled:c(),respectReducedMotion:u(),onAnimationStart:p(),onAnimationComplete:d()})),J=a.derived(()=>R.serializeConfig(a.get(M)));a.user_effect(()=>{if(a.get(J),a.get(f))return T.attach(a.get(f),a.get(M)),()=>{T.destroy();}}),a.user_effect(()=>{T.update(t.text);});var _=a.comment(),z=a.first_child(_);a.element(z,m,false,(D,st)=>{a.bind_this(D,V=>a.set(f,V,true),()=>a.get(f)),a.attribute_effect(D,()=>({...l}));}),a.append(e,_),a.pop();}export{Q as TextMorph};
@@ -1,4 +1,147 @@
1
- import { TextMorphOptions } from '../../lib/text-morph/types';
1
+ import * as vue from 'vue';
2
+
3
+ declare const _default: vue.DefineComponent<vue.ExtractPropTypes<{
4
+ text: {
5
+ type: StringConstructor;
6
+ required: true;
7
+ };
8
+ class: {
9
+ type: any;
10
+ default: undefined;
11
+ };
12
+ style: {
13
+ type: ObjectConstructor;
14
+ default: undefined;
15
+ };
16
+ as: {
17
+ type: StringConstructor;
18
+ default: string;
19
+ };
20
+ locale: {
21
+ type: StringConstructor;
22
+ default: "en";
23
+ };
24
+ duration: {
25
+ type: NumberConstructor;
26
+ default: 400;
27
+ };
28
+ ease: {
29
+ type: any;
30
+ default: "cubic-bezier(0.19, 1, 0.22, 1)";
31
+ };
32
+ scale: {
33
+ type: BooleanConstructor;
34
+ default: true;
35
+ };
36
+ debug: {
37
+ type: BooleanConstructor;
38
+ default: undefined;
39
+ };
40
+ disabled: {
41
+ type: BooleanConstructor;
42
+ default: false;
43
+ };
44
+ respectReducedMotion: {
45
+ type: BooleanConstructor;
46
+ default: true;
47
+ };
48
+ onAnimationStart: {
49
+ type: FunctionConstructor;
50
+ default: undefined;
51
+ };
52
+ onAnimationComplete: {
53
+ type: FunctionConstructor;
54
+ default: undefined;
55
+ };
56
+ }>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
57
+ [key: string]: any;
58
+ }>, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
59
+ text: {
60
+ type: StringConstructor;
61
+ required: true;
62
+ };
63
+ class: {
64
+ type: any;
65
+ default: undefined;
66
+ };
67
+ style: {
68
+ type: ObjectConstructor;
69
+ default: undefined;
70
+ };
71
+ as: {
72
+ type: StringConstructor;
73
+ default: string;
74
+ };
75
+ locale: {
76
+ type: StringConstructor;
77
+ default: "en";
78
+ };
79
+ duration: {
80
+ type: NumberConstructor;
81
+ default: 400;
82
+ };
83
+ ease: {
84
+ type: any;
85
+ default: "cubic-bezier(0.19, 1, 0.22, 1)";
86
+ };
87
+ scale: {
88
+ type: BooleanConstructor;
89
+ default: true;
90
+ };
91
+ debug: {
92
+ type: BooleanConstructor;
93
+ default: undefined;
94
+ };
95
+ disabled: {
96
+ type: BooleanConstructor;
97
+ default: false;
98
+ };
99
+ respectReducedMotion: {
100
+ type: BooleanConstructor;
101
+ default: true;
102
+ };
103
+ onAnimationStart: {
104
+ type: FunctionConstructor;
105
+ default: undefined;
106
+ };
107
+ onAnimationComplete: {
108
+ type: FunctionConstructor;
109
+ default: undefined;
110
+ };
111
+ }>> & Readonly<{}>, {
112
+ duration: number;
113
+ ease: any;
114
+ scale: boolean;
115
+ style: Record<string, any>;
116
+ debug: boolean;
117
+ locale: string;
118
+ disabled: boolean;
119
+ respectReducedMotion: boolean;
120
+ onAnimationStart: Function;
121
+ onAnimationComplete: Function;
122
+ class: any;
123
+ as: string;
124
+ }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
125
+
126
+ interface SpringParams {
127
+ stiffness?: number;
128
+ damping?: number;
129
+ mass?: number;
130
+ precision?: number;
131
+ }
132
+
133
+ interface TextMorphOptions {
134
+ debug?: boolean;
135
+ element: HTMLElement;
136
+ locale?: Intl.LocalesArgument;
137
+ scale?: boolean;
138
+ duration?: number;
139
+ ease?: string | SpringParams;
140
+ disabled?: boolean;
141
+ respectReducedMotion?: boolean;
142
+ onAnimationStart?: () => void;
143
+ onAnimationComplete?: () => void;
144
+ }
2
145
 
3
146
  interface TextMorphProps extends Omit<TextMorphOptions, "element"> {
4
147
  text: string;
@@ -7,4 +150,4 @@ interface TextMorphProps extends Omit<TextMorphOptions, "element"> {
7
150
  as?: string;
8
151
  }
9
152
 
10
- export type { TextMorphProps };
153
+ export { _default as TextMorph, type TextMorphProps };
@@ -0,0 +1,153 @@
1
+ import * as vue from 'vue';
2
+
3
+ declare const _default: vue.DefineComponent<vue.ExtractPropTypes<{
4
+ text: {
5
+ type: StringConstructor;
6
+ required: true;
7
+ };
8
+ class: {
9
+ type: any;
10
+ default: undefined;
11
+ };
12
+ style: {
13
+ type: ObjectConstructor;
14
+ default: undefined;
15
+ };
16
+ as: {
17
+ type: StringConstructor;
18
+ default: string;
19
+ };
20
+ locale: {
21
+ type: StringConstructor;
22
+ default: "en";
23
+ };
24
+ duration: {
25
+ type: NumberConstructor;
26
+ default: 400;
27
+ };
28
+ ease: {
29
+ type: any;
30
+ default: "cubic-bezier(0.19, 1, 0.22, 1)";
31
+ };
32
+ scale: {
33
+ type: BooleanConstructor;
34
+ default: true;
35
+ };
36
+ debug: {
37
+ type: BooleanConstructor;
38
+ default: undefined;
39
+ };
40
+ disabled: {
41
+ type: BooleanConstructor;
42
+ default: false;
43
+ };
44
+ respectReducedMotion: {
45
+ type: BooleanConstructor;
46
+ default: true;
47
+ };
48
+ onAnimationStart: {
49
+ type: FunctionConstructor;
50
+ default: undefined;
51
+ };
52
+ onAnimationComplete: {
53
+ type: FunctionConstructor;
54
+ default: undefined;
55
+ };
56
+ }>, () => vue.VNode<vue.RendererNode, vue.RendererElement, {
57
+ [key: string]: any;
58
+ }>, {}, {}, {}, vue.ComponentOptionsMixin, vue.ComponentOptionsMixin, {}, string, vue.PublicProps, Readonly<vue.ExtractPropTypes<{
59
+ text: {
60
+ type: StringConstructor;
61
+ required: true;
62
+ };
63
+ class: {
64
+ type: any;
65
+ default: undefined;
66
+ };
67
+ style: {
68
+ type: ObjectConstructor;
69
+ default: undefined;
70
+ };
71
+ as: {
72
+ type: StringConstructor;
73
+ default: string;
74
+ };
75
+ locale: {
76
+ type: StringConstructor;
77
+ default: "en";
78
+ };
79
+ duration: {
80
+ type: NumberConstructor;
81
+ default: 400;
82
+ };
83
+ ease: {
84
+ type: any;
85
+ default: "cubic-bezier(0.19, 1, 0.22, 1)";
86
+ };
87
+ scale: {
88
+ type: BooleanConstructor;
89
+ default: true;
90
+ };
91
+ debug: {
92
+ type: BooleanConstructor;
93
+ default: undefined;
94
+ };
95
+ disabled: {
96
+ type: BooleanConstructor;
97
+ default: false;
98
+ };
99
+ respectReducedMotion: {
100
+ type: BooleanConstructor;
101
+ default: true;
102
+ };
103
+ onAnimationStart: {
104
+ type: FunctionConstructor;
105
+ default: undefined;
106
+ };
107
+ onAnimationComplete: {
108
+ type: FunctionConstructor;
109
+ default: undefined;
110
+ };
111
+ }>> & Readonly<{}>, {
112
+ duration: number;
113
+ ease: any;
114
+ scale: boolean;
115
+ style: Record<string, any>;
116
+ debug: boolean;
117
+ locale: string;
118
+ disabled: boolean;
119
+ respectReducedMotion: boolean;
120
+ onAnimationStart: Function;
121
+ onAnimationComplete: Function;
122
+ class: any;
123
+ as: string;
124
+ }, {}, {}, {}, string, vue.ComponentProvideOptions, true, {}, any>;
125
+
126
+ interface SpringParams {
127
+ stiffness?: number;
128
+ damping?: number;
129
+ mass?: number;
130
+ precision?: number;
131
+ }
132
+
133
+ interface TextMorphOptions {
134
+ debug?: boolean;
135
+ element: HTMLElement;
136
+ locale?: Intl.LocalesArgument;
137
+ scale?: boolean;
138
+ duration?: number;
139
+ ease?: string | SpringParams;
140
+ disabled?: boolean;
141
+ respectReducedMotion?: boolean;
142
+ onAnimationStart?: () => void;
143
+ onAnimationComplete?: () => void;
144
+ }
145
+
146
+ interface TextMorphProps extends Omit<TextMorphOptions, "element"> {
147
+ text: string;
148
+ class?: string;
149
+ style?: Record<string, string | number>;
150
+ as?: string;
151
+ }
152
+
153
+ export { _default as TextMorph, type TextMorphProps };
package/dist/vue/index.js CHANGED
@@ -1,2 +1,23 @@
1
- 'use strict';var TextMorphSZJHGWGL_vue=require('../TextMorph-SZJHGWGL.vue');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var TextMorphSZJHGWGL_vue__default=/*#__PURE__*/_interopDefault(TextMorphSZJHGWGL_vue);Object.defineProperty(exports,"TextMorph",{enumerable:true,get:function(){return TextMorphSZJHGWGL_vue__default.default}});//# sourceMappingURL=index.js.map
2
- //# sourceMappingURL=index.js.map
1
+ 'use strict';var vue=require('vue');function H(t,e,n){if(n<1){let u=e*Math.sqrt(1-n*n);return 1-Math.exp(-n*e*t)*(Math.cos(u*t)+n*e/u*Math.sin(u*t))}let s=Math.sqrt(n*n-1),r=-e*(n+s),i=-e*(n-s),o=-r/(i-r);return 1-(1-o)*Math.exp(r*t)-o*Math.exp(i*t)}function W(t,e,n){let i=0;for(let o=0;o<10;o+=.001)if(Math.abs(H(o,t,e)-1)>n)i=0;else if(i+=.001,i>.1)return Math.ceil((o-i+.001)*1e3);return Math.ceil(10*1e3)}var D=new Map;function C(t){let{stiffness:e=100,damping:n=10,mass:s=1,precision:r=.001}=t??{},i=`${e}:${n}:${s}:${r}`,o=D.get(i);if(o)return o;let a=Math.sqrt(e/s),u=n/(2*Math.sqrt(e*s)),c=W(a,u,r),d=Math.min(100,Math.max(32,Math.round(c/15))),p=[];for(let y=0;y<d;y++){let S=y/(d-1)*(c/1e3),R=y===d-1?1:H(S,a,u);p.push(Math.round(R*1e4)/1e4+"");}for(;p.length>2&&p[p.length-2]==="1";)p.splice(p.length-2,1);let l={easing:`linear(${p.join(", ")})`,duration:c};return D.set(i,l),l}function _(t,e){let n=t.includes(" ");if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(e,{granularity:n?"word":"grapheme"}).segment(t)[Symbol.iterator]();return Y(r)}return J(t,n)}function Y(t){return Array.from(t).reduce((e,n)=>n.segment===" "?[...e,{id:`space-${n.index}`,string:"\xA0"}]:e.find(r=>r.string===n.segment)?[...e,{id:`${n.segment}-${n.index}`,string:n.segment}]:[...e,{id:n.segment,string:n.segment}],[])}function Q(t,e,n){let s=t.find(r=>r.string===e);t.push(s?{id:`${e}-${n}`,string:e}:{id:e,string:e});}function J(t,e){let n=e?t.split(" "):t.split(""),s=[];return n.forEach((r,i)=>{e&&i>0&&s.push({id:`space-${i}`,string:"\xA0"}),Q(s,r,i);}),s}var x="torph-root",b="torph-item",m="torph-id",f="torph-exiting",E="torph-debug";function w(t){let e=Array.from(t.children),n={},s=t.getBoundingClientRect();return e.forEach((r,i)=>{if(r.hasAttribute(f))return;let o=r.getAttribute(m)||`child-${i}`,a=r.getBoundingClientRect();n[o]={x:a.left-s.left,y:a.top-s.top};}),n}function v(t,e,n){let s=t[n],r=e[n];return !s||!r?{dx:0,dy:0}:{dx:s.x-r.x,dy:s.y-r.y}}function L(t,e,n,s="backward-first"){let[r,i]=s==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=t-1;u>=0;u--)if(n.has(e[u]))return e[u]}else for(let u=t+1;u<e.length;u++)if(n.has(e[u]))return e[u];return null};return o(r)??o(i)}function N(t,e,n,s){let r=new Set(n.filter((o,a)=>s.has(o)&&!e.has(t[a]))),i=new Map;for(let o=0;o<t.length;o++){let a=t[o];e.has(a)&&i.set(a,L(o,n,r,"forward-first"));}return i}function F(t,e){return Math.min(t*e,150)}function I(t){let e=getComputedStyle(t).transform;if(!e||e==="none")return {tx:0,ty:0};let n=e.match(/matrix\(([^)]+)\)/);if(!n)return {tx:0,ty:0};let s=n[1].split(",").map(Number);return {tx:s[4]||0,ty:s[5]||0}}function z(t){let{tx:e,ty:n}=I(t),s=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(r=>r.cancel()),{tx:e,ty:n,opacity:s}}function P(t,e){let{dx:n,dy:s,duration:r,ease:i,scale:o}=e;t.animate({transform:o?`translate(${n}px, ${s}px) scale(0.95)`:`translate(${n}px, ${s}px)`,offset:1},{duration:r,easing:i,fill:"both"});let a=t.animate({opacity:0,offset:1},{duration:F(r,.25),easing:"linear",fill:"both"});a.onfinish=()=>t.remove();}function k(t,e){let{deltaX:n,deltaY:s,isNew:r,duration:i,ease:o}=e,a=z(t),u=n+a.tx,c=s+a.ty;t.animate({transform:`translate(${u}px, ${c}px) scale(${r?.95:1})`,offset:0},{duration:i,easing:o,fill:"both"});let d=r&&a.opacity>=1?0:a.opacity;d<1&&t.animate([{opacity:d},{opacity:1}],{duration:F(i,r?.5:.25),easing:"linear",fill:"both"});}var T=null;function B(t,e,n,s,r){if(T&&(T(),T=null),e===0||n===0)return;t.style.width="auto",t.style.height="auto",t.offsetWidth;let i=t.offsetWidth,o=t.offsetHeight;t.style.width=`${e}px`,t.style.height=`${n}px`,t.offsetWidth,t.style.width=`${i}px`,t.style.height=`${o}px`;function a(){t.removeEventListener("transitionend",u),clearTimeout(c),T=null,t.style.width="auto",t.style.height="auto",r?.();}function u(d){d.target===t&&(d.propertyName!=="width"&&d.propertyName!=="height"||a());}t.addEventListener("transitionend",u);let c=setTimeout(a,s+50);T=()=>{t.removeEventListener("transitionend",u),clearTimeout(c),T=null;};}function X(t){let n=t[0]?.parentElement?.getBoundingClientRect(),s=t.map(r=>{let{tx:i,ty:o}=I(r),a=Number(getComputedStyle(r).opacity)||1;r.getAnimations().forEach(c=>c.cancel());let u=r.getBoundingClientRect();return {left:(n?u.left-n.left:u.left)+i,top:(n?u.top-n.top:u.top)+o,width:u.width,height:u.height,opacity:a}});t.forEach((r,i)=>{let o=s[i];r.setAttribute(f,""),r.style.position="absolute",r.style.pointerEvents="none",r.style.left=`${o.left}px`,r.style.top=`${o.top}px`,r.style.width=`${o.width}px`,r.style.height=`${o.height}px`,r.style.opacity=String(o.opacity);});}function U(t,e,n,s){let r=new Map;e.forEach(i=>{let o=i.getAttribute(m);n.has(o)&&!i.hasAttribute(f)&&(r.set(o,i),i.remove());}),Array.from(t.childNodes).forEach(i=>{i.nodeType===Node.TEXT_NODE&&i.remove();}),s.forEach(i=>{let o=r.get(i.id);if(o)o.textContent=i.string,t.appendChild(o);else {let a=document.createElement("span");a.setAttribute(b,""),a.setAttribute(m,i.id),a.textContent=i.string,t.appendChild(a);}});}var V=`
2
+ [${x}] {
3
+ display: inline-block;
4
+ position: relative;
5
+ will-change: width, height;
6
+ transition-property: width, height;
7
+ white-space: nowrap;
8
+ }
9
+
10
+ [${b}] {
11
+ display: inline-block;
12
+ will-change: opacity, transform;
13
+ transform: none;
14
+ opacity: 1;
15
+ }
16
+
17
+ [${x}][${E}] {
18
+ outline: 2px solid magenta;
19
+ [${b}] {
20
+ outline: 2px solid cyan;
21
+ outline-offset: -4px;
22
+ }
23
+ }`,h=null,O=0;function G(){O++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=V,document.head.appendChild(h));}function j(){O--,!(O>0||!h)&&(h.remove(),h=null);}function q(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let t=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:t.matches,destroy:s};function n(r){e.prefersReducedMotion=r.matches;}function s(){t.removeEventListener("change",n);}return t.addEventListener("change",n),e}var M=class t{instance=null;lastText="";configKey="";attach(e,n){this.instance?.destroy(),this.instance=new A({element:e,...n}),this.configKey=t.serializeConfig(n),this.lastText&&this.instance.update(this.lastText);}update(e){this.lastText=e,this.instance?.update(e);}needsRecreate(e){return t.serializeConfig(e)!==this.configKey}destroy(){this.instance?.destroy(),this.instance=null;}static serializeConfig(e){return JSON.stringify({ease:e.ease,duration:e.duration,locale:e.locale,scale:e.scale,debug:e.debug,disabled:e.disabled,respectReducedMotion:e.respectReducedMotion})}};var K="span",g={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},A=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(e){let{ease:n,...s}={...g,...e},r,i;if(typeof n=="object"){let o=C(n);r=o.easing,i=o.duration;}else r=n,i=s.duration;this.options={...s,ease:r,duration:i},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=q()),this.isDisabled()||(this.element.setAttribute(x,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute(E,"")),this.data="",this.isDisabled()||G();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(x),this.element.removeAttribute(E),j();}isDisabled(){return !!(this.options.disabled||this.reducedMotion?.prefersReducedMotion)}update(e){if(e!==this.data){if(this.data=e,this.isDisabled()){typeof e=="string"&&(this.element.textContent=e);return}if(this.data instanceof HTMLElement)throw new Error("HTMLElement not yet supported");this.options.onAnimationStart&&!this.isInitialRender&&this.options.onAnimationStart(),this.createTextGroup(this.data,this.element);}}createTextGroup(e,n){let s=n.offsetWidth,r=n.offsetHeight,i=_(e,this.options.locale);this.prevMeasures=w(this.element);let o=Array.from(n.children),a=new Set(i.map(l=>l.id)),u=o.filter(l=>!a.has(l.getAttribute(m))&&!l.hasAttribute(f)),c=new Set(u),d=o.map(l=>l.getAttribute(m)),p=N(o,c,d,a);if(X(u),U(n,o,a,i),this.currentMeasures=w(this.element),this.updateStyles(i),u.forEach(l=>{if(this.isInitialRender){l.remove();return}let y=p.get(l),{dx:S,dy:R}=y?v(this.currentMeasures,this.prevMeasures,y):{dx:0,dy:0};P(l,{dx:S,dy:R,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,n.style.width="auto",n.style.height="auto";return}B(n,s,r,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let n=Array.from(this.element.children),s=e.map(i=>i.id),r=new Set(s.filter(i=>this.prevMeasures[i]));n.forEach((i,o)=>{if(i.hasAttribute(f))return;let a=i.getAttribute(m)||`child-${o}`,u=!this.prevMeasures[a],c=u?L(e.findIndex(l=>l.id===a),s,r):a,{dx:d,dy:p}=c?v(this.prevMeasures,this.currentMeasures,c):{dx:0,dy:0};k(i,{deltaX:d,deltaY:p,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};var it=vue.defineComponent({name:"TextMorph",props:{text:{type:String,required:true},class:{type:[String,Object,Array],default:void 0},style:{type:Object,default:void 0},as:{type:String,default:K},locale:{type:String,default:g.locale},duration:{type:Number,default:g.duration},ease:{type:[String,Object],default:g.ease},scale:{type:Boolean,default:g.scale},debug:{type:Boolean,default:void 0},disabled:{type:Boolean,default:g.disabled},respectReducedMotion:{type:Boolean,default:g.respectReducedMotion},onAnimationStart:{type:Function,default:void 0},onAnimationComplete:{type:Function,default:void 0}},setup(t){let e=vue.ref(null),n=new M,s=vue.computed(()=>M.serializeConfig(t));function r(){e.value&&n.attach(e.value,{locale:t.locale,duration:t.duration,ease:t.ease,debug:t.debug,scale:t.scale,disabled:t.disabled,respectReducedMotion:t.respectReducedMotion,onAnimationStart:t.onAnimationStart,onAnimationComplete:t.onAnimationComplete});}return vue.watch(e,()=>r(),{flush:"post"}),vue.watch(s,()=>r()),vue.onUnmounted(()=>{n.destroy();}),vue.watch(()=>t.text,i=>{n.update(i);}),()=>vue.h(t.as,{ref:e,class:t.class,style:t.style})}});exports.TextMorph=it;
@@ -1,2 +1,23 @@
1
- export{default as TextMorph}from'../TextMorph-SZJHGWGL.vue';//# sourceMappingURL=index.mjs.map
2
- //# sourceMappingURL=index.mjs.map
1
+ import {defineComponent,ref,computed,h as h$1,watch,onUnmounted}from'vue';function H(t,e,n){if(n<1){let u=e*Math.sqrt(1-n*n);return 1-Math.exp(-n*e*t)*(Math.cos(u*t)+n*e/u*Math.sin(u*t))}let s=Math.sqrt(n*n-1),r=-e*(n+s),i=-e*(n-s),o=-r/(i-r);return 1-(1-o)*Math.exp(r*t)-o*Math.exp(i*t)}function W(t,e,n){let i=0;for(let o=0;o<10;o+=.001)if(Math.abs(H(o,t,e)-1)>n)i=0;else if(i+=.001,i>.1)return Math.ceil((o-i+.001)*1e3);return Math.ceil(10*1e3)}var D=new Map;function C(t){let{stiffness:e=100,damping:n=10,mass:s=1,precision:r=.001}=t??{},i=`${e}:${n}:${s}:${r}`,o=D.get(i);if(o)return o;let a=Math.sqrt(e/s),u=n/(2*Math.sqrt(e*s)),c=W(a,u,r),d=Math.min(100,Math.max(32,Math.round(c/15))),p=[];for(let y=0;y<d;y++){let S=y/(d-1)*(c/1e3),R=y===d-1?1:H(S,a,u);p.push(Math.round(R*1e4)/1e4+"");}for(;p.length>2&&p[p.length-2]==="1";)p.splice(p.length-2,1);let l={easing:`linear(${p.join(", ")})`,duration:c};return D.set(i,l),l}function _(t,e){let n=t.includes(" ");if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(e,{granularity:n?"word":"grapheme"}).segment(t)[Symbol.iterator]();return Y(r)}return J(t,n)}function Y(t){return Array.from(t).reduce((e,n)=>n.segment===" "?[...e,{id:`space-${n.index}`,string:"\xA0"}]:e.find(r=>r.string===n.segment)?[...e,{id:`${n.segment}-${n.index}`,string:n.segment}]:[...e,{id:n.segment,string:n.segment}],[])}function Q(t,e,n){let s=t.find(r=>r.string===e);t.push(s?{id:`${e}-${n}`,string:e}:{id:e,string:e});}function J(t,e){let n=e?t.split(" "):t.split(""),s=[];return n.forEach((r,i)=>{e&&i>0&&s.push({id:`space-${i}`,string:"\xA0"}),Q(s,r,i);}),s}var x="torph-root",b="torph-item",m="torph-id",f="torph-exiting",E="torph-debug";function w(t){let e=Array.from(t.children),n={},s=t.getBoundingClientRect();return e.forEach((r,i)=>{if(r.hasAttribute(f))return;let o=r.getAttribute(m)||`child-${i}`,a=r.getBoundingClientRect();n[o]={x:a.left-s.left,y:a.top-s.top};}),n}function v(t,e,n){let s=t[n],r=e[n];return !s||!r?{dx:0,dy:0}:{dx:s.x-r.x,dy:s.y-r.y}}function L(t,e,n,s="backward-first"){let[r,i]=s==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=t-1;u>=0;u--)if(n.has(e[u]))return e[u]}else for(let u=t+1;u<e.length;u++)if(n.has(e[u]))return e[u];return null};return o(r)??o(i)}function N(t,e,n,s){let r=new Set(n.filter((o,a)=>s.has(o)&&!e.has(t[a]))),i=new Map;for(let o=0;o<t.length;o++){let a=t[o];e.has(a)&&i.set(a,L(o,n,r,"forward-first"));}return i}function F(t,e){return Math.min(t*e,150)}function I(t){let e=getComputedStyle(t).transform;if(!e||e==="none")return {tx:0,ty:0};let n=e.match(/matrix\(([^)]+)\)/);if(!n)return {tx:0,ty:0};let s=n[1].split(",").map(Number);return {tx:s[4]||0,ty:s[5]||0}}function z(t){let{tx:e,ty:n}=I(t),s=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(r=>r.cancel()),{tx:e,ty:n,opacity:s}}function P(t,e){let{dx:n,dy:s,duration:r,ease:i,scale:o}=e;t.animate({transform:o?`translate(${n}px, ${s}px) scale(0.95)`:`translate(${n}px, ${s}px)`,offset:1},{duration:r,easing:i,fill:"both"});let a=t.animate({opacity:0,offset:1},{duration:F(r,.25),easing:"linear",fill:"both"});a.onfinish=()=>t.remove();}function k(t,e){let{deltaX:n,deltaY:s,isNew:r,duration:i,ease:o}=e,a=z(t),u=n+a.tx,c=s+a.ty;t.animate({transform:`translate(${u}px, ${c}px) scale(${r?.95:1})`,offset:0},{duration:i,easing:o,fill:"both"});let d=r&&a.opacity>=1?0:a.opacity;d<1&&t.animate([{opacity:d},{opacity:1}],{duration:F(i,r?.5:.25),easing:"linear",fill:"both"});}var T=null;function B(t,e,n,s,r){if(T&&(T(),T=null),e===0||n===0)return;t.style.width="auto",t.style.height="auto",t.offsetWidth;let i=t.offsetWidth,o=t.offsetHeight;t.style.width=`${e}px`,t.style.height=`${n}px`,t.offsetWidth,t.style.width=`${i}px`,t.style.height=`${o}px`;function a(){t.removeEventListener("transitionend",u),clearTimeout(c),T=null,t.style.width="auto",t.style.height="auto",r?.();}function u(d){d.target===t&&(d.propertyName!=="width"&&d.propertyName!=="height"||a());}t.addEventListener("transitionend",u);let c=setTimeout(a,s+50);T=()=>{t.removeEventListener("transitionend",u),clearTimeout(c),T=null;};}function X(t){let n=t[0]?.parentElement?.getBoundingClientRect(),s=t.map(r=>{let{tx:i,ty:o}=I(r),a=Number(getComputedStyle(r).opacity)||1;r.getAnimations().forEach(c=>c.cancel());let u=r.getBoundingClientRect();return {left:(n?u.left-n.left:u.left)+i,top:(n?u.top-n.top:u.top)+o,width:u.width,height:u.height,opacity:a}});t.forEach((r,i)=>{let o=s[i];r.setAttribute(f,""),r.style.position="absolute",r.style.pointerEvents="none",r.style.left=`${o.left}px`,r.style.top=`${o.top}px`,r.style.width=`${o.width}px`,r.style.height=`${o.height}px`,r.style.opacity=String(o.opacity);});}function U(t,e,n,s){let r=new Map;e.forEach(i=>{let o=i.getAttribute(m);n.has(o)&&!i.hasAttribute(f)&&(r.set(o,i),i.remove());}),Array.from(t.childNodes).forEach(i=>{i.nodeType===Node.TEXT_NODE&&i.remove();}),s.forEach(i=>{let o=r.get(i.id);if(o)o.textContent=i.string,t.appendChild(o);else {let a=document.createElement("span");a.setAttribute(b,""),a.setAttribute(m,i.id),a.textContent=i.string,t.appendChild(a);}});}var V=`
2
+ [${x}] {
3
+ display: inline-block;
4
+ position: relative;
5
+ will-change: width, height;
6
+ transition-property: width, height;
7
+ white-space: nowrap;
8
+ }
9
+
10
+ [${b}] {
11
+ display: inline-block;
12
+ will-change: opacity, transform;
13
+ transform: none;
14
+ opacity: 1;
15
+ }
16
+
17
+ [${x}][${E}] {
18
+ outline: 2px solid magenta;
19
+ [${b}] {
20
+ outline: 2px solid cyan;
21
+ outline-offset: -4px;
22
+ }
23
+ }`,h=null,O=0;function G(){O++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=V,document.head.appendChild(h));}function j(){O--,!(O>0||!h)&&(h.remove(),h=null);}function q(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let t=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:t.matches,destroy:s};function n(r){e.prefersReducedMotion=r.matches;}function s(){t.removeEventListener("change",n);}return t.addEventListener("change",n),e}var M=class t{instance=null;lastText="";configKey="";attach(e,n){this.instance?.destroy(),this.instance=new A({element:e,...n}),this.configKey=t.serializeConfig(n),this.lastText&&this.instance.update(this.lastText);}update(e){this.lastText=e,this.instance?.update(e);}needsRecreate(e){return t.serializeConfig(e)!==this.configKey}destroy(){this.instance?.destroy(),this.instance=null;}static serializeConfig(e){return JSON.stringify({ease:e.ease,duration:e.duration,locale:e.locale,scale:e.scale,debug:e.debug,disabled:e.disabled,respectReducedMotion:e.respectReducedMotion})}};var K="span",g={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},A=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(e){let{ease:n,...s}={...g,...e},r,i;if(typeof n=="object"){let o=C(n);r=o.easing,i=o.duration;}else r=n,i=s.duration;this.options={...s,ease:r,duration:i},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=q()),this.isDisabled()||(this.element.setAttribute(x,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute(E,"")),this.data="",this.isDisabled()||G();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(x),this.element.removeAttribute(E),j();}isDisabled(){return !!(this.options.disabled||this.reducedMotion?.prefersReducedMotion)}update(e){if(e!==this.data){if(this.data=e,this.isDisabled()){typeof e=="string"&&(this.element.textContent=e);return}if(this.data instanceof HTMLElement)throw new Error("HTMLElement not yet supported");this.options.onAnimationStart&&!this.isInitialRender&&this.options.onAnimationStart(),this.createTextGroup(this.data,this.element);}}createTextGroup(e,n){let s=n.offsetWidth,r=n.offsetHeight,i=_(e,this.options.locale);this.prevMeasures=w(this.element);let o=Array.from(n.children),a=new Set(i.map(l=>l.id)),u=o.filter(l=>!a.has(l.getAttribute(m))&&!l.hasAttribute(f)),c=new Set(u),d=o.map(l=>l.getAttribute(m)),p=N(o,c,d,a);if(X(u),U(n,o,a,i),this.currentMeasures=w(this.element),this.updateStyles(i),u.forEach(l=>{if(this.isInitialRender){l.remove();return}let y=p.get(l),{dx:S,dy:R}=y?v(this.currentMeasures,this.prevMeasures,y):{dx:0,dy:0};P(l,{dx:S,dy:R,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,n.style.width="auto",n.style.height="auto";return}B(n,s,r,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let n=Array.from(this.element.children),s=e.map(i=>i.id),r=new Set(s.filter(i=>this.prevMeasures[i]));n.forEach((i,o)=>{if(i.hasAttribute(f))return;let a=i.getAttribute(m)||`child-${o}`,u=!this.prevMeasures[a],c=u?L(e.findIndex(l=>l.id===a),s,r):a,{dx:d,dy:p}=c?v(this.prevMeasures,this.currentMeasures,c):{dx:0,dy:0};k(i,{deltaX:d,deltaY:p,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};var it=defineComponent({name:"TextMorph",props:{text:{type:String,required:true},class:{type:[String,Object,Array],default:void 0},style:{type:Object,default:void 0},as:{type:String,default:K},locale:{type:String,default:g.locale},duration:{type:Number,default:g.duration},ease:{type:[String,Object],default:g.ease},scale:{type:Boolean,default:g.scale},debug:{type:Boolean,default:void 0},disabled:{type:Boolean,default:g.disabled},respectReducedMotion:{type:Boolean,default:g.respectReducedMotion},onAnimationStart:{type:Function,default:void 0},onAnimationComplete:{type:Function,default:void 0}},setup(t){let e=ref(null),n=new M,s=computed(()=>M.serializeConfig(t));function r(){e.value&&n.attach(e.value,{locale:t.locale,duration:t.duration,ease:t.ease,debug:t.debug,scale:t.scale,disabled:t.disabled,respectReducedMotion:t.respectReducedMotion,onAnimationStart:t.onAnimationStart,onAnimationComplete:t.onAnimationComplete});}return watch(e,()=>r(),{flush:"post"}),watch(s,()=>r()),onUnmounted(()=>{n.destroy();}),watch(()=>t.text,i=>{n.update(i);}),()=>h$1(t.as,{ref:e,class:t.class,style:t.style})}});export{it as TextMorph};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "torph",
3
- "version": "0.0.6",
3
+ "version": "0.0.8",
4
4
  "description": "Dependency-free animated text component.",
5
5
  "homepage": "https://torph.lochie.me",
6
6
  "author": "Lochie Axon",
@@ -10,6 +10,8 @@
10
10
  "url": "git+https://github.com/lochie/torph.git",
11
11
  "directory": "packages/torph"
12
12
  },
13
+ "main": "dist/index.js",
14
+ "module": "dist/index.mjs",
13
15
  "types": "dist/index.d.ts",
14
16
  "exports": {
15
17
  ".": {
@@ -23,12 +25,18 @@
23
25
  "require": "./dist/react/index.js"
24
26
  },
25
27
  "./vue": {
26
- "types": "./dist/vue/index.d.ts",
28
+ "types": {
29
+ "import": "./dist/vue/index.d.mts",
30
+ "require": "./dist/vue/index.d.ts"
31
+ },
27
32
  "import": "./dist/vue/index.mjs",
28
33
  "require": "./dist/vue/index.js"
29
34
  },
30
35
  "./svelte": {
31
- "types": "./dist/svelte/index.d.ts",
36
+ "types": {
37
+ "import": "./dist/svelte/index.d.mts",
38
+ "require": "./dist/svelte/index.d.ts"
39
+ },
32
40
  "import": "./dist/svelte/index.mjs",
33
41
  "require": "./dist/svelte/index.js"
34
42
  }
package/dist/.DS_Store DELETED
Binary file