Next.js scroll animations
Use @awesome-reveal/react to add scroll animations to a Next.js app. Install the package, and import it in your components. The App Router needs no setup, with Turbopack or webpack. The Pages Router with webpack needs one line of config.
Render a reveal from a Server Component
Section titled “Render a reveal from a Server Component”Reveal and Cascade are Client Components. A Server Component can render them, and pass them props that React can serialize:
import { Cascade, Reveal } from "@awesome-reveal/react";
export default function Page() { return ( <> <Reveal as="h1">Reveal from a Server Component</Reveal> <Reveal keyframes={[ { opacity: 0, transform: "scale(.8)", easing: "ease-out" }, { opacity: 1, transform: "scale(1)" }, ]} > Native keyframes from the server </Reveal> <Cascade as="ul" from={{ opacity: 0, y: 16 }}> <li>Fast</li> <li>Small</li> <li>Accessible</li> </Cascade> </> );}A Server Component can pass all the options except onReveal, keyframes included. It cannot pass a function, such as onReveal or a callback ref.
Use the hooks in a Client Component
Section titled “Use the hooks in a Client Component”useReveal and useCascade need a Client Component. Add "use client" at the top of the file:
"use client";
import { useReveal } from "@awesome-reveal/react";
export function Card() { const { getProps } = useReveal<HTMLElement>({ from: "left" });
return <article {...getProps({ className: "card" })}>A card</article>;}Then render Card from a Server Component.
Control the reveal from state
Section titled “Control the reveal from state”when and onReveal use the state of a Client Component:
"use client";
import { Reveal } from "@awesome-reveal/react";import { useState } from "react";
export function ManualReveal() { const [shown, setShown] = useState(false); const [count, setCount] = useState(0);
return ( <> <button type="button" aria-expanded={shown} onClick={() => setShown((value) => !value)} > Show the details </button> <output>{count}</output> reveals <Reveal when={shown} hidden={!shown} onReveal={() => setCount((value) => value + 1)} > <p>The details</p> </Reveal> </> );}Then render ManualReveal from a Server Component.
Set up the Pages Router with webpack
Section titled “Set up the Pages Router with webpack”The package imports its stylesheet. With webpack, the Pages Router does not let a package in node_modules import global CSS, and the build fails with “Global CSS cannot be imported from within node_modules”. Add the package to transpilePackages:
export default { transpilePackages: ["@awesome-reveal/react"],};Before hydration
Section titled “Before hydration”The server HTML hides each waiting element until hydration attaches it, for at most 5 seconds. See The 5 second hold.
With when={true}, the server still renders the element hidden. The entrance plays after hydration.
Content Security Policy
Section titled “Content Security Policy”The package writes no style attribute and no inline script. Next.js adds its own inline scripts, so a strict policy needs a nonce, or the experimental hashes with Subresource Integrity. See Content Security Policy in the Next.js docs.