It works like elm/html, and the context can be used anywhere.
Refer to Example.elm for details.
Import ContextHtml instead of Html.
import ContextHtml exposing (..)
import ContextHtml.Lazy exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)Define Context as part of Model.
type alias Model =
{ hoge : Hoge
, context : Context
}
type alias Context =
{ contextValue : Value
}Update Model and Context.
HTML tags defined in ContextHtml returns ContextHtml ctx msg.
view : Model -> HtmlWithContext Msg
view model =
div [] [ viewHoge model ]
-- It is useful to define an alias.
type alias HtmlWithContext msg =
ContextHtml Context msgTo use a value of Context, use useContext.
For handling multiple values, useContext2, useContext3, etc. are provided.
viewWithContext : HtmlWithContext Msg
viewWithContext =
-- Context has a contextValue
useContext .contextValue <|
\v -> div [] [text v ]main =
Browser.sandbox
{ init = init
, update = update
-- Inject context
, view = viewWithContext .context view
}