main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[d565449] | 1 | import { useEffect } from 'react';
|
---|
| 2 | import useRafState from './useRafState';
|
---|
| 3 | import { off, on } from './misc/util';
|
---|
| 4 | var useMouse = function (ref) {
|
---|
| 5 | if (process.env.NODE_ENV === 'development') {
|
---|
| 6 | if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
---|
| 7 | console.error('useMouse expects a single ref argument.');
|
---|
| 8 | }
|
---|
| 9 | }
|
---|
| 10 | var _a = useRafState({
|
---|
| 11 | docX: 0,
|
---|
| 12 | docY: 0,
|
---|
| 13 | posX: 0,
|
---|
| 14 | posY: 0,
|
---|
| 15 | elX: 0,
|
---|
| 16 | elY: 0,
|
---|
| 17 | elH: 0,
|
---|
| 18 | elW: 0,
|
---|
| 19 | }), state = _a[0], setState = _a[1];
|
---|
| 20 | useEffect(function () {
|
---|
| 21 | var moveHandler = function (event) {
|
---|
| 22 | if (ref && ref.current) {
|
---|
| 23 | var _a = ref.current.getBoundingClientRect(), left = _a.left, top_1 = _a.top, elW = _a.width, elH = _a.height;
|
---|
| 24 | var posX = left + window.pageXOffset;
|
---|
| 25 | var posY = top_1 + window.pageYOffset;
|
---|
| 26 | var elX = event.pageX - posX;
|
---|
| 27 | var elY = event.pageY - posY;
|
---|
| 28 | setState({
|
---|
| 29 | docX: event.pageX,
|
---|
| 30 | docY: event.pageY,
|
---|
| 31 | posX: posX,
|
---|
| 32 | posY: posY,
|
---|
| 33 | elX: elX,
|
---|
| 34 | elY: elY,
|
---|
| 35 | elH: elH,
|
---|
| 36 | elW: elW,
|
---|
| 37 | });
|
---|
| 38 | }
|
---|
| 39 | };
|
---|
| 40 | on(document, 'mousemove', moveHandler);
|
---|
| 41 | return function () {
|
---|
| 42 | off(document, 'mousemove', moveHandler);
|
---|
| 43 | };
|
---|
| 44 | }, [ref]);
|
---|
| 45 | return state;
|
---|
| 46 | };
|
---|
| 47 | export default useMouse;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.