X Enterprises
Composables

useScrollReveal

Intersection Observer composable that adds is-visible to animation target elements when they enter the viewport.

useScrollReveal

Initializes an IntersectionObserver that watches all [data-reveal], .xFadeUp, .xFadeIn, .xFadeLeft, .xFadeRight, .xScale, and .xFadeUp-stagger elements. When any target enters the viewport the class is-visible is added, triggering the corresponding CSS animation. Called automatically by the marketing.client.ts plugin — use directly for fine-grained control.

The composable registers lifecycle hooks automatically, so it must be called inside a component setup. Calling it outside a component instance returns a no-op stub for safe imports in shared modules.

Usage

// Called automatically by the global plugin on mount.
// Call directly when you need manual control:
useScrollReveal()

// With custom options:
useScrollReveal({ threshold: 0.2, rootMargin: '0px 0px -80px 0px', once: false })

Parameters

ParameterTypeDefaultDescription
optionsobject{}Observer + behavior options. Supports threshold, rootMargin, once.
options.thresholdnumber0.15Forwarded to IntersectionObserver. Fraction of element that must be visible to trigger.
options.rootMarginstring'0px 0px -80px 0px'Forwarded to IntersectionObserver. Negative bottom inset fires earlier as the element scrolls into view.
options.oncebooleantrueIf true (default), the observer stops watching an element after it has appeared once. If false, the class is removed when the element scrolls out of view and re-added on re-entry.

Returns

KeyTypeDescription
initScrollReveal() => voidManually initialize the observer (e.g. after dynamic DOM changes). The plugin calls this for you on mount.
cleanup() => voidDisconnect the observer and release DOM references. Called automatically on unmount.

Observed Selectors

SelectorEffect
[data-reveal]Any element with data-reveal attribute is watched.
.xFadeUpFade-up animation targets.
.xFadeInFade-in animation targets.
.xFadeLeftFade-left animation targets.
.xFadeRightFade-right animation targets.
.xScaleScale animation targets.
.xFadeUp-staggerStagger container targets.

When a target intersects the viewport, the class is-visible is applied.

Example

<script setup>
// Fine-grained control — observe with a tighter threshold
useScrollReveal({ threshold: 0.3 })
</script>

<template>
  <section>
    <h2 class="xFadeUp">Revealed on scroll</h2>
    <p data-reveal>This paragraph fades in when visible.</p>
  </section>
</template>

Re-initializing after dynamic DOM changes:

<script setup>
const { initScrollReveal, cleanup } = useScrollReveal()

async function reload() {
  // ... fetch and inject new content
  cleanup()
  await nextTick()
  initScrollReveal()
}
</script>

AI Context

composable: useScrollReveal
package: "@xenterprises/nuxt-x-marketing"
use-when: >
  Adding scroll-triggered CSS animations to any element. The plugin calls this
  automatically on app mount — only call it directly when you need custom
  IntersectionObserver options (threshold, rootMargin, once) or want to
  re-initialize after dynamic DOM changes. Pairs with xFadeUp, xFadeIn,
  xFadeLeft, xFadeRight, xScale, and xFadeUp-stagger CSS classes from
  x-marketing.css.
Copyright © 2026