torph 0.0.6 → 0.0.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Torph
2
2
 
3
- Animated text morphing component for React, Vue, and Svelte.
3
+ Dependency-free animated text morphing component for React, Vue, Svelte, and vanilla JavaScript.
4
4
 
5
5
  ## Installation
6
6
 
@@ -124,6 +124,31 @@ const morph = new TextMorph({
124
124
  morph.update("Hello World");
125
125
  ```
126
126
 
127
+ ## Spring Animations
128
+
129
+ Pass spring parameters to `ease` for physics-based easing. The duration is computed automatically from the spring physics.
130
+
131
+ ```tsx
132
+ import { TextMorph } from "torph/react";
133
+
134
+ function App() {
135
+ const [text, setText] = useState("Hello World");
136
+
137
+ return (
138
+ <TextMorph ease={{ stiffness: 200, damping: 20 }}>{text}</TextMorph>
139
+ );
140
+ }
141
+ ```
142
+
143
+ ### Spring Parameters
144
+
145
+ | Parameter | Type | Default | Description |
146
+ | ----------- | -------- | ------- | ----------------------------------------- |
147
+ | `stiffness` | `number` | `100` | Spring stiffness coefficient |
148
+ | `damping` | `number` | `10` | Damping coefficient |
149
+ | `mass` | `number` | `1` | Mass of the spring |
150
+ | `precision` | `number` | `0.001` | Threshold for determining settled position |
151
+
127
152
  ## API
128
153
 
129
154
  ### Options
@@ -131,17 +156,18 @@ morph.update("Hello World");
131
156
  All components accept the following props/options:
132
157
 
133
158
  - `text` / `children: string` - The text to display (required)
134
- - `duration?: number` - Animation duration in milliseconds (default: 400)
135
- - `ease?: string` - CSS easing function (default: "cubic-bezier(0.19, 1, 0.22, 1)")
136
- - `locale?: Intl.LocalesArgument` - Locale for text segmentation (default: "en")
159
+ - `duration?: number` - Animation duration in milliseconds (default: `400`)
160
+ - `ease?: string | SpringParams` - CSS easing function or spring parameters (default: `"cubic-bezier(0.19, 1, 0.22, 1)"`)
161
+ - `scale?: boolean` - Enable scale animation on exiting segments (default: `true`)
162
+ - `locale?: Intl.LocalesArgument` - Locale for text segmentation (default: `"en"`)
137
163
  - `debug?: boolean` - Enable debug mode with visual indicators
138
- - `disabled?: boolean` - Disable all morphing animations (default: false)
139
- - `respectReducedMotion?: boolean` - Respect user's prefers-reduced-motion setting (default: true)
164
+ - `disabled?: boolean` - Disable all morphing animations (default: `false`)
165
+ - `respectReducedMotion?: boolean` - Respect user's prefers-reduced-motion setting (default: `true`)
140
166
  - `onAnimationStart?: () => void` - Callback fired when animation begins
141
167
  - `onAnimationComplete?: () => void` - Callback fired when animation completes
142
168
  - `className?: string` - CSS class name (React/Vue: `class`)
143
169
  - `style?: object | string` - Inline styles
144
- - `as?: string` - HTML element type (default: "div")
170
+ - `as?: string` - HTML element type (default: `"span"`)
145
171
 
146
172
  ## Found this useful?
147
173
 
@@ -8,12 +8,12 @@
8
8
  </template>
9
9
 
10
10
  <script setup lang="ts">
11
- import { ref, onMounted, onUnmounted, watch } from "vue";
11
+ import { ref, computed, onUnmounted, watch } from "vue";
12
12
  import {
13
13
  DEFAULT_AS,
14
14
  DEFAULT_TEXT_MORPH_OPTIONS,
15
- TextMorph as Morph,
16
15
  } from "../lib/text-morph";
16
+ import { MorphController } from "../lib/text-morph/controller";
17
17
  import type { TextMorphProps } from "./types";
18
18
 
19
19
  const props = withDefaults(defineProps<TextMorphProps>(), {
@@ -27,12 +27,15 @@ const props = withDefaults(defineProps<TextMorphProps>(), {
27
27
  });
28
28
 
29
29
  const containerRef = ref<HTMLElement | null>(null);
30
- let morphInstance: Morph | null = null;
30
+ const controller = new MorphController();
31
31
 
32
- onMounted(() => {
32
+ const configKey = computed(() =>
33
+ MorphController.serializeConfig(props),
34
+ );
35
+
36
+ function createInstance() {
33
37
  if (containerRef.value) {
34
- morphInstance = new Morph({
35
- element: containerRef.value,
38
+ controller.attach(containerRef.value, {
36
39
  locale: props.locale,
37
40
  duration: props.duration,
38
41
  ease: props.ease,
@@ -43,18 +46,20 @@ onMounted(() => {
43
46
  onAnimationStart: props.onAnimationStart,
44
47
  onAnimationComplete: props.onAnimationComplete,
45
48
  });
46
- morphInstance.update(props.text);
47
49
  }
48
- });
50
+ }
51
+
52
+ watch(containerRef, () => createInstance(), { flush: "post" });
53
+ watch(configKey, () => createInstance());
49
54
 
50
55
  onUnmounted(() => {
51
- morphInstance?.destroy();
56
+ controller.destroy();
52
57
  });
53
58
 
54
59
  watch(
55
60
  () => props.text,
56
61
  (newText) => {
57
- morphInstance?.update(newText);
62
+ controller.update(newText);
58
63
  },
59
64
  );
60
65
  </script>
package/dist/index.d.mts CHANGED
@@ -1,10 +1,17 @@
1
+ interface SpringParams {
2
+ stiffness?: number;
3
+ damping?: number;
4
+ mass?: number;
5
+ precision?: number;
6
+ }
7
+
1
8
  interface TextMorphOptions {
2
9
  debug?: boolean;
3
10
  element: HTMLElement;
4
11
  locale?: Intl.LocalesArgument;
5
12
  scale?: boolean;
6
13
  duration?: number;
7
- ease?: string;
14
+ ease?: string | SpringParams;
8
15
  disabled?: boolean;
9
16
  respectReducedMotion?: boolean;
10
17
  onAnimationStart?: () => void;
@@ -28,22 +35,13 @@ declare class TextMorph {
28
35
  private currentMeasures;
29
36
  private prevMeasures;
30
37
  private isInitialRender;
31
- private prefersReducedMotion;
32
- private mediaQuery?;
33
- static styleEl: HTMLStyleElement;
38
+ private reducedMotion;
34
39
  constructor(options: TextMorphOptions);
35
40
  destroy(): void;
36
- private handleMediaQueryChange;
37
41
  private isDisabled;
38
42
  update(value: HTMLElement | string): void;
39
43
  private createTextGroup;
40
- private measure;
41
44
  private updateStyles;
42
- private addStyles;
43
- private removeStyles;
44
- private blocks;
45
- private blocksFallback;
46
- private log;
47
45
  }
48
46
 
49
- export { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, TextMorph, type TextMorphOptions };
47
+ export { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, type SpringParams, TextMorph, type TextMorphOptions };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,17 @@
1
+ interface SpringParams {
2
+ stiffness?: number;
3
+ damping?: number;
4
+ mass?: number;
5
+ precision?: number;
6
+ }
7
+
1
8
  interface TextMorphOptions {
2
9
  debug?: boolean;
3
10
  element: HTMLElement;
4
11
  locale?: Intl.LocalesArgument;
5
12
  scale?: boolean;
6
13
  duration?: number;
7
- ease?: string;
14
+ ease?: string | SpringParams;
8
15
  disabled?: boolean;
9
16
  respectReducedMotion?: boolean;
10
17
  onAnimationStart?: () => void;
@@ -28,22 +35,13 @@ declare class TextMorph {
28
35
  private currentMeasures;
29
36
  private prevMeasures;
30
37
  private isInitialRender;
31
- private prefersReducedMotion;
32
- private mediaQuery?;
33
- static styleEl: HTMLStyleElement;
38
+ private reducedMotion;
34
39
  constructor(options: TextMorphOptions);
35
40
  destroy(): void;
36
- private handleMediaQueryChange;
37
41
  private isDisabled;
38
42
  update(value: HTMLElement | string): void;
39
43
  private createTextGroup;
40
- private measure;
41
44
  private updateStyles;
42
- private addStyles;
43
- private removeStyles;
44
- private blocks;
45
- private blocksFallback;
46
- private log;
47
45
  }
48
46
 
49
- export { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, TextMorph, type TextMorphOptions };
47
+ export { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, type SpringParams, TextMorph, type TextMorphOptions };
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- 'use strict';var T="span",A={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},b=class g{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;prefersReducedMotion=false;mediaQuery;static styleEl;constructor(e){this.options={...A,...e},this.element=e.element,typeof window<"u"&&this.options.respectReducedMotion&&(this.mediaQuery=window.matchMedia("(prefers-reduced-motion: reduce)"),this.prefersReducedMotion=this.mediaQuery.matches,this.mediaQuery.addEventListener("change",this.handleMediaQueryChange)),this.isDisabled()||(this.element.setAttribute("torph-root",""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute("torph-debug","")),this.data="",this.isDisabled()||this.addStyles();}destroy(){this.mediaQuery&&this.mediaQuery.removeEventListener("change",this.handleMediaQueryChange),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute("torph-root"),this.element.removeAttribute("torph-debug"),this.removeStyles();}handleMediaQueryChange=e=>{this.prefersReducedMotion=e.matches;};isDisabled(){return !!(this.options.disabled||this.options.respectReducedMotion&&this.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,s){let h=s.offsetWidth,i=s.offsetHeight,a=e.includes(" "),p;if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(this.options.locale,{granularity:a?"word":"grapheme"}).segment(e)[Symbol.iterator]();p=this.blocks(r);}else p=this.blocksFallback(e,a);this.prevMeasures=this.measure();let l=Array.from(s.children),d=new Set(p.map(t=>t.id)),c=l.filter(t=>!d.has(t.getAttribute("torph-id"))&&!t.hasAttribute("torph-exiting")),m=new Set(c),y=new Map;for(let t=0;t<l.length;t++){let r=l[t];if(!m.has(r))continue;let o=null;for(let n=t+1;n<l.length;n++){let u=l[n].getAttribute("torph-id");if(d.has(u)&&!m.has(l[n])){o=u;break}}if(!o)for(let n=t-1;n>=0;n--){let u=l[n].getAttribute("torph-id");if(d.has(u)&&!m.has(l[n])){o=u;break}}y.set(r,o);}let x=c.map(t=>(t.getAnimations().forEach(r=>r.cancel()),{left:t.offsetLeft,top:t.offsetTop,width:t.offsetWidth,height:t.offsetHeight}));if(c.forEach((t,r)=>{let o=x[r];t.setAttribute("torph-exiting",""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${o.left}px`,t.style.top=`${o.top}px`,t.style.width=`${o.width}px`,t.style.height=`${o.height}px`;}),l.forEach(t=>{let r=t.getAttribute("torph-id");d.has(r)&&t.remove();}),Array.from(s.childNodes).forEach(t=>{t.nodeType===Node.TEXT_NODE&&t.remove();}),p.forEach(t=>{let r=document.createElement("span");r.setAttribute("torph-item",""),r.setAttribute("torph-id",t.id),r.textContent=t.string,s.appendChild(r);}),this.currentMeasures=this.measure(),this.updateStyles(p),c.forEach(t=>{if(this.isInitialRender){t.remove();return}let r=y.get(t),o=0,n=0;if(r&&this.prevMeasures[r]&&this.currentMeasures[r]){let E=this.prevMeasures[r],v=this.currentMeasures[r];o=v.x-E.x,n=v.y-E.y;}t.animate({transform:this.options.scale?`translate(${o}px, ${n}px) scale(0.95)`:`translate(${o}px, ${n}px)`,offset:1},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let u=t.animate({opacity:0,offset:1},{duration:this.options.duration*.25,easing:"linear",fill:"both"});u.onfinish=()=>t.remove();}),this.isInitialRender){this.isInitialRender=false,s.style.width="auto",s.style.height="auto";return}if(h===0||i===0)return;s.style.width="auto",s.style.height="auto",s.offsetWidth;let f=s.offsetWidth,M=s.offsetHeight;s.style.width=`${h}px`,s.style.height=`${i}px`,s.offsetWidth,s.style.width=`${f}px`,s.style.height=`${M}px`,setTimeout(()=>{s.style.width="auto",s.style.height="auto",this.options.onAnimationComplete&&this.options.onAnimationComplete();},this.options.duration);}measure(){let e=Array.from(this.element.children),s={};return e.forEach((h,i)=>{if(h.hasAttribute("torph-exiting"))return;let a=h.getAttribute("torph-id")||`child-${i}`;s[a]={x:h.offsetLeft,y:h.offsetTop};}),s}updateStyles(e){if(this.isInitialRender)return;let s=Array.from(this.element.children),h=new Set(e.map(i=>i.id).filter(i=>this.prevMeasures[i]));s.forEach((i,a)=>{if(i.hasAttribute("torph-exiting"))return;let p=i.getAttribute("torph-id")||`child-${a}`,l=this.prevMeasures[p],d=this.currentMeasures[p],c=d?.x||0,m=d?.y||0,y=l?l.x-c:0,x=l?l.y-m:0,f=!l;if(f){let r=e.findIndex(n=>n.id===p),o=null;for(let n=r-1;n>=0;n--)if(h.has(e[n].id)){o=e[n].id;break}if(!o){for(let n=r+1;n<e.length;n++)if(h.has(e[n].id)){o=e[n].id;break}}if(o){let n=this.prevMeasures[o],u=this.currentMeasures[o];y=n.x-u.x,x=n.y-u.y;}}i.getAnimations().forEach(r=>r.cancel()),i.animate({transform:`translate(${y}px, ${x}px) scale(${f?.95:1})`,offset:0},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let M=f?this.options.duration*.25:0,t=f?this.options.duration*.25:0;i.animate({opacity:f?0:1,offset:0},{duration:M,delay:t,easing:"linear",fill:"both"});});}addStyles(){if(g.styleEl)return;let e=document.createElement("style");e.dataset.torph="true",e.innerHTML=`
2
- [torph-root] {
1
+ 'use strict';function $(n,e,t){if(t<1){let u=e*Math.sqrt(1-t*t);return 1-Math.exp(-t*e*n)*(Math.cos(u*n)+t*e/u*Math.sin(u*n))}let i=Math.sqrt(t*t-1),s=-e*(t+i),r=-e*(t-i),o=-s/(r-s);return 1-(1-o)*Math.exp(s*n)-o*Math.exp(r*n)}function B(n,e,t){let r=0;for(let o=0;o<10;o+=.001)if(Math.abs($(o,n,e)-1)>t)r=0;else if(r+=.001,r>.1)return Math.ceil((o-r+.001)*1e3);return Math.ceil(10*1e3)}var I=new Map;function D(n){let{stiffness:e=100,damping:t=10,mass:i=1,precision:s=.001}=n??{},r=`${e}:${t}:${i}:${s}`,o=I.get(r);if(o)return o;let a=Math.sqrt(e/i),u=t/(2*Math.sqrt(e*i)),m=B(a,u,s),c=Math.min(100,Math.max(32,Math.round(m/15))),l=[];for(let g=0;g<c;g++){let b=g/(c-1)*(m/1e3),E=g===c-1?1:$(b,a,u);l.push(Math.round(E*1e4)/1e4+"");}for(;l.length>2&&l[l.length-2]==="1";)l.splice(l.length-2,1);let p={easing:`linear(${l.join(", ")})`,duration:m};return I.set(r,p),p}function H(n,e){let t=n.includes(" ");if(typeof Intl.Segmenter<"u"){let s=new Intl.Segmenter(e,{granularity:t?"word":"grapheme"}).segment(n)[Symbol.iterator]();return j(s)}return Y(n,t)}function j(n){return Array.from(n).reduce((e,t)=>t.segment===" "?[...e,{id:`space-${t.index}`,string:"\xA0"}]:e.find(s=>s.string===t.segment)?[...e,{id:`${t.segment}-${t.index}`,string:t.segment}]:[...e,{id:t.segment,string:t.segment}],[])}function q(n,e,t){let i=n.find(s=>s.string===e);n.push(i?{id:`${e}-${t}`,string:e}:{id:e,string:e});}function Y(n,e){let t=e?n.split(" "):n.split(""),i=[];return t.forEach((s,r)=>{e&&r>0&&i.push({id:`space-${r}`,string:"\xA0"}),q(i,s,r);}),i}var y="torph-root",T="torph-item",f="torph-id",d="torph-exiting",M="torph-debug";function A(n){let e=Array.from(n.children),t={};return e.forEach((i,s)=>{if(i.hasAttribute(d))return;let r=i.getAttribute(f)||`child-${s}`;t[r]={x:i.offsetLeft,y:i.offsetTop};}),t}function S(n,e,t){let i=n[t],s=e[t];return !i||!s?{dx:0,dy:0}:{dx:i.x-s.x,dy:i.y-s.y}}function w(n,e,t,i="backward-first"){let[s,r]=i==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=n-1;u>=0;u--)if(t.has(e[u]))return e[u]}else for(let u=n+1;u<e.length;u++)if(t.has(e[u]))return e[u];return null};return o(s)??o(r)}function _(n,e,t,i){let s=new Set(t.filter((o,a)=>i.has(o)&&!e.has(n[a]))),r=new Map;for(let o=0;o<n.length;o++){let a=n[o];e.has(a)&&r.set(a,w(o,t,s,"forward-first"));}return r}function O(n,e){return Math.min(n*e,150)}function R(n){let e=getComputedStyle(n).transform;if(!e||e==="none")return {tx:0,ty:0};let t=e.match(/matrix\(([^)]+)\)/);if(!t)return {tx:0,ty:0};let i=t[1].split(",").map(Number);return {tx:i[4]||0,ty:i[5]||0}}function Q(n){let{tx:e,ty:t}=R(n),i=Number(getComputedStyle(n).opacity)||1;return n.getAnimations().forEach(s=>s.cancel()),{tx:e,ty:t,opacity:i}}function N(n,e){let{dx:t,dy:i,duration:s,ease:r,scale:o}=e;n.animate({transform:o?`translate(${t}px, ${i}px) scale(0.95)`:`translate(${t}px, ${i}px)`,offset:1},{duration:s,easing:r,fill:"both"});let a=n.animate({opacity:0,offset:1},{duration:O(s,.25),easing:"linear",fill:"both"});a.onfinish=()=>n.remove();}function C(n,e){let{deltaX:t,deltaY:i,isNew:s,duration:r,ease:o}=e,a=Q(n),u=t+a.tx,m=i+a.ty;n.animate({transform:`translate(${u}px, ${m}px) scale(${s?.95:1})`,offset:0},{duration:r,easing:o,fill:"both"});let c=s&&a.opacity>=1?0:a.opacity;c<1&&n.animate([{opacity:c},{opacity:1}],{duration:O(r,s?.5:.25),easing:"linear",fill:"both"});}var x=null;function k(n,e,t,i,s){if(x&&(x(),x=null),e===0||t===0)return;n.style.width="auto",n.style.height="auto",n.offsetWidth;let r=n.offsetWidth,o=n.offsetHeight;n.style.width=`${e}px`,n.style.height=`${t}px`,n.offsetWidth,n.style.width=`${r}px`,n.style.height=`${o}px`;function a(){n.removeEventListener("transitionend",u),clearTimeout(m),x=null,n.style.width="auto",n.style.height="auto",s?.();}function u(c){c.target===n&&(c.propertyName!=="width"&&c.propertyName!=="height"||a());}n.addEventListener("transitionend",u);let m=setTimeout(a,i+50);x=()=>{n.removeEventListener("transitionend",u),clearTimeout(m),x=null;};}function F(n){let e=n.map(t=>{let{tx:i,ty:s}=R(t),r=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(o=>o.cancel()),{left:t.offsetLeft+i,top:t.offsetTop+s,width:t.offsetWidth,height:t.offsetHeight,opacity:r}});n.forEach((t,i)=>{let s=e[i];t.setAttribute(d,""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${s.left}px`,t.style.top=`${s.top}px`,t.style.width=`${s.width}px`,t.style.height=`${s.height}px`,t.style.opacity=String(s.opacity);});}function P(n,e,t,i){let s=new Map;e.forEach(r=>{let o=r.getAttribute(f);t.has(o)&&!r.hasAttribute(d)&&(s.set(o,r),r.remove());}),Array.from(n.childNodes).forEach(r=>{r.nodeType===Node.TEXT_NODE&&r.remove();}),i.forEach(r=>{let o=s.get(r.id);if(o)o.textContent=r.string,n.appendChild(o);else {let a=document.createElement("span");a.setAttribute(T,""),a.setAttribute(f,r.id),a.textContent=r.string,n.appendChild(a);}});}var K=`
2
+ [${y}] {
3
3
  display: inline-flex;
4
4
  position: relative;
5
5
  will-change: width, height;
@@ -7,19 +7,17 @@
7
7
  white-space: nowrap;
8
8
  }
9
9
 
10
- [torph-item] {
10
+ [${T}] {
11
11
  display: inline-block;
12
12
  will-change: opacity, transform;
13
13
  transform: none;
14
14
  opacity: 1;
15
15
  }
16
16
 
17
- [torph-root][torph-debug] {
18
- outline:2px solid magenta;
19
- [torph-item] {
20
- outline:2px solid cyan;
17
+ [${y}][${M}] {
18
+ outline: 2px solid magenta;
19
+ [${T}] {
20
+ outline: 2px solid cyan;
21
21
  outline-offset: -4px;
22
22
  }
23
- }
24
- `,document.head.appendChild(e),g.styleEl=e;}removeStyles(){g.styleEl&&(g.styleEl.remove(),g.styleEl=void 0);}blocks(e){return Array.from(e).reduce((h,i)=>i.segment===" "?[...h,{id:`space-${i.index}`,string:"\xA0"}]:h.find(p=>p.string===i.segment)?[...h,{id:`${i.segment}-${i.index}`,string:i.segment}]:[...h,{id:i.segment,string:i.segment}],[])}blocksFallback(e,s){let h=s?e.split(" "):e.split(""),i=[];return s?h.forEach((a,p)=>{p>0&&i.push({id:`space-${p}`,string:"\xA0"}),i.find(d=>d.string===a)?i.push({id:`${a}-${p}`,string:a}):i.push({id:a,string:a});}):h.forEach((a,p)=>{i.find(d=>d.string===a)?i.push({id:`${a}-${p}`,string:a}):i.push({id:a,string:a});}),i}log(...e){this.options.debug&&console.log("[TextMorph]",...e);}};exports.DEFAULT_AS=T;exports.DEFAULT_TEXT_MORPH_OPTIONS=A;exports.TextMorph=b;//# sourceMappingURL=index.js.map
25
- //# sourceMappingURL=index.js.map
23
+ }`,h=null,v=0;function X(){v++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=K,document.head.appendChild(h));}function G(){v--,!(v>0||!h)&&(h.remove(),h=null);}function U(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let n=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:n.matches,destroy:i};function t(s){e.prefersReducedMotion=s.matches;}function i(){n.removeEventListener("change",t);}return n.addEventListener("change",t),e}var J="span",W={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},L=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(e){let{ease:t,...i}={...W,...e},s,r;if(typeof t=="object"){let o=D(t);s=o.easing,r=o.duration;}else s=t,r=i.duration;this.options={...i,ease:s,duration:r},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=U()),this.isDisabled()||(this.element.setAttribute(y,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute(M,"")),this.data="",this.isDisabled()||X();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(y),this.element.removeAttribute(M),G();}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,t){let i=t.offsetWidth,s=t.offsetHeight,r=H(e,this.options.locale);this.prevMeasures=A(this.element);let o=Array.from(t.children),a=new Set(r.map(p=>p.id)),u=o.filter(p=>!a.has(p.getAttribute(f))&&!p.hasAttribute(d)),m=new Set(u),c=o.map(p=>p.getAttribute(f)),l=_(o,m,c,a);if(F(u),P(t,o,a,r),this.currentMeasures=A(this.element),this.updateStyles(r),u.forEach(p=>{if(this.isInitialRender){p.remove();return}let g=l.get(p),{dx:b,dy:E}=g?S(this.currentMeasures,this.prevMeasures,g):{dx:0,dy:0};N(p,{dx:b,dy:E,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,t.style.width="auto",t.style.height="auto";return}k(t,i,s,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let t=Array.from(this.element.children),i=e.map(r=>r.id),s=new Set(i.filter(r=>this.prevMeasures[r]));t.forEach((r,o)=>{if(r.hasAttribute(d))return;let a=r.getAttribute(f)||`child-${o}`,u=!this.prevMeasures[a],m=u?w(e.findIndex(p=>p.id===a),i,s):a,{dx:c,dy:l}=m?S(this.prevMeasures,this.currentMeasures,m):{dx:0,dy:0};C(r,{deltaX:c,deltaY:l,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};exports.DEFAULT_AS=J;exports.DEFAULT_TEXT_MORPH_OPTIONS=W;exports.TextMorph=L;
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
- var T="span",A={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},b=class g{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;prefersReducedMotion=false;mediaQuery;static styleEl;constructor(e){this.options={...A,...e},this.element=e.element,typeof window<"u"&&this.options.respectReducedMotion&&(this.mediaQuery=window.matchMedia("(prefers-reduced-motion: reduce)"),this.prefersReducedMotion=this.mediaQuery.matches,this.mediaQuery.addEventListener("change",this.handleMediaQueryChange)),this.isDisabled()||(this.element.setAttribute("torph-root",""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute("torph-debug","")),this.data="",this.isDisabled()||this.addStyles();}destroy(){this.mediaQuery&&this.mediaQuery.removeEventListener("change",this.handleMediaQueryChange),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute("torph-root"),this.element.removeAttribute("torph-debug"),this.removeStyles();}handleMediaQueryChange=e=>{this.prefersReducedMotion=e.matches;};isDisabled(){return !!(this.options.disabled||this.options.respectReducedMotion&&this.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,s){let h=s.offsetWidth,i=s.offsetHeight,a=e.includes(" "),p;if(typeof Intl.Segmenter<"u"){let r=new Intl.Segmenter(this.options.locale,{granularity:a?"word":"grapheme"}).segment(e)[Symbol.iterator]();p=this.blocks(r);}else p=this.blocksFallback(e,a);this.prevMeasures=this.measure();let l=Array.from(s.children),d=new Set(p.map(t=>t.id)),c=l.filter(t=>!d.has(t.getAttribute("torph-id"))&&!t.hasAttribute("torph-exiting")),m=new Set(c),y=new Map;for(let t=0;t<l.length;t++){let r=l[t];if(!m.has(r))continue;let o=null;for(let n=t+1;n<l.length;n++){let u=l[n].getAttribute("torph-id");if(d.has(u)&&!m.has(l[n])){o=u;break}}if(!o)for(let n=t-1;n>=0;n--){let u=l[n].getAttribute("torph-id");if(d.has(u)&&!m.has(l[n])){o=u;break}}y.set(r,o);}let x=c.map(t=>(t.getAnimations().forEach(r=>r.cancel()),{left:t.offsetLeft,top:t.offsetTop,width:t.offsetWidth,height:t.offsetHeight}));if(c.forEach((t,r)=>{let o=x[r];t.setAttribute("torph-exiting",""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${o.left}px`,t.style.top=`${o.top}px`,t.style.width=`${o.width}px`,t.style.height=`${o.height}px`;}),l.forEach(t=>{let r=t.getAttribute("torph-id");d.has(r)&&t.remove();}),Array.from(s.childNodes).forEach(t=>{t.nodeType===Node.TEXT_NODE&&t.remove();}),p.forEach(t=>{let r=document.createElement("span");r.setAttribute("torph-item",""),r.setAttribute("torph-id",t.id),r.textContent=t.string,s.appendChild(r);}),this.currentMeasures=this.measure(),this.updateStyles(p),c.forEach(t=>{if(this.isInitialRender){t.remove();return}let r=y.get(t),o=0,n=0;if(r&&this.prevMeasures[r]&&this.currentMeasures[r]){let E=this.prevMeasures[r],v=this.currentMeasures[r];o=v.x-E.x,n=v.y-E.y;}t.animate({transform:this.options.scale?`translate(${o}px, ${n}px) scale(0.95)`:`translate(${o}px, ${n}px)`,offset:1},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let u=t.animate({opacity:0,offset:1},{duration:this.options.duration*.25,easing:"linear",fill:"both"});u.onfinish=()=>t.remove();}),this.isInitialRender){this.isInitialRender=false,s.style.width="auto",s.style.height="auto";return}if(h===0||i===0)return;s.style.width="auto",s.style.height="auto",s.offsetWidth;let f=s.offsetWidth,M=s.offsetHeight;s.style.width=`${h}px`,s.style.height=`${i}px`,s.offsetWidth,s.style.width=`${f}px`,s.style.height=`${M}px`,setTimeout(()=>{s.style.width="auto",s.style.height="auto",this.options.onAnimationComplete&&this.options.onAnimationComplete();},this.options.duration);}measure(){let e=Array.from(this.element.children),s={};return e.forEach((h,i)=>{if(h.hasAttribute("torph-exiting"))return;let a=h.getAttribute("torph-id")||`child-${i}`;s[a]={x:h.offsetLeft,y:h.offsetTop};}),s}updateStyles(e){if(this.isInitialRender)return;let s=Array.from(this.element.children),h=new Set(e.map(i=>i.id).filter(i=>this.prevMeasures[i]));s.forEach((i,a)=>{if(i.hasAttribute("torph-exiting"))return;let p=i.getAttribute("torph-id")||`child-${a}`,l=this.prevMeasures[p],d=this.currentMeasures[p],c=d?.x||0,m=d?.y||0,y=l?l.x-c:0,x=l?l.y-m:0,f=!l;if(f){let r=e.findIndex(n=>n.id===p),o=null;for(let n=r-1;n>=0;n--)if(h.has(e[n].id)){o=e[n].id;break}if(!o){for(let n=r+1;n<e.length;n++)if(h.has(e[n].id)){o=e[n].id;break}}if(o){let n=this.prevMeasures[o],u=this.currentMeasures[o];y=n.x-u.x,x=n.y-u.y;}}i.getAnimations().forEach(r=>r.cancel()),i.animate({transform:`translate(${y}px, ${x}px) scale(${f?.95:1})`,offset:0},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let M=f?this.options.duration*.25:0,t=f?this.options.duration*.25:0;i.animate({opacity:f?0:1,offset:0},{duration:M,delay:t,easing:"linear",fill:"both"});});}addStyles(){if(g.styleEl)return;let e=document.createElement("style");e.dataset.torph="true",e.innerHTML=`
2
- [torph-root] {
1
+ function $(n,e,t){if(t<1){let u=e*Math.sqrt(1-t*t);return 1-Math.exp(-t*e*n)*(Math.cos(u*n)+t*e/u*Math.sin(u*n))}let i=Math.sqrt(t*t-1),s=-e*(t+i),r=-e*(t-i),o=-s/(r-s);return 1-(1-o)*Math.exp(s*n)-o*Math.exp(r*n)}function B(n,e,t){let r=0;for(let o=0;o<10;o+=.001)if(Math.abs($(o,n,e)-1)>t)r=0;else if(r+=.001,r>.1)return Math.ceil((o-r+.001)*1e3);return Math.ceil(10*1e3)}var I=new Map;function D(n){let{stiffness:e=100,damping:t=10,mass:i=1,precision:s=.001}=n??{},r=`${e}:${t}:${i}:${s}`,o=I.get(r);if(o)return o;let a=Math.sqrt(e/i),u=t/(2*Math.sqrt(e*i)),m=B(a,u,s),c=Math.min(100,Math.max(32,Math.round(m/15))),l=[];for(let g=0;g<c;g++){let b=g/(c-1)*(m/1e3),E=g===c-1?1:$(b,a,u);l.push(Math.round(E*1e4)/1e4+"");}for(;l.length>2&&l[l.length-2]==="1";)l.splice(l.length-2,1);let p={easing:`linear(${l.join(", ")})`,duration:m};return I.set(r,p),p}function H(n,e){let t=n.includes(" ");if(typeof Intl.Segmenter<"u"){let s=new Intl.Segmenter(e,{granularity:t?"word":"grapheme"}).segment(n)[Symbol.iterator]();return j(s)}return Y(n,t)}function j(n){return Array.from(n).reduce((e,t)=>t.segment===" "?[...e,{id:`space-${t.index}`,string:"\xA0"}]:e.find(s=>s.string===t.segment)?[...e,{id:`${t.segment}-${t.index}`,string:t.segment}]:[...e,{id:t.segment,string:t.segment}],[])}function q(n,e,t){let i=n.find(s=>s.string===e);n.push(i?{id:`${e}-${t}`,string:e}:{id:e,string:e});}function Y(n,e){let t=e?n.split(" "):n.split(""),i=[];return t.forEach((s,r)=>{e&&r>0&&i.push({id:`space-${r}`,string:"\xA0"}),q(i,s,r);}),i}var y="torph-root",T="torph-item",f="torph-id",d="torph-exiting",M="torph-debug";function A(n){let e=Array.from(n.children),t={};return e.forEach((i,s)=>{if(i.hasAttribute(d))return;let r=i.getAttribute(f)||`child-${s}`;t[r]={x:i.offsetLeft,y:i.offsetTop};}),t}function S(n,e,t){let i=n[t],s=e[t];return !i||!s?{dx:0,dy:0}:{dx:i.x-s.x,dy:i.y-s.y}}function w(n,e,t,i="backward-first"){let[s,r]=i==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=n-1;u>=0;u--)if(t.has(e[u]))return e[u]}else for(let u=n+1;u<e.length;u++)if(t.has(e[u]))return e[u];return null};return o(s)??o(r)}function _(n,e,t,i){let s=new Set(t.filter((o,a)=>i.has(o)&&!e.has(n[a]))),r=new Map;for(let o=0;o<n.length;o++){let a=n[o];e.has(a)&&r.set(a,w(o,t,s,"forward-first"));}return r}function O(n,e){return Math.min(n*e,150)}function R(n){let e=getComputedStyle(n).transform;if(!e||e==="none")return {tx:0,ty:0};let t=e.match(/matrix\(([^)]+)\)/);if(!t)return {tx:0,ty:0};let i=t[1].split(",").map(Number);return {tx:i[4]||0,ty:i[5]||0}}function Q(n){let{tx:e,ty:t}=R(n),i=Number(getComputedStyle(n).opacity)||1;return n.getAnimations().forEach(s=>s.cancel()),{tx:e,ty:t,opacity:i}}function N(n,e){let{dx:t,dy:i,duration:s,ease:r,scale:o}=e;n.animate({transform:o?`translate(${t}px, ${i}px) scale(0.95)`:`translate(${t}px, ${i}px)`,offset:1},{duration:s,easing:r,fill:"both"});let a=n.animate({opacity:0,offset:1},{duration:O(s,.25),easing:"linear",fill:"both"});a.onfinish=()=>n.remove();}function C(n,e){let{deltaX:t,deltaY:i,isNew:s,duration:r,ease:o}=e,a=Q(n),u=t+a.tx,m=i+a.ty;n.animate({transform:`translate(${u}px, ${m}px) scale(${s?.95:1})`,offset:0},{duration:r,easing:o,fill:"both"});let c=s&&a.opacity>=1?0:a.opacity;c<1&&n.animate([{opacity:c},{opacity:1}],{duration:O(r,s?.5:.25),easing:"linear",fill:"both"});}var x=null;function k(n,e,t,i,s){if(x&&(x(),x=null),e===0||t===0)return;n.style.width="auto",n.style.height="auto",n.offsetWidth;let r=n.offsetWidth,o=n.offsetHeight;n.style.width=`${e}px`,n.style.height=`${t}px`,n.offsetWidth,n.style.width=`${r}px`,n.style.height=`${o}px`;function a(){n.removeEventListener("transitionend",u),clearTimeout(m),x=null,n.style.width="auto",n.style.height="auto",s?.();}function u(c){c.target===n&&(c.propertyName!=="width"&&c.propertyName!=="height"||a());}n.addEventListener("transitionend",u);let m=setTimeout(a,i+50);x=()=>{n.removeEventListener("transitionend",u),clearTimeout(m),x=null;};}function F(n){let e=n.map(t=>{let{tx:i,ty:s}=R(t),r=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(o=>o.cancel()),{left:t.offsetLeft+i,top:t.offsetTop+s,width:t.offsetWidth,height:t.offsetHeight,opacity:r}});n.forEach((t,i)=>{let s=e[i];t.setAttribute(d,""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${s.left}px`,t.style.top=`${s.top}px`,t.style.width=`${s.width}px`,t.style.height=`${s.height}px`,t.style.opacity=String(s.opacity);});}function P(n,e,t,i){let s=new Map;e.forEach(r=>{let o=r.getAttribute(f);t.has(o)&&!r.hasAttribute(d)&&(s.set(o,r),r.remove());}),Array.from(n.childNodes).forEach(r=>{r.nodeType===Node.TEXT_NODE&&r.remove();}),i.forEach(r=>{let o=s.get(r.id);if(o)o.textContent=r.string,n.appendChild(o);else {let a=document.createElement("span");a.setAttribute(T,""),a.setAttribute(f,r.id),a.textContent=r.string,n.appendChild(a);}});}var K=`
2
+ [${y}] {
3
3
  display: inline-flex;
4
4
  position: relative;
5
5
  will-change: width, height;
@@ -7,19 +7,17 @@ var T="span",A={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezi
7
7
  white-space: nowrap;
8
8
  }
9
9
 
10
- [torph-item] {
10
+ [${T}] {
11
11
  display: inline-block;
12
12
  will-change: opacity, transform;
13
13
  transform: none;
14
14
  opacity: 1;
15
15
  }
16
16
 
17
- [torph-root][torph-debug] {
18
- outline:2px solid magenta;
19
- [torph-item] {
20
- outline:2px solid cyan;
17
+ [${y}][${M}] {
18
+ outline: 2px solid magenta;
19
+ [${T}] {
20
+ outline: 2px solid cyan;
21
21
  outline-offset: -4px;
22
22
  }
23
- }
24
- `,document.head.appendChild(e),g.styleEl=e;}removeStyles(){g.styleEl&&(g.styleEl.remove(),g.styleEl=void 0);}blocks(e){return Array.from(e).reduce((h,i)=>i.segment===" "?[...h,{id:`space-${i.index}`,string:"\xA0"}]:h.find(p=>p.string===i.segment)?[...h,{id:`${i.segment}-${i.index}`,string:i.segment}]:[...h,{id:i.segment,string:i.segment}],[])}blocksFallback(e,s){let h=s?e.split(" "):e.split(""),i=[];return s?h.forEach((a,p)=>{p>0&&i.push({id:`space-${p}`,string:"\xA0"}),i.find(d=>d.string===a)?i.push({id:`${a}-${p}`,string:a}):i.push({id:a,string:a});}):h.forEach((a,p)=>{i.find(d=>d.string===a)?i.push({id:`${a}-${p}`,string:a}):i.push({id:a,string:a});}),i}log(...e){this.options.debug&&console.log("[TextMorph]",...e);}};export{T as DEFAULT_AS,A as DEFAULT_TEXT_MORPH_OPTIONS,b as TextMorph};//# sourceMappingURL=index.mjs.map
25
- //# sourceMappingURL=index.mjs.map
23
+ }`,h=null,v=0;function X(){v++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=K,document.head.appendChild(h));}function G(){v--,!(v>0||!h)&&(h.remove(),h=null);}function U(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let n=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:n.matches,destroy:i};function t(s){e.prefersReducedMotion=s.matches;}function i(){n.removeEventListener("change",t);}return n.addEventListener("change",t),e}var J="span",W={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},L=class{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;reducedMotion=null;constructor(e){let{ease:t,...i}={...W,...e},s,r;if(typeof t=="object"){let o=D(t);s=o.easing,r=o.duration;}else s=t,r=i.duration;this.options={...i,ease:s,duration:r},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=U()),this.isDisabled()||(this.element.setAttribute(y,""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,e.debug&&this.element.setAttribute(M,"")),this.data="",this.isDisabled()||X();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(y),this.element.removeAttribute(M),G();}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,t){let i=t.offsetWidth,s=t.offsetHeight,r=H(e,this.options.locale);this.prevMeasures=A(this.element);let o=Array.from(t.children),a=new Set(r.map(p=>p.id)),u=o.filter(p=>!a.has(p.getAttribute(f))&&!p.hasAttribute(d)),m=new Set(u),c=o.map(p=>p.getAttribute(f)),l=_(o,m,c,a);if(F(u),P(t,o,a,r),this.currentMeasures=A(this.element),this.updateStyles(r),u.forEach(p=>{if(this.isInitialRender){p.remove();return}let g=l.get(p),{dx:b,dy:E}=g?S(this.currentMeasures,this.prevMeasures,g):{dx:0,dy:0};N(p,{dx:b,dy:E,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,t.style.width="auto",t.style.height="auto";return}k(t,i,s,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let t=Array.from(this.element.children),i=e.map(r=>r.id),s=new Set(i.filter(r=>this.prevMeasures[r]));t.forEach((r,o)=>{if(r.hasAttribute(d))return;let a=r.getAttribute(f)||`child-${o}`,u=!this.prevMeasures[a],m=u?w(e.findIndex(p=>p.id===a),i,s):a,{dx:c,dy:l}=m?S(this.prevMeasures,this.currentMeasures,m):{dx:0,dy:0};C(r,{deltaX:c,deltaY:l,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};export{J as DEFAULT_AS,W as DEFAULT_TEXT_MORPH_OPTIONS,L as TextMorph};
@@ -1,12 +1,19 @@
1
1
  import React from 'react';
2
2
 
3
+ interface SpringParams {
4
+ stiffness?: number;
5
+ damping?: number;
6
+ mass?: number;
7
+ precision?: number;
8
+ }
9
+
3
10
  interface TextMorphOptions {
4
11
  debug?: boolean;
5
12
  element: HTMLElement;
6
13
  locale?: Intl.LocalesArgument;
7
14
  scale?: boolean;
8
15
  duration?: number;
9
- ease?: string;
16
+ ease?: string | SpringParams;
10
17
  disabled?: boolean;
11
18
  respectReducedMotion?: boolean;
12
19
  onAnimationStart?: () => void;
@@ -1,12 +1,19 @@
1
1
  import React from 'react';
2
2
 
3
+ interface SpringParams {
4
+ stiffness?: number;
5
+ damping?: number;
6
+ mass?: number;
7
+ precision?: number;
8
+ }
9
+
3
10
  interface TextMorphOptions {
4
11
  debug?: boolean;
5
12
  element: HTMLElement;
6
13
  locale?: Intl.LocalesArgument;
7
14
  scale?: boolean;
8
15
  duration?: number;
9
- ease?: string;
16
+ ease?: string | SpringParams;
10
17
  disabled?: boolean;
11
18
  respectReducedMotion?: boolean;
12
19
  onAnimationStart?: () => void;
@@ -1,5 +1,5 @@
1
- 'use strict';var c=require('react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var c__default=/*#__PURE__*/_interopDefault(c);var A="span",$={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},b=class h{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;prefersReducedMotion=false;mediaQuery;static styleEl;constructor(t){this.options={...$,...t},this.element=t.element,typeof window<"u"&&this.options.respectReducedMotion&&(this.mediaQuery=window.matchMedia("(prefers-reduced-motion: reduce)"),this.prefersReducedMotion=this.mediaQuery.matches,this.mediaQuery.addEventListener("change",this.handleMediaQueryChange)),this.isDisabled()||(this.element.setAttribute("torph-root",""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,t.debug&&this.element.setAttribute("torph-debug","")),this.data="",this.isDisabled()||this.addStyles();}destroy(){this.mediaQuery&&this.mediaQuery.removeEventListener("change",this.handleMediaQueryChange),this.element.getAnimations().forEach(t=>t.cancel()),this.element.removeAttribute("torph-root"),this.element.removeAttribute("torph-debug"),this.removeStyles();}handleMediaQueryChange=t=>{this.prefersReducedMotion=t.matches;};isDisabled(){return !!(this.options.disabled||this.options.respectReducedMotion&&this.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,s){let o=s.offsetWidth,i=s.offsetHeight,a=t.includes(" "),l;if(typeof Intl.Segmenter<"u"){let n=new Intl.Segmenter(this.options.locale,{granularity:a?"word":"grapheme"}).segment(t)[Symbol.iterator]();l=this.blocks(n);}else l=this.blocksFallback(t,a);this.prevMeasures=this.measure();let p=Array.from(s.children),d=new Set(l.map(e=>e.id)),m=p.filter(e=>!d.has(e.getAttribute("torph-id"))&&!e.hasAttribute("torph-exiting")),y=new Set(m),x=new Map;for(let e=0;e<p.length;e++){let n=p[e];if(!y.has(n))continue;let u=null;for(let r=e+1;r<p.length;r++){let f=p[r].getAttribute("torph-id");if(d.has(f)&&!y.has(p[r])){u=f;break}}if(!u)for(let r=e-1;r>=0;r--){let f=p[r].getAttribute("torph-id");if(d.has(f)&&!y.has(p[r])){u=f;break}}x.set(n,u);}let M=m.map(e=>(e.getAnimations().forEach(n=>n.cancel()),{left:e.offsetLeft,top:e.offsetTop,width:e.offsetWidth,height:e.offsetHeight}));if(m.forEach((e,n)=>{let u=M[n];e.setAttribute("torph-exiting",""),e.style.position="absolute",e.style.pointerEvents="none",e.style.left=`${u.left}px`,e.style.top=`${u.top}px`,e.style.width=`${u.width}px`,e.style.height=`${u.height}px`;}),p.forEach(e=>{let n=e.getAttribute("torph-id");d.has(n)&&e.remove();}),Array.from(s.childNodes).forEach(e=>{e.nodeType===Node.TEXT_NODE&&e.remove();}),l.forEach(e=>{let n=document.createElement("span");n.setAttribute("torph-item",""),n.setAttribute("torph-id",e.id),n.textContent=e.string,s.appendChild(n);}),this.currentMeasures=this.measure(),this.updateStyles(l),m.forEach(e=>{if(this.isInitialRender){e.remove();return}let n=x.get(e),u=0,r=0;if(n&&this.prevMeasures[n]&&this.currentMeasures[n]){let E=this.prevMeasures[n],v=this.currentMeasures[n];u=v.x-E.x,r=v.y-E.y;}e.animate({transform:this.options.scale?`translate(${u}px, ${r}px) scale(0.95)`:`translate(${u}px, ${r}px)`,offset:1},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let f=e.animate({opacity:0,offset:1},{duration:this.options.duration*.25,easing:"linear",fill:"both"});f.onfinish=()=>e.remove();}),this.isInitialRender){this.isInitialRender=false,s.style.width="auto",s.style.height="auto";return}if(o===0||i===0)return;s.style.width="auto",s.style.height="auto",s.offsetWidth;let g=s.offsetWidth,T=s.offsetHeight;s.style.width=`${o}px`,s.style.height=`${i}px`,s.offsetWidth,s.style.width=`${g}px`,s.style.height=`${T}px`,setTimeout(()=>{s.style.width="auto",s.style.height="auto",this.options.onAnimationComplete&&this.options.onAnimationComplete();},this.options.duration);}measure(){let t=Array.from(this.element.children),s={};return t.forEach((o,i)=>{if(o.hasAttribute("torph-exiting"))return;let a=o.getAttribute("torph-id")||`child-${i}`;s[a]={x:o.offsetLeft,y:o.offsetTop};}),s}updateStyles(t){if(this.isInitialRender)return;let s=Array.from(this.element.children),o=new Set(t.map(i=>i.id).filter(i=>this.prevMeasures[i]));s.forEach((i,a)=>{if(i.hasAttribute("torph-exiting"))return;let l=i.getAttribute("torph-id")||`child-${a}`,p=this.prevMeasures[l],d=this.currentMeasures[l],m=d?.x||0,y=d?.y||0,x=p?p.x-m:0,M=p?p.y-y:0,g=!p;if(g){let n=t.findIndex(r=>r.id===l),u=null;for(let r=n-1;r>=0;r--)if(o.has(t[r].id)){u=t[r].id;break}if(!u){for(let r=n+1;r<t.length;r++)if(o.has(t[r].id)){u=t[r].id;break}}if(u){let r=this.prevMeasures[u],f=this.currentMeasures[u];x=r.x-f.x,M=r.y-f.y;}}i.getAnimations().forEach(n=>n.cancel()),i.animate({transform:`translate(${x}px, ${M}px) scale(${g?.95:1})`,offset:0},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let T=g?this.options.duration*.25:0,e=g?this.options.duration*.25:0;i.animate({opacity:g?0:1,offset:0},{duration:T,delay:e,easing:"linear",fill:"both"});});}addStyles(){if(h.styleEl)return;let t=document.createElement("style");t.dataset.torph="true",t.innerHTML=`
2
- [torph-root] {
1
+ 'use strict';var g=require('react');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var g__default=/*#__PURE__*/_interopDefault(g);function H(n,e,t){if(t<1){let u=e*Math.sqrt(1-t*t);return 1-Math.exp(-t*e*n)*(Math.cos(u*n)+t*e/u*Math.sin(u*n))}let i=Math.sqrt(t*t-1),s=-e*(t+i),r=-e*(t-i),o=-s/(r-s);return 1-(1-o)*Math.exp(s*n)-o*Math.exp(r*n)}function Y(n,e,t){let r=0;for(let o=0;o<10;o+=.001)if(Math.abs(H(o,n,e)-1)>t)r=0;else if(r+=.001,r>.1)return Math.ceil((o-r+.001)*1e3);return Math.ceil(10*1e3)}var $=new Map;function D(n){let{stiffness:e=100,damping:t=10,mass:i=1,precision:s=.001}=n??{},r=`${e}:${t}:${i}:${s}`,o=$.get(r);if(o)return o;let a=Math.sqrt(e/i),u=t/(2*Math.sqrt(e*i)),p=Y(a,u,s),l=Math.min(100,Math.max(32,Math.round(p/15))),m=[];for(let y=0;y<l;y++){let S=y/(l-1)*(p/1e3),R=y===l-1?1:H(S,a,u);m.push(Math.round(R*1e4)/1e4+"");}for(;m.length>2&&m[m.length-2]==="1";)m.splice(m.length-2,1);let c={easing:`linear(${m.join(", ")})`,duration:p};return $.set(r,c),c}function _(n,e){let t=n.includes(" ");if(typeof Intl.Segmenter<"u"){let s=new Intl.Segmenter(e,{granularity:t?"word":"grapheme"}).segment(n)[Symbol.iterator]();return Q(s)}return V(n,t)}function Q(n){return Array.from(n).reduce((e,t)=>t.segment===" "?[...e,{id:`space-${t.index}`,string:"\xA0"}]:e.find(s=>s.string===t.segment)?[...e,{id:`${t.segment}-${t.index}`,string:t.segment}]:[...e,{id:t.segment,string:t.segment}],[])}function J(n,e,t){let i=n.find(s=>s.string===e);n.push(i?{id:`${e}-${t}`,string:e}:{id:e,string:e});}function V(n,e){let t=e?n.split(" "):n.split(""),i=[];return t.forEach((s,r)=>{e&&r>0&&i.push({id:`space-${r}`,string:"\xA0"}),J(i,s,r);}),i}var x="torph-root",M="torph-item",f="torph-id",d="torph-exiting",b="torph-debug";function w(n){let e=Array.from(n.children),t={};return e.forEach((i,s)=>{if(i.hasAttribute(d))return;let r=i.getAttribute(f)||`child-${s}`;t[r]={x:i.offsetLeft,y:i.offsetTop};}),t}function v(n,e,t){let i=n[t],s=e[t];return !i||!s?{dx:0,dy:0}:{dx:i.x-s.x,dy:i.y-s.y}}function L(n,e,t,i="backward-first"){let[s,r]=i==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=n-1;u>=0;u--)if(t.has(e[u]))return e[u]}else for(let u=n+1;u<e.length;u++)if(t.has(e[u]))return e[u];return null};return o(s)??o(r)}function C(n,e,t,i){let s=new Set(t.filter((o,a)=>i.has(o)&&!e.has(n[a]))),r=new Map;for(let o=0;o<n.length;o++){let a=n[o];e.has(a)&&r.set(a,L(o,t,s,"forward-first"));}return r}function N(n,e){return Math.min(n*e,150)}function I(n){let e=getComputedStyle(n).transform;if(!e||e==="none")return {tx:0,ty:0};let t=e.match(/matrix\(([^)]+)\)/);if(!t)return {tx:0,ty:0};let i=t[1].split(",").map(Number);return {tx:i[4]||0,ty:i[5]||0}}function z(n){let{tx:e,ty:t}=I(n),i=Number(getComputedStyle(n).opacity)||1;return n.getAnimations().forEach(s=>s.cancel()),{tx:e,ty:t,opacity:i}}function k(n,e){let{dx:t,dy:i,duration:s,ease:r,scale:o}=e;n.animate({transform:o?`translate(${t}px, ${i}px) scale(0.95)`:`translate(${t}px, ${i}px)`,offset:1},{duration:s,easing:r,fill:"both"});let a=n.animate({opacity:0,offset:1},{duration:N(s,.25),easing:"linear",fill:"both"});a.onfinish=()=>n.remove();}function P(n,e){let{deltaX:t,deltaY:i,isNew:s,duration:r,ease:o}=e,a=z(n),u=t+a.tx,p=i+a.ty;n.animate({transform:`translate(${u}px, ${p}px) scale(${s?.95:1})`,offset:0},{duration:r,easing:o,fill:"both"});let l=s&&a.opacity>=1?0:a.opacity;l<1&&n.animate([{opacity:l},{opacity:1}],{duration:N(r,s?.5:.25),easing:"linear",fill:"both"});}var T=null;function F(n,e,t,i,s){if(T&&(T(),T=null),e===0||t===0)return;n.style.width="auto",n.style.height="auto",n.offsetWidth;let r=n.offsetWidth,o=n.offsetHeight;n.style.width=`${e}px`,n.style.height=`${t}px`,n.offsetWidth,n.style.width=`${r}px`,n.style.height=`${o}px`;function a(){n.removeEventListener("transitionend",u),clearTimeout(p),T=null,n.style.width="auto",n.style.height="auto",s?.();}function u(l){l.target===n&&(l.propertyName!=="width"&&l.propertyName!=="height"||a());}n.addEventListener("transitionend",u);let p=setTimeout(a,i+50);T=()=>{n.removeEventListener("transitionend",u),clearTimeout(p),T=null;};}function X(n){let e=n.map(t=>{let{tx:i,ty:s}=I(t),r=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(o=>o.cancel()),{left:t.offsetLeft+i,top:t.offsetTop+s,width:t.offsetWidth,height:t.offsetHeight,opacity:r}});n.forEach((t,i)=>{let s=e[i];t.setAttribute(d,""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${s.left}px`,t.style.top=`${s.top}px`,t.style.width=`${s.width}px`,t.style.height=`${s.height}px`,t.style.opacity=String(s.opacity);});}function G(n,e,t,i){let s=new Map;e.forEach(r=>{let o=r.getAttribute(f);t.has(o)&&!r.hasAttribute(d)&&(s.set(o,r),r.remove());}),Array.from(n.childNodes).forEach(r=>{r.nodeType===Node.TEXT_NODE&&r.remove();}),i.forEach(r=>{let o=s.get(r.id);if(o)o.textContent=r.string,n.appendChild(o);else {let a=document.createElement("span");a.setAttribute(M,""),a.setAttribute(f,r.id),a.textContent=r.string,n.appendChild(a);}});}var Z=`
2
+ [${x}] {
3
3
  display: inline-flex;
4
4
  position: relative;
5
5
  will-change: width, height;
@@ -7,19 +7,17 @@
7
7
  white-space: nowrap;
8
8
  }
9
9
 
10
- [torph-item] {
10
+ [${M}] {
11
11
  display: inline-block;
12
12
  will-change: opacity, transform;
13
13
  transform: none;
14
14
  opacity: 1;
15
15
  }
16
16
 
17
- [torph-root][torph-debug] {
18
- outline:2px solid magenta;
19
- [torph-item] {
20
- outline:2px solid cyan;
17
+ [${x}][${b}] {
18
+ outline: 2px solid magenta;
19
+ [${M}] {
20
+ outline: 2px solid cyan;
21
21
  outline-offset: -4px;
22
22
  }
23
- }
24
- `,document.head.appendChild(t),h.styleEl=t;}removeStyles(){h.styleEl&&(h.styleEl.remove(),h.styleEl=void 0);}blocks(t){return Array.from(t).reduce((o,i)=>i.segment===" "?[...o,{id:`space-${i.index}`,string:"\xA0"}]:o.find(l=>l.string===i.segment)?[...o,{id:`${i.segment}-${i.index}`,string:i.segment}]:[...o,{id:i.segment,string:i.segment}],[])}blocksFallback(t,s){let o=s?t.split(" "):t.split(""),i=[];return s?o.forEach((a,l)=>{l>0&&i.push({id:`space-${l}`,string:"\xA0"}),i.find(d=>d.string===a)?i.push({id:`${a}-${l}`,string:a}):i.push({id:a,string:a});}):o.forEach((a,l)=>{i.find(d=>d.string===a)?i.push({id:`${a}-${l}`,string:a}):i.push({id:a,string:a});}),i}log(...t){this.options.debug&&console.log("[TextMorph]",...t);}};function w(h){if(typeof h=="string")return h;if(typeof h=="number")return String(h);if(!h||typeof h=="boolean")return "";if(Array.isArray(h))return h.map(w).join("");throw c__default.default.isValidElement(h)?new Error("TextMorph only accepts text content. Found a React element \u2014 use strings, numbers, or expressions instead."):new Error(`TextMorph received an unsupported child of type "${typeof h}".`)}var R=({children:h,className:t,style:s,as:o=A,...i})=>{let{ref:a,update:l}=S(i),p=w(h),d=c__default.default.useRef({__html:p});return c__default.default.useEffect(()=>{l(p);},[p,l]),c__default.default.createElement(o,{ref:a,className:t,style:s,dangerouslySetInnerHTML:d.current})};function S(h){let t=c__default.default.useRef(null),s=c__default.default.useRef(null);c__default.default.useEffect(()=>(t.current&&(s.current=new b({element:t.current,...h})),()=>{s.current?.destroy();}),[]);let o=c__default.default.useCallback(i=>{s.current?.update(i);},[]);return {ref:t,update:o}}exports.TextMorph=R;exports.useTextMorph=S;//# sourceMappingURL=index.js.map
25
- //# sourceMappingURL=index.js.map
23
+ }`,h=null,O=0;function U(){O++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=Z,document.head.appendChild(h));}function W(){O--,!(O>0||!h)&&(h.remove(),h=null);}function j(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let n=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:n.matches,destroy:i};function t(s){e.prefersReducedMotion=s.matches;}function i(){n.removeEventListener("change",t);}return n.addEventListener("change",t),e}var E=class n{instance=null;lastText="";configKey="";attach(e,t){this.instance?.destroy(),this.instance=new A({element:e,...t}),this.configKey=n.serializeConfig(t),this.lastText&&this.instance.update(this.lastText);}update(e){this.lastText=e,this.instance?.update(e);}needsRecreate(e){return n.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 B="span",tt={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:t,...i}={...tt,...e},s,r;if(typeof t=="object"){let o=D(t);s=o.easing,r=o.duration;}else s=t,r=i.duration;this.options={...i,ease:s,duration:r},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=j()),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(b,"")),this.data="",this.isDisabled()||U();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(x),this.element.removeAttribute(b),W();}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,t){let i=t.offsetWidth,s=t.offsetHeight,r=_(e,this.options.locale);this.prevMeasures=w(this.element);let o=Array.from(t.children),a=new Set(r.map(c=>c.id)),u=o.filter(c=>!a.has(c.getAttribute(f))&&!c.hasAttribute(d)),p=new Set(u),l=o.map(c=>c.getAttribute(f)),m=C(o,p,l,a);if(X(u),G(t,o,a,r),this.currentMeasures=w(this.element),this.updateStyles(r),u.forEach(c=>{if(this.isInitialRender){c.remove();return}let y=m.get(c),{dx:S,dy:R}=y?v(this.currentMeasures,this.prevMeasures,y):{dx:0,dy:0};k(c,{dx:S,dy:R,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,t.style.width="auto",t.style.height="auto";return}F(t,i,s,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let t=Array.from(this.element.children),i=e.map(r=>r.id),s=new Set(i.filter(r=>this.prevMeasures[r]));t.forEach((r,o)=>{if(r.hasAttribute(d))return;let a=r.getAttribute(f)||`child-${o}`,u=!this.prevMeasures[a],p=u?L(e.findIndex(c=>c.id===a),i,s):a,{dx:l,dy:m}=p?v(this.prevMeasures,this.currentMeasures,p):{dx:0,dy:0};P(r,{deltaX:l,deltaY:m,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};function K(n){if(typeof n=="string")return n;if(typeof n=="number")return String(n);if(!n||typeof n=="boolean")return "";if(Array.isArray(n))return n.map(K).join("");throw g__default.default.isValidElement(n)?new Error("TextMorph only accepts text content. Found a React element \u2014 use strings, numbers, or expressions instead."):new Error(`TextMorph received an unsupported child of type "${typeof n}".`)}var et=({children:n,className:e,style:t,as:i=B,...s})=>{let{ref:r,update:o}=q(s),a=K(n),u=g__default.default.useRef({__html:a});return g__default.default.useEffect(()=>{o(a);},[a,o]),g__default.default.createElement(i,{ref:r,className:e,style:t,dangerouslySetInnerHTML:u.current})};function q(n){let e=g__default.default.useRef(null),t=g__default.default.useRef(new E),i=E.serializeConfig(n);g__default.default.useEffect(()=>(e.current&&t.current.attach(e.current,n),()=>{t.current.destroy();}),[i]);let s=g__default.default.useCallback(r=>{t.current.update(r);},[]);return {ref:e,update:s}}exports.TextMorph=et;exports.useTextMorph=q;
@@ -1,5 +1,5 @@
1
- import c from'react';var A="span",$={debug:false,locale:"en",duration:400,scale:true,ease:"cubic-bezier(0.19, 1, 0.22, 1)",disabled:false,respectReducedMotion:true},b=class h{element;options={};data;currentMeasures={};prevMeasures={};isInitialRender=true;prefersReducedMotion=false;mediaQuery;static styleEl;constructor(t){this.options={...$,...t},this.element=t.element,typeof window<"u"&&this.options.respectReducedMotion&&(this.mediaQuery=window.matchMedia("(prefers-reduced-motion: reduce)"),this.prefersReducedMotion=this.mediaQuery.matches,this.mediaQuery.addEventListener("change",this.handleMediaQueryChange)),this.isDisabled()||(this.element.setAttribute("torph-root",""),this.element.style.transitionDuration=`${this.options.duration}ms`,this.element.style.transitionTimingFunction=this.options.ease,t.debug&&this.element.setAttribute("torph-debug","")),this.data="",this.isDisabled()||this.addStyles();}destroy(){this.mediaQuery&&this.mediaQuery.removeEventListener("change",this.handleMediaQueryChange),this.element.getAnimations().forEach(t=>t.cancel()),this.element.removeAttribute("torph-root"),this.element.removeAttribute("torph-debug"),this.removeStyles();}handleMediaQueryChange=t=>{this.prefersReducedMotion=t.matches;};isDisabled(){return !!(this.options.disabled||this.options.respectReducedMotion&&this.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,s){let o=s.offsetWidth,i=s.offsetHeight,a=t.includes(" "),l;if(typeof Intl.Segmenter<"u"){let n=new Intl.Segmenter(this.options.locale,{granularity:a?"word":"grapheme"}).segment(t)[Symbol.iterator]();l=this.blocks(n);}else l=this.blocksFallback(t,a);this.prevMeasures=this.measure();let p=Array.from(s.children),d=new Set(l.map(e=>e.id)),m=p.filter(e=>!d.has(e.getAttribute("torph-id"))&&!e.hasAttribute("torph-exiting")),y=new Set(m),x=new Map;for(let e=0;e<p.length;e++){let n=p[e];if(!y.has(n))continue;let u=null;for(let r=e+1;r<p.length;r++){let f=p[r].getAttribute("torph-id");if(d.has(f)&&!y.has(p[r])){u=f;break}}if(!u)for(let r=e-1;r>=0;r--){let f=p[r].getAttribute("torph-id");if(d.has(f)&&!y.has(p[r])){u=f;break}}x.set(n,u);}let M=m.map(e=>(e.getAnimations().forEach(n=>n.cancel()),{left:e.offsetLeft,top:e.offsetTop,width:e.offsetWidth,height:e.offsetHeight}));if(m.forEach((e,n)=>{let u=M[n];e.setAttribute("torph-exiting",""),e.style.position="absolute",e.style.pointerEvents="none",e.style.left=`${u.left}px`,e.style.top=`${u.top}px`,e.style.width=`${u.width}px`,e.style.height=`${u.height}px`;}),p.forEach(e=>{let n=e.getAttribute("torph-id");d.has(n)&&e.remove();}),Array.from(s.childNodes).forEach(e=>{e.nodeType===Node.TEXT_NODE&&e.remove();}),l.forEach(e=>{let n=document.createElement("span");n.setAttribute("torph-item",""),n.setAttribute("torph-id",e.id),n.textContent=e.string,s.appendChild(n);}),this.currentMeasures=this.measure(),this.updateStyles(l),m.forEach(e=>{if(this.isInitialRender){e.remove();return}let n=x.get(e),u=0,r=0;if(n&&this.prevMeasures[n]&&this.currentMeasures[n]){let E=this.prevMeasures[n],v=this.currentMeasures[n];u=v.x-E.x,r=v.y-E.y;}e.animate({transform:this.options.scale?`translate(${u}px, ${r}px) scale(0.95)`:`translate(${u}px, ${r}px)`,offset:1},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let f=e.animate({opacity:0,offset:1},{duration:this.options.duration*.25,easing:"linear",fill:"both"});f.onfinish=()=>e.remove();}),this.isInitialRender){this.isInitialRender=false,s.style.width="auto",s.style.height="auto";return}if(o===0||i===0)return;s.style.width="auto",s.style.height="auto",s.offsetWidth;let g=s.offsetWidth,T=s.offsetHeight;s.style.width=`${o}px`,s.style.height=`${i}px`,s.offsetWidth,s.style.width=`${g}px`,s.style.height=`${T}px`,setTimeout(()=>{s.style.width="auto",s.style.height="auto",this.options.onAnimationComplete&&this.options.onAnimationComplete();},this.options.duration);}measure(){let t=Array.from(this.element.children),s={};return t.forEach((o,i)=>{if(o.hasAttribute("torph-exiting"))return;let a=o.getAttribute("torph-id")||`child-${i}`;s[a]={x:o.offsetLeft,y:o.offsetTop};}),s}updateStyles(t){if(this.isInitialRender)return;let s=Array.from(this.element.children),o=new Set(t.map(i=>i.id).filter(i=>this.prevMeasures[i]));s.forEach((i,a)=>{if(i.hasAttribute("torph-exiting"))return;let l=i.getAttribute("torph-id")||`child-${a}`,p=this.prevMeasures[l],d=this.currentMeasures[l],m=d?.x||0,y=d?.y||0,x=p?p.x-m:0,M=p?p.y-y:0,g=!p;if(g){let n=t.findIndex(r=>r.id===l),u=null;for(let r=n-1;r>=0;r--)if(o.has(t[r].id)){u=t[r].id;break}if(!u){for(let r=n+1;r<t.length;r++)if(o.has(t[r].id)){u=t[r].id;break}}if(u){let r=this.prevMeasures[u],f=this.currentMeasures[u];x=r.x-f.x,M=r.y-f.y;}}i.getAnimations().forEach(n=>n.cancel()),i.animate({transform:`translate(${x}px, ${M}px) scale(${g?.95:1})`,offset:0},{duration:this.options.duration,easing:this.options.ease,fill:"both"});let T=g?this.options.duration*.25:0,e=g?this.options.duration*.25:0;i.animate({opacity:g?0:1,offset:0},{duration:T,delay:e,easing:"linear",fill:"both"});});}addStyles(){if(h.styleEl)return;let t=document.createElement("style");t.dataset.torph="true",t.innerHTML=`
2
- [torph-root] {
1
+ import g from'react';function H(n,e,t){if(t<1){let u=e*Math.sqrt(1-t*t);return 1-Math.exp(-t*e*n)*(Math.cos(u*n)+t*e/u*Math.sin(u*n))}let i=Math.sqrt(t*t-1),s=-e*(t+i),r=-e*(t-i),o=-s/(r-s);return 1-(1-o)*Math.exp(s*n)-o*Math.exp(r*n)}function Y(n,e,t){let r=0;for(let o=0;o<10;o+=.001)if(Math.abs(H(o,n,e)-1)>t)r=0;else if(r+=.001,r>.1)return Math.ceil((o-r+.001)*1e3);return Math.ceil(10*1e3)}var $=new Map;function D(n){let{stiffness:e=100,damping:t=10,mass:i=1,precision:s=.001}=n??{},r=`${e}:${t}:${i}:${s}`,o=$.get(r);if(o)return o;let a=Math.sqrt(e/i),u=t/(2*Math.sqrt(e*i)),p=Y(a,u,s),l=Math.min(100,Math.max(32,Math.round(p/15))),m=[];for(let y=0;y<l;y++){let S=y/(l-1)*(p/1e3),R=y===l-1?1:H(S,a,u);m.push(Math.round(R*1e4)/1e4+"");}for(;m.length>2&&m[m.length-2]==="1";)m.splice(m.length-2,1);let c={easing:`linear(${m.join(", ")})`,duration:p};return $.set(r,c),c}function _(n,e){let t=n.includes(" ");if(typeof Intl.Segmenter<"u"){let s=new Intl.Segmenter(e,{granularity:t?"word":"grapheme"}).segment(n)[Symbol.iterator]();return Q(s)}return V(n,t)}function Q(n){return Array.from(n).reduce((e,t)=>t.segment===" "?[...e,{id:`space-${t.index}`,string:"\xA0"}]:e.find(s=>s.string===t.segment)?[...e,{id:`${t.segment}-${t.index}`,string:t.segment}]:[...e,{id:t.segment,string:t.segment}],[])}function J(n,e,t){let i=n.find(s=>s.string===e);n.push(i?{id:`${e}-${t}`,string:e}:{id:e,string:e});}function V(n,e){let t=e?n.split(" "):n.split(""),i=[];return t.forEach((s,r)=>{e&&r>0&&i.push({id:`space-${r}`,string:"\xA0"}),J(i,s,r);}),i}var x="torph-root",M="torph-item",f="torph-id",d="torph-exiting",b="torph-debug";function w(n){let e=Array.from(n.children),t={};return e.forEach((i,s)=>{if(i.hasAttribute(d))return;let r=i.getAttribute(f)||`child-${s}`;t[r]={x:i.offsetLeft,y:i.offsetTop};}),t}function v(n,e,t){let i=n[t],s=e[t];return !i||!s?{dx:0,dy:0}:{dx:i.x-s.x,dy:i.y-s.y}}function L(n,e,t,i="backward-first"){let[s,r]=i==="backward-first"?["backward","forward"]:["forward","backward"],o=a=>{if(a==="backward"){for(let u=n-1;u>=0;u--)if(t.has(e[u]))return e[u]}else for(let u=n+1;u<e.length;u++)if(t.has(e[u]))return e[u];return null};return o(s)??o(r)}function C(n,e,t,i){let s=new Set(t.filter((o,a)=>i.has(o)&&!e.has(n[a]))),r=new Map;for(let o=0;o<n.length;o++){let a=n[o];e.has(a)&&r.set(a,L(o,t,s,"forward-first"));}return r}function N(n,e){return Math.min(n*e,150)}function I(n){let e=getComputedStyle(n).transform;if(!e||e==="none")return {tx:0,ty:0};let t=e.match(/matrix\(([^)]+)\)/);if(!t)return {tx:0,ty:0};let i=t[1].split(",").map(Number);return {tx:i[4]||0,ty:i[5]||0}}function z(n){let{tx:e,ty:t}=I(n),i=Number(getComputedStyle(n).opacity)||1;return n.getAnimations().forEach(s=>s.cancel()),{tx:e,ty:t,opacity:i}}function k(n,e){let{dx:t,dy:i,duration:s,ease:r,scale:o}=e;n.animate({transform:o?`translate(${t}px, ${i}px) scale(0.95)`:`translate(${t}px, ${i}px)`,offset:1},{duration:s,easing:r,fill:"both"});let a=n.animate({opacity:0,offset:1},{duration:N(s,.25),easing:"linear",fill:"both"});a.onfinish=()=>n.remove();}function P(n,e){let{deltaX:t,deltaY:i,isNew:s,duration:r,ease:o}=e,a=z(n),u=t+a.tx,p=i+a.ty;n.animate({transform:`translate(${u}px, ${p}px) scale(${s?.95:1})`,offset:0},{duration:r,easing:o,fill:"both"});let l=s&&a.opacity>=1?0:a.opacity;l<1&&n.animate([{opacity:l},{opacity:1}],{duration:N(r,s?.5:.25),easing:"linear",fill:"both"});}var T=null;function F(n,e,t,i,s){if(T&&(T(),T=null),e===0||t===0)return;n.style.width="auto",n.style.height="auto",n.offsetWidth;let r=n.offsetWidth,o=n.offsetHeight;n.style.width=`${e}px`,n.style.height=`${t}px`,n.offsetWidth,n.style.width=`${r}px`,n.style.height=`${o}px`;function a(){n.removeEventListener("transitionend",u),clearTimeout(p),T=null,n.style.width="auto",n.style.height="auto",s?.();}function u(l){l.target===n&&(l.propertyName!=="width"&&l.propertyName!=="height"||a());}n.addEventListener("transitionend",u);let p=setTimeout(a,i+50);T=()=>{n.removeEventListener("transitionend",u),clearTimeout(p),T=null;};}function X(n){let e=n.map(t=>{let{tx:i,ty:s}=I(t),r=Number(getComputedStyle(t).opacity)||1;return t.getAnimations().forEach(o=>o.cancel()),{left:t.offsetLeft+i,top:t.offsetTop+s,width:t.offsetWidth,height:t.offsetHeight,opacity:r}});n.forEach((t,i)=>{let s=e[i];t.setAttribute(d,""),t.style.position="absolute",t.style.pointerEvents="none",t.style.left=`${s.left}px`,t.style.top=`${s.top}px`,t.style.width=`${s.width}px`,t.style.height=`${s.height}px`,t.style.opacity=String(s.opacity);});}function G(n,e,t,i){let s=new Map;e.forEach(r=>{let o=r.getAttribute(f);t.has(o)&&!r.hasAttribute(d)&&(s.set(o,r),r.remove());}),Array.from(n.childNodes).forEach(r=>{r.nodeType===Node.TEXT_NODE&&r.remove();}),i.forEach(r=>{let o=s.get(r.id);if(o)o.textContent=r.string,n.appendChild(o);else {let a=document.createElement("span");a.setAttribute(M,""),a.setAttribute(f,r.id),a.textContent=r.string,n.appendChild(a);}});}var Z=`
2
+ [${x}] {
3
3
  display: inline-flex;
4
4
  position: relative;
5
5
  will-change: width, height;
@@ -7,19 +7,17 @@ import c from'react';var A="span",$={debug:false,locale:"en",duration:400,scale:
7
7
  white-space: nowrap;
8
8
  }
9
9
 
10
- [torph-item] {
10
+ [${M}] {
11
11
  display: inline-block;
12
12
  will-change: opacity, transform;
13
13
  transform: none;
14
14
  opacity: 1;
15
15
  }
16
16
 
17
- [torph-root][torph-debug] {
18
- outline:2px solid magenta;
19
- [torph-item] {
20
- outline:2px solid cyan;
17
+ [${x}][${b}] {
18
+ outline: 2px solid magenta;
19
+ [${M}] {
20
+ outline: 2px solid cyan;
21
21
  outline-offset: -4px;
22
22
  }
23
- }
24
- `,document.head.appendChild(t),h.styleEl=t;}removeStyles(){h.styleEl&&(h.styleEl.remove(),h.styleEl=void 0);}blocks(t){return Array.from(t).reduce((o,i)=>i.segment===" "?[...o,{id:`space-${i.index}`,string:"\xA0"}]:o.find(l=>l.string===i.segment)?[...o,{id:`${i.segment}-${i.index}`,string:i.segment}]:[...o,{id:i.segment,string:i.segment}],[])}blocksFallback(t,s){let o=s?t.split(" "):t.split(""),i=[];return s?o.forEach((a,l)=>{l>0&&i.push({id:`space-${l}`,string:"\xA0"}),i.find(d=>d.string===a)?i.push({id:`${a}-${l}`,string:a}):i.push({id:a,string:a});}):o.forEach((a,l)=>{i.find(d=>d.string===a)?i.push({id:`${a}-${l}`,string:a}):i.push({id:a,string:a});}),i}log(...t){this.options.debug&&console.log("[TextMorph]",...t);}};function w(h){if(typeof h=="string")return h;if(typeof h=="number")return String(h);if(!h||typeof h=="boolean")return "";if(Array.isArray(h))return h.map(w).join("");throw c.isValidElement(h)?new Error("TextMorph only accepts text content. Found a React element \u2014 use strings, numbers, or expressions instead."):new Error(`TextMorph received an unsupported child of type "${typeof h}".`)}var R=({children:h,className:t,style:s,as:o=A,...i})=>{let{ref:a,update:l}=S(i),p=w(h),d=c.useRef({__html:p});return c.useEffect(()=>{l(p);},[p,l]),c.createElement(o,{ref:a,className:t,style:s,dangerouslySetInnerHTML:d.current})};function S(h){let t=c.useRef(null),s=c.useRef(null);c.useEffect(()=>(t.current&&(s.current=new b({element:t.current,...h})),()=>{s.current?.destroy();}),[]);let o=c.useCallback(i=>{s.current?.update(i);},[]);return {ref:t,update:o}}export{R as TextMorph,S as useTextMorph};//# sourceMappingURL=index.mjs.map
25
- //# sourceMappingURL=index.mjs.map
23
+ }`,h=null,O=0;function U(){O++,!h&&(h=document.createElement("style"),h.dataset.torph="true",h.textContent=Z,document.head.appendChild(h));}function W(){O--,!(O>0||!h)&&(h.remove(),h=null);}function j(){if(typeof window>"u")return {prefersReducedMotion:false,destroy:()=>{}};let n=window.matchMedia("(prefers-reduced-motion: reduce)"),e={prefersReducedMotion:n.matches,destroy:i};function t(s){e.prefersReducedMotion=s.matches;}function i(){n.removeEventListener("change",t);}return n.addEventListener("change",t),e}var E=class n{instance=null;lastText="";configKey="";attach(e,t){this.instance?.destroy(),this.instance=new A({element:e,...t}),this.configKey=n.serializeConfig(t),this.lastText&&this.instance.update(this.lastText);}update(e){this.lastText=e,this.instance?.update(e);}needsRecreate(e){return n.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 B="span",tt={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:t,...i}={...tt,...e},s,r;if(typeof t=="object"){let o=D(t);s=o.easing,r=o.duration;}else s=t,r=i.duration;this.options={...i,ease:s,duration:r},this.element=e.element,this.options.respectReducedMotion&&(this.reducedMotion=j()),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(b,"")),this.data="",this.isDisabled()||U();}destroy(){this.reducedMotion?.destroy(),this.element.getAnimations().forEach(e=>e.cancel()),this.element.removeAttribute(x),this.element.removeAttribute(b),W();}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,t){let i=t.offsetWidth,s=t.offsetHeight,r=_(e,this.options.locale);this.prevMeasures=w(this.element);let o=Array.from(t.children),a=new Set(r.map(c=>c.id)),u=o.filter(c=>!a.has(c.getAttribute(f))&&!c.hasAttribute(d)),p=new Set(u),l=o.map(c=>c.getAttribute(f)),m=C(o,p,l,a);if(X(u),G(t,o,a,r),this.currentMeasures=w(this.element),this.updateStyles(r),u.forEach(c=>{if(this.isInitialRender){c.remove();return}let y=m.get(c),{dx:S,dy:R}=y?v(this.currentMeasures,this.prevMeasures,y):{dx:0,dy:0};k(c,{dx:S,dy:R,duration:this.options.duration,ease:this.options.ease,scale:this.options.scale});}),this.isInitialRender){this.isInitialRender=false,t.style.width="auto",t.style.height="auto";return}F(t,i,s,this.options.duration,this.options.onAnimationComplete);}updateStyles(e){if(this.isInitialRender)return;let t=Array.from(this.element.children),i=e.map(r=>r.id),s=new Set(i.filter(r=>this.prevMeasures[r]));t.forEach((r,o)=>{if(r.hasAttribute(d))return;let a=r.getAttribute(f)||`child-${o}`,u=!this.prevMeasures[a],p=u?L(e.findIndex(c=>c.id===a),i,s):a,{dx:l,dy:m}=p?v(this.prevMeasures,this.currentMeasures,p):{dx:0,dy:0};P(r,{deltaX:l,deltaY:m,isNew:u,duration:this.options.duration,ease:this.options.ease});});}};function K(n){if(typeof n=="string")return n;if(typeof n=="number")return String(n);if(!n||typeof n=="boolean")return "";if(Array.isArray(n))return n.map(K).join("");throw g.isValidElement(n)?new Error("TextMorph only accepts text content. Found a React element \u2014 use strings, numbers, or expressions instead."):new Error(`TextMorph received an unsupported child of type "${typeof n}".`)}var et=({children:n,className:e,style:t,as:i=B,...s})=>{let{ref:r,update:o}=q(s),a=K(n),u=g.useRef({__html:a});return g.useEffect(()=>{o(a);},[a,o]),g.createElement(i,{ref:r,className:e,style:t,dangerouslySetInnerHTML:u.current})};function q(n){let e=g.useRef(null),t=g.useRef(new E),i=E.serializeConfig(n);g.useEffect(()=>(e.current&&t.current.attach(e.current,n),()=>{t.current.destroy();}),[i]);let s=g.useCallback(r=>{t.current.update(r);},[]);return {ref:e,update:s}}export{et as TextMorph,q as useTextMorph};
@@ -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 @@
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 e=require('svelte/internal/client'),textMorph=require('../lib/text-morph'),controller=require('../lib/text-morph/controller');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 c(s,t){e__namespace.push(t,true);let p=e__namespace.prop(t,"locale",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.locale),u=e__namespace.prop(t,"duration",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.duration),m=e__namespace.prop(t,"ease",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.ease),f=e__namespace.prop(t,"scale",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.scale),g=e__namespace.prop(t,"debug",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.debug),$=e__namespace.prop(t,"disabled",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.disabled),b=e__namespace.prop(t,"respectReducedMotion",19,()=>textMorph.DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),A=e__namespace.prop(t,"onAnimationStart",19,()=>{}),M=e__namespace.prop(t,"onAnimationComplete",19,()=>{}),_=e__namespace.prop(t,"as",3,textMorph.DEFAULT_AS),h=e__namespace.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),a=e__namespace.state(void 0),n=new controller.MorphController,i=e__namespace.derived(()=>({locale:p(),duration:u(),ease:m(),debug:g(),scale:f(),disabled:$(),respectReducedMotion:b(),onAnimationStart:A(),onAnimationComplete:M()})),v=e__namespace.derived(()=>controller.MorphController.serializeConfig(e__namespace.get(i)));e__namespace.user_effect(()=>{if(e__namespace.get(v),e__namespace.get(a))return n.attach(e__namespace.get(a),e__namespace.get(i)),()=>{n.destroy();}}),e__namespace.user_effect(()=>{n.update(t.text);});var r=e__namespace.comment(),x=e__namespace.first_child(r);e__namespace.element(x,_,false,(d,C)=>{e__namespace.bind_this(d,T=>e__namespace.set(a,T,true),()=>e__namespace.get(a)),e__namespace.attribute_effect(d,()=>({...h}));}),e__namespace.append(s,r),e__namespace.pop();}exports.TextMorph=c;
@@ -1,2 +1 @@
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 e from'svelte/internal/client';import {DEFAULT_TEXT_MORPH_OPTIONS,DEFAULT_AS}from'../lib/text-morph';import {MorphController}from'../lib/text-morph/controller';function c(s,t){e.push(t,true);let p=e.prop(t,"locale",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.locale),u=e.prop(t,"duration",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.duration),m=e.prop(t,"ease",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.ease),f=e.prop(t,"scale",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.scale),g=e.prop(t,"debug",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.debug),$=e.prop(t,"disabled",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.disabled),b=e.prop(t,"respectReducedMotion",19,()=>DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),A=e.prop(t,"onAnimationStart",19,()=>{}),M=e.prop(t,"onAnimationComplete",19,()=>{}),_=e.prop(t,"as",3,DEFAULT_AS),h=e.rest_props(t,["$$slots","$$events","$$legacy","text","locale","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as"]),a=e.state(void 0),n=new MorphController,i=e.derived(()=>({locale:p(),duration:u(),ease:m(),debug:g(),scale:f(),disabled:$(),respectReducedMotion:b(),onAnimationStart:A(),onAnimationComplete:M()})),v=e.derived(()=>MorphController.serializeConfig(e.get(i)));e.user_effect(()=>{if(e.get(v),e.get(a))return n.attach(e.get(a),e.get(i)),()=>{n.destroy();}}),e.user_effect(()=>{n.update(t.text);});var r=e.comment(),x=e.first_child(r);e.element(x,_,false,(d,C)=>{e.bind_this(d,T=>e.set(a,T,true),()=>e.get(a)),e.attribute_effect(d,()=>({...h}));}),e.append(s,r),e.pop();}export{c as TextMorph};
@@ -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?: Record<string, string | number>;
25
+ as?: string;
26
+ }
27
+
28
+ export type { TextMorphProps };
package/dist/vue/index.js CHANGED
@@ -1,2 +1 @@
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 TextMorphLC6RO5C5_vue=require('../TextMorph-LC6RO5C5.vue');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var TextMorphLC6RO5C5_vue__default=/*#__PURE__*/_interopDefault(TextMorphLC6RO5C5_vue);Object.defineProperty(exports,"TextMorph",{enumerable:true,get:function(){return TextMorphLC6RO5C5_vue__default.default}});
@@ -1,2 +1 @@
1
- export{default as TextMorph}from'../TextMorph-SZJHGWGL.vue';//# sourceMappingURL=index.mjs.map
2
- //# sourceMappingURL=index.mjs.map
1
+ export{default as TextMorph}from'../TextMorph-LC6RO5C5.vue';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "torph",
3
- "version": "0.0.6",
3
+ "version": "0.0.7",
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/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/lib/text-morph/index.ts"],"names":["DEFAULT_AS","DEFAULT_TEXT_MORPH_OPTIONS","TextMorph","_TextMorph","options","anim","event","value","element","oldWidth","oldHeight","byWord","blocks","iterator","oldChildren","newIds","b","exiting","child","exitingSet","exitingAnchorId","i","anchor","j","siblingId","exitPositions","a","pos","id","node","block","span","anchorId","dx","dy","anchorPrev","anchorCurr","animation","newWidth","newHeight","children","measures","index","key","persistentIds","prev","current","cx","cy","deltaX","deltaY","isNew","blockIndex","duration","delay","style","acc","string","x","segments","segment","args"],"mappings":"aAIO,IAAMA,CAAAA,CAAa,MAAA,CACbC,CAAAA,CAA6B,CACxC,KAAA,CAAO,KAAA,CACP,MAAA,CAAQ,IAAA,CACR,QAAA,CAAU,GAAA,CACV,KAAA,CAAO,IAAA,CACP,KAAM,gCAAA,CACN,QAAA,CAAU,KAAA,CACV,oBAAA,CAAsB,IACxB,CAAA,CAUaC,CAAAA,CAAN,MAAMC,CAAU,CACb,OAAA,CACA,OAAA,CAA6C,EAAC,CAE9C,IAAA,CAEA,eAAA,CAA4B,GAC5B,YAAA,CAAyB,EAAC,CAC1B,eAAA,CAAkB,IAAA,CAClB,oBAAA,CAAuB,KAAA,CACvB,UAAA,CAER,OAAO,OAAA,CAEP,WAAA,CAAYC,CAAAA,CAA2B,CACrC,IAAA,CAAK,OAAA,CAAU,CACb,GAAGH,CAAAA,CACH,GAAGG,CACL,CAAA,CAEA,IAAA,CAAK,OAAA,CAAUA,CAAAA,CAAQ,OAAA,CAGnB,OAAO,MAAA,CAAW,GAAA,EAAe,IAAA,CAAK,OAAA,CAAQ,oBAAA,GAChD,IAAA,CAAK,UAAA,CAAa,MAAA,CAAO,WAAW,kCAAkC,CAAA,CACtE,IAAA,CAAK,oBAAA,CAAuB,IAAA,CAAK,UAAA,CAAW,OAAA,CAC5C,IAAA,CAAK,UAAA,CAAW,gBAAA,CAAiB,QAAA,CAAU,IAAA,CAAK,sBAAsB,CAAA,CAAA,CAGnE,IAAA,CAAK,UAAA,KACR,IAAA,CAAK,OAAA,CAAQ,YAAA,CAAa,YAAA,CAAc,EAAE,CAAA,CAC1C,IAAA,CAAK,OAAA,CAAQ,MAAM,kBAAA,CAAqB,CAAA,EAAG,IAAA,CAAK,OAAA,CAAQ,QAAQ,CAAA,EAAA,CAAA,CAChE,IAAA,CAAK,OAAA,CAAQ,MAAM,wBA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EAAE,CAAA,CACrDK,EAAkB,EAAC,CAEzB,OAAID,CAAAA,CACFgD,CAAAA,CAAS,QAAQ,CAACC,CAAAA,CAASlB,CAAAA,GAAU,CAC/BA,CAAAA,CAAQ,CAAA,EACV9B,EAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,MAAA,EAAS8B,CAAK,CAAA,CAAA,CAAI,OAAQ,MAAS,CAAC,CAAA,CAEvC9B,CAAAA,CAAO,IAAA,CAAM8C,CAAAA,EAAMA,EAAE,MAAA,GAAWE,CAAO,EAEtDhD,CAAAA,CAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,EAAGgD,CAAO,CAAA,CAAA,EAAIlB,CAAK,CAAA,CAAA,CAAI,OAAQkB,CAAQ,CAAC,CAAA,CAE1DhD,CAAAA,CAAO,IAAA,CAAK,CAAE,GAAIgD,CAAAA,CAAS,MAAA,CAAQA,CAAQ,CAAC,EAEhD,CAAC,EAEDD,CAAAA,CAAS,OAAA,CAAQ,CAACC,CAAAA,CAASlB,CAAAA,GAAU,CAClB9B,CAAAA,CAAO,IAAA,CAAM8C,CAAAA,EAAMA,CAAAA,CAAE,MAAA,GAAWE,CAAO,EAEtDhD,CAAAA,CAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,EAAGgD,CAAO,IAAIlB,CAAK,CAAA,CAAA,CAAI,MAAA,CAAQkB,CAAQ,CAAC,CAAA,CAE1DhD,EAAO,IAAA,CAAK,CAAE,GAAIgD,CAAAA,CAAS,MAAA,CAAQA,CAAQ,CAAC,EAEhD,CAAC,CAAA,CAGIhD,CACT,CAEQ,OAAOiD,CAAAA,CAAa,CACtB,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,OAAA,CAAQ,IAAI,aAAA,CAAe,GAAGA,CAAI,EAC5D,CACF","file":"index.js","sourcesContent":["import type { TextMorphOptions } from \"./types\";\n\nexport type { TextMorphOptions } from \"./types\";\n\nexport const DEFAULT_AS = \"span\";\nexport const DEFAULT_TEXT_MORPH_OPTIONS = {\n debug: false,\n locale: \"en\",\n duration: 400,\n scale: true,\n ease: \"cubic-bezier(0.19, 1, 0.22, 1)\",\n disabled: false,\n respectReducedMotion: true,\n} as const satisfies Omit<TextMorphOptions, \"element\">;\n\ntype Block = {\n id: string;\n string: string;\n};\ntype Measures = {\n [key: string]: { x: number; y: number };\n};\n\nexport class TextMorph {\n private element: HTMLElement;\n private options: Omit<TextMorphOptions, \"element\"> = {};\n\n private data: HTMLElement | string;\n\n private currentMeasures: Measures = {};\n private prevMeasures: Measures = {};\n private isInitialRender = true;\n private prefersReducedMotion = false;\n private mediaQuery?: MediaQueryList;\n\n static styleEl: HTMLStyleElement;\n\n constructor(options: TextMorphOptions) {\n this.options = {\n ...DEFAULT_TEXT_MORPH_OPTIONS,\n ...options,\n };\n\n this.element = options.element;\n\n // reduced motion detection\n if (typeof window !== \"undefined\" && this.options.respectReducedMotion) {\n this.mediaQuery = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n this.prefersReducedMotion = this.mediaQuery.matches;\n this.mediaQuery.addEventListener(\"change\", this.handleMediaQueryChange);\n }\n\n if (!this.isDisabled()) {\n this.element.setAttribute(\"torph-root\", \"\");\n this.element.style.transitionDuration = `${this.options.duration}ms`;\n this.element.style.transitionTimingFunction = this.options.ease!;\n\n if (options.debug) this.element.setAttribute(\"torph-debug\", \"\");\n }\n\n this.data = \"\";\n if (!this.isDisabled()) {\n this.addStyles();\n }\n }\n\n destroy() {\n if (this.mediaQuery) {\n this.mediaQuery.removeEventListener(\n \"change\",\n this.handleMediaQueryChange,\n );\n }\n this.element.getAnimations().forEach((anim) => anim.cancel());\n this.element.removeAttribute(\"torph-root\");\n this.element.removeAttribute(\"torph-debug\");\n this.removeStyles();\n }\n\n private handleMediaQueryChange = (event: MediaQueryListEvent) => {\n this.prefersReducedMotion = event.matches;\n };\n\n private isDisabled(): boolean {\n return Boolean(\n this.options.disabled ||\n (this.options.respectReducedMotion && this.prefersReducedMotion),\n );\n }\n\n update(value: HTMLElement | string) {\n if (value === this.data) return;\n this.data = value;\n\n if (this.isDisabled()) {\n if (typeof value === \"string\") {\n this.element.textContent = value;\n }\n return;\n }\n\n if (this.data instanceof HTMLElement) {\n // TODO: handle HTMLElement case\n throw new Error(\"HTMLElement not yet supported\");\n } else {\n if (this.options.onAnimationStart && !this.isInitialRender) {\n this.options.onAnimationStart();\n }\n this.createTextGroup(this.data, this.element);\n }\n }\n\n private createTextGroup(value: string, element: HTMLElement) {\n const oldWidth = element.offsetWidth;\n const oldHeight = element.offsetHeight;\n\n const byWord = value.includes(\" \");\n let blocks: Block[];\n\n if (typeof Intl.Segmenter !== \"undefined\") {\n const segmenter = new Intl.Segmenter(this.options.locale, {\n granularity: byWord ? \"word\" : \"grapheme\",\n });\n const iterator = segmenter.segment(value)[Symbol.iterator]();\n blocks = this.blocks(iterator);\n } else {\n // Fallback for browsers without Intl.Segmenter\n blocks = this.blocksFallback(value, byWord);\n }\n\n this.prevMeasures = this.measure();\n const oldChildren = Array.from(element.children) as HTMLElement[];\n const newIds = new Set(blocks.map((b) => b.id));\n\n const exiting = oldChildren.filter(\n (child) =>\n !newIds.has(child.getAttribute(\"torph-id\") as string) &&\n !child.hasAttribute(\"torph-exiting\"),\n );\n\n // For each exiting char, find the nearest persistent neighbor in old order\n // so we can make it follow that neighbor's FLIP movement\n const exitingSet = new Set(exiting);\n const exitingAnchorId = new Map<HTMLElement, string | null>();\n for (let i = 0; i < oldChildren.length; i++) {\n const child = oldChildren[i]!;\n if (!exitingSet.has(child)) continue;\n\n // Look forward for nearest persistent char\n let anchor: string | null = null;\n for (let j = i + 1; j < oldChildren.length; j++) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n // If none forward, look backward\n if (!anchor) {\n for (let j = i - 1; j >= 0; j--) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n }\n exitingAnchorId.set(child, anchor);\n }\n\n // Two-pass: read all positions before modifying any element,\n // since setting position:absolute removes from flow and shifts siblings\n const exitPositions = exiting.map((child) => {\n child.getAnimations().forEach((a) => a.cancel());\n return {\n left: child.offsetLeft,\n top: child.offsetTop,\n width: child.offsetWidth,\n height: child.offsetHeight,\n };\n });\n exiting.forEach((child, i) => {\n const pos = exitPositions[i]!;\n child.setAttribute(\"torph-exiting\", \"\");\n child.style.position = \"absolute\";\n child.style.pointerEvents = \"none\";\n child.style.left = `${pos.left}px`;\n child.style.top = `${pos.top}px`;\n child.style.width = `${pos.width}px`;\n child.style.height = `${pos.height}px`;\n });\n\n oldChildren.forEach((child) => {\n const id = child.getAttribute(\"torph-id\") as string;\n if (newIds.has(id)) child.remove();\n });\n\n // Disabled-mode updates set plain text via textContent; remove that text node\n // before appending torph items so old content is not duplicated.\n Array.from(element.childNodes).forEach((node) => {\n if (node.nodeType === Node.TEXT_NODE) {\n node.remove();\n }\n });\n\n blocks.forEach((block) => {\n const span = document.createElement(\"span\");\n span.setAttribute(\"torph-item\", \"\");\n span.setAttribute(\"torph-id\", block.id);\n span.textContent = block.string;\n element.appendChild(span);\n });\n\n this.currentMeasures = this.measure();\n this.updateStyles(blocks);\n\n exiting.forEach((child) => {\n if (this.isInitialRender) {\n child.remove();\n return;\n }\n\n // Find the anchor neighbor's FLIP delta so we move in sync with it\n const anchorId = exitingAnchorId.get(child);\n let dx = 0;\n let dy = 0;\n\n if (\n anchorId &&\n this.prevMeasures[anchorId] &&\n this.currentMeasures[anchorId]\n ) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n dx = anchorCurr.x - anchorPrev.x;\n dy = anchorCurr.y - anchorPrev.y;\n }\n\n child.animate(\n {\n transform: this.options.scale\n ? `translate(${dx}px, ${dy}px) scale(0.95)`\n : `translate(${dx}px, ${dy}px)`,\n offset: 1,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const animation: Animation = child.animate(\n {\n opacity: 0,\n offset: 1,\n },\n {\n duration: this.options.duration! * 0.25,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n animation.onfinish = () => child.remove();\n });\n\n if (this.isInitialRender) {\n this.isInitialRender = false;\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n return;\n }\n\n if (oldWidth === 0 || oldHeight === 0) return;\n\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n void element.offsetWidth; // force reflow\n\n const newWidth = element.offsetWidth;\n const newHeight = element.offsetHeight;\n\n element.style.width = `${oldWidth}px`;\n element.style.height = `${oldHeight}px`;\n void element.offsetWidth; // force reflow\n\n element.style.width = `${newWidth}px`;\n element.style.height = `${newHeight}px`;\n\n // TODO: move to `transitionend` event listener\n setTimeout(() => {\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n if (this.options.onAnimationComplete) {\n this.options.onAnimationComplete();\n }\n }, this.options.duration);\n }\n\n private measure() {\n const children = Array.from(this.element.children) as HTMLElement[];\n const measures: Measures = {};\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n measures[key] = {\n x: child.offsetLeft,\n y: child.offsetTop,\n };\n });\n\n return measures;\n }\n\n private updateStyles(blocks: Block[]) {\n if (this.isInitialRender) return;\n\n const children = Array.from(this.element.children) as HTMLElement[];\n\n const persistentIds = new Set(\n blocks.map((b) => b.id).filter((id) => this.prevMeasures[id]),\n );\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n const prev = this.prevMeasures[key];\n const current = this.currentMeasures[key];\n\n const cx = current?.x || 0;\n const cy = current?.y || 0;\n\n let deltaX = prev ? prev.x - cx : 0;\n let deltaY = prev ? prev.y - cy : 0;\n const isNew = !prev;\n\n // For new chars, use the nearest persistent neighbor's FLIP delta\n // so all new chars get the same consistent offset\n if (isNew) {\n const blockIndex = blocks.findIndex((b) => b.id === key);\n let anchorId: string | null = null;\n\n for (let j = blockIndex - 1; j >= 0; j--) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n if (!anchorId) {\n for (let j = blockIndex + 1; j < blocks.length; j++) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n }\n\n if (anchorId) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n deltaX = anchorPrev.x - anchorCurr.x;\n deltaY = anchorPrev.y - anchorCurr.y;\n }\n }\n\n child.getAnimations().forEach((a) => a.cancel());\n child.animate(\n {\n transform: `translate(${deltaX}px, ${deltaY}px) scale(${isNew ? 0.95 : 1})`,\n offset: 0,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const duration = isNew ? this.options.duration! * 0.25 : 0;\n const delay = isNew ? this.options.duration! * 0.25 : 0;\n child.animate(\n {\n opacity: isNew ? 0 : 1,\n offset: 0,\n },\n {\n duration: duration,\n delay: delay,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n });\n }\n\n private addStyles() {\n if (TextMorph.styleEl) return;\n\n const style = document.createElement(\"style\");\n style.dataset.torph = \"true\";\n style.innerHTML = `\n[torph-root] {\n display: inline-flex;\n position: relative;\n will-change: width, height;\n transition-property: width, height;\n white-space: nowrap;\n}\n\n[torph-item] {\n display: inline-block;\n will-change: opacity, transform;\n transform: none;\n opacity: 1;\n}\n\n[torph-root][torph-debug] {\n outline:2px solid magenta;\n [torph-item] {\n outline:2px solid cyan;\n outline-offset: -4px;\n }\n}\n `;\n document.head.appendChild(style);\n TextMorph.styleEl = style;\n }\n\n private removeStyles() {\n if (TextMorph.styleEl) {\n TextMorph.styleEl.remove();\n TextMorph.styleEl = undefined!;\n }\n }\n\n // utils\n\n private blocks(iterator: Intl.SegmentIterator<Intl.SegmentData>) {\n const uniqueStrings: Block[] = Array.from(iterator).reduce(\n (acc, string) => {\n if (string.segment === \" \") {\n return [...acc, { id: `space-${string.index}`, string: \"\\u00A0\" }];\n }\n\n const existingString = acc.find((x) => x.string === string.segment);\n if (existingString) {\n return [\n ...acc,\n { id: `${string.segment}-${string.index}`, string: string.segment },\n ];\n }\n\n return [\n ...acc,\n {\n id: string.segment,\n string: string.segment,\n },\n ];\n },\n [] as Block[],\n );\n\n return uniqueStrings;\n }\n\n private blocksFallback(value: string, byWord: boolean): Block[] {\n const segments = byWord ? value.split(\" \") : value.split(\"\");\n const blocks: Block[] = [];\n\n if (byWord) {\n segments.forEach((segment, index) => {\n if (index > 0) {\n blocks.push({ id: `space-${index}`, string: \"\\u00A0\" });\n }\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n } else {\n segments.forEach((segment, index) => {\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n }\n\n return blocks;\n }\n\n private log(...args: any[]) {\n if (this.options.debug) console.log(\"[TextMorph]\", ...args);\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/lib/text-morph/index.ts"],"names":["DEFAULT_AS","DEFAULT_TEXT_MORPH_OPTIONS","TextMorph","_TextMorph","options","anim","event","value","element","oldWidth","oldHeight","byWord","blocks","iterator","oldChildren","newIds","b","exiting","child","exitingSet","exitingAnchorId","i","anchor","j","siblingId","exitPositions","a","pos","id","node","block","span","anchorId","dx","dy","anchorPrev","anchorCurr","animation","newWidth","newHeight","children","measures","index","key","persistentIds","prev","current","cx","cy","deltaX","deltaY","isNew","blockIndex","duration","delay","style","acc","string","x","segments","segment","args"],"mappings":"AAIO,IAAMA,CAA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CAAQ,CAAA,EACV9B,EAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,MAAA,EAAS8B,CAAK,CAAA,CAAA,CAAI,OAAQ,MAAS,CAAC,CAAA,CAEvC9B,CAAAA,CAAO,IAAA,CAAM8C,CAAAA,EAAMA,EAAE,MAAA,GAAWE,CAAO,EAEtDhD,CAAAA,CAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,EAAGgD,CAAO,CAAA,CAAA,EAAIlB,CAAK,CAAA,CAAA,CAAI,OAAQkB,CAAQ,CAAC,CAAA,CAE1DhD,CAAAA,CAAO,IAAA,CAAK,CAAE,GAAIgD,CAAAA,CAAS,MAAA,CAAQA,CAAQ,CAAC,EAEhD,CAAC,EAEDD,CAAAA,CAAS,OAAA,CAAQ,CAACC,CAAAA,CAASlB,CAAAA,GAAU,CAClB9B,CAAAA,CAAO,IAAA,CAAM8C,CAAAA,EAAMA,CAAAA,CAAE,MAAA,GAAWE,CAAO,EAEtDhD,CAAAA,CAAO,IAAA,CAAK,CAAE,EAAA,CAAI,CAAA,EAAGgD,CAAO,IAAIlB,CAAK,CAAA,CAAA,CAAI,MAAA,CAAQkB,CAAQ,CAAC,CAAA,CAE1DhD,EAAO,IAAA,CAAK,CAAE,GAAIgD,CAAAA,CAAS,MAAA,CAAQA,CAAQ,CAAC,EAEhD,CAAC,CAAA,CAGIhD,CACT,CAEQ,OAAOiD,CAAAA,CAAa,CACtB,IAAA,CAAK,OAAA,CAAQ,KAAA,EAAO,OAAA,CAAQ,IAAI,aAAA,CAAe,GAAGA,CAAI,EAC5D,CACF","file":"index.mjs","sourcesContent":["import type { TextMorphOptions } from \"./types\";\n\nexport type { TextMorphOptions } from \"./types\";\n\nexport const DEFAULT_AS = \"span\";\nexport const DEFAULT_TEXT_MORPH_OPTIONS = {\n debug: false,\n locale: \"en\",\n duration: 400,\n scale: true,\n ease: \"cubic-bezier(0.19, 1, 0.22, 1)\",\n disabled: false,\n respectReducedMotion: true,\n} as const satisfies Omit<TextMorphOptions, \"element\">;\n\ntype Block = {\n id: string;\n string: string;\n};\ntype Measures = {\n [key: string]: { x: number; y: number };\n};\n\nexport class TextMorph {\n private element: HTMLElement;\n private options: Omit<TextMorphOptions, \"element\"> = {};\n\n private data: HTMLElement | string;\n\n private currentMeasures: Measures = {};\n private prevMeasures: Measures = {};\n private isInitialRender = true;\n private prefersReducedMotion = false;\n private mediaQuery?: MediaQueryList;\n\n static styleEl: HTMLStyleElement;\n\n constructor(options: TextMorphOptions) {\n this.options = {\n ...DEFAULT_TEXT_MORPH_OPTIONS,\n ...options,\n };\n\n this.element = options.element;\n\n // reduced motion detection\n if (typeof window !== \"undefined\" && this.options.respectReducedMotion) {\n this.mediaQuery = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n this.prefersReducedMotion = this.mediaQuery.matches;\n this.mediaQuery.addEventListener(\"change\", this.handleMediaQueryChange);\n }\n\n if (!this.isDisabled()) {\n this.element.setAttribute(\"torph-root\", \"\");\n this.element.style.transitionDuration = `${this.options.duration}ms`;\n this.element.style.transitionTimingFunction = this.options.ease!;\n\n if (options.debug) this.element.setAttribute(\"torph-debug\", \"\");\n }\n\n this.data = \"\";\n if (!this.isDisabled()) {\n this.addStyles();\n }\n }\n\n destroy() {\n if (this.mediaQuery) {\n this.mediaQuery.removeEventListener(\n \"change\",\n this.handleMediaQueryChange,\n );\n }\n this.element.getAnimations().forEach((anim) => anim.cancel());\n this.element.removeAttribute(\"torph-root\");\n this.element.removeAttribute(\"torph-debug\");\n this.removeStyles();\n }\n\n private handleMediaQueryChange = (event: MediaQueryListEvent) => {\n this.prefersReducedMotion = event.matches;\n };\n\n private isDisabled(): boolean {\n return Boolean(\n this.options.disabled ||\n (this.options.respectReducedMotion && this.prefersReducedMotion),\n );\n }\n\n update(value: HTMLElement | string) {\n if (value === this.data) return;\n this.data = value;\n\n if (this.isDisabled()) {\n if (typeof value === \"string\") {\n this.element.textContent = value;\n }\n return;\n }\n\n if (this.data instanceof HTMLElement) {\n // TODO: handle HTMLElement case\n throw new Error(\"HTMLElement not yet supported\");\n } else {\n if (this.options.onAnimationStart && !this.isInitialRender) {\n this.options.onAnimationStart();\n }\n this.createTextGroup(this.data, this.element);\n }\n }\n\n private createTextGroup(value: string, element: HTMLElement) {\n const oldWidth = element.offsetWidth;\n const oldHeight = element.offsetHeight;\n\n const byWord = value.includes(\" \");\n let blocks: Block[];\n\n if (typeof Intl.Segmenter !== \"undefined\") {\n const segmenter = new Intl.Segmenter(this.options.locale, {\n granularity: byWord ? \"word\" : \"grapheme\",\n });\n const iterator = segmenter.segment(value)[Symbol.iterator]();\n blocks = this.blocks(iterator);\n } else {\n // Fallback for browsers without Intl.Segmenter\n blocks = this.blocksFallback(value, byWord);\n }\n\n this.prevMeasures = this.measure();\n const oldChildren = Array.from(element.children) as HTMLElement[];\n const newIds = new Set(blocks.map((b) => b.id));\n\n const exiting = oldChildren.filter(\n (child) =>\n !newIds.has(child.getAttribute(\"torph-id\") as string) &&\n !child.hasAttribute(\"torph-exiting\"),\n );\n\n // For each exiting char, find the nearest persistent neighbor in old order\n // so we can make it follow that neighbor's FLIP movement\n const exitingSet = new Set(exiting);\n const exitingAnchorId = new Map<HTMLElement, string | null>();\n for (let i = 0; i < oldChildren.length; i++) {\n const child = oldChildren[i]!;\n if (!exitingSet.has(child)) continue;\n\n // Look forward for nearest persistent char\n let anchor: string | null = null;\n for (let j = i + 1; j < oldChildren.length; j++) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n // If none forward, look backward\n if (!anchor) {\n for (let j = i - 1; j >= 0; j--) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n }\n exitingAnchorId.set(child, anchor);\n }\n\n // Two-pass: read all positions before modifying any element,\n // since setting position:absolute removes from flow and shifts siblings\n const exitPositions = exiting.map((child) => {\n child.getAnimations().forEach((a) => a.cancel());\n return {\n left: child.offsetLeft,\n top: child.offsetTop,\n width: child.offsetWidth,\n height: child.offsetHeight,\n };\n });\n exiting.forEach((child, i) => {\n const pos = exitPositions[i]!;\n child.setAttribute(\"torph-exiting\", \"\");\n child.style.position = \"absolute\";\n child.style.pointerEvents = \"none\";\n child.style.left = `${pos.left}px`;\n child.style.top = `${pos.top}px`;\n child.style.width = `${pos.width}px`;\n child.style.height = `${pos.height}px`;\n });\n\n oldChildren.forEach((child) => {\n const id = child.getAttribute(\"torph-id\") as string;\n if (newIds.has(id)) child.remove();\n });\n\n // Disabled-mode updates set plain text via textContent; remove that text node\n // before appending torph items so old content is not duplicated.\n Array.from(element.childNodes).forEach((node) => {\n if (node.nodeType === Node.TEXT_NODE) {\n node.remove();\n }\n });\n\n blocks.forEach((block) => {\n const span = document.createElement(\"span\");\n span.setAttribute(\"torph-item\", \"\");\n span.setAttribute(\"torph-id\", block.id);\n span.textContent = block.string;\n element.appendChild(span);\n });\n\n this.currentMeasures = this.measure();\n this.updateStyles(blocks);\n\n exiting.forEach((child) => {\n if (this.isInitialRender) {\n child.remove();\n return;\n }\n\n // Find the anchor neighbor's FLIP delta so we move in sync with it\n const anchorId = exitingAnchorId.get(child);\n let dx = 0;\n let dy = 0;\n\n if (\n anchorId &&\n this.prevMeasures[anchorId] &&\n this.currentMeasures[anchorId]\n ) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n dx = anchorCurr.x - anchorPrev.x;\n dy = anchorCurr.y - anchorPrev.y;\n }\n\n child.animate(\n {\n transform: this.options.scale\n ? `translate(${dx}px, ${dy}px) scale(0.95)`\n : `translate(${dx}px, ${dy}px)`,\n offset: 1,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const animation: Animation = child.animate(\n {\n opacity: 0,\n offset: 1,\n },\n {\n duration: this.options.duration! * 0.25,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n animation.onfinish = () => child.remove();\n });\n\n if (this.isInitialRender) {\n this.isInitialRender = false;\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n return;\n }\n\n if (oldWidth === 0 || oldHeight === 0) return;\n\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n void element.offsetWidth; // force reflow\n\n const newWidth = element.offsetWidth;\n const newHeight = element.offsetHeight;\n\n element.style.width = `${oldWidth}px`;\n element.style.height = `${oldHeight}px`;\n void element.offsetWidth; // force reflow\n\n element.style.width = `${newWidth}px`;\n element.style.height = `${newHeight}px`;\n\n // TODO: move to `transitionend` event listener\n setTimeout(() => {\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n if (this.options.onAnimationComplete) {\n this.options.onAnimationComplete();\n }\n }, this.options.duration);\n }\n\n private measure() {\n const children = Array.from(this.element.children) as HTMLElement[];\n const measures: Measures = {};\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n measures[key] = {\n x: child.offsetLeft,\n y: child.offsetTop,\n };\n });\n\n return measures;\n }\n\n private updateStyles(blocks: Block[]) {\n if (this.isInitialRender) return;\n\n const children = Array.from(this.element.children) as HTMLElement[];\n\n const persistentIds = new Set(\n blocks.map((b) => b.id).filter((id) => this.prevMeasures[id]),\n );\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n const prev = this.prevMeasures[key];\n const current = this.currentMeasures[key];\n\n const cx = current?.x || 0;\n const cy = current?.y || 0;\n\n let deltaX = prev ? prev.x - cx : 0;\n let deltaY = prev ? prev.y - cy : 0;\n const isNew = !prev;\n\n // For new chars, use the nearest persistent neighbor's FLIP delta\n // so all new chars get the same consistent offset\n if (isNew) {\n const blockIndex = blocks.findIndex((b) => b.id === key);\n let anchorId: string | null = null;\n\n for (let j = blockIndex - 1; j >= 0; j--) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n if (!anchorId) {\n for (let j = blockIndex + 1; j < blocks.length; j++) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n }\n\n if (anchorId) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n deltaX = anchorPrev.x - anchorCurr.x;\n deltaY = anchorPrev.y - anchorCurr.y;\n }\n }\n\n child.getAnimations().forEach((a) => a.cancel());\n child.animate(\n {\n transform: `translate(${deltaX}px, ${deltaY}px) scale(${isNew ? 0.95 : 1})`,\n offset: 0,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const duration = isNew ? this.options.duration! * 0.25 : 0;\n const delay = isNew ? this.options.duration! * 0.25 : 0;\n child.animate(\n {\n opacity: isNew ? 0 : 1,\n offset: 0,\n },\n {\n duration: duration,\n delay: delay,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n });\n }\n\n private addStyles() {\n if (TextMorph.styleEl) return;\n\n const style = document.createElement(\"style\");\n style.dataset.torph = \"true\";\n style.innerHTML = `\n[torph-root] {\n display: inline-flex;\n position: relative;\n will-change: width, height;\n transition-property: width, height;\n white-space: nowrap;\n}\n\n[torph-item] {\n display: inline-block;\n will-change: opacity, transform;\n transform: none;\n opacity: 1;\n}\n\n[torph-root][torph-debug] {\n outline:2px solid magenta;\n [torph-item] {\n outline:2px solid cyan;\n outline-offset: -4px;\n }\n}\n `;\n document.head.appendChild(style);\n TextMorph.styleEl = style;\n }\n\n private removeStyles() {\n if (TextMorph.styleEl) {\n TextMorph.styleEl.remove();\n TextMorph.styleEl = undefined!;\n }\n }\n\n // utils\n\n private blocks(iterator: Intl.SegmentIterator<Intl.SegmentData>) {\n const uniqueStrings: Block[] = Array.from(iterator).reduce(\n (acc, string) => {\n if (string.segment === \" \") {\n return [...acc, { id: `space-${string.index}`, string: \"\\u00A0\" }];\n }\n\n const existingString = acc.find((x) => x.string === string.segment);\n if (existingString) {\n return [\n ...acc,\n { id: `${string.segment}-${string.index}`, string: string.segment },\n ];\n }\n\n return [\n ...acc,\n {\n id: string.segment,\n string: string.segment,\n },\n ];\n },\n [] as Block[],\n );\n\n return uniqueStrings;\n }\n\n private blocksFallback(value: string, byWord: boolean): Block[] {\n const segments = byWord ? value.split(\" \") : value.split(\"\");\n const blocks: Block[] = [];\n\n if (byWord) {\n segments.forEach((segment, index) => {\n if (index > 0) {\n blocks.push({ id: `space-${index}`, string: \"\\u00A0\" });\n }\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n } else {\n segments.forEach((segment, index) => {\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n }\n\n return blocks;\n }\n\n private log(...args: any[]) {\n if (this.options.debug) console.log(\"[TextMorph]\", ...args);\n }\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lib/text-morph/index.ts","../../src/react/TextMorph.tsx"],"names":["DEFAULT_AS","DEFAULT_TEXT_MORPH_OPTIONS","TextMorph","_TextMorph","options","anim","event","value","element","oldWidth","oldHeight","byWord","blocks","iterator","oldChildren","newIds","b","exiting","child","exitingSet","exitingAnchorId","i","anchor","j","siblingId","exitPositions","a","pos","id","node","block","span","anchorId","dx","dy","anchorPrev","anchorCurr","animation","newWidth","newHeight","children","measures","index","key","persistentIds","prev","current","cx","cy","deltaX","deltaY","isNew","blockIndex","duration","delay","style","acc","string","x","segments","segment","args","childrenToString","React","className","as","props","ref","update","useTextMorph","text","initialHTML","morphRef"],"mappings":"oJAIO,IAAMA,CAAAA,CAAa,MAAA,CACbC,CAAAA,CAA6B,CACxC,KAAA,CAAO,KAAA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A,CAAOF,CAAI,EAC/B,CAAA,CAAG,EAAE,CAAA,CAEL,OAAO,CAAE,GAAA,CAAAH,CAAAA,CAAK,MAAA,CAAAC,CAAO,CACvB","file":"index.js","sourcesContent":["import type { TextMorphOptions } from \"./types\";\n\nexport type { TextMorphOptions } from \"./types\";\n\nexport const DEFAULT_AS = \"span\";\nexport const DEFAULT_TEXT_MORPH_OPTIONS = {\n debug: false,\n locale: \"en\",\n duration: 400,\n scale: true,\n ease: \"cubic-bezier(0.19, 1, 0.22, 1)\",\n disabled: false,\n respectReducedMotion: true,\n} as const satisfies Omit<TextMorphOptions, \"element\">;\n\ntype Block = {\n id: string;\n string: string;\n};\ntype Measures = {\n [key: string]: { x: number; y: number };\n};\n\nexport class TextMorph {\n private element: HTMLElement;\n private options: Omit<TextMorphOptions, \"element\"> = {};\n\n private data: HTMLElement | string;\n\n private currentMeasures: Measures = {};\n private prevMeasures: Measures = {};\n private isInitialRender = true;\n private prefersReducedMotion = false;\n private mediaQuery?: MediaQueryList;\n\n static styleEl: HTMLStyleElement;\n\n constructor(options: TextMorphOptions) {\n this.options = {\n ...DEFAULT_TEXT_MORPH_OPTIONS,\n ...options,\n };\n\n this.element = options.element;\n\n // reduced motion detection\n if (typeof window !== \"undefined\" && this.options.respectReducedMotion) {\n this.mediaQuery = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n this.prefersReducedMotion = this.mediaQuery.matches;\n this.mediaQuery.addEventListener(\"change\", this.handleMediaQueryChange);\n }\n\n if (!this.isDisabled()) {\n this.element.setAttribute(\"torph-root\", \"\");\n this.element.style.transitionDuration = `${this.options.duration}ms`;\n this.element.style.transitionTimingFunction = this.options.ease!;\n\n if (options.debug) this.element.setAttribute(\"torph-debug\", \"\");\n }\n\n this.data = \"\";\n if (!this.isDisabled()) {\n this.addStyles();\n }\n }\n\n destroy() {\n if (this.mediaQuery) {\n this.mediaQuery.removeEventListener(\n \"change\",\n this.handleMediaQueryChange,\n );\n }\n this.element.getAnimations().forEach((anim) => anim.cancel());\n this.element.removeAttribute(\"torph-root\");\n this.element.removeAttribute(\"torph-debug\");\n this.removeStyles();\n }\n\n private handleMediaQueryChange = (event: MediaQueryListEvent) => {\n this.prefersReducedMotion = event.matches;\n };\n\n private isDisabled(): boolean {\n return Boolean(\n this.options.disabled ||\n (this.options.respectReducedMotion && this.prefersReducedMotion),\n );\n }\n\n update(value: HTMLElement | string) {\n if (value === this.data) return;\n this.data = value;\n\n if (this.isDisabled()) {\n if (typeof value === \"string\") {\n this.element.textContent = value;\n }\n return;\n }\n\n if (this.data instanceof HTMLElement) {\n // TODO: handle HTMLElement case\n throw new Error(\"HTMLElement not yet supported\");\n } else {\n if (this.options.onAnimationStart && !this.isInitialRender) {\n this.options.onAnimationStart();\n }\n this.createTextGroup(this.data, this.element);\n }\n }\n\n private createTextGroup(value: string, element: HTMLElement) {\n const oldWidth = element.offsetWidth;\n const oldHeight = element.offsetHeight;\n\n const byWord = value.includes(\" \");\n let blocks: Block[];\n\n if (typeof Intl.Segmenter !== \"undefined\") {\n const segmenter = new Intl.Segmenter(this.options.locale, {\n granularity: byWord ? \"word\" : \"grapheme\",\n });\n const iterator = segmenter.segment(value)[Symbol.iterator]();\n blocks = this.blocks(iterator);\n } else {\n // Fallback for browsers without Intl.Segmenter\n blocks = this.blocksFallback(value, byWord);\n }\n\n this.prevMeasures = this.measure();\n const oldChildren = Array.from(element.children) as HTMLElement[];\n const newIds = new Set(blocks.map((b) => b.id));\n\n const exiting = oldChildren.filter(\n (child) =>\n !newIds.has(child.getAttribute(\"torph-id\") as string) &&\n !child.hasAttribute(\"torph-exiting\"),\n );\n\n // For each exiting char, find the nearest persistent neighbor in old order\n // so we can make it follow that neighbor's FLIP movement\n const exitingSet = new Set(exiting);\n const exitingAnchorId = new Map<HTMLElement, string | null>();\n for (let i = 0; i < oldChildren.length; i++) {\n const child = oldChildren[i]!;\n if (!exitingSet.has(child)) continue;\n\n // Look forward for nearest persistent char\n let anchor: string | null = null;\n for (let j = i + 1; j < oldChildren.length; j++) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n // If none forward, look backward\n if (!anchor) {\n for (let j = i - 1; j >= 0; j--) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n }\n exitingAnchorId.set(child, anchor);\n }\n\n // Two-pass: read all positions before modifying any element,\n // since setting position:absolute removes from flow and shifts siblings\n const exitPositions = exiting.map((child) => {\n child.getAnimations().forEach((a) => a.cancel());\n return {\n left: child.offsetLeft,\n top: child.offsetTop,\n width: child.offsetWidth,\n height: child.offsetHeight,\n };\n });\n exiting.forEach((child, i) => {\n const pos = exitPositions[i]!;\n child.setAttribute(\"torph-exiting\", \"\");\n child.style.position = \"absolute\";\n child.style.pointerEvents = \"none\";\n child.style.left = `${pos.left}px`;\n child.style.top = `${pos.top}px`;\n child.style.width = `${pos.width}px`;\n child.style.height = `${pos.height}px`;\n });\n\n oldChildren.forEach((child) => {\n const id = child.getAttribute(\"torph-id\") as string;\n if (newIds.has(id)) child.remove();\n });\n\n // Disabled-mode updates set plain text via textContent; remove that text node\n // before appending torph items so old content is not duplicated.\n Array.from(element.childNodes).forEach((node) => {\n if (node.nodeType === Node.TEXT_NODE) {\n node.remove();\n }\n });\n\n blocks.forEach((block) => {\n const span = document.createElement(\"span\");\n span.setAttribute(\"torph-item\", \"\");\n span.setAttribute(\"torph-id\", block.id);\n span.textContent = block.string;\n element.appendChild(span);\n });\n\n this.currentMeasures = this.measure();\n this.updateStyles(blocks);\n\n exiting.forEach((child) => {\n if (this.isInitialRender) {\n child.remove();\n return;\n }\n\n // Find the anchor neighbor's FLIP delta so we move in sync with it\n const anchorId = exitingAnchorId.get(child);\n let dx = 0;\n let dy = 0;\n\n if (\n anchorId &&\n this.prevMeasures[anchorId] &&\n this.currentMeasures[anchorId]\n ) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n dx = anchorCurr.x - anchorPrev.x;\n dy = anchorCurr.y - anchorPrev.y;\n }\n\n child.animate(\n {\n transform: this.options.scale\n ? `translate(${dx}px, ${dy}px) scale(0.95)`\n : `translate(${dx}px, ${dy}px)`,\n offset: 1,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const animation: Animation = child.animate(\n {\n opacity: 0,\n offset: 1,\n },\n {\n duration: this.options.duration! * 0.25,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n animation.onfinish = () => child.remove();\n });\n\n if (this.isInitialRender) {\n this.isInitialRender = false;\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n return;\n }\n\n if (oldWidth === 0 || oldHeight === 0) return;\n\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n void element.offsetWidth; // force reflow\n\n const newWidth = element.offsetWidth;\n const newHeight = element.offsetHeight;\n\n element.style.width = `${oldWidth}px`;\n element.style.height = `${oldHeight}px`;\n void element.offsetWidth; // force reflow\n\n element.style.width = `${newWidth}px`;\n element.style.height = `${newHeight}px`;\n\n // TODO: move to `transitionend` event listener\n setTimeout(() => {\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n if (this.options.onAnimationComplete) {\n this.options.onAnimationComplete();\n }\n }, this.options.duration);\n }\n\n private measure() {\n const children = Array.from(this.element.children) as HTMLElement[];\n const measures: Measures = {};\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n measures[key] = {\n x: child.offsetLeft,\n y: child.offsetTop,\n };\n });\n\n return measures;\n }\n\n private updateStyles(blocks: Block[]) {\n if (this.isInitialRender) return;\n\n const children = Array.from(this.element.children) as HTMLElement[];\n\n const persistentIds = new Set(\n blocks.map((b) => b.id).filter((id) => this.prevMeasures[id]),\n );\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n const prev = this.prevMeasures[key];\n const current = this.currentMeasures[key];\n\n const cx = current?.x || 0;\n const cy = current?.y || 0;\n\n let deltaX = prev ? prev.x - cx : 0;\n let deltaY = prev ? prev.y - cy : 0;\n const isNew = !prev;\n\n // For new chars, use the nearest persistent neighbor's FLIP delta\n // so all new chars get the same consistent offset\n if (isNew) {\n const blockIndex = blocks.findIndex((b) => b.id === key);\n let anchorId: string | null = null;\n\n for (let j = blockIndex - 1; j >= 0; j--) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n if (!anchorId) {\n for (let j = blockIndex + 1; j < blocks.length; j++) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n }\n\n if (anchorId) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n deltaX = anchorPrev.x - anchorCurr.x;\n deltaY = anchorPrev.y - anchorCurr.y;\n }\n }\n\n child.getAnimations().forEach((a) => a.cancel());\n child.animate(\n {\n transform: `translate(${deltaX}px, ${deltaY}px) scale(${isNew ? 0.95 : 1})`,\n offset: 0,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const duration = isNew ? this.options.duration! * 0.25 : 0;\n const delay = isNew ? this.options.duration! * 0.25 : 0;\n child.animate(\n {\n opacity: isNew ? 0 : 1,\n offset: 0,\n },\n {\n duration: duration,\n delay: delay,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n });\n }\n\n private addStyles() {\n if (TextMorph.styleEl) return;\n\n const style = document.createElement(\"style\");\n style.dataset.torph = \"true\";\n style.innerHTML = `\n[torph-root] {\n display: inline-flex;\n position: relative;\n will-change: width, height;\n transition-property: width, height;\n white-space: nowrap;\n}\n\n[torph-item] {\n display: inline-block;\n will-change: opacity, transform;\n transform: none;\n opacity: 1;\n}\n\n[torph-root][torph-debug] {\n outline:2px solid magenta;\n [torph-item] {\n outline:2px solid cyan;\n outline-offset: -4px;\n }\n}\n `;\n document.head.appendChild(style);\n TextMorph.styleEl = style;\n }\n\n private removeStyles() {\n if (TextMorph.styleEl) {\n TextMorph.styleEl.remove();\n TextMorph.styleEl = undefined!;\n }\n }\n\n // utils\n\n private blocks(iterator: Intl.SegmentIterator<Intl.SegmentData>) {\n const uniqueStrings: Block[] = Array.from(iterator).reduce(\n (acc, string) => {\n if (string.segment === \" \") {\n return [...acc, { id: `space-${string.index}`, string: \"\\u00A0\" }];\n }\n\n const existingString = acc.find((x) => x.string === string.segment);\n if (existingString) {\n return [\n ...acc,\n { id: `${string.segment}-${string.index}`, string: string.segment },\n ];\n }\n\n return [\n ...acc,\n {\n id: string.segment,\n string: string.segment,\n },\n ];\n },\n [] as Block[],\n );\n\n return uniqueStrings;\n }\n\n private blocksFallback(value: string, byWord: boolean): Block[] {\n const segments = byWord ? value.split(\" \") : value.split(\"\");\n const blocks: Block[] = [];\n\n if (byWord) {\n segments.forEach((segment, index) => {\n if (index > 0) {\n blocks.push({ id: `space-${index}`, string: \"\\u00A0\" });\n }\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n } else {\n segments.forEach((segment, index) => {\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n }\n\n return blocks;\n }\n\n private log(...args: any[]) {\n if (this.options.debug) console.log(\"[TextMorph]\", ...args);\n }\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { DEFAULT_AS, TextMorph as Morph } from \"../lib/text-morph\";\nimport type { TextMorphOptions } from \"../lib/text-morph/types\";\n\nexport type TextMorphProps = Omit<TextMorphOptions, \"element\"> & {\n children: React.ReactNode;\n className?: string;\n style?: React.CSSProperties;\n as?: React.ElementType;\n};\n\nfunction childrenToString(node: React.ReactNode): string {\n if (typeof node === \"string\") return node;\n if (typeof node === \"number\") return String(node);\n if (!node || typeof node === \"boolean\") return \"\";\n if (Array.isArray(node)) return node.map(childrenToString).join(\"\");\n if (React.isValidElement(node)) {\n throw new Error(\n \"TextMorph only accepts text content. Found a React element — use strings, numbers, or expressions instead.\"\n );\n }\n throw new Error(\n `TextMorph received an unsupported child of type \"${typeof node}\".`\n );\n}\n\nexport const TextMorph = ({\n children,\n className,\n style,\n as = DEFAULT_AS,\n ...props\n}: TextMorphProps) => {\n const { ref, update } = useTextMorph(props);\n const text = childrenToString(children);\n const initialHTML = React.useRef({ __html: text });\n\n React.useEffect(() => {\n update(text);\n }, [text, update]);\n\n const Component = as;\n\n return (\n <Component\n ref={ref}\n className={className}\n style={style}\n dangerouslySetInnerHTML={initialHTML.current}\n />\n );\n};\n\nexport function useTextMorph(props: Omit<TextMorphOptions, \"element\">) {\n const ref = React.useRef<HTMLDivElement | null>(null);\n const morphRef = React.useRef<Morph | null>(null);\n\n React.useEffect(() => {\n if (ref.current) {\n morphRef.current = new Morph({ element: ref.current, ...props });\n }\n\n return () => {\n morphRef.current?.destroy();\n };\n }, []);\n\n const update = React.useCallback((text: string) => {\n morphRef.current?.update(text);\n }, []);\n\n return { ref, update };\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/lib/text-morph/index.ts","../../src/react/TextMorph.tsx"],"names":["DEFAULT_AS","DEFAULT_TEXT_MORPH_OPTIONS","TextMorph","_TextMorph","options","anim","event","value","element","oldWidth","oldHeight","byWord","blocks","iterator","oldChildren","newIds","b","exiting","child","exitingSet","exitingAnchorId","i","anchor","j","siblingId","exitPositions","a","pos","id","node","block","span","anchorId","dx","dy","anchorPrev","anchorCurr","animation","newWidth","newHeight","children","measures","index","key","persistentIds","prev","current","cx","cy","deltaX","deltaY","isNew","blockIndex","duration","delay","style","acc","string","x","segments","segment","args","childrenToString","React","className","as","props","ref","update","useTextMorph","text","initialHTML","morphRef"],"mappings":"qBAIO,IAAMA,CAAAA,CAAa,MAAA,CACbC,CAAAA,CAA6B,CACxC,KAAA,CAAO,KAAA,CACP,MAAA,CAAQ,IAAA,CACR,QAAA,CAAU,IACV,KAAA,CAAO,IAAA,CACP,IAAA,CAAM,gCAAA,CACN,QAAA,CAAU,KAAA,CACV,oBAAA,CAAsB,IACxB,EAUaC,CAAAA,CAAN,MAAMC,CAAU,CACb,OAAA,CACA,OAAA,CAA6C,EAAC,CAE9C,KAEA,eAAA,CAA4B,EAAC,CAC7B,YAAA,CAAyB,EAAC,CAC1B,eAAA,CAAkB,IAAA,CAClB,oBAAA,CAAuB,KAAA,CACvB,UAAA,CAER,OAAO,OAAA,CAEP,WAAA,CAAYC,CAAAA,CAA2B,CACrC,KAAK,OAAA,CAAU,CACb,GAAGH,CAAAA,CACH,GAAGG,CACL,CAAA,CAEA,IAAA,CAAK,QAAUA,CAAAA,CAAQ,OAAA,CAGnB,OAAO,MAAA,CAAW,GAAA,EAAe,IAAA,CAAK,OAAA,CAAQ,oBAAA,GAChD,KAAK,UAAA,CAAa,MAAA,CAAO,UAAA,CAAW,kCAAkC,CAAA,CACtE,IAAA,CAAK,oBAAA,CAAuB,IAAA,CAAK,UAAA,CAAW,OAAA,CAC5C,IAAA,CAAK,UAAA,CAAW,gBAAA,CAAiB,QAAA,CAAU,IAAA,CAAK,sBAAsB,GAGnE,IAAA,CAAK,UAAA,EAAW,GACnB,IAAA,CAAK,OAAA,CAAQ,YAAA,CAAa,YAAA,CAAc,EAAE,EAC1C,IAAA,CAAK,OAAA,CAAQ,KAAA,CAAM,kBAAA,CAAqB,CAAA,EAAG,IAAA,CAAK,OAAA,CAAQ,QAAQ,KAChE,IAAA,CAAK,OAAA,CAAQ,KAAA,CAAM,wBAAA,CAA2B,IAAA,CAAK,OAAA,CAAQ,IAAA,CAEvDA,CAAAA,CAAQ,OAAO,IAAA,CAAK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ACpB,CAAA,CAAA,CACC,EAAE,CAAA,CAEL,IAAMJ,CAAAA,CAASL,CAAAA,CAAM,YAAaO,CAAAA,EAAiB,CACjDE,CAAAA,CAAS,OAAA,EAAS,MAAA,CAAOF,CAAI,EAC/B,CAAA,CAAG,EAAE,CAAA,CAEL,OAAO,CAAE,GAAA,CAAAH,CAAAA,CAAK,MAAA,CAAAC,CAAO,CACvB","file":"index.mjs","sourcesContent":["import type { TextMorphOptions } from \"./types\";\n\nexport type { TextMorphOptions } from \"./types\";\n\nexport const DEFAULT_AS = \"span\";\nexport const DEFAULT_TEXT_MORPH_OPTIONS = {\n debug: false,\n locale: \"en\",\n duration: 400,\n scale: true,\n ease: \"cubic-bezier(0.19, 1, 0.22, 1)\",\n disabled: false,\n respectReducedMotion: true,\n} as const satisfies Omit<TextMorphOptions, \"element\">;\n\ntype Block = {\n id: string;\n string: string;\n};\ntype Measures = {\n [key: string]: { x: number; y: number };\n};\n\nexport class TextMorph {\n private element: HTMLElement;\n private options: Omit<TextMorphOptions, \"element\"> = {};\n\n private data: HTMLElement | string;\n\n private currentMeasures: Measures = {};\n private prevMeasures: Measures = {};\n private isInitialRender = true;\n private prefersReducedMotion = false;\n private mediaQuery?: MediaQueryList;\n\n static styleEl: HTMLStyleElement;\n\n constructor(options: TextMorphOptions) {\n this.options = {\n ...DEFAULT_TEXT_MORPH_OPTIONS,\n ...options,\n };\n\n this.element = options.element;\n\n // reduced motion detection\n if (typeof window !== \"undefined\" && this.options.respectReducedMotion) {\n this.mediaQuery = window.matchMedia(\"(prefers-reduced-motion: reduce)\");\n this.prefersReducedMotion = this.mediaQuery.matches;\n this.mediaQuery.addEventListener(\"change\", this.handleMediaQueryChange);\n }\n\n if (!this.isDisabled()) {\n this.element.setAttribute(\"torph-root\", \"\");\n this.element.style.transitionDuration = `${this.options.duration}ms`;\n this.element.style.transitionTimingFunction = this.options.ease!;\n\n if (options.debug) this.element.setAttribute(\"torph-debug\", \"\");\n }\n\n this.data = \"\";\n if (!this.isDisabled()) {\n this.addStyles();\n }\n }\n\n destroy() {\n if (this.mediaQuery) {\n this.mediaQuery.removeEventListener(\n \"change\",\n this.handleMediaQueryChange,\n );\n }\n this.element.getAnimations().forEach((anim) => anim.cancel());\n this.element.removeAttribute(\"torph-root\");\n this.element.removeAttribute(\"torph-debug\");\n this.removeStyles();\n }\n\n private handleMediaQueryChange = (event: MediaQueryListEvent) => {\n this.prefersReducedMotion = event.matches;\n };\n\n private isDisabled(): boolean {\n return Boolean(\n this.options.disabled ||\n (this.options.respectReducedMotion && this.prefersReducedMotion),\n );\n }\n\n update(value: HTMLElement | string) {\n if (value === this.data) return;\n this.data = value;\n\n if (this.isDisabled()) {\n if (typeof value === \"string\") {\n this.element.textContent = value;\n }\n return;\n }\n\n if (this.data instanceof HTMLElement) {\n // TODO: handle HTMLElement case\n throw new Error(\"HTMLElement not yet supported\");\n } else {\n if (this.options.onAnimationStart && !this.isInitialRender) {\n this.options.onAnimationStart();\n }\n this.createTextGroup(this.data, this.element);\n }\n }\n\n private createTextGroup(value: string, element: HTMLElement) {\n const oldWidth = element.offsetWidth;\n const oldHeight = element.offsetHeight;\n\n const byWord = value.includes(\" \");\n let blocks: Block[];\n\n if (typeof Intl.Segmenter !== \"undefined\") {\n const segmenter = new Intl.Segmenter(this.options.locale, {\n granularity: byWord ? \"word\" : \"grapheme\",\n });\n const iterator = segmenter.segment(value)[Symbol.iterator]();\n blocks = this.blocks(iterator);\n } else {\n // Fallback for browsers without Intl.Segmenter\n blocks = this.blocksFallback(value, byWord);\n }\n\n this.prevMeasures = this.measure();\n const oldChildren = Array.from(element.children) as HTMLElement[];\n const newIds = new Set(blocks.map((b) => b.id));\n\n const exiting = oldChildren.filter(\n (child) =>\n !newIds.has(child.getAttribute(\"torph-id\") as string) &&\n !child.hasAttribute(\"torph-exiting\"),\n );\n\n // For each exiting char, find the nearest persistent neighbor in old order\n // so we can make it follow that neighbor's FLIP movement\n const exitingSet = new Set(exiting);\n const exitingAnchorId = new Map<HTMLElement, string | null>();\n for (let i = 0; i < oldChildren.length; i++) {\n const child = oldChildren[i]!;\n if (!exitingSet.has(child)) continue;\n\n // Look forward for nearest persistent char\n let anchor: string | null = null;\n for (let j = i + 1; j < oldChildren.length; j++) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n // If none forward, look backward\n if (!anchor) {\n for (let j = i - 1; j >= 0; j--) {\n const siblingId = oldChildren[j]!.getAttribute(\"torph-id\") as string;\n if (newIds.has(siblingId) && !exitingSet.has(oldChildren[j]!)) {\n anchor = siblingId;\n break;\n }\n }\n }\n exitingAnchorId.set(child, anchor);\n }\n\n // Two-pass: read all positions before modifying any element,\n // since setting position:absolute removes from flow and shifts siblings\n const exitPositions = exiting.map((child) => {\n child.getAnimations().forEach((a) => a.cancel());\n return {\n left: child.offsetLeft,\n top: child.offsetTop,\n width: child.offsetWidth,\n height: child.offsetHeight,\n };\n });\n exiting.forEach((child, i) => {\n const pos = exitPositions[i]!;\n child.setAttribute(\"torph-exiting\", \"\");\n child.style.position = \"absolute\";\n child.style.pointerEvents = \"none\";\n child.style.left = `${pos.left}px`;\n child.style.top = `${pos.top}px`;\n child.style.width = `${pos.width}px`;\n child.style.height = `${pos.height}px`;\n });\n\n oldChildren.forEach((child) => {\n const id = child.getAttribute(\"torph-id\") as string;\n if (newIds.has(id)) child.remove();\n });\n\n // Disabled-mode updates set plain text via textContent; remove that text node\n // before appending torph items so old content is not duplicated.\n Array.from(element.childNodes).forEach((node) => {\n if (node.nodeType === Node.TEXT_NODE) {\n node.remove();\n }\n });\n\n blocks.forEach((block) => {\n const span = document.createElement(\"span\");\n span.setAttribute(\"torph-item\", \"\");\n span.setAttribute(\"torph-id\", block.id);\n span.textContent = block.string;\n element.appendChild(span);\n });\n\n this.currentMeasures = this.measure();\n this.updateStyles(blocks);\n\n exiting.forEach((child) => {\n if (this.isInitialRender) {\n child.remove();\n return;\n }\n\n // Find the anchor neighbor's FLIP delta so we move in sync with it\n const anchorId = exitingAnchorId.get(child);\n let dx = 0;\n let dy = 0;\n\n if (\n anchorId &&\n this.prevMeasures[anchorId] &&\n this.currentMeasures[anchorId]\n ) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n dx = anchorCurr.x - anchorPrev.x;\n dy = anchorCurr.y - anchorPrev.y;\n }\n\n child.animate(\n {\n transform: this.options.scale\n ? `translate(${dx}px, ${dy}px) scale(0.95)`\n : `translate(${dx}px, ${dy}px)`,\n offset: 1,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const animation: Animation = child.animate(\n {\n opacity: 0,\n offset: 1,\n },\n {\n duration: this.options.duration! * 0.25,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n animation.onfinish = () => child.remove();\n });\n\n if (this.isInitialRender) {\n this.isInitialRender = false;\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n return;\n }\n\n if (oldWidth === 0 || oldHeight === 0) return;\n\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n void element.offsetWidth; // force reflow\n\n const newWidth = element.offsetWidth;\n const newHeight = element.offsetHeight;\n\n element.style.width = `${oldWidth}px`;\n element.style.height = `${oldHeight}px`;\n void element.offsetWidth; // force reflow\n\n element.style.width = `${newWidth}px`;\n element.style.height = `${newHeight}px`;\n\n // TODO: move to `transitionend` event listener\n setTimeout(() => {\n element.style.width = \"auto\";\n element.style.height = \"auto\";\n if (this.options.onAnimationComplete) {\n this.options.onAnimationComplete();\n }\n }, this.options.duration);\n }\n\n private measure() {\n const children = Array.from(this.element.children) as HTMLElement[];\n const measures: Measures = {};\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n measures[key] = {\n x: child.offsetLeft,\n y: child.offsetTop,\n };\n });\n\n return measures;\n }\n\n private updateStyles(blocks: Block[]) {\n if (this.isInitialRender) return;\n\n const children = Array.from(this.element.children) as HTMLElement[];\n\n const persistentIds = new Set(\n blocks.map((b) => b.id).filter((id) => this.prevMeasures[id]),\n );\n\n children.forEach((child, index) => {\n if (child.hasAttribute(\"torph-exiting\")) return;\n const key = child.getAttribute(\"torph-id\") || `child-${index}`;\n const prev = this.prevMeasures[key];\n const current = this.currentMeasures[key];\n\n const cx = current?.x || 0;\n const cy = current?.y || 0;\n\n let deltaX = prev ? prev.x - cx : 0;\n let deltaY = prev ? prev.y - cy : 0;\n const isNew = !prev;\n\n // For new chars, use the nearest persistent neighbor's FLIP delta\n // so all new chars get the same consistent offset\n if (isNew) {\n const blockIndex = blocks.findIndex((b) => b.id === key);\n let anchorId: string | null = null;\n\n for (let j = blockIndex - 1; j >= 0; j--) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n if (!anchorId) {\n for (let j = blockIndex + 1; j < blocks.length; j++) {\n if (persistentIds.has(blocks[j]!.id)) {\n anchorId = blocks[j]!.id;\n break;\n }\n }\n }\n\n if (anchorId) {\n const anchorPrev = this.prevMeasures[anchorId]!;\n const anchorCurr = this.currentMeasures[anchorId]!;\n deltaX = anchorPrev.x - anchorCurr.x;\n deltaY = anchorPrev.y - anchorCurr.y;\n }\n }\n\n child.getAnimations().forEach((a) => a.cancel());\n child.animate(\n {\n transform: `translate(${deltaX}px, ${deltaY}px) scale(${isNew ? 0.95 : 1})`,\n offset: 0,\n },\n {\n duration: this.options.duration,\n easing: this.options.ease,\n fill: \"both\",\n },\n );\n const duration = isNew ? this.options.duration! * 0.25 : 0;\n const delay = isNew ? this.options.duration! * 0.25 : 0;\n child.animate(\n {\n opacity: isNew ? 0 : 1,\n offset: 0,\n },\n {\n duration: duration,\n delay: delay,\n easing: \"linear\",\n fill: \"both\",\n },\n );\n });\n }\n\n private addStyles() {\n if (TextMorph.styleEl) return;\n\n const style = document.createElement(\"style\");\n style.dataset.torph = \"true\";\n style.innerHTML = `\n[torph-root] {\n display: inline-flex;\n position: relative;\n will-change: width, height;\n transition-property: width, height;\n white-space: nowrap;\n}\n\n[torph-item] {\n display: inline-block;\n will-change: opacity, transform;\n transform: none;\n opacity: 1;\n}\n\n[torph-root][torph-debug] {\n outline:2px solid magenta;\n [torph-item] {\n outline:2px solid cyan;\n outline-offset: -4px;\n }\n}\n `;\n document.head.appendChild(style);\n TextMorph.styleEl = style;\n }\n\n private removeStyles() {\n if (TextMorph.styleEl) {\n TextMorph.styleEl.remove();\n TextMorph.styleEl = undefined!;\n }\n }\n\n // utils\n\n private blocks(iterator: Intl.SegmentIterator<Intl.SegmentData>) {\n const uniqueStrings: Block[] = Array.from(iterator).reduce(\n (acc, string) => {\n if (string.segment === \" \") {\n return [...acc, { id: `space-${string.index}`, string: \"\\u00A0\" }];\n }\n\n const existingString = acc.find((x) => x.string === string.segment);\n if (existingString) {\n return [\n ...acc,\n { id: `${string.segment}-${string.index}`, string: string.segment },\n ];\n }\n\n return [\n ...acc,\n {\n id: string.segment,\n string: string.segment,\n },\n ];\n },\n [] as Block[],\n );\n\n return uniqueStrings;\n }\n\n private blocksFallback(value: string, byWord: boolean): Block[] {\n const segments = byWord ? value.split(\" \") : value.split(\"\");\n const blocks: Block[] = [];\n\n if (byWord) {\n segments.forEach((segment, index) => {\n if (index > 0) {\n blocks.push({ id: `space-${index}`, string: \"\\u00A0\" });\n }\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n } else {\n segments.forEach((segment, index) => {\n const existing = blocks.find((x) => x.string === segment);\n if (existing) {\n blocks.push({ id: `${segment}-${index}`, string: segment });\n } else {\n blocks.push({ id: segment, string: segment });\n }\n });\n }\n\n return blocks;\n }\n\n private log(...args: any[]) {\n if (this.options.debug) console.log(\"[TextMorph]\", ...args);\n }\n}\n","\"use client\";\n\nimport React from \"react\";\nimport { DEFAULT_AS, TextMorph as Morph } from \"../lib/text-morph\";\nimport type { TextMorphOptions } from \"../lib/text-morph/types\";\n\nexport type TextMorphProps = Omit<TextMorphOptions, \"element\"> & {\n children: React.ReactNode;\n className?: string;\n style?: React.CSSProperties;\n as?: React.ElementType;\n};\n\nfunction childrenToString(node: React.ReactNode): string {\n if (typeof node === \"string\") return node;\n if (typeof node === \"number\") return String(node);\n if (!node || typeof node === \"boolean\") return \"\";\n if (Array.isArray(node)) return node.map(childrenToString).join(\"\");\n if (React.isValidElement(node)) {\n throw new Error(\n \"TextMorph only accepts text content. Found a React element — use strings, numbers, or expressions instead.\"\n );\n }\n throw new Error(\n `TextMorph received an unsupported child of type \"${typeof node}\".`\n );\n}\n\nexport const TextMorph = ({\n children,\n className,\n style,\n as = DEFAULT_AS,\n ...props\n}: TextMorphProps) => {\n const { ref, update } = useTextMorph(props);\n const text = childrenToString(children);\n const initialHTML = React.useRef({ __html: text });\n\n React.useEffect(() => {\n update(text);\n }, [text, update]);\n\n const Component = as;\n\n return (\n <Component\n ref={ref}\n className={className}\n style={style}\n dangerouslySetInnerHTML={initialHTML.current}\n />\n );\n};\n\nexport function useTextMorph(props: Omit<TextMorphOptions, \"element\">) {\n const ref = React.useRef<HTMLDivElement | null>(null);\n const morphRef = React.useRef<Morph | null>(null);\n\n React.useEffect(() => {\n if (ref.current) {\n morphRef.current = new Morph({ element: ref.current, ...props });\n }\n\n return () => {\n morphRef.current?.destroy();\n };\n }, []);\n\n const update = React.useCallback((text: string) => {\n morphRef.current?.update(text);\n }, []);\n\n return { ref, update };\n}\n"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/svelte/TextMorph.svelte"],"names":["TextMorph","$$anchor","$$props","e","locale","DEFAULT_TEXT_MORPH_OPTIONS","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as","DEFAULT_AS","props","containerRef","morphInstance","onMount","Morph","fragment","node","$$element","$$value"],"mappings":"ugBAKe,S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AAQ,IAAI,EAE1C,CAAC,CAAA,CAED,IAAImB,CAAAA,CAAalB,YAAA,CAAA,OAAA,EAAQ,CACrBmB,CAAAA,CAASnB,YAAA,CAAA,WAAA,CAAYkB,CAAQ,CAAA,CAE/BlB,YAAA,CAAA,OAAA,CAAQmB,CAAAA,CAAMR,EAAI,KAAA,CAAO,CAACS,CAAAA,CAAWtB,CAAAA,GAAa,CACjDE,YAAA,CAAA,SAAA,CAAUoB,CAAAA,CAAYC,CAAAA,EAAcrB,YAAA,CAAA,GAAA,CAAIc,CAAAA,CAAcO,CAAAA,CAAS,IAAI,CAAA,CAAG,IAAQrB,YAAA,CAAA,GAAA,CAAIc,CAAY,CAAC,CAAA,CAC/Fd,YAAA,CAAA,gBAAA,CAAiBoB,CAAAA,CAAW,KAAO,CAAE,GAAGP,CAAM,CAAA,CAAE,EACnD,CAAC,CAAA,CAECb,YAAA,CAAA,MAAA,CAAOF,CAAAA,CAAUoB,CAAQ,CAAA,CACzBlB,mBACH","file":"index.js","sourcesContent":["import 'svelte/internal/disclose-version';\nimport * as $ from 'svelte/internal/client';\nimport { onMount } from \"svelte\";\nimport { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, TextMorph as Morph } from \"../lib/text-morph\";\n\nexport default function TextMorph($$anchor, $$props) {\n\t$.push($$props, true);\n\n\tlet locale = $.prop($$props, 'locale', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.locale),\n\t\tduration = $.prop($$props, 'duration', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.duration),\n\t\tease = $.prop($$props, 'ease', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.ease),\n\t\tscale = $.prop($$props, 'scale', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.scale),\n\t\tdebug = $.prop($$props, 'debug', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.debug),\n\t\tdisabled = $.prop($$props, 'disabled', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.disabled),\n\t\trespectReducedMotion = $.prop($$props, 'respectReducedMotion', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),\n\t\tonAnimationStart = $.prop($$props, 'onAnimationStart', 19, () => void 0),\n\t\tonAnimationComplete = $.prop($$props, 'onAnimationComplete', 19, () => void 0),\n\t\tas = $.prop($$props, 'as', 3, DEFAULT_AS),\n\t\tprops = $.rest_props($$props, [\n\t\t\t'$$slots',\n\t\t\t'$$events',\n\t\t\t'$$legacy',\n\t\t\t'text',\n\t\t\t'locale',\n\t\t\t'duration',\n\t\t\t'ease',\n\t\t\t'scale',\n\t\t\t'debug',\n\t\t\t'disabled',\n\t\t\t'respectReducedMotion',\n\t\t\t'onAnimationStart',\n\t\t\t'onAnimationComplete',\n\t\t\t'as'\n\t\t]);\n\n\tlet containerRef = $.state(void 0);\n\tlet morphInstance = $.state(null);\n\n\tonMount(() => {\n\t\tif ($.get(containerRef)) {\n\t\t\t$.set(\n\t\t\t\tmorphInstance,\n\t\t\t\tnew Morph({\n\t\t\t\t\telement: $.get(containerRef),\n\t\t\t\t\tlocale: locale(),\n\t\t\t\t\tduration: duration(),\n\t\t\t\t\tease: ease(),\n\t\t\t\t\tdebug: debug(),\n\t\t\t\t\tscale: scale(),\n\t\t\t\t\tdisabled: disabled(),\n\t\t\t\t\trespectReducedMotion: respectReducedMotion(),\n\t\t\t\t\tonAnimationStart: onAnimationStart(),\n\t\t\t\t\tonAnimationComplete: onAnimationComplete()\n\t\t\t\t}),\n\t\t\t\ttrue\n\t\t\t);\n\n\t\t\t$.get(morphInstance).update($$props.text);\n\t\t}\n\n\t\treturn () => {\n\t\t\t$.get(morphInstance)?.destroy();\n\t\t};\n\t});\n\n\t$.user_effect(() => {\n\t\tif ($.get(morphInstance)) {\n\t\t\t$.get(morphInstance).update($$props.text);\n\t\t}\n\t});\n\n\tvar fragment = $.comment();\n\tvar node = $.first_child(fragment);\n\n\t$.element(node, as, false, ($$element, $$anchor) => {\n\t\t$.bind_this($$element, ($$value) => $.set(containerRef, $$value, true), () => $.get(containerRef));\n\t\t$.attribute_effect($$element, () => ({ ...props }));\n\t});\n\n\t$.append($$anchor, fragment);\n\t$.pop();\n}"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/svelte/TextMorph.svelte"],"names":["TextMorph","$$anchor","$$props","locale","DEFAULT_TEXT_MORPH_OPTIONS","duration","ease","scale","debug","disabled","respectReducedMotion","onAnimationStart","onAnimationComplete","as","DEFAULT_AS","props","containerRef","morphInstance","onMount","Morph","fragment","node","$$element","$$value"],"mappings":"+LAKe,SAARA,CAAAA,CAA2BC,CAAAA,CAAUC,CAAAA,CAAS,CAClD,CAAA,CAAA,IAAA,CAAKA,CAAAA,CAAS,IAAI,CAAA,CAEpB,IAAIC,CAAAA,CAAW,CAAA,CAAA,IAAA,CAAKD,EAAS,QAAA,CAAU,EAAA,CAAI,IAAME,0BAAAA,CAA2B,MAAM,CAAA,CACjFC,CAAAA,CAAa,CAAA,CAAA,IAAA,CAAKH,CAAAA,CAAS,UAAA,CAAY,EAAA,CAAI,IAAME,0BAAAA,CAA2B,QAAQ,CAAA,CACpFE,CAAAA,CAAS,CAAA,CAAA,IAAA,CAAKJ,CAAAA,CAAS,MAAA,CAAQ,EAAA,CAAI,IAAME,0BAAAA,CAA2B,IAAI,CAAA,CACxEG,CAAAA,CAAU,CAAA,CAAA,IAAA,CAAKL,CAAAA,CAAS,OAAA,CAAS,EAAA,CAAI,IAAME,0BAAAA,CAA2B,KAAK,EAC3EI,CAAAA,CAAU,CAAA,CAAA,IAAA,CAAKN,CAAAA,CAAS,OAAA,CAAS,EAAA,CAAI,IAAME,0BAAAA,CAA2B,KAAK,CAAA,CAC3EK,CAAAA,CAAa,CAAA,CAAA,IAAA,CAAKP,CAAAA,CAAS,UAAA,CAAY,EAAA,CAAI,IAAME,0BAAAA,CAA2B,QAAQ,CAAA,CACpFM,CAAAA,CAAyB,CAAA,CAAA,IAAA,CAAKR,CAAAA,CAAS,sBAAA,CAAwB,EAAA,CAAI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a,CAAA,CAAE,MAAA,CAAOf,CAAAA,CAAQ,IAAI,CAAA,CAAA,CAGlC,IAAM,CACV,CAAA,CAAA,GAAA,CAAIe,CAAa,CAAA,EAAG,OAAA,GACvB,CAAA,CACA,CAAA,CAEC,CAAA,CAAA,WAAA,CAAY,IAAM,CACb,CAAA,CAAA,GAAA,CAAIA,CAAa,CAAA,EACpB,CAAA,CAAA,GAAA,CAAIA,CAAa,CAAA,CAAE,MAAA,CAAOf,CAAAA,CAAQ,IAAI,EAE1C,CAAC,CAAA,CAED,IAAIkB,CAAAA,CAAa,CAAA,CAAA,OAAA,EAAQ,CACrBC,CAAAA,CAAS,CAAA,CAAA,WAAA,CAAYD,CAAQ,CAAA,CAE/B,CAAA,CAAA,OAAA,CAAQC,CAAAA,CAAMR,EAAI,KAAA,CAAO,CAACS,CAAAA,CAAWrB,CAAAA,GAAa,CACjD,CAAA,CAAA,SAAA,CAAUqB,CAAAA,CAAYC,CAAAA,EAAc,CAAA,CAAA,GAAA,CAAIP,CAAAA,CAAcO,CAAAA,CAAS,IAAI,CAAA,CAAG,IAAQ,CAAA,CAAA,GAAA,CAAIP,CAAY,CAAC,CAAA,CAC/F,CAAA,CAAA,gBAAA,CAAiBM,CAAAA,CAAW,KAAO,CAAE,GAAGP,CAAM,CAAA,CAAE,EACnD,CAAC,CAAA,CAEC,CAAA,CAAA,MAAA,CAAOd,CAAAA,CAAUmB,CAAQ,CAAA,CACzB,QACH","file":"index.mjs","sourcesContent":["import 'svelte/internal/disclose-version';\nimport * as $ from 'svelte/internal/client';\nimport { onMount } from \"svelte\";\nimport { DEFAULT_AS, DEFAULT_TEXT_MORPH_OPTIONS, TextMorph as Morph } from \"../lib/text-morph\";\n\nexport default function TextMorph($$anchor, $$props) {\n\t$.push($$props, true);\n\n\tlet locale = $.prop($$props, 'locale', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.locale),\n\t\tduration = $.prop($$props, 'duration', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.duration),\n\t\tease = $.prop($$props, 'ease', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.ease),\n\t\tscale = $.prop($$props, 'scale', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.scale),\n\t\tdebug = $.prop($$props, 'debug', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.debug),\n\t\tdisabled = $.prop($$props, 'disabled', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.disabled),\n\t\trespectReducedMotion = $.prop($$props, 'respectReducedMotion', 19, () => DEFAULT_TEXT_MORPH_OPTIONS.respectReducedMotion),\n\t\tonAnimationStart = $.prop($$props, 'onAnimationStart', 19, () => void 0),\n\t\tonAnimationComplete = $.prop($$props, 'onAnimationComplete', 19, () => void 0),\n\t\tas = $.prop($$props, 'as', 3, DEFAULT_AS),\n\t\tprops = $.rest_props($$props, [\n\t\t\t'$$slots',\n\t\t\t'$$events',\n\t\t\t'$$legacy',\n\t\t\t'text',\n\t\t\t'locale',\n\t\t\t'duration',\n\t\t\t'ease',\n\t\t\t'scale',\n\t\t\t'debug',\n\t\t\t'disabled',\n\t\t\t'respectReducedMotion',\n\t\t\t'onAnimationStart',\n\t\t\t'onAnimationComplete',\n\t\t\t'as'\n\t\t]);\n\n\tlet containerRef = $.state(void 0);\n\tlet morphInstance = $.state(null);\n\n\tonMount(() => {\n\t\tif ($.get(containerRef)) {\n\t\t\t$.set(\n\t\t\t\tmorphInstance,\n\t\t\t\tnew Morph({\n\t\t\t\t\telement: $.get(containerRef),\n\t\t\t\t\tlocale: locale(),\n\t\t\t\t\tduration: duration(),\n\t\t\t\t\tease: ease(),\n\t\t\t\t\tdebug: debug(),\n\t\t\t\t\tscale: scale(),\n\t\t\t\t\tdisabled: disabled(),\n\t\t\t\t\trespectReducedMotion: respectReducedMotion(),\n\t\t\t\t\tonAnimationStart: onAnimationStart(),\n\t\t\t\t\tonAnimationComplete: onAnimationComplete()\n\t\t\t\t}),\n\t\t\t\ttrue\n\t\t\t);\n\n\t\t\t$.get(morphInstance).update($$props.text);\n\t\t}\n\n\t\treturn () => {\n\t\t\t$.get(morphInstance)?.destroy();\n\t\t};\n\t});\n\n\t$.user_effect(() => {\n\t\tif ($.get(morphInstance)) {\n\t\t\t$.get(morphInstance).update($$props.text);\n\t\t}\n\t});\n\n\tvar fragment = $.comment();\n\tvar node = $.first_child(fragment);\n\n\t$.element(node, as, false, ($$element, $$anchor) => {\n\t\t$.bind_this($$element, ($$value) => $.set(containerRef, $$value, true), () => $.get(containerRef));\n\t\t$.attribute_effect($$element, () => ({ ...props }));\n\t});\n\n\t$.append($$anchor, fragment);\n\t$.pop();\n}"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"index.mjs","sourcesContent":[]}