Apply context to ContextHtml
view : Html msg
view =
let
context = ... -- make Context
in
applyContext context viewChild
viewChild : ContextHtml Context msg
viewChild = ...Create ContextHtml
view : ContextHtml ctx msg
view =
withContext <| \ctx -> Html.div [] []Wrap Html to ContextHtml
view : ContextHtml ctx msg
view =
div [] [ wrapHtml <| viewCommon "Text" ]
viewCommon : String -> Html msg
viewCommon = ...Use a context value
type alias Context =
{ text : String }
view =
useContext .text <| \text_ -> div [] [ text text_ ]When Context is Opaque type
-- Context.elm
type Context
= Context { text : String }
-- Expose Selector accessing context value
textSelector (Context { text }) =
text
textWithPrefixSelector (Context { text }) =
"prefix_" ++ text
-- Main.elm
view1 =
useContext textSelector <| \text_ -> div [] [ text text_ ]
view2 =
useContext textWithPrefixSelector <| \text_ -> div [] [ text text_ ]Use context values
type alias Context =
{ text : String
, color : String
}
view =
useContext2 .text .color <| \text_ color -> div [ style "color" color ] [ text text_ ]
Html with context