React Scrim
Page transitions for React, written as components: every cover puts a new curtain on top, animated however you like.
The problem
Covering the screen during a page change sounds like two lines: play an animation, then swap what's underneath. It stops being two lines the moment someone clicks a second link before the first animation is done.
That's where most transitions try to be clever. They reverse the curtain halfway, jump it to the end, or make the click wait. Whatever they pick, what you see depends on exactly when the second click landed.
react-scrim doesn't try. Every cover() puts a new curtain on top of the ones already there, with its whole entrance. A curtain on screen never restarts and never reverses, so a fast run of clicks always looks the same: one curtain over the other, and only the last one leaves.
You've already seen it: every section of this site has its own curtain. Open the sidebar and jump between sections as fast as you can.
Curtains are components
A curtain is a component you write, animated with whatever you already use: CSS, Motion, GSAP, the Web Animations API. react-scrim never animates anything, and it never knows how long your animation takes.
It tells the curtain where it is with scrim.phase, and waits for the curtain to call scrim.done(). That's the whole contract:
const Curtain = ({ scrim }: ScrimProps) => (
<motion.div
className="fixed inset-0 bg-black"
initial={{ x: "-100%" }}
animate={{ x: scrim.phase === "leaving" ? "100%" : "0%" }}
onAnimationComplete={() => scrim.done()}
/>
);Phases
A curtain goes through three phases, always in the same order: entering while its entrance plays, covered once it has called done(), leaving while its exit plays. When it calls done() again, it's gone.
cover() resolves when its curtain is covered, and the curtains underneath are unmounted, since nobody can see them anymore. uncover() only ever moves the top curtain.
The first paint
The first page is a special case: there's no previous page to hide, and usually something worth waiting for before the content shows, like fonts. Give createScrim a curtain and the page starts covered. The server already sends it on screen, and nobody has to call cover().
export const AppScrim = createScrim(AppCurtain, {
hold: 650,
until: () => document.fonts.ready,
});until says what to wait for, and hold how long to stay up anyway, so a cached font doesn't make the curtain flash on and off. Both belong to each curtain: every cover() can pass its own.