Code Monkey home page Code Monkey logo

elm-toast's Introduction

elm-toast

A view agnostic way to handle toasts and other temporary notifications.

A typical application would be to display temporary notifications as Html views in your elm app. This module takes care of updating the notifications and let's you decide what content should be included in your notifications and how these should be represented in your view.

A demo can be found here: https://ellie-app.com/3rkbwjFF6KBa1/0

Screenshot

This is a screenshot of the example app. The design is part of your app's responsibility. The concept of this package is that it does not impose any view type or design on your app (it does not even require views to be Html).

Screenshot of elm-toast example

Minimal implementation

Your app should contain the current time and the Toast somewhere in its model.

type alias Model =
    { time : Time
    , toast : Toast String
    }

You will then need to subscribe to time updates. The frequency of the updates will determine the update frequency of the toast and how accurately start times, expiration times and transition delays are respected:

type Msg
    = Tick Time
    | PostNotification String


subscriptions : Model -> Sub Msg
subscriptions model =
    Time.every (Time.second * 1) Tick

In your update method you will need to update the time and update the toast in your model on every Tick message. In addition you will probably want to add any posted notification to your model's Toast:

pureUpdate : Msg -> Model -> Model
pureUpdate msg model =
    case msg of
        Tick newTime ->
            let
                newToast =
                    Toast.updateTimestamp newTime model.toast
            in
                updateTime newTime model
                    |> updateToast newToast

        PostNotification notification ->
            let
                newToastNotification =
                    Toast.createNotification notification (model.time + Time.second * 3)

                newToast =
                    Toast.addNotification newToastNotification model.toast
            in
                updateToast newToast model


updateTime : Time -> Model -> Model
updateTime time model =
    { model | time = time }


updateToast : Toast String -> Model -> Model
updateToast toast model =
    { model | toast = toast }

elm-toast's People

Contributors

felixlam avatar

Watchers

Julian Pistorius avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.