joshburgess/elm-route-craft/1.0.0

Layout

Composable layout wrappers.

Layouts wrap page views with surrounding UI (navigation, sidebar, footer, etc). They can be stateless or stateful.

Stateless layout

sidebarLayout : Layout shared () Never msg
sidebarLayout =
    Layout.static
        { key = "sidebar"
        , view = \shared pageView ->
            Html.div []
                [ sidebar shared
                , Html.div [ class "content" ] [ pageView ]
                ]
        }

Stateful layout

collapsibleSidebar : Layout shared SidebarModel SidebarMsg msg
collapsibleSidebar =
    Layout.stateful
        { key = "sidebar"
        , init = \shared -> ( { open = True }, Cmd.none )
        , update = \msg model -> ( { model | open = not model.open }, Cmd.none )
        , view = \shared model pageView ->
            Html.div []
                [ if model.open then sidebar shared else Html.text ""
                , pageView
                ]
        , subscriptions = \shared model -> Sub.none
        }

Types

type Layout shared layoutModel layoutMsg pageMsg

An opaque layout type.

Type parameters:

  • shared : the shared state type
  • layoutModel : this layout's own model (use () for stateless)
  • layoutMsg : this layout's own message type (use Never for stateless)
  • pageMsg : the page message type that flows through

Constructors

none : Layout shared () Never msg

No layout -- the page renders bare.

{ key : String
, view : shared -> Html pageMsg -> Html pageMsg
}
-> Layout shared () Never pageMsg

Create a stateless layout.

The key is used to determine layout identity: pages sharing the same layout key share layout state (relevant for stateful layouts) and don't trigger layout re-initialization during navigation.

{ key : String
, init : shared -> ( layoutModel, Cmd layoutMsg )
, update : layoutMsg -> layoutModel -> ( layoutModel, Cmd layoutMsg )
, view :
shared -> layoutModel -> Html pageMsg -> Html (Msg layoutMsg pageMsg)
, subscriptions : shared -> layoutModel -> Sub layoutMsg
}
-> Layout shared layoutModel layoutMsg pageMsg

Create a stateful layout with its own model, messages, and lifecycle.

Layout state persists across page transitions when pages share the same layout key. The layout is only re-initialized when navigating to a page with a different layout key.

Wiring

withLayout : Layout shared lm lmsg pageMsg -> String

Placeholder: associate a layout with a page. Returns the layout key for use by the App module.

getLayoutKey : Layout shared layoutModel layoutMsg pageMsg -> String

Get the key that identifies this layout.

Layout shared () Never pageMsg
-> shared
-> Html pageMsg
-> Html pageMsg

Apply a stateless layout to page content.

Internal Types

type Msg layoutMsg pageMsg
= LayoutMsg layoutMsg
| PageMsg pageMsg

Combined message type for stateful layouts. Pages produce PageMsg, the layout itself produces LayoutMsg.

type LayoutState layoutModel
= NoState
| HasState String layoutModel

Runtime state for a layout instance.

Runtime

Layout shared layoutModel layoutMsg pageMsg
-> shared
-> ( LayoutState layoutModel, Cmd layoutMsg )

Initialize layout state.

Layout shared layoutModel layoutMsg pageMsg
-> layoutMsg
-> LayoutState layoutModel
-> ( LayoutState layoutModel, Cmd layoutMsg )

Update layout state with a layout message.

Layout shared layoutModel layoutMsg pageMsg
-> shared
-> LayoutState layoutModel
-> Html pageMsg
-> Html (Msg layoutMsg pageMsg)

Render a stateful layout with its state and page content.

Layout shared layoutModel layoutMsg pageMsg
-> shared
-> LayoutState layoutModel
-> Sub layoutMsg

Get subscriptions from a stateful layout.