Socket
Socket
Sign inDemoInstall

@jwdinker/interactivity-hooks

Package Overview
Dependencies
7
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @jwdinker/interactivity-hooks

The useInteractivity hook adds mouse/touch events to HTML elements, returning state that minimizes the need for calculations.


Version published
Maintainers
1
Created

Readme

Source

useInteractivity

The useInteractivity hook adds mouse/touch events to HTML elements, returning state that minimizes the need for calculations.


Installation

Yarn

yarn add @jwdinker/interactivity-hooks

NPM

npm install @jwdinker/interactivity-hooks



Usage

With Re-rendering

import useInteractivity from "@jwdinker/interactivity-hooks";
import { useRef } from "react";

const MyInteractiveComponent = () => {
  const ref = useRef();
  const [state] = useInteractivity(ref);

  const {
    move: {
      continued: { x, y }
    }
  } = state;

  return (
    <div style={{ width: "100%", height: "100%" }}>
      <div
        ref={ref}
        style={{ transform: `translate3d(${x}px,${y}px,0)`, width: "100px" }}
      >
        <div
          style={{
            background: "white",
            borderRadius: "4px",
            boxShadow:
              "0px 5px 15px rgba(54, 64, 70, 0.05), 0px 1px 3px rgba(16, 16, 16, 0.1)"
          }}
        >
          <div style={{ height: "100px", width: "100px" }}>Move Me Around</div>
        </div>
      </div>
    </div>
  );
};


Preventing Rerenders

Since touch / mouse events can trigger expensive rerenders, setting the imperative option to true prevents rerenders and allows you to access the state through callbacks. This is useful with animation libraries that operate directly on DOM nodes.

import useInteractivity from "@jwdinker/interactivity-hooks";
import { useRef, useState } from "react";

const MyInteractiveComponent = () => {
  const [coordinates, setCoordinates] = useState([0, 0]);
  const ref = useRef();

  useInteractivity(ref, {
    imperative: true,
    onInteraction: ({
      move: {
        continued: { x, y }
      }
    }) => {
      if (coordinates.x > 100) {
        setCoordinates([x, y]);
      }
    }
  });

  return (
    <div style={{ width: "100%", height: "100%" }}>
      <div
        ref={ref}
        style={{
          transform: `translate3d(${coordinates.x}px,${coordinates.y}px,0)`,
          width: "100px"
        }}
      >
        <div
          style={{
            background: "white",
            borderRadius: "4px",
            boxShadow:
              "0px 5px 15px rgba(54, 64, 70, 0.05), 0px 1px 3px rgba(16, 16, 16, 0.1)"
          }}
        >
          <div style={{ height: "100px", width: "100px" }}>Move Me Around</div>
        </div>
      </div>
    </div>
  );
};



Arguments

Argument 1

PropertyTypeDefaultDescription
refreact ref or window, etcnullThe element the touchstart, touchmove, touchend, and mousedown event listeners are attached to. mousemove and mouseup are attached dynamically to the window in order to account for fast mouse movements that may cause the target element to lose focus.


Argument 2

PropertytypeDefaultDescription
onStartfunction(state, event) => {}Callback function executed on the start phase of the touch or mouse event.
onMovefunction(state, event) => {}Callback function executed on the move phase of the touch or mouse event.
onEndfunction(state, event) => {}Callback function executed on the end phase of the touch or mouse event.
onInteractionfunction(state, event) => {}Callback function executed on the start,move, and end phase of the touch or mouse event.
touchbooleantrueEnables touch event listeners on the element.
mousebooleantrueEnables mouse event listeners on the element.
imperativebooleanfalsePrevents re-renders during the phases of the interaction events. This ideal for use with the callbacks when using an animation library that acts directly on the DOM element.
capturebooleanfalseBoolean for altering the order of the handler's invocation. Click here for more info.
oncebooleanfalseBoolean indicating that the listener should be called only once and then be removed.
passivebooleanfalseBoolean indicating whether or not to create a passive listener (which stops preventDefault from being called).
preventContextMenubooleanfalseBoolean indicating whether to preventDefault if the target element ever triggers a context menu event. While the use case for this is very much dependent on the css of the target element, setting this to true is recommended if mouse events are used. The contextmenu event can be triggered accidently from all sorts of user interactions with the target element while using a mouse, therefore canceling the mouseup event and causing the mousemove event to run indefintely.



State

PropertytypeDefaultDescription
activebooleanfalseWhether a touch or mouse event is active or not.
identifierintegernullUsed to identify the touch. This is used in a multi touch package.
phasestringnullDescribes the current phase of the touch or mouse event. The phase can be start, move, or end.
clientobject{
  x:0,
  y:0
}
The x and y coordinates of the event relative to the viewport, not including the scroll offset.
screenobject{
  x:0,
  y:0
}
The x and y coordinates of the event relative to the screen, not including any scroll offset.
pageobject{
  x:0,
  y:0
}
The x and y coordinates of the event relative to the screen, including the scroll offset.
startobject{
  x:0,
  y:0,
  time:null
}
Includes the page x and y coordinates, and the time in milliseconds during the start phase of the interaction event.
moveobject{
  x:0,
  y:0,
continued:{
    x:0,
    y:0,
  }
}
Includes the page x and y coordinates, during the move phase of the interaction event. The move.continued object provides a continuation of the x and y coordinates between events.
endobject{
  x:0,
  y:0,
  time:null
}
Includes the page x and y coordinates, and the time in milliseconds during the end phase of the interaction event.
directionstringnoneProvides the current direction of the interaction event from the original starting coordinate. The possibilities are:
up,
upleft,
up_right,
left,
right,
down,
down_left,
down_right,
none
distanceobject{
  x:0,
  y:0,
total:null
}
The current x and y distances traveled from start to move.The total includes the total distance traveled.
velocityobject{
  x:0,
  y:0
}
The current x and y velocity of the mouse or touch event.
timedatenullThe on going time of the event.
durationobject{
milliseconds:0,
  seconds:0
}
The duration of the event in milliseconds and formatted in seconds.



Keywords

FAQs

Last updated on 07 Nov 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc