DEV Community

Karolis Ramanauskas
Karolis Ramanauskas

Posted on • Originally published at karolisram.com on

4

React v16.4.0 - Pointer events

On May 23rd, 2018 a new minor release v16.4.0 of React has been launched. Overall, it's a rather uneventful release mostly concerned with fixing a variety of bugs. Quite notably though, it adds support for pointer events which has been an often requested feature for almost the entire existence of React.

Pointer events, in essence, are very similar to mouse events (mousedown, mouseup, etc.) but are hardware-agnostic and thus can handle all input devices such as a mouse, stylus or touch. This is great since it removes the need for separate implementations for each device and makes authoring for cross-device pointers easier.

Pointer events

The API of pointer events works in the same manner as existing various event handlers. Pointer events are added as attributes to React component and are passed a callback that accepts an event. Inside the callback we process the event. The following pointer events have been added to React DOM:

  • onPointerDown
  • onPointerMove
  • onPointerUp
  • onPointerCancel
  • onGotPointerCapture
  • onLostPointerCapture
  • onPointerEnter
  • onPointerLeave
  • onPointerOver
  • onPointerOut

When pointer events are fired is well explained on MDN Pointer events documentation so keep it as a reference for in-depth explanation of each event. Note that at the time of this writing, pointer events are not supported by Safari.

For an example of how pointer events can be incorporated on your own application, have a look at the below example by Philip Spiess. I added PEP polyfill to also make it work on Safari. Note that onGotPointerCapture and onLostPointerCapture events still do not work even with the polyfill.

Check out this demo on Code Sandbox to get a glimpse of what's possible with new pointer events. Hint: try dragging the circle on various devices.

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil — patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay