Guide

Framework integrations

Use @samline/reveal in React, Vue, Svelte, Astro, Shopify and WordPress, and keep it working with client-side navigation and page swaps.

reveal returns a controller, and its destroy method is the cleanup, so it fits every framework’s mount and unmount hooks. Import the stylesheet once, for example in your app entry.

React

import '@samline/reveal/style.css'
import { reveal } from '@samline/reveal'
import { useEffect, useRef } from 'react'

export function Features({ items }) {
  const list = useRef(null)
  useEffect(() => reveal(list.current.children, { stagger: 80 }).destroy, [items])
  return (
    <ul ref={list}>
      {items.map(item => (
        <li key={item.id} data-reveal>
          {item.title}
        </li>
      ))}
    </ul>
  )
}

Vue

<script setup>
import { onMounted, onBeforeUnmount, ref } from 'vue'
import { reveal } from '@samline/reveal'

const list = ref()
let controller
onMounted(() => (controller = reveal(list.value.children, { stagger: 80 })))
onBeforeUnmount(() => controller?.destroy())
</script>

Svelte

<script>
  import { reveal } from '@samline/reveal'

  const revealChildren = node => reveal(node.children, { stagger: 80 })
</script>

<ul use:revealChildren>…</ul>

Astro, page swaps and AJAX

For a page built from many components, call autoReveal() once. It watches the document, so it reveals elements added later (infinite scroll, AJAX, Swup or Astro view transitions that replace the content) and releases removed ones. The CDN script does the same.

<script>
  import '@samline/reveal/style.css'
  import { autoReveal } from '@samline/reveal'

  autoReveal()
</script>

Server rendering is safe: on the server reveal and autoReveal return controllers whose methods do nothing, and server HTML is visible until the script runs, so there is no blank first paint.

Shopify

Load the pinned classic script once from theme assets. Copy the dist/browser/reveal.global.js to assets/reveal.global.js and dist/style.css to assets/reveal.css from the matching published release (@samline/reveal@2.0.0), then include it once in layout/theme.liquid before </body>:

{{ 'reveal.css' | asset_url | stylesheet_tag }}
{{ 'reveal.global.js' | asset_url | script_tag }}

Mark up any section with the data-reveal* attributes; the script defines window.Reveal. A deferred <script src="{{ 'reveal.global.js' | asset_url }}" defer></script> works the same.

<section data-reveal>
  <h2>Shipping and returns</h2>
</section>

The script starts autoReveal() on load and keeps watching the document, so elements inside a section the theme editor adds are revealed and elements in a section it removes are released: you do not need shopify:section:load or shopify:section:unload handlers. To start it with your own options, load the script with a raw tag that has data-manual (script_tag cannot add attributes) and call Reveal.auto(options); Reveal.controller.destroy() releases everything it started:

<script src="{{ 'reveal.global.js' | asset_url }}" data-manual defer></script>

See Shopify’s section events.

WordPress

Enqueue the pinned script and stylesheet from your theme or plugin, copied from the matching published release (@samline/reveal@2.0.0), in the wp_enqueue_scripts hook. The strategy argument needs WordPress 6.3 or later.

add_action('wp_enqueue_scripts', function () {
  wp_enqueue_style('reveal', plugins_url('assets/reveal.css', __FILE__), [], '2.0.0');
  wp_enqueue_script('reveal', plugins_url('assets/reveal.global.js', __FILE__), [], '2.0.0', [
    'in_footer' => true,
    'strategy' => 'defer',
  ]);
});

The script reveals every [data-reveal] element on load and as elements are added, so the markup can live in a block, a template or a shortcode output. To use your own options, add data-manual to the script tag with the script_loader_tag filter and call Reveal.auto(options):

add_filter('script_loader_tag', fn($tag, $handle) => $handle === 'reveal' ? str_replace(' src=', ' data-manual src=', $tag) : $tag, 10, 2);

See wp_enqueue_script.