A not-quite-drop-in replacement for the Svg.Lazy module from the
elm-lang/svg package.
The only difference between this and the lazy from elm-lang/svg is that the
caller must call toUnstyled inside the function being passed to lazy.
For example:
import Css exposing (backgroundColor, rgb)
import Svg.Styled exposing (Svg, circle, svg, toUnstyled)
import Svg.Styled.Attributes exposing (css, cx, cy, r, viewBox)
import Svg.Styled.Lazy exposing (lazy)
import VirtualDom
view : Model -> Svg Msg
view model =
svg [ viewBox "0 0 100 100" ]
[ lazy viewCircle model.radius
]
viewCircle : Int -> VirtualDom.Node msg
viewCircle radius =
circle
[ cx "60"
, cy "60"
, r (toString radius)
, css [ backgroundColor (rgb 200 0 0) ]
]
|> toUnstyled -- toUnstyled must happen inside viewCircle!Same as lazy but checks on two arguments.
Same as lazy but checks on three arguments.
A performance optimization that delays the building of virtual DOM nodes.
Calling
(view model)will definitely build some virtual DOM, perhaps a lot of it. Calling(lazy view model)delays the call until later. During diffing, we can check to see ifmodelis referentially equal to the previous value used, and if so, we just stop. No need to build up the tree structure and diff it, we know if the input toviewis the same, the output must be the same!