#1112·snabbdom

Attribute behavior should be improved

Author: bgradinCreated Mar 2, 2024Updated May 18, 2024

TL;DR: The module system in Snabbdom provides a powerful way to extend the core functionality, but it leads to conventions for vnode attributes that, as a newcomer to this project, I found counterintuitive and would like to improve.

Problem statement

For example, whereas with React.createElement, you would write something like createElement("a", { href: "/" }, "Home"), Snabbdom requires you to wrap attributes in an object called attrs so they can be handled by the attributes module: h("a", { attrs: { href: "/" } }, "Home"). I found this particularly unexpected when using JSX - a JSX snippet of <a href="/">Home</a> will create an HTML element without an href: <a>Home</a>!

Proposal

I think the crux of the issue is that Snabbdom does not provide a mechanism for modules to specify a limited set of keys they will use to access data on a vnode. If a comprehensive list of these keys was maintained internally, any additional keys in the vnode data could be treated as attributes, rather than being disregarded. I think it would make the most sense to make this update in the h function, so both JS and JSX syntax would reap the benefits.

My preference would be for this to be the default functionality, but I suppose this might require a new major or at least minor release. If the maintenance team prefers, it should be possible to gate this functionality behind a configuration option in the init method to preserve backward compatibility.

It's worth pointing out that other projects like herp-inc/snabbdom-jsx provide functionality similar to what I'm describing. However, my point is the behavior of attributes is worth improving within this project, either by default or as a configuration option.

Barring this update, the readme should at least be updated to explicitly call out that JSX syntax still requires usage of the attributes module to assign attributes on elements.

Conclusion

I would appreciate feedback from a maintainer on these ideas. If an agreement is reached, I would be happy to create a pull request. Thanks for your time!