@liiift-studio/magnettype 1.1.4 → 1.1.6

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
@@ -86,6 +86,9 @@ import { MagnetBlock } from '@liiift-studio/magnettype'
86
86
  | `spreadRadius` | — | Pixel distance from the cursor within which each **character's** weight rises to `maxWeight`. When omitted, per-character splitting is skipped |
87
87
  | `proximityRadius` | — | Pixel distance from the element **edge** that gates the effect. Without `spreadRadius`, drives a whole-element weight ramp. With `spreadRadius`, acts as an outer gate — the spread only fires while the cursor is within this distance |
88
88
  | `fixedAxes` | `{}` | Additional axis values to hold constant in every `font-variation-settings` string (e.g. `{ opsz: 144 }`) |
89
+ | `stabilizeLayout` | `true` | Apply compensating letter-spacing to prevent text reflow as weight rises. Measures the element's width at rest and peak weight via an off-screen probe and applies proportional negative letter-spacing per character. Disable if you want natural bold spacing, or if your font expands glyphs very unevenly (compensation is a per-element average) |
90
+ | `cachePositions` | `true` | Cache character centre positions in page-relative coordinates, eliminating `getBoundingClientRect` calls on every `mousemove`. Rebuilt on resize and after fonts load. Disable if the element is inside a custom scroll container (`overflow: scroll` on a non-window element) |
91
+ | `rafThrottle` | `true` | Throttle proximity updates to one per animation frame (≈ 60 fps on most displays). Disable for lowest input latency on 120 Hz displays or very fast-moving effects |
89
92
  | `className` | — | Forwarded to the root element |
90
93
  | `style` | — | Merged with the root element's style; `fontVariationSettings` at `minWeight` is set as the base |
91
94
 
@@ -244,4 +247,4 @@ The package itself has zero runtime dependencies. Do not remove this entry.
244
247
 
245
248
  ---
246
249
 
