IllustrationsPlays on its own. Click to take over.
Messaging
Paper Plane Send
A message box that sends your note as a paper plane: the ruled page slides out, folds into a plane and flies off with a dotted trail.
Enter to send, Shift Enter for a new line
Install
Copies the source into components/illustrations/paper-plane-send.tsx and installs what it needs.
$ pnpm dlx shadcn@latest add https://makeui.xyz/r/paper-plane-send.jsonUsage
app/page.tsx
import { PaperPlaneSend } from "@/components/illustrations/paper-plane-send"
export default function Example() {
return (
<PaperPlaneSend
placeholder="Write a note…"
onSend={async (message) => {
await fetch("/api/notes", { method: "POST", body: message })
}}
/>
)
}Source
Prefer to copy by hand? It's one file. Requires motion and the shadcn cn helper.
components/illustrations/paper-plane-send.tsx
"use client"
import { useCallback, useEffect, useRef, useState } from "react"
import { createPortal } from "react-dom"
import { AnimatePresence, motion, useReducedMotion } from "motion/react"
import { ArrowUp, Check } from "lucide-react"
import { cn } from "@/lib/utils"
export interface PaperPlaneSendProps {
placeholder?: string
/** Called with the message when it is sent. Return a promise to hold the plane until it resolves. */
onSend?: (message: string) => unknown
/** Types a few messages and sends them on its own (until someone touches it). Pass your own list or `true`. */
autoPlay?: boolean | string[]
defaultValue?: string
className?: string
}
/* ------------------------------------------------------------------ geometry
The message is written on a ruled page that slides out of the box, is folded
like a real paper plane (two steps of folds, computed as polygons), tilts to
face its heading and flies along a curve, leaving a dotted trail. */
type Pt = [number, number]
type Mat = [number, number, number, number, number, number] // SVG matrix(a b c d e f)
type Piece = { pts: Pt[]; m: Mat; shade: number }
type Path = [Pt, Pt, Pt, Pt]
const PW = 180 // letter width
const PH = 240 // letter height
const HAND = '"Caveat Variable", Caveat, "Segoe Print", "Bradley Hand", "Chalkboard SE", cursive'
// Timeline in ms: the letter rises, is folded in two steps, tilts, then flies.
const T = { rise: 320, fold1: 220, fold2: 680, tilt: 1020, fly: 1120, flyLen: 820, end: 2000 }
const palettes = {
light: { paper: "#fffefa", paper2: "#ebe7dc", rule: "#e6e2d6", margin: "#a5b4fc", ink: "#24221d", edge: "#d6d1c3", trail: "#6366f1", shadow: "#3c280a" },
dark: { paper: "#2c2a26", paper2: "#201f1c", rule: "#3a3832", margin: "#6366f1", ink: "#f1efe8", edge: "#46433b", trail: "#818cf8", shadow: "#000000" },
}
type Palette = (typeof palettes)["light"]
const IDENT: Mat = [1, 0, 0, 1, 0, 0]
const clamp01 = (v: number) => Math.min(1, Math.max(0, v))
const lerp = (a: number, b: number, t: number) => a + (b - a) * t
const easeOut = (t: number) => 1 - Math.pow(1 - t, 3)
const easeInOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2)
const mul = (m: Mat, n: Mat): Mat => [
m[0] * n[0] + m[2] * n[1],
m[1] * n[0] + m[3] * n[1],
m[0] * n[2] + m[2] * n[3],
m[1] * n[2] + m[3] * n[3],
m[0] * n[4] + m[2] * n[5] + m[4],
m[1] * n[4] + m[3] * n[5] + m[5],
]
const apply = (m: Mat, p: Pt): Pt => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]]
/** Folding along a crease (point c, unit normal n pointing at the moving side), g = 0..1. */
const foldMat = (c: Pt, n: Pt, g: number): Mat => {
const k = 1 - Math.cos(Math.PI * g)
const d = n[0] * c[0] + n[1] * c[1]
return [1 - k * n[0] * n[0], -k * n[0] * n[1], -k * n[0] * n[1], 1 - k * n[1] * n[1], k * n[0] * d, k * n[1] * d]
}
const area = (poly: Pt[]) => {
let a = 0
for (let i = 0; i < poly.length; i++) {
const p = poly[i]
const q = poly[(i + 1) % poly.length]
a += p[0] * q[1] - q[0] * p[1]
}
return Math.abs(a) / 2
}
/** Keep the part of a polygon on one side of the crease. */
const clip = (poly: Pt[], c: Pt, n: Pt, keep: 1 | -1): Pt[] => {
const out: Pt[] = []
const side = (p: Pt) => keep * (n[0] * (p[0] - c[0]) + n[1] * (p[1] - c[1]))
for (let i = 0; i < poly.length; i++) {
const a = poly[i]
const b = poly[(i + 1) % poly.length]
const sa = side(a)
const sb = side(b)
if (sa >= 0) out.push(a)
if (sa >= 0 !== sb >= 0) {
const t = sa / (sa - sb)
out.push([a[0] + t * (b[0] - a[0]), a[1] + t * (b[1] - a[1])])
}
}
return out
}
const foldPieces = (pieces: Piece[], c: Pt, n: Pt, g: number): Piece[] => {
if (g <= 0) return pieces
const F = foldMat(c, n, g)
const still: Piece[] = []
const moved: Piece[] = []
for (const p of pieces) {
const a = clip(p.pts, c, n, 1)
const b = clip(p.pts, c, n, -1)
if (area(b) > 0.5) still.push({ ...p, pts: b })
if (area(a) > 0.5) moved.push({ pts: a.map((q) => apply(F, q)), m: mul(F, p.m), shade: Math.max(p.shade, Math.sin(Math.PI * g)) })
}
return [...still, ...moved] // folded layers lie on top
}
const NOSE: Pt = [PW / 2, 0]
const R = Math.SQRT1_2
const S = Math.sin(Math.PI / 8)
const C = Math.cos(Math.PI / 8)
/** The folds of a classic paper plane. Steps 1 and 2 run together, then 3 and 4. */
const foldLetter = (g1: number, g2: number): Piece[] => {
let pieces: Piece[] = [{ pts: [[0, 0], [PW, 0], [PW, PH], [0, PH]], m: IDENT, shade: 0 }]
pieces = foldPieces(pieces, NOSE, [-R, -R], g1)
pieces = foldPieces(pieces, NOSE, [R, -R], g1)
pieces = foldPieces(pieces, NOSE, [-C, -S], g2)
pieces = foldPieces(pieces, NOSE, [C, -S], g2)
return pieces
}
const bezier = (p: Path, t: number): Pt => {
const u = 1 - t
return [
u * u * u * p[0][0] + 3 * u * u * t * p[1][0] + 3 * u * t * t * p[2][0] + t * t * t * p[3][0],
u * u * u * p[0][1] + 3 * u * u * t * p[1][1] + 3 * u * t * t * p[2][1] + t * t * t * p[3][1],
]
}
/** Rotation (deg) that points the plane's nose along the path. */
const heading = (p: Path, t: number) => {
const u = 1 - t
const dx = 3 * u * u * (p[1][0] - p[0][0]) + 6 * u * t * (p[2][0] - p[1][0]) + 3 * t * t * (p[3][0] - p[2][0])
const dy = 3 * u * u * (p[1][1] - p[0][1]) + 6 * u * t * (p[2][1] - p[1][1]) + 3 * t * t * (p[3][1] - p[2][1])
return (Math.atan2(dy, dx) * 180) / Math.PI + 90
}
/* ------------------------------------------------------------------ flight */
type FlightData = {
id: number
text: string
/** Horizontal centre and top edge of the box, in viewport pixels. */
cx: number
top: number
width: number
dark: boolean
}
type Geo = { s: number; fs: number; start: Pt; path: Path; h0: number; vw: number; vh: number }
const makeGeo = (d: FlightData): Geo => {
const vw = window.innerWidth
const vh = window.innerHeight
// shrink the letter if there is little room above the box
const s = Math.max(0.4, Math.min(1, (d.top - 28) / (PH + 14), (d.width * 0.6) / PW))
const start: Pt = [d.cx, d.top - 14 - (PH * s) / 2]
const q = Math.min(1, vw / 820)
const dir = d.cx > vw * 0.6 ? -1 : 1 // fly towards the middle of the screen
const path: Path = [
[start[0], start[1]],
[start[0] - 20 * q * dir, start[1] - 230 * q],
[start[0] + 300 * q * dir, start[1] - 380 * q],
[start[0] + 720 * q * dir, start[1] - 260 * q - 120],
]
return { s, fs: s * 0.5, start, path, h0: heading(path, 0), vw, vh }
}
const pose = (e: number, g: Geo) => {
const rise = (1 - easeOut(clamp01(e / T.rise))) * (PH * g.s + 14)
const tt = easeInOut(clamp01((e - T.tilt) / (T.fly - T.tilt)))
const u = clamp01((e - T.fly) / T.flyLen)
const ue = 0.3 * u + 0.7 * u * u
let pos: Pt = [g.start[0], g.start[1] + rise]
let rot = g.h0 * tt
if (e >= T.fly) {
pos = bezier(g.path, ue)
rot = heading(g.path, ue)
}
return { pos, rot, sc: lerp(g.s, g.fs, tt), sx: lerp(1, 0.62, tt), u, opacity: e >= T.fly ? 1 - clamp01((u - 0.8) / 0.2) : 1 }
}
const wrap = (text: string, max = 21, maxLines = 12) => {
const out: string[] = []
for (const para of text.split("\n")) {
let line = ""
for (let word of para.split(/\s+/).filter(Boolean)) {
while (word.length > max) {
if (line) {
out.push(line)
line = ""
}
out.push(word.slice(0, max))
word = word.slice(max)
}
if (!line) line = word
else if ((line + " " + word).length <= max) line += " " + word
else {
out.push(line)
line = word
}
}
out.push(line)
}
if (out.length > maxLines) {
out.length = maxLines
out[maxLines - 1] = out[maxLines - 1].slice(0, max - 1) + "…"
}
return out
}
const LINE = 16 // spacing of the rules on the letter
const TOP = 34 // first rule
/** What is on the letter: a ruled page with a margin line and handwriting. */
function LetterFace({ lines, c }: { lines: string[]; c: Palette }) {
const rules = []
for (let y = TOP; y < PH - 8; y += LINE) rules.push(y)
return (
<g>
{rules.map((y) => (
<line key={y} x1={0} x2={PW} y1={y} y2={y} stroke={c.rule} strokeWidth={0.8} />
))}
<line x1={27} x2={27} y1={0} y2={PH} stroke={c.margin} strokeWidth={1.1} />
{lines.map((l, i) => (
<text key={i} x={34} y={TOP + i * LINE - 3} fontSize={15} fill={c.ink} style={{ fontFamily: HAND }} xmlSpace="preserve">
{l}
</text>
))}
</g>
)
}
/** The overlay: a fixed, unclipped layer so nothing in the page can cut the plane off. */
function Flight({ data, onDone }: { data: FlightData; onDone: () => void }) {
const geo = useRef<Geo>(null as unknown as Geo)
if (!geo.current) geo.current = makeGeo(data)
const g = geo.current
const c = data.dark ? palettes.dark : palettes.light
const lines = useRef(wrap(data.text)).current
const [e, setE] = useState(0)
const trail = useRef<Pt[]>([])
useEffect(() => {
let raf = 0
const t0 = performance.now()
const tick = (now: number) => {
const el = now - t0
const p = pose(el, g)
if (el >= T.fly && p.u < 1) trail.current.push(p.pos)
if (p.u >= 1) trail.current.splice(0, 4) // let the trail drain away
if (trail.current.length > 48) trail.current.shift()
setE(el)
if ((el >= T.end && trail.current.length === 0) || el > T.end + 800) {
onDone()
return
}
raf = requestAnimationFrame(tick)
}
raf = requestAnimationFrame(tick)
return () => cancelAnimationFrame(raf)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
const p = pose(e, g)
const g1 = easeInOut(clamp01((e - T.fold1) / (T.fold2 - T.fold1)))
const g2 = easeInOut(clamp01((e - T.fold2) / (T.tilt + 100 - T.fold2)))
const pieces = foldLetter(g1, g2)
const risingOut = e < T.rise + 40
const id = `ppf${data.id}`
const pts = trail.current
const trailPath = pts.length > 1 ? "M" + pts.map((q) => `${q[0].toFixed(1)} ${q[1].toFixed(1)}`).join(" L") : ""
return (
<svg
aria-hidden
width={g.vw}
height={g.vh}
viewBox={`0 0 ${g.vw} ${g.vh}`}
className="pointer-events-none fixed left-0 top-0 z-[60] overflow-visible"
>
<defs>
<clipPath id={`${id}-rise`}>
<rect x={0} y={0} width={g.vw} height={data.top} />
</clipPath>
<filter id={`${id}-shadow`} x="-40%" y="-40%" width="180%" height="180%">
<feDropShadow dx={0} dy={5} stdDeviation={6} floodColor={c.shadow} floodOpacity={data.dark ? 0.5 : 0.22} />
</filter>
</defs>
{/* dotted trail, like a doodle in the margin */}
{trailPath && (
<path
d={trailPath}
fill="none"
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2.6}
strokeDasharray="0.1 8"
stroke={c.trail}
opacity={Math.min(0.85, pts.length / 20)}
/>
)}
{/* the letter / plane. While it rises it is hidden behind the box. */}
<g clipPath={risingOut ? `url(#${id}-rise)` : undefined}>
<g
opacity={p.opacity}
filter={`url(#${id}-shadow)`}
transform={`translate(${p.pos[0]} ${p.pos[1]}) rotate(${p.rot}) scale(${p.sc * p.sx} ${p.sc}) translate(${-PW / 2} ${-PH / 2})`}
>
{pieces.map((pc, i) => {
const poly = pc.pts.map((q) => q.join(",")).join(" ")
const front = pc.m[0] * pc.m[3] - pc.m[1] * pc.m[2] > 0
return (
<g key={i}>
<clipPath id={`${id}-p${i}`}>
<polygon points={poly} />
</clipPath>
<polygon points={poly} fill={front ? c.paper : c.paper2} stroke={c.edge} strokeWidth={1.2} strokeLinejoin="round" />
{front && (
<g clipPath={`url(#${id}-p${i})`}>
<g transform={`matrix(${pc.m.join(" ")})`}>
<LetterFace lines={lines} c={c} />
</g>
</g>
)}
{/* creases get a soft shadow while folding */}
<polygon points={poly} fill={c.shadow} opacity={pc.shade * 0.16} />
</g>
)
})}
</g>
</g>
</svg>
)
}
/* ------------------------------------------------------------------ component */
const demoMessages = [
"Shipping the new onboarding flow tomorrow. Docs are up!",
"Coffee at 4? I'll bring the notes from today.",
"Thank you for the quick review, merged and live.",
]
/**
* A message box. On send, the page slides out of the box, folds into a paper plane and flies off,
* leaving a dotted trail. Works in light and dark, respects reduced motion.
*/
export function PaperPlaneSend({ placeholder = "Write a note…", onSend, autoPlay, defaultValue = "", className }: PaperPlaneSendProps) {
const reduce = useReducedMotion()
const [text, setText] = useState(defaultValue)
const [sending, setSending] = useState(false)
const [flight, setFlight] = useState<FlightData | null>(null)
const [sent, setSent] = useState(false)
const area = useRef<HTMLTextAreaElement>(null)
const card = useRef<HTMLDivElement>(null)
const hold = useRef(0) // autoplay backs off while someone is using the box
const visible = useRef(true)
useEffect(() => {
const el = area.current
if (!el) return
el.style.height = "auto"
el.style.height = `${el.scrollHeight}px`
}, [text])
useEffect(() => {
if (!sent) return
const t = setTimeout(() => setSent(false), 2400)
return () => clearTimeout(t)
}, [sent])
// only autoplay while the box is on screen
useEffect(() => {
const el = card.current
if (!el || !autoPlay) return
const io = new IntersectionObserver(([entry]) => (visible.current = entry.isIntersecting), { threshold: 0.6 })
io.observe(el)
return () => io.disconnect()
}, [autoPlay])
const launch = useCallback(
async (value: string) => {
const box = card.current?.getBoundingClientRect()
setSending(true)
try {
await onSend?.(value)
} catch {
setSending(false)
return false // keep the text so nothing is lost
}
setSending(false)
setText("")
setSent(true)
if (box && !reduce) {
setFlight({
id: Date.now(),
text: value,
cx: box.left + box.width / 2,
top: box.top,
width: box.width,
dark: document.documentElement.classList.contains("dark"),
})
}
return true
},
[onSend, reduce]
)
const launchRef = useRef(launch)
launchRef.current = launch
const canSend = !!text.trim() && !sending
// autoplay: types a message, sends it, waits for the plane to leave, repeats
useEffect(() => {
if (!autoPlay) return
const list = Array.isArray(autoPlay) ? autoPlay : demoMessages
let cancelled = false
let i = 0
const timers: ReturnType<typeof setTimeout>[] = []
const sleep = (ms: number) => new Promise<void>((r) => timers.push(setTimeout(r, ms)))
const idle = () => Date.now() >= hold.current
;(async () => {
await sleep(900)
while (!cancelled) {
if (!idle() || !visible.current || reduce) {
await sleep(900)
continue
}
const msg = list[i++ % list.length]
let typed = true
for (let k = 1; k <= msg.length && !cancelled; k++) {
if (!idle()) {
typed = false
break
}
setText(msg.slice(0, k))
await sleep(36)
}
if (cancelled) return
if (!typed) continue
await sleep(650)
if (!idle()) continue
await launchRef.current(msg)
await sleep(T.end + 1700)
}
})()
return () => {
cancelled = true
timers.forEach(clearTimeout)
}
}, [autoPlay, reduce])
const touch = () => (hold.current = Date.now() + 12000)
return (
<div className={cn("w-full max-w-md", className)}>
<div
ref={card}
onPointerDownCapture={touch}
onKeyDownCapture={touch}
className="rounded-3xl border border-neutral-200 bg-white p-3 shadow-[0_1px_2px_rgb(0_0_0/0.04),0_12px_28px_-16px_rgb(0_0_0/0.18)] dark:border-neutral-800 dark:bg-neutral-900 dark:shadow-[0_12px_28px_-16px_rgb(0_0_0/0.6)]"
>
<textarea
ref={area}
rows={2}
value={text}
placeholder={placeholder}
disabled={sending}
aria-label="Message"
onChange={(e) => setText(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault()
if (canSend) void launch(text.trim())
}
}}
className="block max-h-40 min-h-[3.25rem] w-full resize-none bg-transparent px-2 py-1.5 text-[15px] leading-relaxed text-neutral-900 outline-none placeholder:text-neutral-400 dark:text-neutral-50 dark:placeholder:text-neutral-500"
/>
<div className="mt-2 flex items-center justify-between gap-3 pl-2">
<span className="text-xs text-neutral-400 dark:text-neutral-500">Enter to send, Shift Enter for a new line</span>
<motion.button
type="button"
aria-label="Send"
disabled={!canSend}
onClick={() => void launch(text.trim())}
whileTap={{ scale: 0.9 }}
transition={{ type: "spring", stiffness: 500, damping: 30 }}
className="grid size-9 shrink-0 place-items-center rounded-full bg-neutral-900 text-white outline-none transition-opacity focus-visible:ring-2 focus-visible:ring-neutral-400/60 focus-visible:ring-offset-2 disabled:cursor-not-allowed disabled:opacity-30 dark:bg-white dark:text-neutral-900"
>
<ArrowUp className="size-4" strokeWidth={2.4} />
</motion.button>
</div>
</div>
<div role="status" className="mt-2 h-5 pl-4 text-xs text-neutral-500 dark:text-neutral-400">
<AnimatePresence>
{sent && (
<motion.span
initial={{ opacity: 0, y: 4 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0 }}
className="inline-flex items-center gap-1.5"
>
<Check className="size-3.5 text-emerald-500" strokeWidth={3} /> On its way
</motion.span>
)}
</AnimatePresence>
</div>
{flight &&
createPortal(
<Flight key={flight.id} data={flight} onDone={() => setFlight((f) => (f?.id === flight.id ? null : f))} />,
document.body
)}
</div>
)
}