Skip to content

Run code when an element reveals

React, Vue and Astro tell your code when an element reveals. @awesome-reveal/core has no callback.

Each package gives the revealed element in a different way:

  • React: the onReveal prop.
  • Vue: the reveal event of Reveal and Cascade, and the onReveal option of v-reveal and v-cascade.
  • Astro: the reveal DOM event.
  1. Waiting for the reveal.

Your device asks for reduced motion, so the content shows with no animation. The log shows the revealed state when the page loads.

import { Reveal } from "@awesome-reveal/react";
export function PricingTitle() {
return (
<Reveal onReveal={(element) => console.log("Revealed", element)}>
<h2>Pricing</h2>
</Reveal>
);
}

The example logs the reveal event and the end of the entrance. Select Replay to see the events again.

The code runs one time for each reveal, when the reveal starts. In a cascade, it gets the container.

  • It also runs when no entrance plays, for example with reduced motion, or when focus shows a waiting element.
  • In React, it runs one time also in StrictMode.
  • After when becomes false, the next reveal runs it again. With reduced motion, the element does not reset, so the code does not run again.

The entrance is a set of Web Animations on the element. To wait for its end, wait for the finished promises of these animations:

function entranceFinished(element) {
return Promise.all(
element.getAnimations().map((animation) => animation.finished),
);
}
// In the reveal callback:
entranceFinished(element)
.then(() => element.classList.add("entered"))
.catch(() => {});
  • The promise rejects when the entrance stops early, for example after when becomes false or an unmount. Catch the rejection.
  • The promise resolves immediately when no entrance plays, for example with reduced motion.
  • The promise also waits for your own animations of the element. An animation that repeats forever never finishes, so the promise does not resolve.

For a cascade, call getAnimations({ subtree: true }) on the container, because the children animate. The promise then also waits for the animations of all the descendants.

In the JavaScript API, a waiting entrance is a paused animation, and a paused animation does not finish. If your code waits for all of document.getAnimations(), skip the animations with playState === "paused".

React and Vue give a revealed value that is true after the reveal. In Astro, the result of getReveal has a revealed property.

import { useReveal } from "@awesome-reveal/react";
export function RevealedCard() {
const { getProps, revealed } = useReveal();
return <article {...getProps()}>{revealed ? "Hello" : "Waiting"}</article>;
}

In React, useReveal animates your own element with no wrapper. Spread getProps() on one DOM element.