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

Pointer events. #584

Merged
merged 6 commits into from
May 20, 2020
Merged

Pointer events. #584

merged 6 commits into from
May 20, 2020

Conversation

nickretallack
Copy link
Contributor

@nickretallack nickretallack commented May 20, 2020

I annotated these based on the pointer events spec. I tested it out and was able to register these event handlers in reason-react and log the event fields. However, I wasn't able to test any advanced features because reason-react doesn't implement Element.setPointerCapture. This is implemented in bs-webapi though, so I tried importing that package, but it had an incompatible type for pointerId.

I don't know how I can make these two packages compatible unless reason-react treats bs-webapi as a dependency and references its custom type. @peterpme can we do that?

@nickretallack
Copy link
Contributor Author

Example usage:

[@react.component]
let make = () => {
  <div
    onGotPointerCapture={_ => Js.log("Captured")}
    onLostPointerCapture={_ => Js.log("Lost")}
    onPointerDown={event => {
      Js.log(ReactEvent.Pointer.pointerId(event));
      Webapi.Dom.Element.setPointerCapture(
        ReactEvent.Pointer.pointerId(event),
        Webapi.Dom.EventTarget.unsafeAsElement(
          ReactEvent.Pointer.target(event),
        ),
      );
    }}
    onPointerMove={event => {Js.log(ReactEvent.Pointer.movementX(event))}}>
    {React.string("hello")}
  </div>;
};

If you hover over "hello" it will emit your movements. If you click and drag on "hello" it will continue to emit your movements for the duration of your drag. Cool, huh?

Unfortunately this code doesn't actually work unless I change ReactEvent.Pointer.target to return a Dom.eventTarget like the comments say it should. But then I'd have to change all events to do that, which would be a backward-incompatible change. Is there any work-around for this in the meantime? Should we fix these types?

@nickretallack
Copy link
Contributor Author

Oh, I suppose this works.

[@react.component]
let make = () => {
  <div
    onGotPointerCapture={_ => Js.log("Captured")}
    onLostPointerCapture={_ => Js.log("Lost")}
    onPointerDown={event => {
      ReactEvent.Pointer.target(event)##setPointerCapture(
        ReactEvent.Pointer.pointerId(event),
      )
    }}
    onPointerMove={event => {Js.log(ReactEvent.Pointer.movementX(event))}}>
    {React.string("hello")}
  </div>;
};

@nickretallack nickretallack changed the title Attempt to add pointer events. Add pointer events. May 20, 2020
@nickretallack nickretallack changed the title Add pointer events. Pointer events. May 20, 2020
@nickretallack
Copy link
Contributor Author

Can we merge this now?

@peterpme peterpme requested a review from rickyvetter May 20, 2020 15:37
@rickyvetter
Copy link
Contributor

This looks awesome. Thanks!

@rickyvetter rickyvetter merged commit 9c377ae into reasonml:master May 20, 2020
@peterpme peterpme added the 0.9.0 label May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants