Helper functions for conditionally rendering HTML elements.
These utilities make it easier to work with Html
in a more declarative way.
A lazy version of contentList for performance optimization.
This function takes a list of thunks (functions that take unit () and return HTML)
paired with boolean values. The thunks are evaluated only when their corresponding
boolean is True, making this function more efficient when the HTML generation is
expensive but condition evaluation is cheap.
Example:
viewAnimals : List Animal -> Html msg
viewAnimals animals =
Html.section [ Attributes.id "animals" ] <|
lazyContentList
[ ( \_ -> expensiveRenderFunction animal1, animal1.visible )
, ( \_ -> expensiveRenderFunction animal2, animal2.visible )
, ( \_ -> expensiveRenderFunction animal3, animal3.visible )
]Conditionally render HTML nodes based on Maybe values.
This function is similar to lazyContentList but works with Maybe values instead of
booleans. HTML nodes will only be rendered for items that have a Just value.
When the value is Nothing, the node is omitted from the output.
Example:
view =
maybeContentList
[ ( \text -> Html.text text, Just "This will be rendered" )
, ( \text -> Html.text text, Nothing ) -- This will be omitted
, ( \user -> viewUser user, maybeUser ) -- Rendered only if maybeUser is Just
]Convert a list of HTML nodes into a single node, following these rules:
nothing (an empty text node)div using display: contentsUsing display: contents makes the wrapper transparent in the DOM layout,
allowing children to participate in the parent's layout as if they were direct children.
More about display: contents: https://developer.mozilla.org/en-US/docs/Web/CSS/display#contents
A concise way to render nothing.
This is a convenience function that provides the idiomatic approach for rendering
nothing in Elm: an empty text node. Use this instead of Html.text "" for improved
readability and consistency.
Render HTML based on a Maybe value.
This function takes a view function and a Maybe value. If the value is Just, it renders
the value using the provided view function. If the value is Nothing, it returns an empty
text node.
Example:
viewUser : Maybe User -> Html msg
viewUser maybeUser =
maybeNode viewUserDetails maybeUserRender HTML based on a Result value.
This function takes two view functions - one for the error case and one for the success case - and a Result value. It renders either the error or success view depending on the Result.
Example:
viewUserData : Result String User -> Html msg
viewUserData result =
resultNode viewError viewUser resultRender HTML for the success case of a Result.
This is a convenience function that only renders the success case of a Result, returning an empty text node for the error case.
Example:
viewUserData : Result String User -> Html msg
viewUserData result =
successNode viewUser resultRender HTML for the error case of a Result.
This is a convenience function that only renders the error case of a Result, returning an empty text node for the success case.
Example:
viewError : Result String User -> Html msg
viewError result =
errNode viewErrorMessage result
Conditionally render HTML nodes based on boolean flags.
This function is conceptually similar to
Attributes.classList, but for content nodes rather than CSS classes. Each entry in the list will be included in the output only if its associated boolean value isTrue.For example: