Hello!
I thought I'd continue the conversation from the other issue.
Let's take a look at the library as String
s:
stringExample = div [ class box ] []
If we want to add another class, we have a few obvious options:
stringsExample1 = div [ class box, class hasTextCentered ] []
stringsExample2 = div [ class <| box ++ " " ++ hasTextCentered ] []
stringsExample3 = div [ classList [ box, hasTextCentered ] ] []
And if we want to vary the alignment, we're looking at something like this:
empty = ""
stringsExample4 alignment
= div [ class box
, case alignment of
Left -> class empty
Center -> class hasTextCentered
Right -> class hasTextRight
]
[
]
By moving to Html.Attribute
, we get something like this:
attrExample = div [ box ] []
Already much cleaner!
If we want to add another class, we have one straightforward path:
attrsExample = div [ box, hasTextCentered ] []
And to vary the alignment, we get something like this:
empty = class ""
stringsExample4 alignment
= div [ box
, case alignment of
Left -> empty
Center -> hasTextCentered
Right -> hasTextRight
]
[
]
For more on multiple class attributes, check out this issue.
So here's the tradeoff: moving to Html.Attribute
will make most code much cleaner, but may make compound classes more difficult. Consider the following:
centeredBox = Html.Attribute
centeredBox = classList [ box, hasTextCentered ]
stringExample = div [ style [], centeredBox ] []
There's no way to transform multiple Html.Attribute
s into a single one, so we're stuck with the following:
centeredBox = List Html.Attribute
centeredBox = [ box, hasTextCentered ]
attrExample = div ( style [] :: centeredBox ) []
For me, this tradeoff is totally worth it -- which is why I've opened this issue!
Are there any problems that I'm overlooking?