matchMedia-like API for element matches #1225
Labels
addition/proposal
New features or enhancements
needs implementer interest
Moving the issue forward requires implementers to express interest
It would be useful to receive an event when an element's
matches(selector)
changes, similar to how you can do so withmatchMedia
.The ability to reactively change styles based on selector changes in CSS is a powerful feature we do not have in the DOM. Instead to achieve similar reactivity we need to account for the variety of ways in which state may change that includes things like:
message
eventsAnd probably a lot more.
Use-case
You want to mark a submit button as disabled when the form is invalid. You might naively write this code:
This works pretty well, but if the requirements change and say you do something like:
These things will not trigger an
input
so the submit button might be incorrectly enabled. Here's an example codepen that demonstrates this problem: https://codepen.io/matthewp/pen/MWZpBYdProposal 1
This is inspired by
matchMedia
, Element.prototype.match:Proposal 2
I prefer proposal 1, but there might be issues with it and I'm not sure if this style API is still in style. I defer to the experts here. My second proposal would follow the observer pattern:
The text was updated successfully, but these errors were encountered: