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

Support 'passive' option on event listeners #2210

Closed
goswinr opened this issue Jul 4, 2024 · 6 comments
Closed

Support 'passive' option on event listeners #2210

goswinr opened this issue Jul 4, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@goswinr
Copy link

goswinr commented Jul 4, 2024

Describe the bug

Most DOM events are not passive by default, but some are ( wheel, scroll, ..) .
see https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#passive
And it is not consistent among major Browsers.
Should Solids JSX support explicit passive options for event listeners ?
See how React addressed this issue: facebook/react#6436 and the resolution: facebook/react#19654

Steps to Reproduce the Bug or Issue

see example on SolidJS Playground and below: (update Sep 2024: on chrome the playground doesn't show the bug anymore)

import { render } from "solid-js/web";
import { onMount } from "solid-js";

function Counter() {  
  const wheel = (e:MouseEvent) => {
    e.preventDefault() // only works on not passive events
    e.stopPropagation()  
    console.log("wheel stopped?")
  }

  let div ;
  onMount(() => div.addEventListener("wheel", wheel, {passive:false}) ) // most events are not passive by default but "wheel" is in Chrome

  return (
    <>
    <div oncapture:wheel={wheel} style={{"background-color":"lightgreen","height":"26vh"}} />
    <div ref={div} style={{"background-color":"lightgray"  ,"height":"26vh"}} > In Chrome this gray div is the only div that prevents scrolling</div>
    <div on:wheel={wheel} style={{"background-color":"lightblue","height":"26vh"}} />
    <div onWheel={wheel} style={{"background-color":"crimson"  ,"height":"26vh"}} />
    </>
  );
}

render(() => <Counter />, document.getElementById("app")!);

Expected behavior

Like on:* and oncapture:*
There could be a JSX syntax to specify the passive option for events. Maybe onpassive:* and onnotpassive: * or onWheelNotPassive and onScrollNotPassive

@goswinr goswinr changed the title Support Passive Event Listeners Support 'passive' option on event listeners Jul 5, 2024
@ryansolid
Copy link
Member

Great suggestion. How are other frameworks handling it. Love to see some inspiration for this feature.

@ryansolid ryansolid added the enhancement New feature or request label Jul 24, 2024
@codenimble
Copy link

+1

@chaosprint
Copy link

This is very important. Otherwise, do we have to use addEventListener temporarily?

@goswinr
Copy link
Author

goswinr commented Aug 12, 2024

@chaosprint yes, currently you have to use el.addEventListener("wheel", handler, {passive:false}) so that preventDefault() works in the handler.

@titoBouzout
Copy link
Contributor

Opened this PR to solve this capture/once/passive/signal
ryansolid/dom-expressions#341

@ryansolid
Copy link
Member

This is merged in commit: 2a3a198

It will be in Solid 1.9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants