Skip to content

Boing

A React toast library where one toast springs, squashes and morphs in place on real spring physics, instead of a pile stacking up in the corner.

Type
Library
Built
Role
Library Author & Designer
Source
GitHub

Tech stack

ReactTypeScriptWeb Audio APINext.jsTailwind CSSVitesttsupPlaywrightpnpm
0:00
at runtime; React 18 or newer is the only peer, and there's no stylesheet to import
0 deps
gzipped ESM build, with the styles and sound synth inside it
15KB
from the spring math and the store up to React StrictMode
131 tests
on screen by default; every new call reshapes it
1 toast

TL;DR: Boing (react-boing on npm) is a toast notification library for React with zero runtime dependencies. One toast morphs in place as the app changes state, driven by a mass-spring-damper solved every frame, with the same springs baked into CSS linear() easings for the stack. It is written in TypeScript with a framework-free core, tested with Node's test runner and Vitest, and documented on a Next.js site with a playground.

01

The itch

A loading toast and its result should be the same toast

Every app I've built fires a toast for a save, then another one when the save finishes. Most libraries show that as two toasts: a spinner, then a success stacked on top of it or swapped in with a fade. The thing on screen changed state, so I wanted the toast on screen to change shape with it.

So Boing keeps one toast and morphs it. It arrives as a 44px circle, opens sideways into a pill with the message, and when the next call comes in, width, height, icon and text move to the new content together. The film above runs through every type on the live site; the Try it row on boing.aayushbharti.in is the fastest way to feel it.

02

Architecture

A framework-free core, and React as a thin adapter

packages/react-boing/src/coreYour appuseSyncExternalStoretoast.success()toast.promise()<Toaster />react/Toaster.tsxstore.tsphases, owners, held modesbind.tspill morph, text roll, dragmotion.tsframe-loop springsspring.tsspringEasing → linear()styles.tsone injected style tagsound.tsWeb Audio chimeapps/docsNext.js 16, imports src
packages/react-boing/src/coreYour appuseSyncExternalStoretoast.success()toast.promise()<Toaster />react/Toaster.tsxstore.tsphases, owners, held modesbind.tspill morph, text roll, dragmotion.tsframe-loop springsspring.tsspringEasing → linear()styles.tsone injected style tagsound.tsWeb Audio chimeapps/docsNext.js 16, imports src

store.ts is the only source of truth: the toast API, a subscribe/getState pair, and the lifecycle each toast walks through (enter, open, closing, exit, at 450ms, 340ms and 220ms). It knows nothing about React or the DOM, so the core tests run on Node's built-in runner with no browser at all.

<Toaster /> subscribes through useSyncExternalStore and renders one slot per toast. Everything that moves lives in bind.ts, which owns the pill's DOM once React has rendered it. React decides what's on screen; it never sees a single animation frame.

Styles are one <style> tag the package injects at runtime, with a nonce prop for strict CSPs. That's why there's no stylesheet to import, and it lets the stack's easing be generated from the same spring numbers as the JavaScript motion.

Server components and the docs site

The package never touches window or document at import time, and ssr.test.ts imports it under plain Node to keep it that way. The build adds a "use client" banner, so <Toaster /> can sit in a server component layout in Next.js.

The docs site at boing.aayushbharti.in maps react-boing to the package's TypeScript source through tsconfig paths, so a library edit shows up on the site without a rebuild. Every docs and guide page is a typed record that generates the HTML page, its JSON-LD, a Markdown twin and its llms.txt entry.

03

The hard part

Springs that keep their momentum

A CSS transition interrupted halfway starts again from where it is, at zero velocity. For a toast that's about to change shape again, that reads as a hiccup. So the pill's width, height and position are real springs: a mass, a stiffness and a damping, stepped with semi-implicit Euler in 1/240-second sub-steps on one shared requestAnimationFrame loop. A new target changes where the spring pulls, and the velocity it already had carries into the new shape.

The velocity also does the squash and stretch. Vertical speed divided by 6,000 stretches the pill along its path, capped at 18%, and width speed over 20,000 squashes it, capped at 5%. Those divisors came from trying values until a fast entrance looked like a rubber ball and a slow one didn't wobble.

The stacked slots behind the newest toast move with plain CSS instead, because there can be several and they only ever slide. springEasing() solves the same spring in closed form, finds when it settles within 0.001 of its target, and samples it 60 times per second of motion into a CSS linear() easing. The default spring (stiffness 170, damping 18) becomes 49 stops over 800ms. It's exported, so your own transitions can move on Boing's springs. This is the same function, ported line for line:

Live · springEasing()

800ms · 49 stops · 5% overshoot

Text that rolls, errors that shake

When a toast's copy changes, the old text is cloned into a ghost element that rolls out over 280ms while the new text rolls in, with at least 450ms between two rolls so a burst of updates doesn't strobe. When loading turns into success, the new icon starts at the angle the spinner had reached and finishes the turn over 650ms, so the spin never jumps back to zero.

Each type has a reaction after it opens: an error gets a sideways kick of 380px/s into a stiff spring (stiffness 520, damping 13), and a warning hops 200px/s. Under prefers-reduced-motion every spring snaps to its end state and toasts fade instead.

04

Ownership

A late result can't take back the toast

Morphing one toast has a failure mode that stacking doesn't. Start a slow save, then a fast one. The fast save takes the toast and shows success. Then the slow one resolves, and a naive update would flip the toast back to "Draft A saved", which is now a lie about the most recent action.

The store keeps an owners map from each toast to the caller that last took it. Every show call gets a caller id, and update(callerId, patch) returns early unless that caller still owns the toast. toast.promise goes through update, so the slow promise still resolves for your code but leaves the toast alone.

const id = callers.get(callerId) ?? callerId;
if (owners.get(id) !== callerId) {
  return; // someone newer took this toast
}
applyPatch(id, patch);
Repeats, held toasts and queues

Calling the same toast twice adds a counter (×2) to the live one instead of a second toast, unless the call carries something that makes it distinct, such as an id, a button or an image.

Errors, loading toasts and toasts with a button stay until dismissed, which raises the question of what a quick "Saved" should do while an error is waiting. held answers it with three modes: replace (the default) morphs the held toast like any other, list keeps it and shows quick toasts beside it, and queue holds new calls until it's gone. update works on queued calls too, so a promise that settles while it's waiting shows its result when its turn comes.

05

Trade-offs

What I chose, and what it cost

ChoseOverWhyCost
One toast that morphsA stack of toasts, like SonnerA state change reads as one thing changing, and nothing piles up in the corner.Needed the owners map so stale results can't overwrite newer ones, and the held modes for errors.
Frame-loop springs for the pillCSS transitions with a spring-shaped easingInterrupting keeps velocity, and velocity drives the squash and stretch.The pill writes width and height every frame, which means layout work while it moves.
CSS linear() for the stacked slotsRunning them on the frame loop tooSeveral slots that only translate and scale; the browser can run them without JavaScript.Two motion systems that have to agree, so both read the same spring numbers.
Styles injected from JavaScriptA CSS file to importOne install step, and the stack easing is generated from the spring config.A runtime <style> tag, which strict CSPs need a nonce for.
Tones synthesized with Web AudioShipping audio filesNo assets to host or load. Success rises (G4 to C5), error falls, warning taps twice.Sound is off by default, and the first tone has to follow a click or key press (thanks, autoplay policies).
Zero dependenciesMotion or react-springNothing extra in the bundle or the lockfile, and spring.ts plus motion.ts come to under 200 lines.I own the integrator, its rest threshold and its edge cases.

06

Shipped

What shipped, and what I'd change

What shipped: react-boing 0.1.0 on npm, 15 days after the first commit, as ESM and CJS with type declarations. Five toast types, toast.promise, in-place updates, undo actions that return a patch, progress bars, repeat counters, three held modes, six positions, light, dark and system themes, optional sound, and 131 tests across the core and the DOM. The docs site has the API reference, 11 guides, a playground that writes the code for the toast you build, and Markdown twins of every page for assistants.

The width and height writes are what I'd look at first. They're the honest cost of letting the text reflow naturally while the pill grows, and I haven't profiled them on a slow phone yet. If they show up there, the fix is to animate a clip and a scale and measure the text once, at the cost of the copy stretching mid-morph.

The store is a module-level singleton, so two <Toaster /> instances in one app render the same toasts. That's right for most apps and wrong for a micro-frontend that wants separate regions. createToaster() already exists inside the core; exporting it with a store prop on <Toaster /> is the next API change.

Common questions

Boing FAQs

What people ask about Boing, answered from the code itself.

What is Boing?

Boing is a toast notification library for React, published on npm as react-boing. A toast arrives as a small circle, opens sideways into a pill with its message and shrinks back before it leaves. A new call reshapes the toast already on screen instead of stacking another one, and all of it moves on spring physics. It has zero runtime dependencies and the gzipped build is about 15KB.

How does Boing animate toasts with spring physics?

Boing runs a mass-spring-damper for the pill's width, height and position, integrated with semi-implicit Euler in 1/240-second sub-steps on one shared requestAnimationFrame loop. Because each spring keeps its velocity, interrupting a toast mid-motion carries its momentum into the new shape. The vertical and horizontal velocity also drive squash and stretch, capped at 18% and 5%.

What does springEasing() in Boing do?

springEasing({ stiffness, damping, mass }) solves a damped spring in closed form, finds when it settles within 0.001 of its target, and samples it 60 times per second of motion into a CSS linear() easing. It returns that easing with a duration in milliseconds, so CSS transitions and the Web Animations API can move on the same spring as Boing. The default spring (stiffness 170, damping 18) gives 49 stops over 800ms.

How is Boing different from Sonner or react-hot-toast?

Boing shows one toast by default and morphs it, so loading turns into success and an action turns into its confirmation in the same element, instead of a stack of separate toasts. The motion comes from a spring solver rather than fixed-duration curves. Identical repeat calls fold into a counter (×2), and three held modes (replace, list, queue) decide how errors and toasts with buttons share the screen.

Does Boing work with the Next.js App Router?

Yes. The react-boing build ships a "use client" banner, so <Toaster /> can sit in a server component layout, and the package never touches window or document at import time, which a test checks. toast() and toast.configure() are called from client components.

Is Boing accessible?

Boing announces toasts through two live regions that stay on the page, assertive for errors and polite for everything else. Escape dismisses, Alt+T moves focus into the toast and pauses its timer, and under prefers-reduced-motion the springs snap to their end state and toasts fade instead of moving. Layout mirrors on right-to-left pages.