Skip to content

Use attributes instead of properties #259

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

lydell
Copy link

@lydell lydell commented Jun 1, 2025

Intro

This PR is a companion to elm/virtual-dom#187. That elm/virtual-dom PR works without this elm/html PR, but virtualization won’t work 100 % without it.

This PR removes the stringProperty helper function, and uses attributes for the exposed functions that used it instead. The boolProperty function is kept, and I added a comment for why.

Details

Most functions in Html.Attributes that take a String used this helper function:

stringProperty : String -> String -> Attribute msg
stringProperty key string =
  Elm.Kernel.VirtualDom.property key (Json.string string)

For example, href:

href : String -> Attribute msg
href url =
  stringProperty "href" (Elm.Kernel.VirtualDom.noJavaScriptUri url)

In other words, lots of the Html.Attributes functions were implemented by setting properties.

This PR removes stringProperty, and instead prefers attributes over properties. Here’s href in this PR:

href : String -> Attribute msg
href url =
  Elm.Kernel.VirtualDom.attribute "href" (Elm.Kernel.VirtualDom.noJavaScriptUri url)

In short, attributes are preferred because:

  1. Attributes can be removed, while properties often cannot. Closes elm/html#228, closes elm/html#148, closes elm/virtual-dom#122, closes elm/virtual-dom#169
  2. Virtualization is way easier when most Html.Attributes functions are attributes. Closes elm/virtual-dom#144
  3. Some properties are read-only and therefore throw errors if you try to set them.
  4. Attributes are easier to diff.

I explain those points more in the properties-vs-attributes.md file (which I updated in this PR).

Comment on lines -786 to -788
downloadAs : String -> Attribute msg
downloadAs =
stringProperty "download"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was unused (not exposed).

Comment on lines +463 to +468
value string =
-- Note: `.value` has no corresponding attribute, so we have to set it
-- using a property. It can also be modified by the user by typing in inputs.
-- Properties are diffed against the actual DOM, not the virtual DOM, so
-- this ensures that the DOM is up-to-date with the model.
Elm.Kernel.VirtualDom.property "value" (Json.string string)
Copy link
Author

@lydell lydell Jun 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is an exception where we have to use a property, not an attribute.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Properties are diffed against the actual DOM, not the virtual DOM, so this ensures that the DOM is up-to-date with the model.

This refers to the implementation in elm/virtual-dom#187

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant