# Run code when an element reveals

> Run code when an element scrolls into view with onReveal, the Vue reveal event or the Astro reveal DOM event, and wait for the end of the entrance animation.

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

## Run code when the reveal starts

Each package gives the revealed element in a different way:

* React: the [`onReveal`](/reference/options/#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](/reference/astro/#the-reveal-event).

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](https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/At-rules/@media/prefers-reduced-motion), or when focus shows a waiting element.
* In React, it runs one time also in [StrictMode](https://react.dev/reference/react/StrictMode).
* After [`when`](/guides/manual-control/#reveal-from-your-state) becomes `false`, 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

The entrance is a set of [Web Animations](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API) on the element. To wait for its end, wait for the [`finished`](https://developer.mozilla.org/en-US/docs/Web/API/Animation/finished) promises of these animations:

```js
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 })`](https://developer.mozilla.org/en-US/docs/Web/API/Element/getAnimations) 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()`](https://developer.mozilla.org/en-US/docs/Web/API/Document/getAnimations), skip the animations with `playState === "paused"`.

## Read the revealed state

React and Vue give a `revealed` value that is `true` after the reveal. In Astro, the result of [`getReveal`](/reference/astro/#getreveal) has a `revealed` property.

* React

  ```jsx
  import { useReveal } from "@awesome-reveal/react";

  export function RevealedCard() {
    const { getProps, revealed } = useReveal();
    return <article {...getProps()}>{revealed ? "Hello" : "Waiting"}</article>;
  }
  ```

* Vue

  ```vue
  <script setup>
  import { Reveal } from "@awesome-reveal/vue";
  </script>

  <template>
    <Reveal v-slot="{ revealed }" as="article">
      {{ revealed ? "Hello" : "Waiting" }}
    </Reveal>
  </template>
  ```

* Astro

  ```astro
  ---
  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.
