dillonkearns / elm-pages / PagesMsg

In elm-pages, Route modules have their own Msg type which can be used like a normal TEA (The Elm Architecture) app. But the Msg defined in a Route module is wrapped in the PagesMsg type.


type alias PagesMsg userMsg =
Pages.Internal.Msg.Msg userMsg

fromMsg : userMsg -> PagesMsg userMsg

import Form
import Pages.Form
import PagesMsg exposing (PagesMsg)

type Msg
    = ToggleMenu

view :
    Maybe PageUrl
    -> Shared.Model
    -> Model
    -> App Data ActionData RouteParams
    -> View (PagesMsg Msg)
view maybeUrl sharedModel model app =
    { title = "My Page"
    , view =
        [ button
            -- we need to wrap our Route module's `Msg` here so we have a `PagesMsg Msg`
            [ onClick (PagesMsg.fromMsg ToggleMenu) ]
            []

        -- `Pages.Form.renderHtml` gives us `Html (PagesMsg msg)`, so we don't need to wrap its Msg type
        , logoutForm
            |> Pages.Form.renderHtml []
                Pages.Form.Serial
                (Form.options "logout"
                    |> Form.withOnSubmit (\_ -> NewItemSubmitted)
                )
                app
        ]
    }

map : (a -> b) -> PagesMsg a -> PagesMsg b

noOp : PagesMsg userMsg

A Msg that is handled by the elm-pages framework and does nothing. Helpful for when you don't want to register a callback.

import Browser.Dom as Dom
import PagesMsg exposing (PagesMsg)
import Task

resetViewport : Cmd (PagesMsg msg)
resetViewport =
    Dom.setViewport 0 0
        |> Task.perform (\() -> PagesMsg.noOp)