Skip to content

Commit

Permalink
🖱 Added useMousePointer hook (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
aromalanil authored Oct 2, 2020
1 parent 27758c4 commit b22559d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ yarn add use-custom-hooks
- [useDebounce](#-usedebounce)
- [useDarkMode](#-usedarkmode)
- [useToggle](#-usetoggle)
- [useMousePosition](#-usemousepointer)
- [useGeoLocation](#-usegeolocation)

## 💾 useLocalStorage
Expand Down Expand Up @@ -161,6 +162,32 @@ const Mood = () =>{
}
```

## 🖱 useMousePosition
Returns an object with the current coordinates of the mouse pointer.

### Usage

```jsx
import React from "react"
import { useMousePointer } from "use-custom-hooks"

const Mouse = () =>{
const {x,y} = useMousePosition();
/*
Using Object destructuring to get x & y coordinates
from mousePosition object.
*/

return(
<div>
<h1>Mouse Pointer Location</div>
<p>The mouse pointer is at : {`(${x},${y})`}</p>
{/* The x,y coordinates will be updated as you move your mouse.*/}
</div>
);
}
```

## 🌎 useGeoLocation
Get latitude and longitude positions from your browser

Expand Down
32 changes: 32 additions & 0 deletions hooks/useMousePosition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { useState, useEffect } from "react";

/**
* @typedef {Object} MousePosition
* @property {number} x - X Coordinate of the mouse
* @property {number} y - Y Coordinate of the mouse.
*/

/**
*
* Custom hook which return an object with current coordinates of mouse pointer.
* @return {MousePosition} Object containing the coordinates of the mouse pointer.
*/
const useMousePosition = () => {
const [coordinates, setCoordinates] = useState({});

useEffect(() => {
//Event listener to track the mouse location.
window.addEventListener("mousemove", (e) => {
const mousePosition = {
x: e.clientX,
y: e.clientY,
};

setCoordinates(mousePosition);
});
}, []);

return coordinates;
};

export default useMousePosition;
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export { default as useDarkMode } from "./hooks/useDarkMode";
export { default as useDebounce } from "./hooks/useDebounce";
export { default as useGeoLocation } from "./hooks/useGeoLocation";
export { default as useLocalStorage } from "./hooks/useLocalStorage";
export { default as useMousePosition } from "./hooks/useMousePosition";
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "use-custom-hooks",
"version": "1.2.2",
"version": "1.3.0",
"description": "A collection of Custom Hooks for React.",
"main": "index.js",
"scripts": {},
Expand Down

0 comments on commit b22559d

Please sign in to comment.