1 | "use strict";
|
---|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
3 | var tslib_1 = require("tslib");
|
---|
4 | var react_1 = require("react");
|
---|
5 | var useRafState_1 = tslib_1.__importDefault(require("./useRafState"));
|
---|
6 | var util_1 = require("./misc/util");
|
---|
7 | var useMouse = function (ref) {
|
---|
8 | if (process.env.NODE_ENV === 'development') {
|
---|
9 | if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
---|
10 | console.error('useMouse expects a single ref argument.');
|
---|
11 | }
|
---|
12 | }
|
---|
13 | var _a = useRafState_1.default({
|
---|
14 | docX: 0,
|
---|
15 | docY: 0,
|
---|
16 | posX: 0,
|
---|
17 | posY: 0,
|
---|
18 | elX: 0,
|
---|
19 | elY: 0,
|
---|
20 | elH: 0,
|
---|
21 | elW: 0,
|
---|
22 | }), state = _a[0], setState = _a[1];
|
---|
23 | react_1.useEffect(function () {
|
---|
24 | var moveHandler = function (event) {
|
---|
25 | if (ref && ref.current) {
|
---|
26 | var _a = ref.current.getBoundingClientRect(), left = _a.left, top_1 = _a.top, elW = _a.width, elH = _a.height;
|
---|
27 | var posX = left + window.pageXOffset;
|
---|
28 | var posY = top_1 + window.pageYOffset;
|
---|
29 | var elX = event.pageX - posX;
|
---|
30 | var elY = event.pageY - posY;
|
---|
31 | setState({
|
---|
32 | docX: event.pageX,
|
---|
33 | docY: event.pageY,
|
---|
34 | posX: posX,
|
---|
35 | posY: posY,
|
---|
36 | elX: elX,
|
---|
37 | elY: elY,
|
---|
38 | elH: elH,
|
---|
39 | elW: elW,
|
---|
40 | });
|
---|
41 | }
|
---|
42 | };
|
---|
43 | util_1.on(document, 'mousemove', moveHandler);
|
---|
44 | return function () {
|
---|
45 | util_1.off(document, 'mousemove', moveHandler);
|
---|
46 | };
|
---|
47 | }, [ref]);
|
---|
48 | return state;
|
---|
49 | };
|
---|
50 | exports.default = useMouse;
|
---|