useElementVisibility#

Whether an element intersects the viewport (or a scroll container), as a reactive boolean. Browser composable — import from @sigx/use-web.

It builds on useIntersectionObserver and returns a ReadSignal<boolean>.

TypeScript
import { signal, effect } from 'sigx';
import { useElementVisibility } from '@sigx/use-web';

const heroRef = signal<HTMLElement | null>(null);
// <section ref={heroRef}> …

const visible = useElementVisibility(heroRef);
effect(() => {
    if (visible.value) startAnimation();
});

Pass any element target. Set scrollTarget to observe within a scroll container instead of the viewport, and threshold to require more of the element on screen before it counts as visible.

Signature#

TypeScript
function useElementVisibility(
    target: MaybeSignalElement,
    options?: UseElementVisibilityOptions,
): ReadSignal<boolean>;

Options#

OptionTypeDefaultDescription
thresholdnumberVisibility ratio required before visible flips true.
scrollTargetMaybeSignalElementviewportObserve within this scroll container instead of the viewport.
windowWindowdefaultWindowOverride the window (SSR/testing).

SSR#

false — the element is reported not visible until the observer runs on mount.

See also#