247
- Current version: 1.1.3
250
+ Current version: 1.1.6
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=require("react"),G=require("react/jsx-runtime"),tt={i:3,l:3,1:3,I:3,r:2,n:1,m:1,0:2,O:2,o:1,b:1,d:1,p:1,q:1,c:1,e:1},D={word:"mt-word",char:"mt-char",probe:"mt-probe"},j={axes:{wght:[300,500]},radius:120,falloff:"quadratic",magnetMode:"attract",wdthBoost:6,scope:"document"};function W(e,r=[]){return e.nodeType===Node.TEXT_NODE?r.push(e):e.childNodes.forEach(o=>W(o,r)),r}function K(e,r,o){if(!e||e==="normal")return`"${r}" ${o}`;const y=new RegExp(`(["'])${r}\\1\\s+[\\d.eE+-]+`),a=`"${r}" ${o}`;return y.test(e)?e.replace(y,a):`${e}, ${a}`}function et(e,r){let o=e;for(const[y,a]of Object.entries(r))o=K(o,y,a);return o}function nt(e,r,o){if(r.opacity!==void 0){const[y,a]=r.opacity;e.style.opacity=String(y+(a-y)*o)}r.italic===!0&&(e.style.fontStyle=o>.5?"italic":"")}function U(e,r){r.opacity!==void 0&&(e.style.opacity=String(r.opacity[0])),r.italic===!0&&(e.style.fontStyle="")}function rt(e){const r=e.cloneNode(!0),o=r.querySelectorAll(`.${D.word}, .${D.char}`);return Array.from(o).reverse().forEach(a=>{const l=a.parentNode;if(l){for(;a.firstChild;)l.insertBefore(a.firstChild,a);l.removeChild(a)}}),r.innerHTML}function lt(e,r){e.innerHTML=r}function ot(e,r,o={}){if(typeof window>"u")return()=>{};if(window.matchMedia("(prefers-reduced-motion: reduce)").matches)return e.innerHTML=r,()=>{};const y=o.wdthBoost??j.wdthBoost,a=o.radius??j.radius,l=o.falloff??j.falloff,x=o.scope??j.scope,E=o.props,f=o.transitionMs??0,w=window.scrollY;e.innerHTML=r;const L=getComputedStyle(e).fontVariationSettings,Y=L.match(/"wdth"\s+([\d.eE+-]+)/),T=Y?parseFloat(Y[1]):100,F=W(e),p=[];for(const s of F){const t=s.textContent??"";if(!t||!t.split("").some(n=>n in tt))continue;const c=document.createDocumentFragment();for(const n of t){const M=tt[n];if(M===void 0){const m=c.lastChild;m&&m.nodeType===Node.TEXT_NODE?m.textContent+=n:c.appendChild(document.createTextNode(n))}else{const m=document.createElement("span");m.className=D.char,m.style.fontVariationSettings=K(L,"wdth",T),m.textContent=n,c.appendChild(m),p.push({span:m,riskLevel:M})}}s.parentNode.replaceChild(c,s)}if(requestAnimationFrame(()=>{typeof window<"u"&&Math.abs(window.scrollY-w)>2&&window.scrollTo({top:w,behavior:"instant"})}),p.length===0)return()=>{};E&&p.forEach(({span:s})=>U(s,E));let g=-9999,V=-9999,S=!1,u=0,b=!0,N=null;function h(){if(!b)return;if(!S){p.forEach(({span:t})=>{f>0&&(t.style.transition=`font-variation-settings ${f}ms ease`),t.style.fontVariationSettings=K(L,"wdth",T),E&&U(t,E)}),f>0&&(N!==null&&clearTimeout(N),N=setTimeout(()=>{p.forEach(({span:t})=>{t.style.transition=""}),N=null},f)),u=0;return}const s=p.map(({span:t})=>t.getBoundingClientRect());p.forEach(({span:t,riskLevel:i},c)=>{t.style.transition="";const n=s[c],M=n.left+n.width/2,m=n.top+n.height/2,B=Math.sqrt((g-M)**2+(V-m)**2),A=Math.max(0,1-B/a),q=l==="quadratic"?A*A:A,H=y*(i/3)*q;t.style.fontVariationSettings=K(L,"wdth",T+H),E&&nt(t,E,q)}),u=requestAnimationFrame(h)}function X(s){g=s.clientX,V=s.clientY,S||(S=!0),u===0&&(u=requestAnimationFrame(h))}function R(){S=!1,u===0&&(u=requestAnimationFrame(h))}function k(s){s.touches.length!==0&&(g=s.touches[0].clientX,V=s.touches[0].clientY,S||(S=!0),u===0&&(u=requestAnimationFrame(h)))}function $(){S=!1,u===0&&(u=requestAnimationFrame(h))}const v=x==="document"?document:e;return v.addEventListener("mousemove",X),v.addEventListener("mouseleave",R),v.addEventListener("touchmove",k,{passive:!0}),v.addEventListener("touchend",$),()=>{b=!1,cancelAnimationFrame(u),N!==null&&clearTimeout(N),v.removeEventListener("mousemove",X),v.removeEventListener("mouseleave",R),v.removeEventListener("touchmove",k),v.removeEventListener("touchend",$),e.innerHTML=r}}function st(e,r,o={}){if(typeof window>"u")return()=>{};if(window.matchMedia("(prefers-reduced-motion: reduce)").matches)return e.innerHTML=r,()=>{};const y=o.axes??j.axes,a=o.radius??j.radius,l=o.falloff??j.falloff,x=o.magnetMode??j.magnetMode,E=o.scope??j.scope,f=o.props,w=o.transitionMs??0,L=window.scrollY;e.innerHTML=r;const Y=W(e),T=[];for(const s of Y){const t=s.textContent??"";if(!t.trim())continue;const i=t.split(/(\S+)/),c=document.createDocumentFragment();for(let n=0;n<i.length;n+=2){const M=i[n],m=i[n+1];if(!m)continue;const A=i[n+3]===void 0?i[n+2]??"":"",q=document.createElement("span");q.className=D.word,q.textContent=M+m+A,c.appendChild(q),T.push(q)}s.parentNode.replaceChild(c,s)}if(requestAnimationFrame(()=>{typeof window<"u"&&Math.abs(window.scrollY-L)>2&&window.scrollTo({top:L,behavior:"instant"})}),T.length===0)return()=>{};const F=getComputedStyle(e).fontVariationSettings,p=et(F,Object.fromEntries(Object.entries(y).map(([s,[t]])=>[s,t])));T.forEach(s=>{s.style.fontVariationSettings=p,f&&U(s,f)});let g=-9999,V=-9999,S=!1,u=0,b=!0,N=null;function h(){if(!b)return;if(!S){T.forEach(t=>{w>0&&(t.style.transition=`font-variation-settings ${w}ms ease`),t.style.fontVariationSettings=p,f&&U(t,f)}),w>0&&(N!==null&&clearTimeout(N),N=setTimeout(()=>{T.forEach(t=>{t.style.transition=""}),N=null},w)),u=0;return}const s=T.map(t=>t.getBoundingClientRect());T.forEach((t,i)=>{t.style.transition="";const c=s[i],n=c.left+c.width/2,M=c.top+c.height/2,m=Math.sqrt((g-n)**2+(V-M)**2),B=Math.max(0,1-m/a),A=l==="quadratic"?B*B:B,q=x==="repel"?1-A:A,H={};for(const _ of Object.keys(y)){const[J,O]=y[_]??[300,500];H[_]=J+(O-J)*q}t.style.fontVariationSettings=et(F,H),f&&nt(t,f,A)}),u=requestAnimationFrame(h)}function X(s){g=s.clientX,V=s.clientY,S||(S=!0),u===0&&(u=requestAnimationFrame(h))}function R(){S=!1,u===0&&(u=requestAnimationFrame(h))}function k(s){s.touches.length!==0&&(g=s.touches[0].clientX,V=s.touches[0].clientY,S||(S=!0),u===0&&(u=requestAnimationFrame(h)))}function $(){S=!1,u===0&&(u=requestAnimationFrame(h))}const v=E==="document"?document:e;return v.addEventListener("mousemove",X),v.addEventListener("mouseleave",R),v.addEventListener("touchmove",k,{passive:!0}),v.addEventListener("touchend",$),()=>{b=!1,cancelAnimationFrame(u),N!==null&&clearTimeout(N),v.removeEventListener("mousemove",X),v.removeEventListener("mouseleave",R),v.removeEventListener("touchmove",k),v.removeEventListener("touchend",$),e.innerHTML=r}}function ct(e){const r=d.useRef(null),o=d.useRef(null),y=d.useRef(e);y.current=e;const a=d.useRef(null),l=e.mode??"field",{axes:x,radius:E,falloff:f,magnetMode:w,wdthBoost:L,scope:Y}=e,T=x?JSON.stringify(x):void 0,F=e.props?JSON.stringify(e.props):void 0,p=d.useCallback(()=>{const g=r.current;if(!g)return;o.current===null&&(o.current=rt(g)),a.current&&(a.current(),a.current=null),(y.current.mode??"field")==="field"?a.current=st(g,o.current,y.current):a.current=ot(g,o.current,y.current)},[l,T,E,f,w,L,Y,F]);return d.useLayoutEffect(()=>(p(),()=>{a.current&&(a.current(),a.current=null)}),[p]),d.useEffect(()=>{document.fonts.ready.then(p)},[p]),r}const it=d.forwardRef(function({children:r,as:o="p",className:y,style:a,...l},x){const E=ct(l),f=d.useCallback(w=>{E.current=w,typeof x=="function"?x(w):x&&(x.current=w)},[x]);return G.jsx(o,{ref:f,className:y,style:a,children:r})});it.displayName="MagnetTypeText";const ut=d.forwardRef(function({children:r,as:o="p",className:y,style:a,minWeight:l=300,maxWeight:x=600,proximityRadius:E,spreadRadius:f,fixedAxes:w={},cachePositions:L=!0,rafThrottle:Y=!0},T){const F=d.useRef(null),p=d.useRef(null),g=d.useRef([]),V=d.useRef([]),S=d.useRef(null),u=d.useRef(!1),b=d.useRef(null),N=d.useCallback(t=>{F.current=t,typeof T=="function"?T(t):T&&(T.current=t)},[T]);function h(t){const i=[`'wght' ${t.toFixed(0)}`];for(const[c,n]of Object.entries(w))i.push(`'${c}' ${n}`);return i.join(", ")}function X(){const t=F.current;if(!t)return;const i=window.scrollX,c=window.scrollY,n=t.getBoundingClientRect();S.current={top:n.top+c,left:n.left+i,right:n.right+i,bottom:n.bottom+c},V.current=g.current.map(M=>{if(!M)return{cx:0,cy:0};const m=M.getBoundingClientRect();return{cx:(m.left+m.right)/2+i,cy:(m.top+m.bottom)/2+c}}),u.current=!0}function R(t,i){const c=F.current;if(!c)return;L&&!u.current&&X();let n,M,m,B,A,q;if(L&&S.current)A=t+window.scrollX,q=i+window.scrollY,{top:n,left:M,right:m,bottom:B}=S.current;else{const O=c.getBoundingClientRect();A=t,q=i,n=O.top,M=O.left,m=O.right,B=O.bottom}const H=Math.max(M-A,0,A-m),_=Math.max(n-q,0,q-B),J=Math.sqrt(H*H+_*_);if(E!==void 0&&!f){const C=1-(1-Math.max(0,1-J/E))**2;c.style.fontVariationSettings=h(l+(x-l)*C);return}if(f){if(E!==void 0&&J>E){c.style.fontVariationSettings=h(l);for(const C of g.current)C&&(C.style.fontVariationSettings=h(l));return}if(J>f){for(const C of g.current)C&&(C.style.fontVariationSettings=h(l));return}const O=g.current;if(L&&V.current.length===O.length)for(let C=0;C<O.length;C++){const I=O[C];if(!I)continue;const{cx:z,cy:P}=V.current[C],Q=Math.sqrt((A-z)**2+(q-P)**2),Z=1-(1-Math.max(0,1-Q/f))**2;I.style.fontVariationSettings=h(l+(x-l)*Z)}else for(const C of O){if(!C)continue;const I=C.getBoundingClientRect(),z=(I.left+I.right)/2,P=(I.top+I.bottom)/2,Q=Math.sqrt((t-z)**2+(i-P)**2),Z=1-(1-Math.max(0,1-Q/f))**2;C.style.fontVariationSettings=h(l+(x-l)*Z)}}}const k=d.useCallback(t=>{p.current={x:t.clientX,y:t.clientY},Y?b.current===null&&(b.current=requestAnimationFrame(()=>{b.current=null,p.current&&R(p.current.x,p.current.y)})):R(t.clientX,t.clientY)},[l,x,E,f,L,Y,JSON.stringify(w)]),$=d.useCallback(()=>{p.current&&R(p.current.x,p.current.y)},[l,x,E,f,L,JSON.stringify(w)]),v=d.useCallback(()=>{p.current=null,b.current!==null&&(cancelAnimationFrame(b.current),b.current=null);const t=F.current;t&&(t.style.fontVariationSettings=h(l));for(const i of g.current)i&&(i.style.fontVariationSettings=h(l))},[l,JSON.stringify(w)]);d.useEffect(()=>(window.addEventListener("mousemove",k,{passive:!0}),window.addEventListener("scroll",$,{passive:!0,capture:!0}),document.documentElement.addEventListener("mouseleave",v),()=>{window.removeEventListener("mousemove",k),window.removeEventListener("scroll",$,{capture:!0}),document.documentElement.removeEventListener("mouseleave",v),b.current!==null&&(cancelAnimationFrame(b.current),b.current=null)}),[k,$,v]),d.useEffect(()=>{if(!L||!f)return;u.current=!1;const t=F.current;if(!t)return;const i=new ResizeObserver(()=>{u.current=!1});return i.observe(t),document.fonts.ready.then(()=>{u.current=!1}),()=>i.disconnect()},[L,f]),d.useEffect(()=>{u.current=!1},[r,f]);const s=d.useMemo(()=>{if(!f)return r;g.current=[];let t=0;function i(c){if(typeof c=="string")return[...c].map(n=>{if(/\s/.test(n))return n;const M=t++;return G.jsx("span",{ref:m=>{g.current[M]=m},style:{fontVariationSettings:h(l)},children:n},M)});if(Array.isArray(c))return c.map((n,M)=>G.jsx(d.Fragment,{children:i(n)},M));if(d.isValidElement(c)){const n=c;if(n.props.children!==void 0)return d.cloneElement(n,{},i(n.props.children))}return c}return i(r)},[r,f,l,JSON.stringify(w)]);return G.jsx(o,{ref:N,className:y,style:{fontVariationSettings:h(l),...a},children:s})});ut.displayName="MagnetBlock";exports.MAGNET_TYPE_CLASSES=D;exports.MagnetBlock=ut;exports.MagnetTypeText=it;exports.applyMagnetType=ot;exports.getCleanHTML=rt;exports.removeMagnetType=lt;exports.startMagnetType=st;exports.useMagnetType=ct;
1
+ "use client";"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("react"),W=require("react/jsx-runtime"),rt={i:3,l:3,1:3,I:3,r:2,n:1,m:1,0:2,O:2,o:1,b:1,d:1,p:1,q:1,c:1,e:1},z={word:"mt-word",char:"mt-char",probe:"mt-probe"},K={axes:{wght:[300,500]},radius:120,falloff:"quadratic",magnetMode:"attract",wdthBoost:6,scope:"document"};function ot(t,n=[]){return t.nodeType===Node.TEXT_NODE?n.push(t):t.childNodes.forEach(o=>ot(o,n)),n}function tt(t,n,o){if(!t||t==="normal")return`"${n}" ${o}`;const E=new RegExp(`(["'])${n}\\1\\s+[\\d.eE+-]+`),y=`"${n}" ${o}`;return E.test(t)?t.replace(E,y):`${t}, ${y}`}function nt(t,n){let o=t;for(const[E,y]of Object.entries(n))o=tt(o,E,y);return o}function st(t,n,o){if(n.opacity!==void 0){const[E,y]=n.opacity;t.style.opacity=String(E+(y-E)*o)}n.italic===!0&&(t.style.fontStyle=o>.5?"italic":"")}function et(t,n){n.opacity!==void 0&&(t.style.opacity=String(n.opacity[0])),n.italic===!0&&(t.style.fontStyle="")}function ct(t){const n=t.cloneNode(!0),o=n.querySelectorAll(`.${z.word}, .${z.char}`);return Array.from(o).reverse().forEach(y=>{const d=y.parentNode;if(d){for(;y.firstChild;)d.insertBefore(y.firstChild,y);d.removeChild(y)}}),n.innerHTML}function mt(t,n){t.innerHTML=n}function it(t,n,o={}){var p,r;if(typeof window>"u")return()=>{};if(window.matchMedia("(prefers-reduced-motion: reduce)").matches)return t.innerHTML=n,()=>{};const E=o.wdthBoost??K.wdthBoost,y=o.radius??K.radius,d=o.falloff??K.falloff,L=o.scope??K.scope,F=o.props,m=o.transitionMs??0,S=o.cachePositions??!0,V=window.scrollY;t.innerHTML=n;const j=getComputedStyle(t).fontVariationSettings,N=j.match(/"wdth"\s+([\d.eE+-]+)/),k=N?parseFloat(N[1]):100,T=ot(t),g=[];for(const a of T){const C=a.textContent??"";if(!C||!C.split("").some(v=>v in rt))continue;const e=document.createDocumentFragment();for(const v of C){const i=rt[v];if(i===void 0){const u=e.lastChild;u&&u.nodeType===Node.TEXT_NODE?u.textContent+=v:e.appendChild(document.createTextNode(v))}else{const u=document.createElement("span");u.className=z.char,u.style.fontVariationSettings=tt(j,"wdth",k),u.textContent=v,e.appendChild(u),g.push({span:u,riskLevel:i})}}a.parentNode.replaceChild(e,a)}if(requestAnimationFrame(()=>{typeof window<"u"&&Math.abs(window.scrollY-V)>2&&window.scrollTo({top:V,behavior:"instant"})}),g.length===0)return()=>{};F&&g.forEach(({span:a})=>et(a,F));let A=[],H=!1;function _(){const a=window.scrollX,C=window.scrollY;A=g.map(({span:c})=>{const e=c.getBoundingClientRect();return{cx:(e.left+e.right)/2+a,cy:(e.top+e.bottom)/2+C}}),H=!0}let R=null;S&&(_(),R=new ResizeObserver(()=>{H=!1}),R.observe(t),(r=(p=document.fonts)==null?void 0:p.ready)==null||r.then(()=>{H=!1}));let q=-9999,B=-9999,$=!1,w=0,P=!0,b=null;function D(){if(!P)return;if(!$){g.forEach(({span:e})=>{m>0&&(e.style.transition=`font-variation-settings ${m}ms ease`),e.style.fontVariationSettings=tt(j,"wdth",k),F&&et(e,F)}),m>0&&(b!==null&&clearTimeout(b),b=setTimeout(()=>{g.forEach(({span:e})=>{e.style.transition=""}),b=null},m)),w=0;return}S&&!H&&_();const a=S?q+window.scrollX:q,C=S?B+window.scrollY:B,c=S?null:g.map(({span:e})=>e.getBoundingClientRect());g.forEach(({span:e,riskLevel:v},i)=>{e.style.transition="";let u,O;if(S)({cx:u,cy:O}=A[i]);else{const I=c[i];u=I.left+I.width/2,O=I.top+I.height/2}const M=Math.sqrt((a-u)**2+(C-O)**2),f=Math.max(0,1-M/y),x=d==="quadratic"?f*f:f,Y=E*(v/3)*x;e.style.fontVariationSettings=tt(j,"wdth",k+Y),F&&st(e,F,x)}),w=requestAnimationFrame(D)}function X(a){q=a.clientX,B=a.clientY,$||($=!0),w===0&&(w=requestAnimationFrame(D))}function J(){$=!1,w===0&&(w=requestAnimationFrame(D))}function Q(a){a.touches.length!==0&&(q=a.touches[0].clientX,B=a.touches[0].clientY,$||($=!0),w===0&&(w=requestAnimationFrame(D)))}function l(){$=!1,w===0&&(w=requestAnimationFrame(D))}const s=L==="document"?document:t;return s.addEventListener("mousemove",X),s.addEventListener("mouseleave",J),s.addEventListener("touchmove",Q,{passive:!0}),s.addEventListener("touchend",l),()=>{P=!1,cancelAnimationFrame(w),b!==null&&clearTimeout(b),R&&R.disconnect(),s.removeEventListener("mousemove",X),s.removeEventListener("mouseleave",J),s.removeEventListener("touchmove",Q),s.removeEventListener("touchend",l),t.innerHTML=n}}function lt(t,n,o={}){var a,C;if(typeof window>"u")return()=>{};if(window.matchMedia("(prefers-reduced-motion: reduce)").matches)return t.innerHTML=n,()=>{};const E=o.axes??K.axes,y=o.radius??K.radius,d=o.falloff??K.falloff,L=o.magnetMode??K.magnetMode,F=o.scope??K.scope,m=o.props,S=o.transitionMs??0,V=o.cachePositions??!0,j=o.stabilizeLayout??!0,N=window.scrollY;t.innerHTML=n;const k=ot(t),T=[];for(const c of k){const e=c.textContent??"";if(!e.trim())continue;const v=e.split(/(\S+)/),i=document.createDocumentFragment();for(let u=0;u<v.length;u+=2){const O=v[u],M=v[u+1];if(!M)continue;const x=v[u+3]===void 0?v[u+2]??"":"",Y=document.createElement("span");Y.className=z.word,Y.textContent=O+M+x,i.appendChild(Y),T.push(Y)}c.parentNode.replaceChild(i,c)}if(requestAnimationFrame(()=>{typeof window<"u"&&Math.abs(window.scrollY-N)>2&&window.scrollTo({top:N,behavior:"instant"})}),T.length===0)return()=>{};const g=getComputedStyle(t).fontVariationSettings,A=nt(g,Object.fromEntries(Object.entries(E).map(([c,[e]])=>[c,e])));let H=0;if(j){const c=nt(g,Object.fromEntries(Object.entries(E).map(([f,[,x]])=>[f,x]))),e=t.style.fontVariationSettings,v=t.style.whiteSpace,i=t.style.overflow;t.style.whiteSpace="nowrap",t.style.overflow="visible",t.style.fontVariationSettings=c;const u=t.scrollWidth;t.style.fontVariationSettings=A;const O=t.scrollWidth;t.style.fontVariationSettings=e,t.style.whiteSpace=v,t.style.overflow=i;const M=T.reduce((f,x)=>{var Y;return f+(((Y=x.textContent)==null?void 0:Y.replace(/\s+/g,"").length)??0)},0);M>0&&u>O&&(H=(u-O)/M)}T.forEach(c=>{c.style.fontVariationSettings=A,m&&et(c,m)});let _=[],R=!1;function q(){const c=window.scrollX,e=window.scrollY;_=T.map(v=>{const i=v.getBoundingClientRect();return{cx:(i.left+i.right)/2+c,cy:(i.top+i.bottom)/2+e}}),R=!0}let B=null;V&&(q(),B=new ResizeObserver(()=>{R=!1}),B.observe(t),(C=(a=document.fonts)==null?void 0:a.ready)==null||C.then(()=>{R=!1}));let $=-9999,w=-9999,P=!1,b=0,D=!0,X=null;function J(){if(!D)return;if(!P){T.forEach(i=>{S>0&&(i.style.transition=`font-variation-settings ${S}ms ease`),i.style.fontVariationSettings=A,j&&(i.style.letterSpacing=""),m&&et(i,m)}),S>0&&(X!==null&&clearTimeout(X),X=setTimeout(()=>{T.forEach(i=>{i.style.transition=""}),X=null},S)),b=0;return}V&&!R&&q();const c=V?$+window.scrollX:$,e=V?w+window.scrollY:w,v=V?null:T.map(i=>i.getBoundingClientRect());T.forEach((i,u)=>{i.style.transition="";let O,M;if(V)({cx:O,cy:M}=_[u]);else{const G=v[u];O=G.left+G.width/2,M=G.top+G.height/2}const f=Math.sqrt((c-O)**2+(e-M)**2),x=Math.max(0,1-f/y),Y=d==="quadratic"?x*x:x,I=L==="repel"?1-Y:Y,Z={};for(const G of Object.keys(E)){const[U,dt]=E[G]??[300,500];Z[G]=U+(dt-U)*I}i.style.fontVariationSettings=nt(g,Z),j&&H!==0&&(i.style.letterSpacing=`${(-H*I).toFixed(3)}px`),m&&st(i,m,Y)}),b=requestAnimationFrame(J)}function Q(c){$=c.clientX,w=c.clientY,P||(P=!0),b===0&&(b=requestAnimationFrame(J))}function l(){P=!1,b===0&&(b=requestAnimationFrame(J))}function s(c){c.touches.length!==0&&($=c.touches[0].clientX,w=c.touches[0].clientY,P||(P=!0),b===0&&(b=requestAnimationFrame(J)))}function p(){P=!1,b===0&&(b=requestAnimationFrame(J))}const r=F==="document"?document:t;return r.addEventListener("mousemove",Q),r.addEventListener("mouseleave",l),r.addEventListener("touchmove",s,{passive:!0}),r.addEventListener("touchend",p),()=>{D=!1,cancelAnimationFrame(b),X!==null&&clearTimeout(X),B&&B.disconnect(),r.removeEventListener("mousemove",Q),r.removeEventListener("mouseleave",l),r.removeEventListener("touchmove",s),r.removeEventListener("touchend",p),t.innerHTML=n}}function at(t){const n=h.useRef(null),o=h.useRef(null),E=h.useRef(t);E.current=t;const y=h.useRef(null),d=t.mode??"field",{axes:L,radius:F,falloff:m,magnetMode:S,wdthBoost:V,scope:j}=t,N=L?JSON.stringify(L):void 0,k=t.props?JSON.stringify(t.props):void 0,T=h.useCallback(()=>{const g=n.current;if(!g)return;o.current===null&&(o.current=ct(g)),y.current&&(y.current(),y.current=null),(E.current.mode??"field")==="field"?y.current=lt(g,o.current,E.current):y.current=it(g,o.current,E.current)},[d,N,F,m,S,V,j,k]);return h.useLayoutEffect(()=>(T(),()=>{y.current&&(y.current(),y.current=null)}),[T]),h.useEffect(()=>{var g,A;(A=(g=document.fonts)==null?void 0:g.ready)==null||A.then(T)},[T]),n}const ut=h.forwardRef(function({children:n,as:o="p",className:E,style:y,...d},L){const F=at(d),m=h.useCallback(S=>{F.current=S,typeof L=="function"?L(S):L&&(L.current=S)},[L]);return W.jsx(o,{ref:m,className:E,style:y,children:n})});ut.displayName="MagnetTypeText";const ft=h.forwardRef(function({children:n,as:o="p",className:E,style:y,minWeight:d=300,maxWeight:L=600,proximityRadius:F,spreadRadius:m,fixedAxes:S={},cachePositions:V=!0,rafThrottle:j=!0,stabilizeLayout:N=!0},k){const T=h.useRef(null),g=h.useRef(null),A=h.useRef([]),H=h.useRef([]),_=h.useRef(null),R=h.useRef(!1),q=h.useRef(null),B=h.useRef(0),$=h.useCallback(l=>{T.current=l,typeof k=="function"?k(l):k&&(k.current=l)},[k]);function w(l){const s=[`'wght' ${l.toFixed(0)}`];for(const[p,r]of Object.entries(S))s.push(`'${p}' ${r}`);return s.join(", ")}function P(){const l=T.current;if(!l)return;const s=window.scrollX,p=window.scrollY,r=l.getBoundingClientRect();_.current={top:r.top+p,left:r.left+s,right:r.right+s,bottom:r.bottom+p},H.current=A.current.map(a=>{if(!a)return{cx:0,cy:0};const C=a.getBoundingClientRect();return{cx:(C.left+C.right)/2+s,cy:(C.top+C.bottom)/2+p}}),R.current=!0}function b(l,s){const p=T.current;if(!p)return;V&&!R.current&&P();let r,a,C,c,e,v;if(V&&_.current)e=l+window.scrollX,v=s+window.scrollY,{top:r,left:a,right:C,bottom:c}=_.current;else{const M=p.getBoundingClientRect();e=l,v=s,r=M.top,a=M.left,C=M.right,c=M.bottom}const i=Math.max(a-e,0,e-C),u=Math.max(r-v,0,v-c),O=Math.sqrt(i*i+u*u);if(F!==void 0&&!m){const f=1-(1-Math.max(0,1-O/F))**2;p.style.fontVariationSettings=w(d+(L-d)*f);return}if(m){if(F!==void 0&&O>F){p.style.fontVariationSettings=w(d);for(const f of A.current)f&&(f.style.fontVariationSettings=w(d),N&&(f.style.letterSpacing=""));return}if(O>m){for(const f of A.current)f&&(f.style.fontVariationSettings=w(d),N&&(f.style.letterSpacing=""));return}const M=A.current;if(V&&H.current.length===M.length)for(let f=0;f<M.length;f++){const x=M[f];if(!x)continue;const{cx:Y,cy:I}=H.current[f],Z=Math.sqrt((e-Y)**2+(v-I)**2),U=1-(1-Math.max(0,1-Z/m))**2;x.style.fontVariationSettings=w(d+(L-d)*U),N&&B.current!==0&&(x.style.letterSpacing=`${(-B.current*U).toFixed(3)}px`)}else for(const f of M){if(!f)continue;const x=f.getBoundingClientRect(),Y=(x.left+x.right)/2,I=(x.top+x.bottom)/2,Z=Math.sqrt((l-Y)**2+(s-I)**2),U=1-(1-Math.max(0,1-Z/m))**2;f.style.fontVariationSettings=w(d+(L-d)*U),N&&B.current!==0&&(f.style.letterSpacing=`${(-B.current*U).toFixed(3)}px`)}}}const D=h.useCallback(l=>{g.current={x:l.clientX,y:l.clientY},j?q.current===null&&(q.current=requestAnimationFrame(()=>{q.current=null,g.current&&b(g.current.x,g.current.y)})):b(l.clientX,l.clientY)},[d,L,F,m,V,j,N,JSON.stringify(S)]),X=h.useCallback(()=>{g.current&&b(g.current.x,g.current.y)},[d,L,F,m,V,N,JSON.stringify(S)]),J=h.useCallback(()=>{g.current=null,q.current!==null&&(cancelAnimationFrame(q.current),q.current=null);const l=T.current;l&&(l.style.fontVariationSettings=w(d));for(const s of A.current)s&&(s.style.fontVariationSettings=w(d),N&&(s.style.letterSpacing=""))},[d,N,JSON.stringify(S)]);h.useEffect(()=>(window.addEventListener("mousemove",D,{passive:!0}),window.addEventListener("scroll",X,{passive:!0,capture:!0}),document.documentElement.addEventListener("mouseleave",J),()=>{window.removeEventListener("mousemove",D),window.removeEventListener("scroll",X,{capture:!0}),document.documentElement.removeEventListener("mouseleave",J),q.current!==null&&(cancelAnimationFrame(q.current),q.current=null)}),[D,X,J]),h.useEffect(()=>{var p,r;if(!V||!m)return;R.current=!1;const l=T.current;if(!l)return;const s=new ResizeObserver(()=>{R.current=!1});return s.observe(l),(r=(p=document.fonts)==null?void 0:p.ready)==null||r.then(()=>{R.current=!1}),()=>s.disconnect()},[V,m]),h.useEffect(()=>{R.current=!1},[n,m]),h.useEffect(()=>{var s,p;if(!N||!m){B.current=0;return}function l(){const r=T.current;if(!r)return;const a=r.textContent??"",C=a.replace(/\s/g,"").length;if(C===0)return;const c=getComputedStyle(r),e=document.createElement("span");e.style.cssText="position:fixed;top:-9999px;left:-9999px;visibility:hidden;white-space:nowrap;pointer-events:none;",e.style.fontFamily=c.fontFamily,e.style.fontSize=c.fontSize,e.style.fontWeight=c.fontWeight,e.style.lineHeight=c.lineHeight,e.style.letterSpacing=c.letterSpacing,e.textContent=a,document.body.appendChild(e);const v=O=>{const M=[`'wght' ${O.toFixed(0)}`];for(const[f,x]of Object.entries(S))M.push(`'${f}' ${x}`);return M.join(", ")};e.style.fontVariationSettings=v(L);const i=e.scrollWidth;e.style.fontVariationSettings=v(d);const u=e.scrollWidth;document.body.removeChild(e),B.current=i>u?(i-u)/C:0}l(),(p=(s=document.fonts)==null?void 0:s.ready)==null||p.then(l)},[N,m,d,L,n,JSON.stringify(S)]);const Q=h.useMemo(()=>{if(!m)return n;A.current=[];let l=0;function s(p){if(typeof p=="string")return[...p].map(r=>{if(/\s/.test(r))return r;const a=l++;return W.jsx("span",{ref:C=>{A.current[a]=C},style:{fontVariationSettings:w(d)},children:r},a)});if(Array.isArray(p))return p.map((r,a)=>W.jsx(h.Fragment,{children:s(r)},a));if(h.isValidElement(p)){const r=p;if(r.props.children!==void 0)return h.cloneElement(r,{},s(r.props.children))}return p}return s(n)},[n,m,d,JSON.stringify(S)]);return W.jsx(o,{ref:$,className:E,style:{fontVariationSettings:w(d),...y},children:Q})});ft.displayName="MagnetBlock";exports.MAGNET_TYPE_CLASSES=z;exports.MagnetBlock=ft;exports.MagnetTypeText=ut;exports.applyMagnetType=it;exports.getCleanHTML=ct;exports.removeMagnetType=mt;exports.startMagnetType=lt;exports.useMagnetType=at;
package/dist/index.d.ts CHANGED
@@ -69,6 +69,18 @@ export declare interface MagnetBlockProps {
69
69
  * @default true
70
70
  */
71
71
  rafThrottle?: boolean;
72
+ /**
73
+ * Apply compensating letter-spacing to keep line lengths stable as font weight changes.
74
+ * Measures the element's text width at rest and peak weight via an off-screen probe span,
75
+ * then applies proportional negative letter-spacing per character as weight rises,
76
+ * cancelling the advance-width increase that would otherwise cause text to reflow.
77
+ *
78
+ * Disable if you prefer natural bold letter-spacing, or if the font expands characters
79
+ * very unevenly across the weight axis (the compensation is a per-element average and
80
+ * may not perfectly cancel highly variable per-character expansion).
81
+ * @default true
82
+ */
83
+ stabilizeLayout?: boolean;
72
84
  }
