Skip to content
Motion · Feedback

Elastic string

Pluck a line: it follows you, then rings back to rest.

Installation

  1. Run the CLI

    Run pnpm dlx asta-ui@latest init once, then this. The file lands in components/astabyte/ and the colour tokens go into your CSS.

    $ pnpm dlx asta-ui@latest add elastic-string

    Or with the shadcn CLI: pnpm dlx shadcn@latest add https://ui.astabyte.com/r/elastic-string.json

  2. Or install the package

    Don’t want the file? Import from asta-ui/elastic-string and update with pnpm update asta-ui. Add the stylesheet once. More.

    $ pnpm add asta-ui
    import { ElasticString } from "asta-ui/elastic-string";
  3. Or copy it by hand

    Copy these files. Keep the paths, or fix the @/ imports. Then add the tokens from globals.css.

    "use client";
    
    import { useEffect, useRef } from "react";
    import { useInView } from "../lab-hooks";
    
    /** A string you can pluck: the midpoint follows the pointer, then springs back with a wobble. */
    export default function ElasticString() {
      const box = useRef<HTMLDivElement>(null);
      const path = useRef<SVGPathElement>(null);
      const visible = useInView(box);
    
      useEffect(() => {
        // Off screen or in a hidden tab: no listeners, no frames.
        if (!visible) return;
        const el = box.current!;
        let w = el.clientWidth;
        let h = el.clientHeight;
        let cx = w / 2;
        let cy = h / 2;
        let vx = 0;
        let vy = 0;
        let target: { x: number; y: number } | null = null;
        let raf = 0;
        // The box's position, measured once and dropped on scroll or resize, so pointermove never reads layout.
        let rect: DOMRect | null = null;
    
        const draw = () => {
          const left = w * 0.12;
          const right = w * 0.88;
          path.current?.setAttribute("d", `M${left},${h / 2} Q${cx},${cy} ${right},${h / 2}`);
        };
        const loop = () => {
          const tx = target ? target.x : w / 2;
          const ty = target ? target.y : h / 2;
          vx += (tx - cx) * (target ? 0.25 : 0.12);
          vy += (ty - cy) * (target ? 0.25 : 0.12);
          vx *= target ? 0.6 : 0.86;
          vy *= target ? 0.6 : 0.86;
          cx += vx;
          cy += vy;
          // Settled on its target: snap there, draw once more and stop until the pointer moves.
          if (Math.abs(tx - cx) < 0.05 && Math.abs(ty - cy) < 0.05 && Math.abs(vx) < 0.05 && Math.abs(vy) < 0.05) {
            cx = tx;
            cy = ty;
            vx = vy = 0;
            draw();
            raf = 0;
            return;
          }
          draw();
          raf = requestAnimationFrame(loop);
        };
        const wake = () => {
          if (!raf) raf = requestAnimationFrame(loop);
        };
        const move = (e: PointerEvent) => {
          rect ??= el.getBoundingClientRect();
          const x = e.clientX - rect.left;
          const y = e.clientY - rect.top;
          // Grab only when the pointer is close to the string.
          if (target || Math.abs(y - h / 2) < 40) target = { x, y: h / 2 + (y - h / 2) * 2 };
          if (Math.abs(y - h / 2) > 110) target = null;
          wake();
        };
        const leave = () => {
          target = null;
          wake();
        };
        const forget = () => {
          rect = null;
        };
        const ro = new ResizeObserver(() => {
          w = el.clientWidth;
          h = el.clientHeight;
          rect = null;
          if (!target) {
            cx = w / 2;
            cy = h / 2;
          }
          draw();
          wake();
        });
        ro.observe(el);
        el.addEventListener("pointermove", move);
        el.addEventListener("pointerleave", leave);
        window.addEventListener("scroll", forget, { capture: true, passive: true });
        draw();
        return () => {
          cancelAnimationFrame(raf);
          ro.disconnect();
          el.removeEventListener("pointermove", move);
          el.removeEventListener("pointerleave", leave);
          window.removeEventListener("scroll", forget, { capture: true });
        };
      }, [visible]);
    
      return (
        <div ref={box} className="relative h-full w-full [touch-action:none]">
          <svg className="absolute inset-0 h-full w-full overflow-visible">
            <path ref={path} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" />
          </svg>
          <span className="font-mono text-[11px] uppercase tracking-[0.08em] absolute bottom-4 left-0 right-0 text-center text-mute">Pluck the string</span>
        </div>
      );
    }
    
  4. Or ask your AI agent

    Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.

    Add the Astabyte UI Elastic string component to my project.
    
    Pluck a line: it follows you, then rings back to rest.
    It fills its parent, so give the wrapper a size. It is designed on a dark surface: put it inside an element with the `dark` class.
    
    Install it:
    ```sh
    pnpm dlx asta-ui@latest add elastic-string --radix
    ```
    Or with the shadcn CLI:
    ```sh
    pnpm dlx shadcn@latest add https://ui.astabyte.com/r/elastic-string.json
    ```
    If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4).
    
    Import it like this:
    ```tsx
    import ElasticString from "@/components/astabyte/elastic-string";
    ```
    
    Usage example and props: https://ui.astabyte.com/elastic-string
    Full catalogue for context: https://ui.astabyte.com/llms.txt
    
    Rules:
    - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS.
    - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback.
    - Keep keyboard, focus and ARIA behaviour.
    - Use fake data and fake brand names in examples.
    
    Use it to: [describe where it goes and what it should do]

Usage

It fills its parent. Give the wrapper a size.

import ElasticString from "@/components/astabyte/elastic-string";

export default function Page() {
  // Elastic string
  return (
    <div className="relative h-96 w-full">
      <ElasticString />
    </div>
  );
}