This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
Pull to refresh API
version 1.3.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 1.3.0
Committed At 2018-04-28 13:13:42 UTC
zwilias/elm-touch-events 1.2.0 <= v < 2.0.0 1.2.0
elm-lang/html 2.0.0 <= v < 3.0.0 2.0.0
elm-lang/dom 1.1.1 <= v < 2.0.0 1.1.1
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1
elm-lang/animation-frame 1.0.1 <= v < 2.0.0 1.0.1
elm-community/easing-functions 1.0.2 <= v < 2.0.0 1.0.2

Modules

README

elm-pull-to-refresh Build Status

elm package install FabienHenon/elm-pull-to-refresh

Pull to refresh allows you to pull the top of your page to refresh your content. This is working with touches and mouse.

Getting started

Types

First you need to add pull to refresh to your messages and your model.

import PullToRefresh

type Msg
    = PullToRefreshMsg PullToRefresh.Msg

type alias Model =
    { pullToRefresh : PullToRefresh.Model
    , content : List String
    }

Configuration

Sets the configuration of the pull to refresh module

pullToRefreshConfig : PullToRefresh.Config Msg
pullToRefreshConfig =
    PullToRefresh.config "your-content-div-id"
        |> PullToRefresh.withRefreshCmd onLoad

Initialization

Initializes your model.

init : ( Model, Cmd Msg )
init =
    let
        ( pullToRefresh, cmd ) =
            PullToRefresh.init pullToRefreshConfig
    in
        ( { pullToRefresh = pullToRefresh, content = initialContent }, Cmd.map PullToRefreshMsg cmd )

View

Then, you need to add your pull to refresh component to your view.

view : Model -> Html Msg
view model =
    PullToRefresh.view PullToRefreshMsg
        pullToRefreshConfig
        model.pullToRefresh
        []
        [ div []
            (List.map text model.content)
        ]

This will create an absolute element using the full space of its parent. Inside it, your content and attributes will be added in another element with touch and scroll handling. If you need to handle scroll manually see withManualScroll function.

Refresh cmd

You have to define a function that will be called when we need to refresh page content. This function must return a Cmd Msg.

Here is an example with data retrieved from a remote API:

type Msg
    -- ... add this message
    | OnDataRetrieved (Result Http.Error String)

onLoad : Cmd Msg
onLoad =
    Http.getString "https://example.com/retrieve-more"
        |> Http.send OnDataRetrieved

Update

Finally, all we need to do is to implement the update function.

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        PullToRefreshMsg msg_ ->
            let
                ( pullToRefresh, cmd ) =
                    PullToRefresh.update PullToRefreshMsg msg_ pullToRefreshConfig model.pullToRefresh
            in
                ( { model | pullToRefresh = pullToRefresh }, cmd )

        OnDataRetrieved (Err _) ->
            -- Don't forget to handle error
            ( model, PullToRefresh.stopLoading PullToRefreshMsg )

        OnDataRetrieved (Ok result) ->
            let
                content =
                    addContent result model.content
            in
                ( { model | content = content }, PullToRefresh.stopLoading PullToRefreshMsg )

In the update you have to handle pull to refresh update. It will return an updated model and a command to execute.

You also have to handle your data refreshing, and don't forget to call stopLoading so that loading animation finishes and the user is able to pull the view again.

Examples

To run the examples go to the examples directory, install dependencies and run elm-reactor:

> cd examples/
> elm package install
> elm-reactor