73
85
 
74
86
  /** Whether cursor proximity attracts toward peak or repels toward rest */
@@ -137,6 +149,28 @@ export declare interface MagnetTypeOptions {
137
149
  * Risk 3 characters receive wdthBoost × (3/3) = full boost at peak.
138
150
  */
139
151
  wdthBoost?: number;
152
+ /**
153
+ * Cache word/character centre positions in page-relative coordinates after setup,
154
+ * eliminating getBoundingClientRect calls during the active interaction loop.
155
+ * The cache is rebuilt on resize and after fonts load. Disable if the element lives
156
+ * inside a custom scroll container (overflow: scroll on a non-window element) —
157
+ * page coordinates are computed using window.scrollX/Y and will be incorrect when
158
+ * a nested element is the scroll parent.
159
+ * @default true
160
+ */
161
+ cachePositions?: boolean;
162
+ /**
163
+ * Apply compensating letter-spacing to keep line lengths stable as font weight
164
+ * changes. Measures the element's text width at rest and peak weight, then applies
165
+ * proportional negative letter-spacing per word/character as weight rises, cancelling
166
+ * the advance-width increase that would otherwise cause text to reflow.
167
+ *
168
+ * Disable if you prefer natural bold letter-spacing, or if the font expands characters
169
+ * very unevenly across the weight axis (the compensation is a per-element average and
170
+ * may not perfectly cancel highly variable per-character expansion).
171
+ * @default true
172
+ */
173
+ stabilizeLayout?: boolean;
140
174
  /**
141
175
  * Duration in milliseconds for the CSS transition back to rest values when the
142
176
  * cursor leaves (mouseleave / touchend). Default: 0 (instant snap, no transition).
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use client";
2
- import et, { useRef as O, useCallback as R, useLayoutEffect as ut, useEffect as K, forwardRef as ct, useMemo as at } from "react";
3
- import { jsx as U } from "react/jsx-runtime";
4
- const rt = {
2
+ import rt, { useRef as I, useCallback as z, useLayoutEffect as ft, useEffect as W, forwardRef as lt, useMemo as dt } from "react";
3
+ import { jsx as tt } from "react/jsx-runtime";
4
+ const it = {
5
5
  i: 3,
6
6
  l: 3,
7
7
  1: 3,
@@ -23,14 +23,14 @@ const rt = {
23
23
  c: 1,
24
24
  e: 1
25
25
  // similar bowls
26
- }, G = {
26
+ }, nt = {
27
27
  /** Applied to each word span in field mode */
28
28
  word: "mt-word",
29
29
  /** Applied to each character span in legibility mode */
30
30
  char: "mt-char",
31
31
  /** Applied to measurement probe spans (never in final output) */
32
32
  probe: "mt-probe"
33
- }, j = {
33
+ }, U = {
34
34
  axes: { wght: [300, 500] },
35
35
  radius: 120,
36
36
  falloff: "quadratic",
@@ -38,389 +38,486 @@ const rt = {
38
38
  wdthBoost: 6,
39
39
  scope: "document"
40
40
  };
41
- function nt(e, r = []) {
42
- return e.nodeType === Node.TEXT_NODE ? r.push(e) : e.childNodes.forEach((o) => nt(o, r)), r;
41
+ function ct(t, n = []) {
42
+ return t.nodeType === Node.TEXT_NODE ? n.push(t) : t.childNodes.forEach((o) => ct(o, n)), n;
43
43
  }
44
- function z(e, r, o) {
45
- if (!e || e === "normal") return `"${r}" ${o}`;
46
- const p = new RegExp(`(["'])${r}\\1\\s+[\\d.eE+-]+`), a = `"${r}" ${o}`;
47
- return p.test(e) ? e.replace(p, a) : `${e}, ${a}`;
44
+ function et(t, n, o) {
45
+ if (!t || t === "normal") return `"${n}" ${o}`;
46
+ const M = new RegExp(`(["'])${n}\\1\\s+[\\d.eE+-]+`), h = `"${n}" ${o}`;
47
+ return M.test(t) ? t.replace(M, h) : `${t}, ${h}`;
48
48
  }
49
- function ot(e, r) {
50
- let o = e;
51
- for (const [p, a] of Object.entries(r))
52
- o = z(o, p, a);
49
+ function st(t, n) {
50
+ let o = t;
51
+ for (const [M, h] of Object.entries(n))
52
+ o = et(o, M, h);
53
53
  return o;
54
54
  }
55
- function st(e, r, o) {
56
- if (r.opacity !== void 0) {
57
- const [p, a] = r.opacity;
58
- e.style.opacity = String(p + (a - p) * o);
55
+ function at(t, n, o) {
56
+ if (n.opacity !== void 0) {
57
+ const [M, h] = n.opacity;
58
+ t.style.opacity = String(M + (h - M) * o);
59
59
  }
60
- r.italic === !0 && (e.style.fontStyle = o > 0.5 ? "italic" : "");
60
+ n.italic === !0 && (t.style.fontStyle = o > 0.5 ? "italic" : "");
61
61
  }
62
- function Q(e, r) {
63
- r.opacity !== void 0 && (e.style.opacity = String(r.opacity[0])), r.italic === !0 && (e.style.fontStyle = "");
62
+ function ot(t, n) {
63
+ n.opacity !== void 0 && (t.style.opacity = String(n.opacity[0])), n.italic === !0 && (t.style.fontStyle = "");
64
64
  }
65
- function lt(e) {
66
- const r = e.cloneNode(!0), o = r.querySelectorAll(
67
- `.${G.word}, .${G.char}`
65
+ function mt(t) {
66
+ const n = t.cloneNode(!0), o = n.querySelectorAll(
67
+ `.${nt.word}, .${nt.char}`
68
68
  );
69
- return Array.from(o).reverse().forEach((a) => {
70
- const l = a.parentNode;
71
- if (l) {
72
- for (; a.firstChild; ) l.insertBefore(a.firstChild, a);
73
- l.removeChild(a);
69
+ return Array.from(o).reverse().forEach((h) => {
70
+ const d = h.parentNode;
71
+ if (d) {
72
+ for (; h.firstChild; ) d.insertBefore(h.firstChild, h);
73
+ d.removeChild(h);
74
74
  }
75
- }), r.innerHTML;
75
+ }), n.innerHTML;
76
76
  }
77
- function gt(e, r) {
78
- e.innerHTML = r;
77
+ function Mt(t, n) {
78
+ t.innerHTML = n;
79
79
  }
80
- function ft(e, r, o = {}) {
80
+ function pt(t, n, o = {}) {
81
+ var p, r;
81
82
  if (typeof window > "u") return () => {
82
83
  };
83
84
  if (window.matchMedia("(prefers-reduced-motion: reduce)").matches)
84
- return e.innerHTML = r, () => {
85
+ return t.innerHTML = n, () => {
85
86
  };
86
- const p = o.wdthBoost ?? j.wdthBoost, a = o.radius ?? j.radius, l = o.falloff ?? j.falloff, S = o.scope ?? j.scope, w = o.props, f = o.transitionMs ?? 0, M = window.scrollY;
87
- e.innerHTML = r;
88
- const L = getComputedStyle(e).fontVariationSettings, Y = L.match(/"wdth"\s+([\d.eE+-]+)/), E = Y ? parseFloat(Y[1]) : 100, q = nt(e), m = [];
89
- for (const c of q) {
90
- const t = c.textContent ?? "";
91
- if (!t || !t.split("").some((n) => n in rt)) continue;
92
- const s = document.createDocumentFragment();
93
- for (const n of t) {
94
- const g = rt[n];
95
- if (g === void 0) {
96
- const d = s.lastChild;
97
- d && d.nodeType === Node.TEXT_NODE ? d.textContent += n : s.appendChild(document.createTextNode(n));
87
+ const M = o.wdthBoost ?? U.wdthBoost, h = o.radius ?? U.radius, d = o.falloff ?? U.falloff, b = o.scope ?? U.scope, F = o.props, m = o.transitionMs ?? 0, w = o.cachePositions ?? !0, L = window.scrollY;
88
+ t.innerHTML = n;
89
+ const X = getComputedStyle(t).fontVariationSettings, V = X.match(/"wdth"\s+([\d.eE+-]+)/), j = V ? parseFloat(V[1]) : 100, x = ct(t), y = [];
90
+ for (const a of x) {
91
+ const E = a.textContent ?? "";
92
+ if (!E || !E.split("").some((g) => g in it)) continue;
93
+ const e = document.createDocumentFragment();
94
+ for (const g of E) {
95
+ const i = it[g];
96
+ if (i === void 0) {
97
+ const u = e.lastChild;
98
+ u && u.nodeType === Node.TEXT_NODE ? u.textContent += g : e.appendChild(document.createTextNode(g));
98
99
  } else {
99
- const d = document.createElement("span");
100
- d.className = G.char, d.style.fontVariationSettings = z(L, "wdth", E), d.textContent = n, s.appendChild(d), m.push({ span: d, riskLevel: g });
100
+ const u = document.createElement("span");
101
+ u.className = nt.char, u.style.fontVariationSettings = et(X, "wdth", j), u.textContent = g, e.appendChild(u), y.push({ span: u, riskLevel: i });
101
102
  }
102
103
  }
103
- c.parentNode.replaceChild(s, c);
104
+ a.parentNode.replaceChild(e, a);
104
105
  }
105
106
  if (requestAnimationFrame(() => {
106
- typeof window < "u" && Math.abs(window.scrollY - M) > 2 && window.scrollTo({ top: M, behavior: "instant" });
107
- }), m.length === 0) return () => {
107
+ typeof window < "u" && Math.abs(window.scrollY - L) > 2 && window.scrollTo({ top: L, behavior: "instant" });
108
+ }), y.length === 0) return () => {
108
109
  };
109
- w && m.forEach(({ span: c }) => Q(c, w));
110
- let y = -9999, F = -9999, T = !1, u = 0, C = !0, N = null;
111
- function h() {
112
- if (!C) return;
113
- if (!T) {
114
- m.forEach(({ span: t }) => {
115
- f > 0 && (t.style.transition = `font-variation-settings ${f}ms ease`), t.style.fontVariationSettings = z(L, "wdth", E), w && Q(t, w);
116
- }), f > 0 && (N !== null && clearTimeout(N), N = setTimeout(() => {
117
- m.forEach(({ span: t }) => {
118
- t.style.transition = "";
119
- }), N = null;
120
- }, f)), u = 0;
110
+ F && y.forEach(({ span: a }) => ot(a, F));
111
+ let N = [], H = !1;
112
+ function _() {
113
+ const a = window.scrollX, E = window.scrollY;
114
+ N = y.map(({ span: c }) => {
115
+ const e = c.getBoundingClientRect();
116
+ return { cx: (e.left + e.right) / 2 + a, cy: (e.top + e.bottom) / 2 + E };
117
+ }), H = !0;
118
+ }
119
+ let Y = null;
120
+ w && (_(), Y = new ResizeObserver(() => {
121
+ H = !1;
122
+ }), Y.observe(t), (r = (p = document.fonts) == null ? void 0 : p.ready) == null || r.then(() => {
123
+ H = !1;
124
+ }));
125
+ let A = -9999, $ = -9999, B = !1, v = 0, R = !0, T = null;
126
+ function D() {
127
+ if (!R) return;
128
+ if (!B) {
129
+ y.forEach(({ span: e }) => {
130
+ m > 0 && (e.style.transition = `font-variation-settings ${m}ms ease`), e.style.fontVariationSettings = et(X, "wdth", j), F && ot(e, F);
131
+ }), m > 0 && (T !== null && clearTimeout(T), T = setTimeout(() => {
132
+ y.forEach(({ span: e }) => {
133
+ e.style.transition = "";
134
+ }), T = null;
135
+ }, m)), v = 0;
121
136
  return;
122
137
  }
123
- const c = m.map(({ span: t }) => t.getBoundingClientRect());
124
- m.forEach(({ span: t, riskLevel: i }, s) => {
125
- t.style.transition = "";
126
- const n = c[s], g = n.left + n.width / 2, d = n.top + n.height / 2, B = Math.sqrt((y - g) ** 2 + (F - d) ** 2), b = Math.max(0, 1 - B / a), A = l === "quadratic" ? b * b : b, I = p * (i / 3) * A;
127
- t.style.fontVariationSettings = z(L, "wdth", E + I), w && st(t, w, A);
128
- }), u = requestAnimationFrame(h);
138
+ w && !H && _();
139
+ const a = w ? A + window.scrollX : A, E = w ? $ + window.scrollY : $, c = w ? null : y.map(({ span: e }) => e.getBoundingClientRect());
140
+ y.forEach(({ span: e, riskLevel: g }, i) => {
141
+ e.style.transition = "";
142
+ let u, O;
143
+ if (w)
144
+ ({ cx: u, cy: O } = N[i]);
145
+ else {
146
+ const P = c[i];
147
+ u = P.left + P.width / 2, O = P.top + P.height / 2;
148
+ }
149
+ const S = Math.sqrt((a - u) ** 2 + (E - O) ** 2), f = Math.max(0, 1 - S / h), C = d === "quadratic" ? f * f : f, q = M * (g / 3) * C;
150
+ e.style.fontVariationSettings = et(X, "wdth", j + q), F && at(e, F, C);
151
+ }), v = requestAnimationFrame(D);
129
152
  }
130
- function H(c) {
131
- y = c.clientX, F = c.clientY, T || (T = !0), u === 0 && (u = requestAnimationFrame(h));
153
+ function k(a) {
154
+ A = a.clientX, $ = a.clientY, B || (B = !0), v === 0 && (v = requestAnimationFrame(D));
132
155
  }
133
- function $() {
134
- T = !1, u === 0 && (u = requestAnimationFrame(h));
156
+ function J() {
157
+ B = !1, v === 0 && (v = requestAnimationFrame(D));
135
158
  }
136
- function X(c) {
137
- c.touches.length !== 0 && (y = c.touches[0].clientX, F = c.touches[0].clientY, T || (T = !0), u === 0 && (u = requestAnimationFrame(h)));
159
+ function Q(a) {
160
+ a.touches.length !== 0 && (A = a.touches[0].clientX, $ = a.touches[0].clientY, B || (B = !0), v === 0 && (v = requestAnimationFrame(D)));
138
161
  }
139
- function k() {
140
- T = !1, u === 0 && (u = requestAnimationFrame(h));
162
+ function l() {
163
+ B = !1, v === 0 && (v = requestAnimationFrame(D));
141
164
  }
142
- const v = S === "document" ? document : e;
143
- return v.addEventListener("mousemove", H), v.addEventListener("mouseleave", $), v.addEventListener("touchmove", X, { passive: !0 }), v.addEventListener("touchend", k), () => {
144
- C = !1, cancelAnimationFrame(u), N !== null && clearTimeout(N), v.removeEventListener("mousemove", H), v.removeEventListener("mouseleave", $), v.removeEventListener("touchmove", X), v.removeEventListener("touchend", k), e.innerHTML = r;
165
+ const s = b === "document" ? document : t;
166
+ return s.addEventListener("mousemove", k), s.addEventListener("mouseleave", J), s.addEventListener("touchmove", Q, { passive: !0 }), s.addEventListener("touchend", l), () => {
167
+ R = !1, cancelAnimationFrame(v), T !== null && clearTimeout(T), Y && Y.disconnect(), s.removeEventListener("mousemove", k), s.removeEventListener("mouseleave", J), s.removeEventListener("touchmove", Q), s.removeEventListener("touchend", l), t.innerHTML = n;
145
168
  };
146
169
  }
147
- function dt(e, r, o = {}) {
170
+ function ht(t, n, o = {}) {
171
+ var a, E;
148
172
  if (typeof window > "u") return () => {
149
173
  };
150
174
  if (window.matchMedia("(prefers-reduced-motion: reduce)").matches)
151
- return e.innerHTML = r, () => {
175
+ return t.innerHTML = n, () => {
152
176
  };
153
- const p = o.axes ?? j.axes, a = o.radius ?? j.radius, l = o.falloff ?? j.falloff, S = o.magnetMode ?? j.magnetMode, w = o.scope ?? j.scope, f = o.props, M = o.transitionMs ?? 0, L = window.scrollY;
154
- e.innerHTML = r;
155
- const Y = nt(e), E = [];
156
- for (const c of Y) {
157
- const t = c.textContent ?? "";
158
- if (!t.trim()) continue;
159
- const i = t.split(/(\S+)/), s = document.createDocumentFragment();
160
- for (let n = 0; n < i.length; n += 2) {
161
- const g = i[n], d = i[n + 1];
162
- if (!d) continue;
163
- const b = i[n + 3] === void 0 ? i[n + 2] ?? "" : "", A = document.createElement("span");
164
- A.className = G.word, A.textContent = g + d + b, s.appendChild(A), E.push(A);
177
+ const M = o.axes ?? U.axes, h = o.radius ?? U.radius, d = o.falloff ?? U.falloff, b = o.magnetMode ?? U.magnetMode, F = o.scope ?? U.scope, m = o.props, w = o.transitionMs ?? 0, L = o.cachePositions ?? !0, X = o.stabilizeLayout ?? !0, V = window.scrollY;
178
+ t.innerHTML = n;
179
+ const j = ct(t), x = [];
180
+ for (const c of j) {
181
+ const e = c.textContent ?? "";
182
+ if (!e.trim()) continue;
183
+ const g = e.split(/(\S+)/), i = document.createDocumentFragment();
184
+ for (let u = 0; u < g.length; u += 2) {
185
+ const O = g[u], S = g[u + 1];
186
+ if (!S) continue;
187
+ const C = g[u + 3] === void 0 ? g[u + 2] ?? "" : "", q = document.createElement("span");
188
+ q.className = nt.word, q.textContent = O + S + C, i.appendChild(q), x.push(q);
165
189
  }
166
- c.parentNode.replaceChild(s, c);
190
+ c.parentNode.replaceChild(i, c);
167
191
  }
168
192
  if (requestAnimationFrame(() => {
169
- typeof window < "u" && Math.abs(window.scrollY - L) > 2 && window.scrollTo({ top: L, behavior: "instant" });
170
- }), E.length === 0) return () => {
193
+ typeof window < "u" && Math.abs(window.scrollY - V) > 2 && window.scrollTo({ top: V, behavior: "instant" });
194
+ }), x.length === 0) return () => {
171
195
  };
172
- const q = getComputedStyle(e).fontVariationSettings, m = ot(
173
- q,
174
- Object.fromEntries(Object.entries(p).map(([c, [t]]) => [c, t]))
196
+ const y = getComputedStyle(t).fontVariationSettings, N = st(
197
+ y,
198
+ Object.fromEntries(Object.entries(M).map(([c, [e]]) => [c, e]))
175
199
  );
176
- E.forEach((c) => {
177
- c.style.fontVariationSettings = m, f && Q(c, f);
200
+ let H = 0;
201
+ if (X) {
202
+ const c = st(
203
+ y,
204
+ Object.fromEntries(Object.entries(M).map(([f, [, C]]) => [f, C]))
205
+ ), e = t.style.fontVariationSettings, g = t.style.whiteSpace, i = t.style.overflow;
206
+ t.style.whiteSpace = "nowrap", t.style.overflow = "visible", t.style.fontVariationSettings = c;
207
+ const u = t.scrollWidth;
208
+ t.style.fontVariationSettings = N;
209
+ const O = t.scrollWidth;
210
+ t.style.fontVariationSettings = e, t.style.whiteSpace = g, t.style.overflow = i;
211
+ const S = x.reduce(
212
+ (f, C) => {
213
+ var q;
214
+ return f + (((q = C.textContent) == null ? void 0 : q.replace(/\s+/g, "").length) ?? 0);
215
+ },
216
+ 0
217
+ );
218
+ S > 0 && u > O && (H = (u - O) / S);
219
+ }
220
+ x.forEach((c) => {
221
+ c.style.fontVariationSettings = N, m && ot(c, m);
178
222
  });
179
- let y = -9999, F = -9999, T = !1, u = 0, C = !0, N = null;
180
- function h() {
181
- if (!C) return;
182
- if (!T) {
183
- E.forEach((t) => {
184
- M > 0 && (t.style.transition = `font-variation-settings ${M}ms ease`), t.style.fontVariationSettings = m, f && Q(t, f);
185
- }), M > 0 && (N !== null && clearTimeout(N), N = setTimeout(() => {
186
- E.forEach((t) => {
187
- t.style.transition = "";
188
- }), N = null;
189
- }, M)), u = 0;
223
+ let _ = [], Y = !1;
224
+ function A() {
225
+ const c = window.scrollX, e = window.scrollY;
226
+ _ = x.map((g) => {
227
+ const i = g.getBoundingClientRect();
228
+ return { cx: (i.left + i.right) / 2 + c, cy: (i.top + i.bottom) / 2 + e };
229
+ }), Y = !0;
230
+ }
231
+ let $ = null;
232
+ L && (A(), $ = new ResizeObserver(() => {
233
+ Y = !1;
234
+ }), $.observe(t), (E = (a = document.fonts) == null ? void 0 : a.ready) == null || E.then(() => {
235
+ Y = !1;
236
+ }));
237
+ let B = -9999, v = -9999, R = !1, T = 0, D = !0, k = null;
238
+ function J() {
239
+ if (!D) return;
240
+ if (!R) {
241
+ x.forEach((i) => {
242
+ w > 0 && (i.style.transition = `font-variation-settings ${w}ms ease`), i.style.fontVariationSettings = N, X && (i.style.letterSpacing = ""), m && ot(i, m);
243
+ }), w > 0 && (k !== null && clearTimeout(k), k = setTimeout(() => {
244
+ x.forEach((i) => {
245
+ i.style.transition = "";
246
+ }), k = null;
247
+ }, w)), T = 0;
190
248
  return;
191
249
  }
192
- const c = E.map((t) => t.getBoundingClientRect());
193
- E.forEach((t, i) => {
194
- t.style.transition = "";
195
- const s = c[i], n = s.left + s.width / 2, g = s.top + s.height / 2, d = Math.sqrt((y - n) ** 2 + (F - g) ** 2), B = Math.max(0, 1 - d / a), b = l === "quadratic" ? B * B : B, A = S === "repel" ? 1 - b : b, I = {};
196
- for (const _ of Object.keys(p)) {
197
- const [D, V] = p[_] ?? [300, 500];
198
- I[_] = D + (V - D) * A;
250
+ L && !Y && A();
251
+ const c = L ? B + window.scrollX : B, e = L ? v + window.scrollY : v, g = L ? null : x.map((i) => i.getBoundingClientRect());
252
+ x.forEach((i, u) => {
253
+ i.style.transition = "";
254
+ let O, S;
255
+ if (L)
256
+ ({ cx: O, cy: S } = _[u]);
257
+ else {
258
+ const K = g[u];
259
+ O = K.left + K.width / 2, S = K.top + K.height / 2;
199
260
  }
200
- t.style.fontVariationSettings = ot(q, I), f && st(t, f, b);
201
- }), u = requestAnimationFrame(h);
261
+ const f = Math.sqrt((c - O) ** 2 + (e - S) ** 2), C = Math.max(0, 1 - f / h), q = d === "quadratic" ? C * C : C, P = b === "repel" ? 1 - q : q, Z = {};
262
+ for (const K of Object.keys(M)) {
263
+ const [G, ut] = M[K] ?? [300, 500];
264
+ Z[K] = G + (ut - G) * P;
265
+ }
266
+ i.style.fontVariationSettings = st(y, Z), X && H !== 0 && (i.style.letterSpacing = `${(-H * P).toFixed(3)}px`), m && at(i, m, q);
267
+ }), T = requestAnimationFrame(J);
202
268
  }
203
- function H(c) {
204
- y = c.clientX, F = c.clientY, T || (T = !0), u === 0 && (u = requestAnimationFrame(h));
269
+ function Q(c) {
270
+ B = c.clientX, v = c.clientY, R || (R = !0), T === 0 && (T = requestAnimationFrame(J));
205
271
  }
206
- function $() {
207
- T = !1, u === 0 && (u = requestAnimationFrame(h));
272
+ function l() {
273
+ R = !1, T === 0 && (T = requestAnimationFrame(J));
208
274
  }
209
- function X(c) {
210
- c.touches.length !== 0 && (y = c.touches[0].clientX, F = c.touches[0].clientY, T || (T = !0), u === 0 && (u = requestAnimationFrame(h)));
275
+ function s(c) {
276
+ c.touches.length !== 0 && (B = c.touches[0].clientX, v = c.touches[0].clientY, R || (R = !0), T === 0 && (T = requestAnimationFrame(J)));
211
277
  }
212
- function k() {
213
- T = !1, u === 0 && (u = requestAnimationFrame(h));
278
+ function p() {
279
+ R = !1, T === 0 && (T = requestAnimationFrame(J));
214
280
  }
215
- const v = w === "document" ? document : e;
216
- return v.addEventListener("mousemove", H), v.addEventListener("mouseleave", $), v.addEventListener("touchmove", X, { passive: !0 }), v.addEventListener("touchend", k), () => {
217
- C = !1, cancelAnimationFrame(u), N !== null && clearTimeout(N), v.removeEventListener("mousemove", H), v.removeEventListener("mouseleave", $), v.removeEventListener("touchmove", X), v.removeEventListener("touchend", k), e.innerHTML = r;
281
+ const r = F === "document" ? document : t;
282
+ return r.addEventListener("mousemove", Q), r.addEventListener("mouseleave", l), r.addEventListener("touchmove", s, { passive: !0 }), r.addEventListener("touchend", p), () => {
283
+ D = !1, cancelAnimationFrame(T), k !== null && clearTimeout(k), $ && $.disconnect(), r.removeEventListener("mousemove", Q), r.removeEventListener("mouseleave", l), r.removeEventListener("touchmove", s), r.removeEventListener("touchend", p), t.innerHTML = n;
218
284
  };
219
285
  }
220
- function mt(e) {
221
- const r = O(null), o = O(null), p = O(e);
222
- p.current = e;
223
- const a = O(null), l = e.mode ?? "field", { axes: S, radius: w, falloff: f, magnetMode: M, wdthBoost: L, scope: Y } = e, E = S ? JSON.stringify(S) : void 0, q = e.props ? JSON.stringify(e.props) : void 0, m = R(() => {
224
- const y = r.current;
286
+ function yt(t) {
287
+ const n = I(null), o = I(null), M = I(t);
288
+ M.current = t;
289
+ const h = I(null), d = t.mode ?? "field", { axes: b, radius: F, falloff: m, magnetMode: w, wdthBoost: L, scope: X } = t, V = b ? JSON.stringify(b) : void 0, j = t.props ? JSON.stringify(t.props) : void 0, x = z(() => {
290
+ const y = n.current;
225
291
  if (!y) return;
226
- o.current === null && (o.current = lt(y)), a.current && (a.current(), a.current = null), (p.current.mode ?? "field") === "field" ? a.current = dt(y, o.current, p.current) : a.current = ft(y, o.current, p.current);
227
- }, [l, E, w, f, M, L, Y, q]);
228
- return ut(() => (m(), () => {
229
- a.current && (a.current(), a.current = null);
230
- }), [m]), K(() => {
231
- document.fonts.ready.then(m);
232
- }, [m]), r;
292
+ o.current === null && (o.current = mt(y)), h.current && (h.current(), h.current = null), (M.current.mode ?? "field") === "field" ? h.current = ht(y, o.current, M.current) : h.current = pt(y, o.current, M.current);
293
+ }, [d, V, F, m, w, L, X, j]);
294
+ return ft(() => (x(), () => {
295
+ h.current && (h.current(), h.current = null);
296
+ }), [x]), W(() => {
297
+ var y, N;
298
+ (N = (y = document.fonts) == null ? void 0 : y.ready) == null || N.then(x);
299
+ }, [x]), n;
233
300
  }
234
- const pt = ct(
235
- function({ children: r, as: o = "p", className: p, style: a, ...l }, S) {
236
- const w = mt(l), f = R(
237
- (M) => {
238
- w.current = M, typeof S == "function" ? S(M) : S && (S.current = M);
301
+ const gt = lt(
302
+ function({ children: n, as: o = "p", className: M, style: h, ...d }, b) {
303
+ const F = yt(d), m = z(
304
+ (w) => {
305
+ F.current = w, typeof b == "function" ? b(w) : b && (b.current = w);
239
306
  },
240
307
  // eslint-disable-next-line react-hooks/exhaustive-deps
241
- [S]
308
+ [b]
242
309
  );
243
- return /* @__PURE__ */ U(o, { ref: f, className: p, style: a, children: r });
310
+ return /* @__PURE__ */ tt(o, { ref: m, className: M, style: h, children: n });
244
311
  }
245
312
  );
246
- pt.displayName = "MagnetTypeText";
247
- const ht = ct(
313
+ gt.displayName = "MagnetTypeText";
314
+ const vt = lt(
248
315
  function({
249
- children: r,
316
+ children: n,
250
317
  as: o = "p",
251
- className: p,
252
- style: a,
253
- minWeight: l = 300,
254
- maxWeight: S = 600,
255
- proximityRadius: w,
256
- spreadRadius: f,
257
- fixedAxes: M = {},
318
+ className: M,
319
+ style: h,
320
+ minWeight: d = 300,
321
+ maxWeight: b = 600,
322
+ proximityRadius: F,
323
+ spreadRadius: m,
324
+ fixedAxes: w = {},
258
325
  cachePositions: L = !0,
259
- rafThrottle: Y = !0
260
- }, E) {
261
- const q = O(null), m = O(null), y = O([]), F = O([]), T = O(null), u = O(!1), C = O(null), N = R(
262
- (t) => {
263
- q.current = t, typeof E == "function" ? E(t) : E && (E.current = t);
326
+ rafThrottle: X = !0,
327
+ stabilizeLayout: V = !0
328
+ }, j) {
329
+ const x = I(null), y = I(null), N = I([]), H = I([]), _ = I(null), Y = I(!1), A = I(null), $ = I(0), B = z(
330
+ (l) => {
331
+ x.current = l, typeof j == "function" ? j(l) : j && (j.current = l);
264
332
  },
265
333
  // eslint-disable-next-line react-hooks/exhaustive-deps
266
- [E]
334
+ [j]
267
335
  );
268
- function h(t) {
269
- const i = [`'wght' ${t.toFixed(0)}`];
270
- for (const [s, n] of Object.entries(M)) i.push(`'${s}' ${n}`);
271
- return i.join(", ");
336
+ function v(l) {
337
+ const s = [`'wght' ${l.toFixed(0)}`];
338
+ for (const [p, r] of Object.entries(w)) s.push(`'${p}' ${r}`);
339
+ return s.join(", ");
272
340
  }
273
- function H() {
274
- const t = q.current;
275
- if (!t) return;
276
- const i = window.scrollX, s = window.scrollY, n = t.getBoundingClientRect();
277
- T.current = {
278
- top: n.top + s,
279
- left: n.left + i,
280
- right: n.right + i,
281
- bottom: n.bottom + s
282
- }, F.current = y.current.map((g) => {
283
- if (!g) return { cx: 0, cy: 0 };
284
- const d = g.getBoundingClientRect();
341
+ function R() {
342
+ const l = x.current;
343
+ if (!l) return;
344
+ const s = window.scrollX, p = window.scrollY, r = l.getBoundingClientRect();
345
+ _.current = {
346
+ top: r.top + p,
347
+ left: r.left + s,
348
+ right: r.right + s,
349
+ bottom: r.bottom + p
350
+ }, H.current = N.current.map((a) => {
351
+ if (!a) return { cx: 0, cy: 0 };
352
+ const E = a.getBoundingClientRect();
285
353
  return {
286
- cx: (d.left + d.right) / 2 + i,
287
- cy: (d.top + d.bottom) / 2 + s
354
+ cx: (E.left + E.right) / 2 + s,
355
+ cy: (E.top + E.bottom) / 2 + p
288
356
  };
289
- }), u.current = !0;
357
+ }), Y.current = !0;
290
358
  }
291
- function $(t, i) {
292
- const s = q.current;
293
- if (!s) return;
294
- L && !u.current && H();
295
- let n, g, d, B, b, A;
296
- if (L && T.current)
297
- b = t + window.scrollX, A = i + window.scrollY, { top: n, left: g, right: d, bottom: B } = T.current;
359
+ function T(l, s) {
360
+ const p = x.current;
361
+ if (!p) return;
362
+ L && !Y.current && R();
363
+ let r, a, E, c, e, g;
364
+ if (L && _.current)
365
+ e = l + window.scrollX, g = s + window.scrollY, { top: r, left: a, right: E, bottom: c } = _.current;
298
366
  else {
299
- const V = s.getBoundingClientRect();
300
- b = t, A = i, n = V.top, g = V.left, d = V.right, B = V.bottom;
367
+ const S = p.getBoundingClientRect();
368
+ e = l, g = s, r = S.top, a = S.left, E = S.right, c = S.bottom;
301
369
  }
302
- const I = Math.max(g - b, 0, b - d), _ = Math.max(n - A, 0, A - B), D = Math.sqrt(I * I + _ * _);
303
- if (w !== void 0 && !f) {
304
- const x = 1 - (1 - Math.max(0, 1 - D / w)) ** 2;
305
- s.style.fontVariationSettings = h(l + (S - l) * x);
370
+ const i = Math.max(a - e, 0, e - E), u = Math.max(r - g, 0, g - c), O = Math.sqrt(i * i + u * u);
371
+ if (F !== void 0 && !m) {
372
+ const f = 1 - (1 - Math.max(0, 1 - O / F)) ** 2;
373
+ p.style.fontVariationSettings = v(d + (b - d) * f);
306
374
  return;
307
375
  }
308
- if (f) {
309
- if (w !== void 0 && D > w) {
310
- s.style.fontVariationSettings = h(l);
311
- for (const x of y.current) x && (x.style.fontVariationSettings = h(l));
376
+ if (m) {
377
+ if (F !== void 0 && O > F) {
378
+ p.style.fontVariationSettings = v(d);
379
+ for (const f of N.current)
380
+ f && (f.style.fontVariationSettings = v(d), V && (f.style.letterSpacing = ""));
312
381
  return;
313
382
  }
314
- if (D > f) {
315
- for (const x of y.current) x && (x.style.fontVariationSettings = h(l));
383
+ if (O > m) {
384
+ for (const f of N.current)
385
+ f && (f.style.fontVariationSettings = v(d), V && (f.style.letterSpacing = ""));
316
386
  return;
317
387
  }
318
- const V = y.current;
319
- if (L && F.current.length === V.length)
320
- for (let x = 0; x < V.length; x++) {
321
- const J = V[x];
322
- if (!J) continue;
323
- const { cx: Z, cy: P } = F.current[x], W = Math.sqrt((b - Z) ** 2 + (A - P) ** 2), tt = 1 - (1 - Math.max(0, 1 - W / f)) ** 2;
324
- J.style.fontVariationSettings = h(l + (S - l) * tt);
388
+ const S = N.current;
389
+ if (L && H.current.length === S.length)
390
+ for (let f = 0; f < S.length; f++) {
391
+ const C = S[f];
392
+ if (!C) continue;
393
+ const { cx: q, cy: P } = H.current[f], Z = Math.sqrt((e - q) ** 2 + (g - P) ** 2), G = 1 - (1 - Math.max(0, 1 - Z / m)) ** 2;
394
+ C.style.fontVariationSettings = v(d + (b - d) * G), V && $.current !== 0 && (C.style.letterSpacing = `${(-$.current * G).toFixed(3)}px`);
325
395
  }
326
396
  else
327
- for (const x of V) {
328
- if (!x) continue;
329
- const J = x.getBoundingClientRect(), Z = (J.left + J.right) / 2, P = (J.top + J.bottom) / 2, W = Math.sqrt((t - Z) ** 2 + (i - P) ** 2), tt = 1 - (1 - Math.max(0, 1 - W / f)) ** 2;
330
- x.style.fontVariationSettings = h(l + (S - l) * tt);
397
+ for (const f of S) {
398
+ if (!f) continue;
399
+ const C = f.getBoundingClientRect(), q = (C.left + C.right) / 2, P = (C.top + C.bottom) / 2, Z = Math.sqrt((l - q) ** 2 + (s - P) ** 2), G = 1 - (1 - Math.max(0, 1 - Z / m)) ** 2;
400
+ f.style.fontVariationSettings = v(d + (b - d) * G), V && $.current !== 0 && (f.style.letterSpacing = `${(-$.current * G).toFixed(3)}px`);
331
401
  }
332
402
  }
333
403
  }
334
- const X = R(
335
- (t) => {
336
- m.current = { x: t.clientX, y: t.clientY }, Y ? C.current === null && (C.current = requestAnimationFrame(() => {
337
- C.current = null, m.current && $(m.current.x, m.current.y);
338
- })) : $(t.clientX, t.clientY);
404
+ const D = z(
405
+ (l) => {
406
+ y.current = { x: l.clientX, y: l.clientY }, X ? A.current === null && (A.current = requestAnimationFrame(() => {
407
+ A.current = null, y.current && T(y.current.x, y.current.y);
408
+ })) : T(l.clientX, l.clientY);
339
409
  },
340
410
  // eslint-disable-next-line react-hooks/exhaustive-deps
341
- [l, S, w, f, L, Y, JSON.stringify(M)]
342
- ), k = R(
411
+ [d, b, F, m, L, X, V, JSON.stringify(w)]
412
+ ), k = z(
343
413
  () => {
344
- m.current && $(m.current.x, m.current.y);
414
+ y.current && T(y.current.x, y.current.y);
345
415
  },
346
416
  // eslint-disable-next-line react-hooks/exhaustive-deps
347
- [l, S, w, f, L, JSON.stringify(M)]
348
- ), v = R(
417
+ [d, b, F, m, L, V, JSON.stringify(w)]
418
+ ), J = z(
349
419
  () => {
350
- m.current = null, C.current !== null && (cancelAnimationFrame(C.current), C.current = null);
351
- const t = q.current;
352
- t && (t.style.fontVariationSettings = h(l));
353
- for (const i of y.current) i && (i.style.fontVariationSettings = h(l));
420
+ y.current = null, A.current !== null && (cancelAnimationFrame(A.current), A.current = null);
421
+ const l = x.current;
422
+ l && (l.style.fontVariationSettings = v(d));
423
+ for (const s of N.current)
424
+ s && (s.style.fontVariationSettings = v(d), V && (s.style.letterSpacing = ""));
354
425
  },
355
426
  // eslint-disable-next-line react-hooks/exhaustive-deps
356
- [l, JSON.stringify(M)]
427
+ [d, V, JSON.stringify(w)]
357
428
  );
358
- K(() => (window.addEventListener("mousemove", X, { passive: !0 }), window.addEventListener("scroll", k, { passive: !0, capture: !0 }), document.documentElement.addEventListener("mouseleave", v), () => {
359
- window.removeEventListener("mousemove", X), window.removeEventListener("scroll", k, { capture: !0 }), document.documentElement.removeEventListener("mouseleave", v), C.current !== null && (cancelAnimationFrame(C.current), C.current = null);
360
- }), [X, k, v]), K(() => {
361
- if (!L || !f) return;
362
- u.current = !1;
363
- const t = q.current;
364
- if (!t) return;
365
- const i = new ResizeObserver(() => {
366
- u.current = !1;
429
+ W(() => (window.addEventListener("mousemove", D, { passive: !0 }), window.addEventListener("scroll", k, { passive: !0, capture: !0 }), document.documentElement.addEventListener("mouseleave", J), () => {
430
+ window.removeEventListener("mousemove", D), window.removeEventListener("scroll", k, { capture: !0 }), document.documentElement.removeEventListener("mouseleave", J), A.current !== null && (cancelAnimationFrame(A.current), A.current = null);
431
+ }), [D, k, J]), W(() => {
432
+ var p, r;
433
+ if (!L || !m) return;
434
+ Y.current = !1;
435
+ const l = x.current;
436
+ if (!l) return;
437
+ const s = new ResizeObserver(() => {
438
+ Y.current = !1;
367
439
  });
368
- return i.observe(t), document.fonts.ready.then(() => {
369
- u.current = !1;
370
- }), () => i.disconnect();
371
- }, [L, f]), K(() => {
372
- u.current = !1;
373
- }, [r, f]);
374
- const c = at(() => {
375
- if (!f) return r;
376
- y.current = [];
377
- let t = 0;
378
- function i(s) {
379
- if (typeof s == "string")
380
- return [...s].map((n) => {
381
- if (/\s/.test(n)) return n;
382
- const g = t++;
383
- return /* @__PURE__ */ U(
440
+ return s.observe(l), (r = (p = document.fonts) == null ? void 0 : p.ready) == null || r.then(() => {
441
+ Y.current = !1;
442
+ }), () => s.disconnect();
443
+ }, [L, m]), W(() => {
444
+ Y.current = !1;
445
+ }, [n, m]), W(() => {
446
+ var s, p;
447
+ if (!V || !m) {
448
+ $.current = 0;
449
+ return;
450
+ }
451
+ function l() {
452
+ const r = x.current;
453
+ if (!r) return;
454
+ const a = r.textContent ?? "", E = a.replace(/\s/g, "").length;
455
+ if (E === 0) return;
456
+ const c = getComputedStyle(r), e = document.createElement("span");
457
+ e.style.cssText = "position:fixed;top:-9999px;left:-9999px;visibility:hidden;white-space:nowrap;pointer-events:none;", e.style.fontFamily = c.fontFamily, e.style.fontSize = c.fontSize, e.style.fontWeight = c.fontWeight, e.style.lineHeight = c.lineHeight, e.style.letterSpacing = c.letterSpacing, e.textContent = a, document.body.appendChild(e);
458
+ const g = (O) => {
459
+ const S = [`'wght' ${O.toFixed(0)}`];
460
+ for (const [f, C] of Object.entries(w)) S.push(`'${f}' ${C}`);
461
+ return S.join(", ");
462
+ };
463
+ e.style.fontVariationSettings = g(b);
464
+ const i = e.scrollWidth;
465
+ e.style.fontVariationSettings = g(d);
466
+ const u = e.scrollWidth;
467
+ document.body.removeChild(e), $.current = i > u ? (i - u) / E : 0;
468
+ }
469
+ l(), (p = (s = document.fonts) == null ? void 0 : s.ready) == null || p.then(l);
470
+ }, [V, m, d, b, n, JSON.stringify(w)]);
471
+ const Q = dt(() => {
472
+ if (!m) return n;
473
+ N.current = [];
474
+ let l = 0;
475
+ function s(p) {
476
+ if (typeof p == "string")
477
+ return [...p].map((r) => {
478
+ if (/\s/.test(r)) return r;
479
+ const a = l++;
480
+ return /* @__PURE__ */ tt(
384
481
  "span",
385
482
  {
386
- ref: (d) => {
387
- y.current[g] = d;
483
+ ref: (E) => {
484
+ N.current[a] = E;
388
485
  },
389
- style: { fontVariationSettings: h(l) },
390
- children: n
486
+ style: { fontVariationSettings: v(d) },
487
+ children: r
391
488
  },
392
- g
489
+ a
393
490
  );
394
491
  });
395
- if (Array.isArray(s)) return s.map((n, g) => /* @__PURE__ */ U(et.Fragment, { children: i(n) }, g));
396
- if (et.isValidElement(s)) {
397
- const n = s;
398
- if (n.props.children !== void 0)
399
- return et.cloneElement(n, {}, i(n.props.children));
492
+ if (Array.isArray(p)) return p.map((r, a) => /* @__PURE__ */ tt(rt.Fragment, { children: s(r) }, a));
493
+ if (rt.isValidElement(p)) {
494
+ const r = p;
495
+ if (r.props.children !== void 0)
496
+ return rt.cloneElement(r, {}, s(r.props.children));
400
497
  }
401
- return s;
498
+ return p;
402
499
  }
403
- return i(r);
404
- }, [r, f, l, JSON.stringify(M)]);
405
- return /* @__PURE__ */ U(
500
+ return s(n);
501
+ }, [n, m, d, JSON.stringify(w)]);
502
+ return /* @__PURE__ */ tt(
406
503
  o,
407
504
  {
408
- ref: N,
409
- className: p,
410
- style: { fontVariationSettings: h(l), ...a },
411
- children: c
505
+ ref: B,
506
+ className: M,
507
+ style: { fontVariationSettings: v(d), ...h },
508
+ children: Q
412
509
  }
413
510
  );
414
511
  }
415
512
  );
416
- ht.displayName = "MagnetBlock";
513
+ vt.displayName = "MagnetBlock";
417
514
  export {
418
- G as MAGNET_TYPE_CLASSES,
419
- ht as MagnetBlock,
420
- pt as MagnetTypeText,
421
- ft as applyMagnetType,
422
- lt as getCleanHTML,
423
- gt as removeMagnetType,
424
- dt as startMagnetType,
425
- mt as useMagnetType
515
+ nt as MAGNET_TYPE_CLASSES,
516
+ vt as MagnetBlock,
517
+ gt as MagnetTypeText,
518
+ pt as applyMagnetType,
519
+ mt as getCleanHTML,
520
+ Mt as removeMagnetType,
521
+ ht as startMagnetType,
522
+ yt as useMagnetType
426
523
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liiift-studio/magnettype",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Cursor-field per-word variable font axis variation and per-character legibility mode",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",