Skip to content

feature: allow same parsing of style attributes as for class #445

Open
@krilllind

Description

@krilllind

As described by this comment it is possible to write expressions like the following for class bindings as false and nil values are discarded:

<div class={["my-class", @foo? and "my-class--modifier"]}>
  ...
</div>

This is great because it allows developers to easily implement additional css classes if a condition is true and otherwise it will just be discarded. However the same behaviour doesn't work for the style attribute:

<div style={["--my-target: 100", "--my-value: 50", @label and "--label: #{@label}"]}>
  ...
</div>

This raises the argument error (from here)

lists in Phoenix.HTML and templates may only contain integers representing bytes, binaries or other lists, got invalid entry: nil

Given the flexibility that CSS custom properties offers I think it would be a great addition to the syntax.

Current workaround to this issue would be to write it as the following (which doesn't read as good and looks more complex than needed to be):

<div style={["--my-target: 100", "--my-value: 50", (if @label, do: "--label: #{@label}", else: "")]}>
  ...
</div>

Is there any security concern in play here or would this be an easy addition?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions