# Springy draggable letters

> Drag one letter and the others follow as if joined by springs; let go and they all wobble back into place.

- Category: [Text & scroll](https://library.aimran.es/components/text-scroll)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-tx-spring-letters
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Each letter of this headline is a small spring simulation: grab any of them and the neighbors stretch after it with less force the further away they are, then bounce home with a little overshoot. A few lines of JavaScript drive it with transforms only. It brings play to portfolio intros, kids' brands and toy-like product pages.

Change the word in the text field and the letters rebuild on their own. The button shakes the whole word, and the arrow keys push it in that direction, so the effect also works without a mouse. With reduced motion the springs barely bounce.

## Code

### HTML

```html
<div class="uvq14-spr"><h3 class="sr">Elastic</h3><div class="st" aria-hidden="true" translate="no">Elastic</div><div class="ft"><span class="hi" aria-hidden="true">Drag the letters</span><button type="button" class="kk" aria-describedby="uvq14-spr-h">Shake</button><span class="sr" id="uvq14-spr-h">Also works with the arrow keys</span></div></div>
```

### CSS

```css
.uvq14-spr {
  --c1: #fb923c;
  --c2: #9a3412;
  --s: 16px;
  /* tamaño */
  --k: 1;
  /* velocidad */
  --fg: #1d1d1f;
  /* texto */
  --bg: #f5f5f7;
  /* fondo */
}

.uvq14-spr {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 20em;
  display: grid;
  justify-items: center;
  gap: 1.1em;
}

.uvq14-spr .st {
  display: flex;
  justify-content: center;
  flex-wrap: nowrap;
  font-size: calc(var(--s) * 3.6);
  font-weight: 900;
  letter-spacing: -.02em;
  line-height: 1;
  color: var(--c1);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.uvq14-spr .st:active {
  cursor: grabbing;
}

.uvq14-spr .st b {
  display: inline-block;
  font-weight: inherit;
  text-shadow: 0 .05em 0 var(--c2),0 .1em .12em color-mix(in srgb,var(--c2) 45%,transparent);
  will-change: transform;
}

.uvq14-spr .ft {
  display: flex;
  align-items: center;
  gap: .8em;
}

.uvq14-spr .hi {
  font-size: .75em;
  font-weight: 600;
  color: color-mix(in srgb,var(--fg) 58%,transparent);
}

.uvq14-spr .kk {
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  padding: .55em 1.1em;
  border-radius: 2em;
  font-size: .78em;
  font-weight: 700;
  color: var(--bg);
  background: var(--fg);
  transition: transform calc(0.25s / var(--k)) cubic-bezier(.34,1.56,.64,1),opacity calc(0.2s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uvq14-spr .kk:hover {
  transform: translateY(-2px);
}

.uvq14-spr .kk:active {
  transform: scale(.95);
}

.uvq14-spr .kk:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}

.uvq14-spr .sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
```

### JavaScript

```js
document.querySelectorAll(".uvq14-spr").forEach(el => {
  const sr=el.querySelector("h3.sr"),st=el.querySelector(".st"),kk=el.querySelector(".kk");const rm=matchMedia("(prefers-reduced-motion: reduce)").matches;const K=rm?.2:.1,D=rm?.85:.16;let L=[],raf=0,drag=null;
const build=()=>{st.textContent="";L=[...sr.textContent.trim()].map(c=>{const b=document.createElement("b");b.textContent=c===" "?"\u00a0":c;st.appendChild(b);return{b,x:0,y:0,vx:0,vy:0,tx:0,ty:0}})};
const step=()=>{let live=!!drag;L.forEach(o=>{o.vx+=(o.tx-o.x)*K-o.vx*D;o.vy+=(o.ty-o.y)*K-o.vy*D;o.x+=o.vx;o.y+=o.vy;if(Math.abs(o.vx)+Math.abs(o.vy)+Math.abs(o.tx-o.x)+Math.abs(o.ty-o.y)>.05)live=true;o.b.style.transform="translate("+o.x.toFixed(2)+"px,"+o.y.toFixed(2)+"px) rotate("+Math.max(-25,Math.min(25,o.vx*1.6)).toFixed(2)+"deg)"});raf=live?requestAnimationFrame(step):0};
const run=()=>{if(!raf)raf=requestAnimationFrame(step)};
const scale=()=>el.getBoundingClientRect().width/el.offsetWidth||1;
st.addEventListener("pointerdown",e=>{if(!L.length)return;let g=0,bd=1e9;L.forEach((o,i)=>{const r=o.b.getBoundingClientRect(),d=Math.abs(r.left+r.width/2-e.clientX);if(d<bd){bd=d;g=i}});drag={g,x:e.clientX,y:e.clientY,s:scale()};st.setPointerCapture(e.pointerId);run()});
st.addEventListener("pointermove",e=>{if(!drag)return;const dx=Math.max(-140,Math.min(140,(e.clientX-drag.x)/drag.s)),dy=Math.max(-90,Math.min(90,(e.clientY-drag.y)/drag.s));L.forEach((o,i)=>{const f=Math.exp(-Math.abs(i-drag.g)*.45);o.tx=dx*f;o.ty=dy*f});run()});
const up=()=>{drag=null;L.forEach(o=>{o.tx=0;o.ty=0});run()};st.addEventListener("pointerup",up);st.addEventListener("pointercancel",up);
const kick=(dx,dy)=>{L.forEach((o,i)=>{const w=dx||dy?1-i/(L.length*1.6):1;o.vx+=dx*w*(rm?.3:1);o.vy+=(dy?dy*w:-9*Math.sin(i*.9+1))*(rm?.3:1)});run()};
kk.addEventListener("click",()=>kick(0,0));
kk.addEventListener("keydown",e=>{const m={ArrowLeft:[-12,0],ArrowRight:[12,0],ArrowUp:[0,-12],ArrowDown:[0,12]}[e.key];if(m){e.preventDefault();kick(m[0],m[1])}});
build();const mo=new MutationObserver(build);el.querySelectorAll("h3.sr").forEach(n=>mo.observe(n,{childList:true,characterData:true,subtree:true}));return()=>{mo.disconnect();cancelAnimationFrame(raf)}
});
```

### React

```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uvq14-spr {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 20em;
  display: grid;
  justify-items: center;
  gap: 1.1em;
}

.uvq14-spr .st {
  display: flex;
  justify-content: center;
  flex-wrap: nowrap;
  font-size: calc(var(--s) * 3.6);
  font-weight: 900;
  letter-spacing: -.02em;
  line-height: 1;
  color: var(--c1);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.uvq14-spr .st:active {
  cursor: grabbing;
}

.uvq14-spr .st b {
  display: inline-block;
  font-weight: inherit;
  text-shadow: 0 .05em 0 var(--c2),0 .1em .12em color-mix(in srgb,var(--c2) 45%,transparent);
  will-change: transform;
}

.uvq14-spr .ft {
  display: flex;
  align-items: center;
  gap: .8em;
}

.uvq14-spr .hi {
  font-size: .75em;
  font-weight: 600;
  color: color-mix(in srgb,var(--fg) 58%,transparent);
}

.uvq14-spr .kk {
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  padding: .55em 1.1em;
  border-radius: 2em;
  font-size: .78em;
  font-weight: 700;
  color: var(--bg);
  background: var(--fg);
  transition: transform calc(0.25s / var(--k)) cubic-bezier(.34,1.56,.64,1),opacity calc(0.2s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uvq14-spr .kk:hover {
  transform: translateY(-2px);
}

.uvq14-spr .kk:active {
  transform: scale(.95);
}

.uvq14-spr .kk:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}

.uvq14-spr .sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const sr=el.querySelector("h3.sr"),st=el.querySelector(".st"),kk=el.querySelector(".kk");const rm=matchMedia("(prefers-reduced-motion: reduce)").matches;const K=rm?.2:.1,D=rm?.85:.16;let L=[],raf=0,drag=null;
const build=()=>{st.textContent="";L=[...sr.textContent.trim()].map(c=>{const b=document.createElement("b");b.textContent=c===" "?"\u00a0":c;st.appendChild(b);return{b,x:0,y:0,vx:0,vy:0,tx:0,ty:0}})};
const step=()=>{let live=!!drag;L.forEach(o=>{o.vx+=(o.tx-o.x)*K-o.vx*D;o.vy+=(o.ty-o.y)*K-o.vy*D;o.x+=o.vx;o.y+=o.vy;if(Math.abs(o.vx)+Math.abs(o.vy)+Math.abs(o.tx-o.x)+Math.abs(o.ty-o.y)>.05)live=true;o.b.style.transform="translate("+o.x.toFixed(2)+"px,"+o.y.toFixed(2)+"px) rotate("+Math.max(-25,Math.min(25,o.vx*1.6)).toFixed(2)+"deg)"});raf=live?requestAnimationFrame(step):0};
const run=()=>{if(!raf)raf=requestAnimationFrame(step)};
const scale=()=>el.getBoundingClientRect().width/el.offsetWidth||1;
st.addEventListener("pointerdown",e=>{if(!L.length)return;let g=0,bd=1e9;L.forEach((o,i)=>{const r=o.b.getBoundingClientRect(),d=Math.abs(r.left+r.width/2-e.clientX);if(d<bd){bd=d;g=i}});drag={g,x:e.clientX,y:e.clientY,s:scale()};st.setPointerCapture(e.pointerId);run()});
st.addEventListener("pointermove",e=>{if(!drag)return;const dx=Math.max(-140,Math.min(140,(e.clientX-drag.x)/drag.s)),dy=Math.max(-90,Math.min(90,(e.clientY-drag.y)/drag.s));L.forEach((o,i)=>{const f=Math.exp(-Math.abs(i-drag.g)*.45);o.tx=dx*f;o.ty=dy*f});run()});
const up=()=>{drag=null;L.forEach(o=>{o.tx=0;o.ty=0});run()};st.addEventListener("pointerup",up);st.addEventListener("pointercancel",up);
const kick=(dx,dy)=>{L.forEach((o,i)=>{const w=dx||dy?1-i/(L.length*1.6):1;o.vx+=dx*w*(rm?.3:1);o.vy+=(dy?dy*w:-9*Math.sin(i*.9+1))*(rm?.3:1)});run()};
kk.addEventListener("click",()=>kick(0,0));
kk.addEventListener("keydown",e=>{const m={ArrowLeft:[-12,0],ArrowRight:[12,0],ArrowUp:[0,-12],ArrowDown:[0,12]}[e.key];if(m){e.preventDefault();kick(m[0],m[1])}});
build();const mo=new MutationObserver(build);el.querySelectorAll("h3.sr").forEach(n=>mo.observe(n,{childList:true,characterData:true,subtree:true}));return()=>{mo.disconnect();cancelAnimationFrame(raf)}
}

export default function TxSpringLetters() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#fb923c",
          "--c2": "#9a3412",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <div className="uvq14-spr"><h3 className="sr">Elastic</h3><div className="st" aria-hidden="true" translate="no">Elastic</div><div className="ft"><span className="hi" aria-hidden="true">Drag the letters</span><button type="button" className="kk" aria-describedby="uvq14-spr-h">Shake</button><span className="sr" id="uvq14-spr-h">Also works with the arrow keys</span></div></div>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Springy draggable letters” block (Text & scroll) from AIMRAN Library to my project.

## What it is
Drag one letter and the others follow as if joined by springs; let go and they all wobble back into place.

## Chosen settings
- Main color: #fb923c
- Secondary color: #9a3412
- Size: 16px (from 12 to 24px)
- Speed: 1x (from 0.5 to 2x)
- Text: "Elástico"

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uvq14-spr {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 20em;
  display: grid;
  justify-items: center;
  gap: 1.1em;
}

.uvq14-spr .st {
  display: flex;
  justify-content: center;
  flex-wrap: nowrap;
  font-size: calc(var(--s) * 3.6);
  font-weight: 900;
  letter-spacing: -.02em;
  line-height: 1;
  color: var(--c1);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.uvq14-spr .st:active {
  cursor: grabbing;
}

.uvq14-spr .st b {
  display: inline-block;
  font-weight: inherit;
  text-shadow: 0 .05em 0 var(--c2),0 .1em .12em color-mix(in srgb,var(--c2) 45%,transparent);
  will-change: transform;
}

.uvq14-spr .ft {
  display: flex;
  align-items: center;
  gap: .8em;
}

.uvq14-spr .hi {
  font-size: .75em;
  font-weight: 600;
  color: color-mix(in srgb,var(--fg) 58%,transparent);
}

.uvq14-spr .kk {
  font: inherit;
  color: inherit;
  border: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  padding: .55em 1.1em;
  border-radius: 2em;
  font-size: .78em;
  font-weight: 700;
  color: var(--bg);
  background: var(--fg);
  transition: transform calc(0.25s / var(--k)) cubic-bezier(.34,1.56,.64,1),opacity calc(0.2s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uvq14-spr .kk:hover {
  transform: translateY(-2px);
}

.uvq14-spr .kk:active {
  transform: scale(.95);
}

.uvq14-spr .kk:focus-visible {
  outline: 2px solid var(--c1);
  outline-offset: 3px;
}

.uvq14-spr .sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const sr=el.querySelector("h3.sr"),st=el.querySelector(".st"),kk=el.querySelector(".kk");const rm=matchMedia("(prefers-reduced-motion: reduce)").matches;const K=rm?.2:.1,D=rm?.85:.16;let L=[],raf=0,drag=null;
const build=()=>{st.textContent="";L=[...sr.textContent.trim()].map(c=>{const b=document.createElement("b");b.textContent=c===" "?"\u00a0":c;st.appendChild(b);return{b,x:0,y:0,vx:0,vy:0,tx:0,ty:0}})};
const step=()=>{let live=!!drag;L.forEach(o=>{o.vx+=(o.tx-o.x)*K-o.vx*D;o.vy+=(o.ty-o.y)*K-o.vy*D;o.x+=o.vx;o.y+=o.vy;if(Math.abs(o.vx)+Math.abs(o.vy)+Math.abs(o.tx-o.x)+Math.abs(o.ty-o.y)>.05)live=true;o.b.style.transform="translate("+o.x.toFixed(2)+"px,"+o.y.toFixed(2)+"px) rotate("+Math.max(-25,Math.min(25,o.vx*1.6)).toFixed(2)+"deg)"});raf=live?requestAnimationFrame(step):0};
const run=()=>{if(!raf)raf=requestAnimationFrame(step)};
const scale=()=>el.getBoundingClientRect().width/el.offsetWidth||1;
st.addEventListener("pointerdown",e=>{if(!L.length)return;let g=0,bd=1e9;L.forEach((o,i)=>{const r=o.b.getBoundingClientRect(),d=Math.abs(r.left+r.width/2-e.clientX);if(d<bd){bd=d;g=i}});drag={g,x:e.clientX,y:e.clientY,s:scale()};st.setPointerCapture(e.pointerId);run()});
st.addEventListener("pointermove",e=>{if(!drag)return;const dx=Math.max(-140,Math.min(140,(e.clientX-drag.x)/drag.s)),dy=Math.max(-90,Math.min(90,(e.clientY-drag.y)/drag.s));L.forEach((o,i)=>{const f=Math.exp(-Math.abs(i-drag.g)*.45);o.tx=dx*f;o.ty=dy*f});run()});
const up=()=>{drag=null;L.forEach(o=>{o.tx=0;o.ty=0});run()};st.addEventListener("pointerup",up);st.addEventListener("pointercancel",up);
const kick=(dx,dy)=>{L.forEach((o,i)=>{const w=dx||dy?1-i/(L.length*1.6):1;o.vx+=dx*w*(rm?.3:1);o.vy+=(dy?dy*w:-9*Math.sin(i*.9+1))*(rm?.3:1)});run()};
kk.addEventListener("click",()=>kick(0,0));
kk.addEventListener("keydown",e=>{const m={ArrowLeft:[-12,0],ArrowRight:[12,0],ArrowUp:[0,-12],ArrowDown:[0,12]}[e.key];if(m){e.preventDefault();kick(m[0],m[1])}});
build();const mo=new MutationObserver(build);el.querySelectorAll("h3.sr").forEach(n=>mo.observe(n,{childList:true,characterData:true,subtree:true}));return()=>{mo.disconnect();cancelAnimationFrame(raf)}
}

export default function TxSpringLetters() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#fb923c",
          "--c2": "#9a3412",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <div className="uvq14-spr"><h3 className="sr">Elastic</h3><div className="st" aria-hidden="true" translate="no">Elastic</div><div className="ft"><span className="hi" aria-hidden="true">Drag the letters</span><button type="button" className="kk" aria-describedby="uvq14-spr-h">Shake</button><span className="sr" id="uvq14-spr-h">Also works with the arrow keys</span></div></div>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
