JavaScript hooks use data- attributes
Introduction
Scripts find elements and read values through data- attributes.
Decision
In the KalaPress starter and new sites, every script hook is a data- attribute:
- To find an element, give it a
data-attribute:data-meetings-filter,data-load-more, etc. - To read a value or style a state, also use a
data-attribute:data-order="descending",data-form-id="contact",data-active, etc. - New code does not add
js-classes, and does not select elements by their styling classes.
Context
No project followed one style. Some builds mostly used data- attributes, while others mostly used js- classes. Standardizing on one method should reduce potential confusion.
Consequences
- Might create confusion with the WordPress Interactivity API, which starts with
data-wp-.
Exceptions
- Live sites prior to the adoption of this ADR keep their current hooks.
Additional Resources
- Use data attributes (MDN)
- Prefixing JavaScript selectors to avoid conflicts (Go Make Things)
- Decoupling Your HTML, CSS, and JavaScript (Philip Walton, the
js-class option this ADR decided against)