Skip to content
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: Event directives cannot be passive #58705

Open
westonruter opened this issue Feb 6, 2024 · 0 comments
Open

Interactivity API: Event directives cannot be passive #58705

westonruter opened this issue Feb 6, 2024 · 0 comments
Labels
[Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended [Type] Performance Related to performance efforts

Comments

@westonruter
Copy link
Member

Description

I have a page that has a dozen Image blocks with lightbox enabled. When I open the console, I see many many warnings:

Added non-passive event listener to a scroll-blocking 'touchstart' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952

[Violation] Added non-passive event listener to a scroll-blocking 'touchmove' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952

At issue here is the Image block's use of these touch event directives:

data-wp-on--touchstart="actions.handleTouchStart"
data-wp-on--touchmove="actions.handleTouchMove"
data-wp-on--touchend="actions.handleTouchEnd"

Such scroll event listeners should be passive for performance to ensure smooth interactivity. In fact, Chrome defaults these events to be passive by default (and Firefox too apparently). Safari, however, does not.

Nevertheless, there is no way currently to specify using event directives to specify that event handler should be passive. Such events should always be passive unless they call event.preventDefault(). The Image block only may call preventDefault in the touchmove handler:

handleTouchStart() {
isTouching = true;
},
handleTouchMove( event ) {
const ctx = getContext();
// On mobile devices, we want to prevent triggering the
// scroll event because otherwise the page jumps around as
// we reset the scroll position. This also means that closing
// the lightbox requires that a user perform a simple tap. This
// may be changed in the future if we find a better alternative
// to override or reset the scroll position during swipe actions.
if ( ctx.lightboxEnabled ) {
event.preventDefault();
}
},
handleTouchEnd() {
// We need to wait a few milliseconds before resetting
// to ensure that pinch to zoom works consistently
// on mobile devices when the lightbox is open.
lastTouchTime = Date.now();
isTouching = false;
},

Preact does not support setting the passive flag when adding event listeners via props: preactjs/preact#428

The workaround I suppose would be to not use event directives at all for the Image block, and to instead call addEventListener at the init action.

👉 In any case, it would be great if perhaps the event directives could somehow default to being passive to better ensure best practices for performance.

Read more from MDN on using passive listeners.

Step-by-step reproduction instructions

  1. Add Image blocks to a post
  2. Enable the lightbox functionality
  3. View the post on the frontend with the console open

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@westonruter westonruter added [Type] Bug An existing feature does not function as intended [Type] Performance Related to performance efforts [Feature] Interactivity API API to add frontend interactivity to blocks. labels Feb 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Interactivity API API to add frontend interactivity to blocks. [Type] Bug An existing feature does not function as intended [Type] Performance Related to performance efforts
Projects
None yet
Development

No branches or pull requests

1 participant