solid icon indicating copy to clipboard operation
solid copied to clipboard

Support 'passive' option on event listeners

Open goswinr opened this issue 1 year ago • 5 comments

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: https://github.com/facebook/react/issues/6436 and the resolution: https://github.com/facebook/react/pull/19654

Steps to Reproduce the Bug or Issue

see example on SolidJS Playground and below:

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 avatar Jul 04 '24 10:07 goswinr

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

ryansolid avatar Jul 24 '24 21:07 ryansolid

+1

codenimble avatar Aug 06 '24 05:08 codenimble

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

chaosprint avatar Aug 07 '24 08:08 chaosprint

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

goswinr avatar Aug 12 '24 11:08 goswinr

Opened this PR to solve this capture/once/passive/signal
https://github.com/ryansolid/dom-expressions/pull/341

titoBouzout avatar Aug 17 '24 13:08 titoBouzout

This is merged in commit: https://github.com/solidjs/solid/commit/2a3a1980643268b9b285976e58070ce646e09247

It will be in Solid 1.9

ryansolid avatar Sep 23 '24 22:09 ryansolid