InViewOptions
InViewOptions configures the pooled IntersectionObserver used by useInViewState. It extends the browser’s IntersectionObserverInit and adds Fractalsvelte’s once and amount conveniences.
Usage
<script lang="ts">
import { type InViewOptions, useInViewState } from "fractalsvelte/motion";
let section = $state<HTMLElement>();
const options: InViewOptions = {
once: true,
amount: 0.4,
rootMargin: "0px 0px -12%",
};
const visibility = useInViewState(() => section, options);
</script>
<section bind:this={section} data-visible={visibility.inView}>
Reveal this content when 40% enters the viewport.
</section>
API
| Field | Type | Default | Description |
|---|---|---|---|
once | boolean | false | Preserves the entered state after the first intersection. |
amount | "some" \| "all" \| number | "some" | Shorthand for the intersection threshold. |
root | Element \| null | viewport | Scroll container used as the observer root. |
rootMargin | string | "0px" | CSS-like margin applied to the observer root bounds. |
threshold | number \| number[] | 0 | Native threshold; used when amount is not a number or "all". |
interface InViewOptions extends IntersectionObserverInit {
once?: boolean;
amount?: "some" | "all" | number;
}
amount: "some" observes at the native threshold (or 0 by default). amount: "all" uses a threshold of 1, and a numeric amount takes precedence over threshold for the observer.
Behaviour
useInViewState shares observer instances when their root, root margin, and effective threshold match. It cleans up the target listener when its Svelte owner is destroyed. When once is enabled, inView remains true after the first intersection; the most recent observed entry remains available through entry.
The helper does nothing in environments without IntersectionObserver, so no browser-only global is accessed during server rendering.
Related API
Use useScrollProgress for continuous scroll values. For reveal effects, combine the inView state with the package’s motion primitives and preserve a meaningful static state for reduced-motion users.
