Interactivity API: Allow multiple event handlers for the same type with data-wp-on.#60661
Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Size Change: +80 B (0%) Total Size: 1.75 MB
ℹ️ View Unchanged
|
|
Should we do the same for |
Probably yes, although I wouldn't consider this as critical as |
|
@DAreRodz @luisherranz I agree we should add support for multiple Maybe for now, let's open an issue for that and add it to the 6.6 roadmap so that it doesn't get forgotten? |
Sure, I can do that later today. 🙂 |
felixarntz
left a comment
There was a problem hiding this comment.
Thank you @DAreRodz, this looks solid to me.
|
It’d be an important fix to include in WordPress 6.5.3 as it limits the extenders to adding multiple event handlers without some complex logic. Let’s promote good practices from the beginning. @DAreRodz, we will need to update the API reference documentation with a proper remark explaining that support for multiple event handlers of the same type is going to be available from hopefully WP 6.5.3. |
…th `data-wp-on`. (#60661) * Allow multiple event handlers for the same type * Add simple test Co-authored-by: DAreRodz <[email protected]> Co-authored-by: felixarntz <[email protected]> Co-authored-by: luisherranz <[email protected]>
|
I just cherry-picked this PR to the update/fixes-for-wp-6-5-3 branch to get it included in the next release: 78859b1 |
…th `data-wp-on`. (#60661) * Allow multiple event handlers for the same type * Add simple test Co-authored-by: DAreRodz <[email protected]> Co-authored-by: felixarntz <[email protected]> Co-authored-by: luisherranz <[email protected]>
What?
This PR allows declaring multiple event listeners with
data-wp-onfor the same event type in the same element. Example:The syntax is the same as the rest of the directives (i.e., a unique ID is appended at the end of the directive name).
Why?
This could be considered a bug fix. The limitation of the number of event handlers that can be registered (only one per element & event type) affects the extensibility of interactive blocks, as well as the reuse of code (e.g., store callbacks).
In addition, all other directives already allow this syntax for the same reason.
How?
The directive internally groups all the event handlers with the same type and assigns a function to the corresponding
on${event}property in the element that runs all the event handlers consecutively.This fix has some limitations that could be addressed later on:
directives.onentries array that thedata-wp-ondirective function receives. That depends on the order they appear in the HTML, which is not guaranteed.data-wp-onAPI to support, e.g., passive event listeners, as all event handlers are grouped into the same event handler.Testing Instructions
I've added a simple E2E test that should pass.