Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute equivalent for `Html.text ""` is missing #141

Open
ChristophP opened this issue Jul 23, 2017 · 10 comments
Open

Attribute equivalent for `Html.text ""` is missing #141

ChristophP opened this issue Jul 23, 2017 · 10 comments

Comments

@ChristophP
Copy link

@ChristophP ChristophP commented Jul 23, 2017

When trying to render an attribute conditionally, one is forced to concatenate lists like this.

div ([style ("border", "red")] ++ (if isAwesome then [attribute "is-awesome" ""] else []) []

It would be nice to have something like an Html.Attributes.none attribute, which would make this simpler:

div [style ("border", "red"), if isAwesome then attribute "is-awesome" "" else none] []

This would be especially useful for custom attributes.

@process-bot

This comment has been minimized.

Copy link

@process-bot process-bot commented Jul 23, 2017

Thanks for the issue! Make sure it satisfies this checklist. My human colleagues will appreciate it!

Here is what to expect next, and if anyone wants to comment, keep these things in mind.

@akoppela

This comment has been minimized.

Copy link

@akoppela akoppela commented Oct 23, 2018

@ChristophP The solution is following:

{-| Empty attribute
-}
empty : Html.Attribute msg
empty =
    Attrs.property "" Encode.null
@ChristophP

This comment has been minimized.

Copy link
Author

@ChristophP ChristophP commented Nov 10, 2018

@akoppela Haha, nice hack :-) It would still write to the "" property of the dom node so it's not exactly the same as doing nothing but I suppose it's good enough :-D

@mismosmi

This comment has been minimized.

Copy link

@mismosmi mismosmi commented May 15, 2019

This would be great. Html.Attributes.batch would work, too.

I end up using

List.filterMap identity
    [ Just some_attribute
    , Maybe.map conditional_attribute
    ]

a lot.

@rlefevre

This comment has been minimized.

Copy link
Member

@rlefevre rlefevre commented Sep 24, 2019

You can also use Html.Attributes.classList [].

@mismosmi

This comment has been minimized.

Copy link

@mismosmi mismosmi commented Sep 24, 2019

@rlefevre

This comment has been minimized.

Copy link
Member

@rlefevre rlefevre commented Sep 24, 2019

But then you have to be careful not to use classList somewhere before
that in the attribute list.

No, see the note in classList documentation:

Note: You can have as many class and classList attributes as you want. They all get applied, so if you say [ class "notice", class "notice-seen" ] you will get both classes!

Html.Attributes.batch would be useful though indeed.

@mismosmi

This comment has been minimized.

Copy link

@mismosmi mismosmi commented Sep 24, 2019

@ChristophP

This comment has been minimized.

Copy link
Author

@ChristophP ChristophP commented Sep 24, 2019

You can also use Html.Attributes.classList [].

Cool hack. What about style "" "" ? would that work?

@rlefevre

This comment has been minimized.

Copy link
Member

@rlefevre rlefevre commented Sep 24, 2019

You can also use Html.Attributes.classList [].

Cool hack. What about style "" "" ? would that work?

As far as I know, all these hacks can have a small impact:

  • classList [] and class "" with lead to class="" if there is no other class
  • style "" "" will create a property "" with value "" in the element style property
  • property "" Encode.null will create a property "" with value null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.