Run code when an element reveals
React, Vue and Astro tell your code when an element reveals. @awesome-reveal/core has no callback.
Run code when the reveal starts
Section titled “Run code when the reveal starts”Each package gives the revealed element in a different way:
- React: the
onRevealprop. - Vue: the
revealevent ofRevealandCascade, and theonRevealoption ofv-revealandv-cascade. - Astro: the
revealDOM event.
- 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> );}<script setup>import { Reveal, vReveal } from "@awesome-reveal/vue";
const log = (element) => console.log("Revealed", element);</script>
<template> <Reveal @reveal="log"><h2>Pricing</h2></Reveal> <p v-reveal="{ onReveal: log }">No wrapper element</p></template>---import { Reveal } from "@awesome-reveal/astro";---
<Reveal> <h2>Pricing</h2></Reveal>
<script> document.addEventListener("reveal", (event) => { console.log("Revealed", event.target); });</script>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
whenbecomesfalse, the next reveal runs it again. With reduced motion, the element does not reset, so the code does not run again.
Wait for the end of the entrance
Section titled “Wait for the end of the entrance”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
whenbecomesfalseor 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".
Read the revealed state
Section titled “Read the revealed state”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>;}<script setup>import { Reveal } from "@awesome-reveal/vue";</script>
<template> <Reveal v-slot="{ revealed }" as="article"> {{ revealed ? "Hello" : "Waiting" }} </Reveal></template>---import { Reveal } from "@awesome-reveal/astro";---
<Reveal as="article" id="card"> Hello</Reveal><button id="check">Check</button>
<script> import { getReveal } from "@awesome-reveal/astro";
const reveal = getReveal(document.querySelector("#card")!);
document.querySelector("#check")!.addEventListener("click", () => { console.log(reveal.revealed ? "Revealed" : "Waiting"); });</script>In React, useReveal animates your own element with no wrapper. Spread getProps() on one DOM element.