0.0.9 • Published 2 years ago

use-mouse-distance v0.0.9

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

A React hook that measures the distance from an element's center to the user's mouse.

Installation

npm install use-mouse-distance

Example (CodeSandbox)

use-mouse-distance example

Usage

Use the hook to receive an element reference and the distance to that element's center.

import { useMouseDistance } from 'use-mouse-distance'

function MyComponent() {
  const { elementRef, distance, distanceX, distanceY } =
    useMouseDistance<HTMLDivElement>()

  return (
    <div>
      <div ref={elementRef}>
        <p>Distance sum (absolute): {distance}</p>
        <p>Distance x: {distanceX}</p>
        <p>Distance y: {distanceY}</p>
      </div>
    </div>
  )
}

API

Return fieldDescription
elementRefElement reference
distanceSum of the distance to the mouse (absolute number)
distanceXHorizontal distance (can be negative)
distanceYVertical distance (can be negative)
0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago