mdgriffith / elm-ui

What if you never had to write CSS again?

Home Page:https://package.elm-lang.org/packages/mdgriffith/elm-ui/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature request: add attributes retroactively

staeter opened this issue · comments

I find myself wanting to add attributes to an element I've initiated in an other function quite often.

addAttributes : List (Element.Attribute msg) -> Element msg -> Element.msg

It would be useful whenever you want to overwrite some attributes

redButton =
    Element.Input.button
         [ Element.Background.color (Element.rgb 1 0 0) ]
         {...}

blueButton =
    addAttributes [ Element.Background.color (Element.rgb 1 0 0) ] redButton

Also when you want to move it around without encapsulating it and adding a parent in between

movedButton topLeft =
    addAttributes [ Element.moveDown point.y, Element.moveRight point.x ] redButton

You can already do something like this:

withAttributes :
    List (Element.Attribute msg)
    -> (List (Element.Attribute msg) -> a)
    -> (List (Element.Attribute msg) -> a)
withAttributes attributes constructor =
    \newAttributes ->
        constructor (newAttributes ++ attributes)

and then

redButton =
    Input.button |> withAttributes [ Background.color (Element.rgb 1 0 0) ]


redBorderedButton =
    redButton
        |> withAttributes
            [ Border.width 1
            , Border.color (Element.rgb 1 0 0)
            ]