Getting Started
Svelte Reveal is a library created with the purpose of helping Svelte users add reveal on scroll animations to their web applications in the easiest way possible. This library leverages the Intersection Observer API in order to know when to trigger the animations.
Installation
Usage
-
Import the library in your Svelte component:
-
Add the imported
reveal
action to the DOM element you want to transition:If you want to use the action on a Svelte component, you need to pass the reveal options via props:
Rationale
If you happened to scout the internet for other similar libraries, you might have noticed that other authors have decided to create their own library using Svelte slots (similar to React children). There is nothing wrong with that approach, but in my opinion it goes a bit against one of Svelte’s core purpose: writing more concise code. Having to wrap every to-be-transitioned component adds at least 2 extra lines of code each time, making your files unnecessarily bloated for such a simple add-on.
You might have also noticed people adding event listeners to the window object in order to transition elements, but in terms of performance it doesn’t scale very well.
Instead, I decided to use Svelte actions, which are functions you can attach to a DOM element and that allow you to get access to that particular element and hook into its lifecycle. They take up considerably fewer lines of code, and so far I haven’t encountered any big obstacle or performance drawback. Morever, this library is backed by the Intersection Observer API, which is great for performance.