Elm condition within attribute list

May 09, 2019

Today I was reviewing some ELM code and noticed a nice simple trick that is going to change my life! It has to do with conditional Html.Attributes. Let’s start by how I was doing my conditional Attributes:

[class "hello"] ++ (if bool then [onClick msg] else [])

Pretty ugly, right? Now imagine that I have more than one conditional Attribute.

Enter the empty Attribute.

empty = 
    Html.Attributes.class ""

This blew my mind, how did I never thought of that? It’s so simple and elegant. The code above would then write:

[class "hello", if bool then onClick msg else empty]