diff --git a/dev/index.js b/dev/index.js index 87b8a31..27fb1ca 100644 --- a/dev/index.js +++ b/dev/index.js @@ -1,7 +1,7 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import { useWindowSize, useFormField, useTitle, useHover } from '../src'; +import { useWindowSize, useFormField, useTitle, useHover, useMouseMove } from '../src'; const PADDING = 15; const MARGIN = 15; @@ -80,6 +80,12 @@ const myStyles = theme => { hoverSectionActive: { backgroundColor: selectedColor.hoverDark, }, + miniBox: { + display: 'flex', + flexDirection: 'column', + alignItems: 'center', + backgroundColor: selectedColor.hoverLight, + }, }; }; @@ -87,11 +93,13 @@ const App = () => { const size = useWindowSize(); const titleInput = useFormField('Melting Pot'); const hoverEl = useHover(); + const mouseCoords = useMouseMove(); + const yellowBoxCoords = useMouseMove(); useTitle(titleInput.value || 'Empty'); const styles = myStyles('superman'); return ( -
+
@@ -102,6 +110,17 @@ const App = () => { window is {size.height}px. Try resizing the window horizontally/vertically & see the values of width & height change.

+

Mouse Coordinates

+

+ Did you know your mouse coordinates are w.r.t screen are x: {mouseCoords.x} and y:{' '} + {mouseCoords.y} +

+
+ {/*
*/} +

+ mouse x,y with respect to blue box {yellowBoxCoords.x}, {yellowBoxCoords.y} +

+

Fun!

Try changing the title of the input field below & watch as the title of the browser window diff --git a/docs/hooks/useMouseMove.mdx b/docs/hooks/useMouseMove.mdx new file mode 100644 index 0000000..87c5854 --- /dev/null +++ b/docs/hooks/useMouseMove.mdx @@ -0,0 +1,44 @@ +--- +name: useMouseMove +menu: Hooks +--- + +import { Playground } from 'docz'; +import { useMouseMove } from '@withvoid/melting-pot'; + +# useMouseMove + +## Purpose + +A hook utility to get current mouse pointer coordinates on screen. + +## Usage + +``` +import { useMouseMove } from '@withvoid/melting-pot'; +``` + +## Syntax +``` +const { mouseCoordinates, bind} = useMouseMove(); + +x: {mouseCoordinates.x} +y: {mouseCoordinates.y} +``` + +## Examples + + + {() => { + const mosueCoords = useMouseMove(); + + return ( +

+

Did you know?

+

Your mouse pointer coordinates are x: {mosueCoords.x} and y: {mosueCoords.y}

+
+ ) + +}} + + diff --git a/src/hooks/useMouseMove.js b/src/hooks/useMouseMove.js new file mode 100644 index 0000000..c794744 --- /dev/null +++ b/src/hooks/useMouseMove.js @@ -0,0 +1,16 @@ +import React from 'react'; + +const useMouseMove = () => { + const [coords, setCoords] = React.useState({ x: 0, y: 0 }); + + return { + ...coords, + bind: { + onMouseMove: event => { + setCoords({ x: event.screenX, y: event.screenY }); + }, + }, + }; +}; + +export default useMouseMove; diff --git a/src/index.js b/src/index.js index 068fa1f..d1acbf3 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ export { default as useWindowScrollPosition } from './hooks/useWindowScrollPosit export { default as useTitle } from './hooks/useTitle'; export { default as useToggle } from './hooks/useToggle'; export { default as useUpdate } from './hooks/useUpdate'; +export { default as useMouseMove } from './hooks/useMouseMove'; // Higher Order Components export { default as withCompose } from './hoc/withCompose';