Class InView<CallbacksMap, StaticProps, MutableProps>

InView is a visibility detection utility that leverages the IntersectionObserver API to monitor when elements enter or leave the viewport. It provides customizable options for triggering events, delaying visibility changes, and dynamically adding CSS classes to elements based on their visibility state.

Documentation

Type Parameters

Hierarchy (View Summary)

Constructors

Properties

_callbacks: Callbacks<CallbacksMap>

Callbacks instance

_destroyable: () => void[] = []

Stores actions that need to be executed when the module is destroyed

_elements: IInViewElement[] = []

Stores the elements being observed.

_isDestroyed: boolean = false

Tracks whether the module has been destroyed

_isInitialStart: boolean = true

Tracks whether this is the first time the elements are being observed.

_observerIn?: IntersectionObserver

Intersection observer for detecting elements entering the viewport.

_observerOut?: IntersectionObserver

Intersection observer for detecting elements leaving the viewport.

_props: TTRequiredProps<MutableProps & StaticProps>

Current properties

Accessors

  • get isDestroyed(): boolean

    Checks if the module has been destroyed.

    Returns boolean

  • get name(): string

    The name of the module, derived from the class name

    Returns string

  • get prefix(): string

    Optional prefix for classnames used by the module

    Returns string

Methods

  • Adds a class name on an element, and keeps track of it for removal when the module is destroyed.

    Parameters

    • element: Element

      The target DOM element.

    • className: string

      The class name to toggle.

    Returns void

  • Helper function to generate classnames with the module's prefix.

    Parameters

    • ...classNames: string[]

      The class names to generate.

    Returns string

    A string of class names with the module's prefix applied.

  • Handles elements entering the viewport.

    Parameters

    • data: IntersectionObserverEntry[]

    Returns void

  • Handles elements leaving the viewport.

    Parameters

    • data: IntersectionObserverEntry[]

    Returns void

  • Registers an element for visibility observation.

    If the element has a data-in-view-class attribute, the specified class will be applied upon entering the viewport.

    Parameters

    • element: Element

    Returns () => void

    A function to stop observing the element.

  • Adds a callback on the module's destruction.

    Parameters

    • action: () => void

      The function to execute during destruction.

    Returns void

  • Removes an element from observation, preventing further visibility tracking.

    Parameters

    • element: Element

    Returns void