-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Interactivity API: Allow multiple event handlers for the same type with data-wp-on
.
#60661
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. 🙂 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <darerodz@git.wordpress.org> Co-authored-by: felixarntz <flixos90@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
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 <darerodz@git.wordpress.org> Co-authored-by: felixarntz <flixos90@git.wordpress.org> Co-authored-by: luisherranz <luisherranz@git.wordpress.org>
What?
This PR allows declaring multiple event listeners with
data-wp-on
for 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.on
entries array that thedata-wp-on
directive function receives. That depends on the order they appear in the HTML, which is not guaranteed.data-wp-on
API 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.