Skip to main content

Gravity Text

Physical falling text with floor and wall collisions, 1:1 pointer-capture drag-and-throw, and spring rewind back to resting layout.

DROP & REWIND

Customize

Gravity1400
Bounce0.58

Installation

npx shadcn@latest add @kinetic/gravity-text

Usage

Pass a string to text and control the drop stage height with stageHeight (default 220 px, clamped 80 to 960). By default trigger="manual" keeps the text resting until you call drop() or grab any character directly on the stage.

import { useRef } from "react";
import { GravityText, type GravityTextHandle } from "@/components/kinetic/ui/gravity-text";

interface GravityBannerProps {
  readonly label: string;
}

export function GravityBanner({ label }: GravityBannerProps) {
  const ref = useRef<GravityTextHandle>(null);

  return (
    <div className="space-y-3">
      <GravityText
        ref={ref}
        text={label}
        stageHeight={220}
        className="rounded-lg border border-border p-6 text-4xl font-semibold tracking-tight"
      />
      <div className="flex gap-2">
        <button type="button" onClick={() => ref.current?.drop()}>
          Drop
        </button>
        <button type="button" onClick={() => ref.current?.rewind()}>
          Rewind
        </button>
        <button type="button" onClick={() => ref.current?.reset()}>
          Reset
        </button>
      </div>
    </div>
  );
}

Physics and drag-and-throw

Choose unit="char" (default, grapheme-safe) for short display titles or unit="word" for longer phrases. Tune gravity (default 1400, clamped 200 to 4000), restitution (default 0.58, clamped 0.1 to 0.92), and friction (default 0.985, clamped 0.85 to 0.999). Bodies bounce off all four stage edges, collide with each other, and put the animation loop to sleep automatically once every unit settles near the floor.

With draggable={true} (default), grabbing any unit captures the pointer and tracks it 1:1 inside the stage, even interrupting an "idle" or "rewinding" phase. Releasing samples recent pointer velocity to throw the unit with spin. Set trigger to "mount" or "inView" to start the drop automatically, and listen to onPhaseChange ("idle" | "dropped" | "rewinding").

import { GravityText, type GravityTextPhase } from "@/components/kinetic/ui/gravity-text";

interface InteractiveStageProps {
  readonly text: string;
  readonly onPhaseChange: (phase: GravityTextPhase) => void;
}

export function InteractiveStage({ text, onPhaseChange }: InteractiveStageProps) {
  return (
    <GravityText
      text={text}
      unit="word"
      gravity={1600}
      restitution={0.68}
      stageHeight={260}
      trigger="inView"
      draggable
      onPhaseChange={onPhaseChange}
      className="rounded-lg border border-border p-6 text-2xl font-medium"
    />
  );
}

Imperative handle

GravityTextHandle exposes three actions:

  • drop(): measures each unit's resting box and launches the gravity simulation.
  • rewind(): smoothly returns every unit to its home coordinates and 0 degree rotation via a staggered spatial spring, then restores "idle" and fires onComplete.
  • reset(): cancels the loop immediately, clears inline transforms, and resets phase to "idle".

Accessibility and reduced motion

GravityText renders through SplitText, exposing the full phrase in a single sr-only element (overridable via aria-label) while marking moving units aria-hidden="true". Draggable units apply touch-none and select-none so touch dragging stays responsive without scrolling the viewport.

Simulation and spring rewind time scale with speed and the global Kinetic tempo. Under reduced motion (prefers-reduced-motion: reduce or data-reduced-motion="true"), physics and dragging are disabled, text stays in its resting "idle" layout, and drop() or rewind() resolves immediately by calling onComplete.

API Reference

GravityText

Interactive 2D physics text stage with pointer-capture dragging, pairwise collision separation, and spring rewind.

PropTypeDefaultDescription
textstring-Text split into physical falling units.
unit"char" | "word""char"Splits by grapheme character or whole word.
gravitynumber1400Downward acceleration in px/s^2 (clamped 200 to 4000).
restitutionnumber0.58Bounce elasticity factor (clamped 0.1 to 0.92).
stageHeightnumber220Stage container height in pixels.
draggablebooleantrueAllows grabbing and flinging pieces via pointer capture.
trigger"manual" | "mount" | "inView""manual"When the drop begins automatically.
onPhaseChange(phase: "idle" | "dropped" | "rewinding") => void-Notifies when the stage phase changes.