Reference

API reference

Every @samline/reveal export, reveal, autoReveal, the controller, presets, effects, events and the window.Reveal browser global.

reveal(target?, options?)

reveal(target?: ElementTarget, options?: RevealOptions): RevealController

Reveals target as it scrolls into view and returns a controller. target is a selector, an element, a NodeList or an array, and defaults to '[data-reveal]'. Elements with data-reveal-scrub are skipped: CSS handles them.

import { reveal } from '@samline/reveal'

const controller = reveal('.card', { effect: 'zoom', stagger: 60 })
// Later: stop observing and remove every attribute and variable reveal added.
controller.destroy()

On the server reveal returns a controller whose methods do nothing and never throw. With no matching elements it returns a live controller with no elements, and refresh() adds matches that appear later.

autoReveal(options?)

autoReveal(options?: AutoRevealOptions): RevealController

Reveals every element matching selector (default '[data-reveal]') inside scope (default document). With observe: true (the default) it also reveals matching elements added later and cleans up elements that are removed. Its controller acts on all of them.

import { autoReveal } from '@samline/reveal'

const controller = autoReveal({ stagger: 80 })
controller.refresh() // pick up elements added while observe is false
controller.destroy() // stop everything

AutoRevealOptions accepts every RevealOptions field plus selector, scope and observe.

RevealController

Returned by reveal() and autoReveal(). Every method is a closure, so you can destructure them (const { show, hide } = reveal('.card')).

Member Description
elements The elements currently controlled.
show(target?) Reveals now, with the transition and the cascade, even off screen. Without target, every controlled element; otherwise the controlled ones among them. Elements that are revealing or revealed are left alone, and an element that was not observed yet is shown without a transition. Returns the controller.
hide(target?) Hides again instantly and calls onHide (and reveal:hide). Elements shown as is (reduced motion, media query not matching) stay visible. A hidden element stays hidden until show() or its next entry into view. Returns the controller.
update(options) Merges options into the current ones and applies them again. Pending elements stay pending, revealed ones stay revealed, and no event fires again. Returns the controller.
refresh() Resolves the target again with the same options: new matches join, disconnected elements leave. Returns the controller.
finished() A promise that resolves when no controlled element is queued or revealing, and on destroy(). It never rejects.
destroy() Removes data-reveal-state, restores the data-reveal value and the custom properties it set, cancels timers and releases the observers. Calling it twice is safe; every other method does nothing afterwards.

Control each element from one call. When two calls share an element, only one of them gets its transition callbacks, and destroying either one can affect the other. On pages where the browser bundle starts on its own, use Reveal.show(), Reveal.hide() and Reveal.controller instead of calling Reveal.reveal() on the same elements.

const cards = reveal('.card', { stagger: 60 })

cards.show() // reveal everything now
cards.hide().show() // replay the effect
await cards.finished() // wait for the transitions
cards.update({ duration: 900 }) // change an option

With autoReveal, update() also applies to elements added later.

Presets and custom effects

presets maps a data-reveal value to per-element options. For an element with data-reveal="hero", its own data-reveal-* attributes win, then the preset, then the options of the call. A preset holds no root, callbacks or presets. The preset name must be the whole attribute value (data-reveal="hero", not "hero up"), and only the names you define match.

reveal('.block', { presets: { hero: { effect: 'up blur', duration: 900 } } })

Any other non-empty data-reveal value is left as is, so your CSS can define it. Without CSS of its own it fades.

[data-reveal~='tilt'][data-reveal-state='pending'] {
  rotate: 8deg;
}

REVEAL_EFFECTS

import { REVEAL_EFFECTS } from '@samline/reveal'
// ['up', 'down', 'left', 'right', 'fade', 'zoom', 'blur', 'rotate',
//  'wipe-up', 'wipe-down', 'wipe-left', 'wipe-right']

data-reveal and the effect option also accept several of them separated by spaces.

REVEAL_EASINGS

import { REVEAL_EASINGS } from '@samline/reveal'
// ['smooth', 'snappy', 'spring', 'bounce']

Presets for data-reveal-easing and the easing option. Any other CSS timing function works too.

REVEAL_STAGGER_FROM, REVEAL_STAGGER_EASE and REVEAL_SPLIT

import { REVEAL_STAGGER_FROM, REVEAL_STAGGER_EASE, REVEAL_SPLIT } from '@samline/reveal'
// ['start', 'end', 'center', 'edges', 'random']
// ['linear', 'out']
// ['words', 'chars']

Values for data-reveal-stagger-from and staggerFrom, data-reveal-stagger-ease and staggerEase, and data-reveal-split and split.

Events and EVENT_PREFIX

EVENT_PREFIX is 'reveal:'. Elements dispatch bubbling CustomEvents with detail: { animated: boolean }:

  • reveal:show: the element is shown, exactly when onShow is called.
  • reveal:end: the element becomes revealed (after the transition, or right away when it was shown without one), together with onEnd.
  • reveal:hide: the element is hidden again (a repeat element left the viewport, or hide() was called), together with onHide.
document.addEventListener('reveal:end', event => {
  console.log(event.target, event.detail.animated)
})

Nothing fires after the cleanup.

RevealController, RevealPreset, RevealEffect, RevealEasing, RevealStaggerFrom, RevealStaggerEase, RevealSplit, RevealEventDetail, RevealState, RevealOptions and AutoRevealOptions are exported as types. @samline/reveal/vanilla exports reveal as its default export.

window.Reveal

The browser bundle (dist/browser/reveal.global.js) defines window.Reveal and reveals every [data-reveal] as soon as the DOM is ready.

Member Description
Reveal.reveal(target, options) Same as reveal
Reveal.autoReveal(options) Same as autoReveal
Reveal.auto(options) Starts, or restarts, the automatic run with options and returns its controller
Reveal.controller The controller of the automatic run, or null
Reveal.show(target?) show of the automatic controller; does nothing before it exists
Reveal.hide(target?) hide of the automatic controller; does nothing before it exists
Reveal.refresh() refresh of the automatic controller; does nothing before it exists
Reveal.effects REVEAL_EFFECTS
Reveal.easings REVEAL_EASINGS

Add data-manual to the script tag to skip the automatic start. Calling Reveal.auto() before the DOM is ready also replaces it, so a deferred or inline script after the bundle can pass its own options.