-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# `useMouse` | ||
|
||
React sensor hook that re-renders on mouse position changes. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import {useMouse} from 'react-use'; | ||
|
||
const Demo = () => { | ||
const ref = React.useRef(null); | ||
const {docX, docY, posX, posY, elX, elY, elW, elH} = useScroll(ref); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
return ( | ||
<div ref={element}> | ||
<div>Mouse position in document - x:{docX} y:{docY}</div> | ||
<div>Mouse position in element - x:{posX} y:{posY}</div> | ||
<div>Element position - x:{elX} y:{elY}</div> | ||
<div>Element dimensions - {elW}x{elH}</div> | ||
</div> | ||
); | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {useMouse} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const ref = React.useRef(null); | ||
const state = useMouse(ref); | ||
|
||
return ( | ||
<> | ||
<pre> | ||
{JSON.stringify(state, null, 2)} | ||
</pre> | ||
<div | ||
ref={ref} | ||
style={{ | ||
width: '400px', | ||
height: '400px', | ||
backgroundColor: 'whitesmoke', | ||
}}> | ||
Move your mouse over me | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
storiesOf('Sensors|useMouse', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useMouse.md')} />) | ||
.add('Demo', () => | ||
<Demo/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {useState, useEffect, useRef, RefObject} from 'react'; | ||
|
||
export interface State { | ||
docX: number; | ||
docY: number; | ||
posX: number; | ||
posY: number; | ||
elX: number; | ||
elY: number; | ||
elH: number; | ||
elW: number; | ||
} | ||
|
||
const useMouse = (ref: RefObject<HTMLElement>): State => { | ||
const frame = useRef(0); | ||
const [state, setState] = useState<State>({ | ||
docX: 0, | ||
docY: 0, | ||
posX: 0, | ||
posY: 0, | ||
elX: 0, | ||
elY: 0, | ||
elH: 0, | ||
elW: 0, | ||
}); | ||
|
||
useEffect(() => { | ||
const handler = (event: MouseEvent) => { | ||
cancelAnimationFrame(frame.current) | ||
frame.current = requestAnimationFrame(() => { | ||
if (ref && ref.current) { | ||
const {left, top} = ref.current.getBoundingClientRect() | ||
const posX = left + window.scrollX; | ||
const posY = top + window.scrollY; | ||
|
||
setState({ | ||
docX: event.pageX, | ||
docY: event.pageY, | ||
posX, | ||
posY, | ||
elX: event.pageX - posX, | ||
elY: event.pageY - posY, | ||
elH: ref.current.offsetHeight, | ||
elW: ref.current.offsetWidth, | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
document.addEventListener('mousemove', handler); | ||
|
||
return () => { | ||
cancelAnimationFrame(frame.current); | ||
document.removeEventListener('mousemove', handler); | ||
}; | ||
}, []); | ||
|
||
return state; | ||
} | ||
|
||
export default useMouse |
Should this be
useMouse
instead ofuseScroll
?