rl-king/elm-inview/2.0.0

NOTE: the latest version is 2.0.1

Rerouted (regenerated, verified). Same repository and tag, but GitHub now builds the zip with different bytes, so it no longer matches the SHA-1 package.elm-lang.org recorded. The Lamdera package server serves a copy recovered from https://github.com/rl-king/elm-inview.

What we checked: Tag 2.0.0 is still on github.com/rl-king/elm-inview; its commit b3b7905f4365 is from 2020-06-22 19:39 UTC, before the version was published (2020-06-22 19:47 UTC); every file Elm uses matches a copy in a Lamdera package cache from 2021-02-13, from before the change; elm.json is byte-identical to package.elm-lang.org's; the exposed modules match docs.json; the zip's SHA-1 is not the published one (GitHub's zip no longer comes out byte for byte the same), so the contents were checked instead.

The Lamdera compiler uses this copy and prints a one-line notice saying so. elm itself fails to download this version.

Details · package.elm-lang.org's zip: https://github.com/rl-king/elm-inview/zipball/2.0.0/

InView

Get information on an element position relative to the current viewport.

Definition

type State

Keeps track of viewport position, viewport dimensions and element positions.

type alias Viewport =
{ scene :
{ width : Float
, height : Float
}
, viewport :
{ x : Float
, y : Float
, maxX : Float
, maxY : Float
, width : Float
, height : Float
}
}

Similar to Browser.Dom.Viewport with the addition of maxX and maxY.

You can use maxX and maxY to check if an element has ever been in the viewport before.

type alias Element =
{ x : Float
, y : Float
, width : Float
, height : Float
}

Similar to Browser.Dom.Element without scene and viewport.

Init

init : (Msg -> msg) -> List String -> ( State, Cmd msg )

Takes a list of element ids you want to track and attempts to find them on the current page.

Update

type Msg

A message type for the state to update.

update : (Msg -> msg) -> Msg -> State -> ( State, Cmd msg )

Update viewport size and element positions.

{ x : Float
, y : Float
}
-> State
-> State

Update current viewport x and y offset.

subscriptions : (Msg -> msg) -> State -> Sub msg

Subscribe to browser resize events to recalculate element positions.

addElements : (Msg -> msg) -> List String -> State -> ( State, Cmd msg )

Track element positions after you've initialized the state.

Use this when the page content moves around after initialization, like for example when images load and stuff gets pushed down.

Check

isInView : String -> State -> Maybe Bool

True if the element with the given id is in the current viewport.

note: The result is a Maybe because the element might not be on the page at all.

check

isInOrAboveView : String -> State -> Maybe Bool

True if the element with the given id is in or above the current viewport.

note: The result is a Maybe because the element might not be on the page at all.

checkAlt

Check with margin

type alias Margin =
{ top : Float
, right : Float
, bottom : Float
, left : Float
}
isInViewWithMargin : String -> Margin -> State -> Maybe Bool

True if the element with the given id is in the current viewport but with an offset. A positive offset will make the viewport smaller and vice versa.

note: The result is a Maybe because the element might not be on the page at all.

isInViewWithMargin

isInOrAboveViewWithMargin : String -> Margin -> State -> Maybe Bool

True if the element with the given id is in or above the current viewport but an offset. A positive offset will make the viewport smaller and vice versa.

note: The result is a Maybe because the element might not be on the page at all.

isInOrAboveViewWithMargin

Custom check

custom : (Viewport -> Maybe Element -> a) -> String -> State -> a

Write your own check function.

For example isInOrAboveViewWithMargin is implemented like:

isInOrAboveViewWithMargin : String -> Margin -> State -> Maybe Bool
isInOrAboveViewWithMargin id margin state =
    let
        calc { viewport } element =
            (viewport.y - margin.bottom + viewport.height > element.y)
                && (viewport.x - margin.right + viewport.width > element.x)
    in
    custom (\a b -> Maybe.map (calc a) b) id state

note: Element is a Maybe because the element might not be on the page at all.