@os-design/use-cursor-position 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENCE.md +1 -0
- package/README.md +15 -0
- package/dist/cjs/index.js +55 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/esm/index.js +28 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/index.d.ts.map +1 -0
- package/package.json +37 -0
package/LICENCE.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
Copyright (c) 2019-present, Ilya Ordin, all rights reserved
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @os-design/use-cursor-position [](https://yarnpkg.com/package/@os-design/use-cursor-position) [](https://bundlephobia.com/result?p=@os-design/use-cursor-position)
|
|
2
|
+
|
|
3
|
+
Returns the position of the cursor. It works on both mouse and touch devices.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Install the package using the following command:
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
yarn add @os-design/use-cursor-position
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
See all the features in the [Storybook](https://os-team.gitlab.io/libs/os-design/).
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports["default"] = void 0;
|
|
7
|
+
|
|
8
|
+
var _react = require("react");
|
|
9
|
+
|
|
10
|
+
var _useEvent = _interopRequireDefault(require("@os-design/use-event"));
|
|
11
|
+
|
|
12
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
13
|
+
|
|
14
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
15
|
+
|
|
16
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
17
|
+
|
|
18
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
19
|
+
|
|
20
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
21
|
+
|
|
22
|
+
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
|
23
|
+
|
|
24
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
25
|
+
|
|
26
|
+
var useCursorPosition = function useCursorPosition() {
|
|
27
|
+
var _useState = (0, _react.useState)({
|
|
28
|
+
x: 0,
|
|
29
|
+
y: 0
|
|
30
|
+
}),
|
|
31
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
32
|
+
cursorPosition = _useState2[0],
|
|
33
|
+
setCursorPosition = _useState2[1];
|
|
34
|
+
|
|
35
|
+
var mouseMoveHandler = (0, _react.useCallback)(function (e) {
|
|
36
|
+
setCursorPosition({
|
|
37
|
+
x: e.clientX,
|
|
38
|
+
y: e.clientY
|
|
39
|
+
});
|
|
40
|
+
}, []);
|
|
41
|
+
var touchMoveHandler = (0, _react.useCallback)(function (e) {
|
|
42
|
+
if (e.touches.length === 0) return;
|
|
43
|
+
setCursorPosition({
|
|
44
|
+
x: e.touches[0].clientX,
|
|
45
|
+
y: e.touches[0].clientY
|
|
46
|
+
});
|
|
47
|
+
}, []);
|
|
48
|
+
(0, _useEvent["default"])(window, 'mousemove', mouseMoveHandler);
|
|
49
|
+
(0, _useEvent["default"])(window, 'touchmove', touchMoveHandler);
|
|
50
|
+
return cursorPosition;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
var _default = useCursorPosition;
|
|
54
|
+
exports["default"] = _default;
|
|
55
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":["useCursorPosition","x","y","cursorPosition","setCursorPosition","mouseMoveHandler","e","clientX","clientY","touchMoveHandler","touches","length","window"],"mappings":";;;;;;;AAAA;;AACA;;;;;;;;;;;;;;;;AAOA,IAAMA,iBAAiB,GAAG,SAApBA,iBAAoB,GAAM;AAC9B,kBAA4C,qBAAyB;AACnEC,IAAAA,CAAC,EAAE,CADgE;AAEnEC,IAAAA,CAAC,EAAE;AAFgE,GAAzB,CAA5C;AAAA;AAAA,MAAOC,cAAP;AAAA,MAAuBC,iBAAvB;;AAKA,MAAMC,gBAAgB,GAAG,wBAAY,UAACC,CAAD,EAAmB;AACtDF,IAAAA,iBAAiB,CAAC;AAAEH,MAAAA,CAAC,EAAEK,CAAC,CAACC,OAAP;AAAgBL,MAAAA,CAAC,EAAEI,CAAC,CAACE;AAArB,KAAD,CAAjB;AACD,GAFwB,EAEtB,EAFsB,CAAzB;AAIA,MAAMC,gBAAgB,GAAG,wBAAY,UAACH,CAAD,EAAmB;AACtD,QAAIA,CAAC,CAACI,OAAF,CAAUC,MAAV,KAAqB,CAAzB,EAA4B;AAC5BP,IAAAA,iBAAiB,CAAC;AAAEH,MAAAA,CAAC,EAAEK,CAAC,CAACI,OAAF,CAAU,CAAV,EAAaH,OAAlB;AAA2BL,MAAAA,CAAC,EAAEI,CAAC,CAACI,OAAF,CAAU,CAAV,EAAaF;AAA3C,KAAD,CAAjB;AACD,GAHwB,EAGtB,EAHsB,CAAzB;AAKA,4BAASI,MAAT,EAAiB,WAAjB,EAA8BP,gBAA9B;AACA,4BAASO,MAAT,EAAiB,WAAjB,EAA8BH,gBAA9B;AAEA,SAAON,cAAP;AACD,CAnBD;;eAqBeH,iB","sourcesContent":["import { useCallback, useState } from 'react';\nimport useEvent from '@os-design/use-event';\n\nexport interface CursorPosition {\n x: number;\n y: number;\n}\n\nconst useCursorPosition = () => {\n const [cursorPosition, setCursorPosition] = useState<CursorPosition>({\n x: 0,\n y: 0,\n });\n\n const mouseMoveHandler = useCallback((e: MouseEvent) => {\n setCursorPosition({ x: e.clientX, y: e.clientY });\n }, []);\n\n const touchMoveHandler = useCallback((e: TouchEvent) => {\n if (e.touches.length === 0) return;\n setCursorPosition({ x: e.touches[0].clientX, y: e.touches[0].clientY });\n }, []);\n\n useEvent(window, 'mousemove', mouseMoveHandler);\n useEvent(window, 'touchmove', touchMoveHandler);\n\n return cursorPosition;\n};\n\nexport default useCursorPosition;\n"],"file":"index.js"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import useEvent from '@os-design/use-event';
|
|
3
|
+
|
|
4
|
+
const useCursorPosition = () => {
|
|
5
|
+
const [cursorPosition, setCursorPosition] = useState({
|
|
6
|
+
x: 0,
|
|
7
|
+
y: 0
|
|
8
|
+
});
|
|
9
|
+
const mouseMoveHandler = useCallback(e => {
|
|
10
|
+
setCursorPosition({
|
|
11
|
+
x: e.clientX,
|
|
12
|
+
y: e.clientY
|
|
13
|
+
});
|
|
14
|
+
}, []);
|
|
15
|
+
const touchMoveHandler = useCallback(e => {
|
|
16
|
+
if (e.touches.length === 0) return;
|
|
17
|
+
setCursorPosition({
|
|
18
|
+
x: e.touches[0].clientX,
|
|
19
|
+
y: e.touches[0].clientY
|
|
20
|
+
});
|
|
21
|
+
}, []);
|
|
22
|
+
useEvent(window, 'mousemove', mouseMoveHandler);
|
|
23
|
+
useEvent(window, 'touchmove', touchMoveHandler);
|
|
24
|
+
return cursorPosition;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default useCursorPosition;
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":["useCallback","useState","useEvent","useCursorPosition","cursorPosition","setCursorPosition","x","y","mouseMoveHandler","e","clientX","clientY","touchMoveHandler","touches","length","window"],"mappings":"AAAA,SAASA,WAAT,EAAsBC,QAAtB,QAAsC,OAAtC;AACA,OAAOC,QAAP,MAAqB,sBAArB;;AAOA,MAAMC,iBAAiB,GAAG,MAAM;AAC9B,QAAM,CAACC,cAAD,EAAiBC,iBAAjB,IAAsCJ,QAAQ,CAAiB;AACnEK,IAAAA,CAAC,EAAE,CADgE;AAEnEC,IAAAA,CAAC,EAAE;AAFgE,GAAjB,CAApD;AAKA,QAAMC,gBAAgB,GAAGR,WAAW,CAAES,CAAD,IAAmB;AACtDJ,IAAAA,iBAAiB,CAAC;AAAEC,MAAAA,CAAC,EAAEG,CAAC,CAACC,OAAP;AAAgBH,MAAAA,CAAC,EAAEE,CAAC,CAACE;AAArB,KAAD,CAAjB;AACD,GAFmC,EAEjC,EAFiC,CAApC;AAIA,QAAMC,gBAAgB,GAAGZ,WAAW,CAAES,CAAD,IAAmB;AACtD,QAAIA,CAAC,CAACI,OAAF,CAAUC,MAAV,KAAqB,CAAzB,EAA4B;AAC5BT,IAAAA,iBAAiB,CAAC;AAAEC,MAAAA,CAAC,EAAEG,CAAC,CAACI,OAAF,CAAU,CAAV,EAAaH,OAAlB;AAA2BH,MAAAA,CAAC,EAAEE,CAAC,CAACI,OAAF,CAAU,CAAV,EAAaF;AAA3C,KAAD,CAAjB;AACD,GAHmC,EAGjC,EAHiC,CAApC;AAKAT,EAAAA,QAAQ,CAACa,MAAD,EAAS,WAAT,EAAsBP,gBAAtB,CAAR;AACAN,EAAAA,QAAQ,CAACa,MAAD,EAAS,WAAT,EAAsBH,gBAAtB,CAAR;AAEA,SAAOR,cAAP;AACD,CAnBD;;AAqBA,eAAeD,iBAAf","sourcesContent":["import { useCallback, useState } from 'react';\nimport useEvent from '@os-design/use-event';\n\nexport interface CursorPosition {\n x: number;\n y: number;\n}\n\nconst useCursorPosition = () => {\n const [cursorPosition, setCursorPosition] = useState<CursorPosition>({\n x: 0,\n y: 0,\n });\n\n const mouseMoveHandler = useCallback((e: MouseEvent) => {\n setCursorPosition({ x: e.clientX, y: e.clientY });\n }, []);\n\n const touchMoveHandler = useCallback((e: TouchEvent) => {\n if (e.touches.length === 0) return;\n setCursorPosition({ x: e.touches[0].clientX, y: e.touches[0].clientY });\n }, []);\n\n useEvent(window, 'mousemove', mouseMoveHandler);\n useEvent(window, 'touchmove', touchMoveHandler);\n\n return cursorPosition;\n};\n\nexport default useCursorPosition;\n"],"file":"index.js"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,cAAc;IAC7B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;CACX;AAED,QAAA,MAAM,iBAAiB,sBAmBtB,CAAC;AAEF,eAAe,iBAAiB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@os-design/use-cursor-position",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"license": "UNLICENSED",
|
|
5
|
+
"repository": "git@gitlab.com:os-team/libs/os-design.git",
|
|
6
|
+
"main": "dist/cjs/index.js",
|
|
7
|
+
"module": "dist/esm/index.js",
|
|
8
|
+
"types": "dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"require": "./dist/cjs/index.js",
|
|
12
|
+
"default": "./dist/esm/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": false,
|
|
19
|
+
"scripts": {
|
|
20
|
+
"clean": "rimraf dist",
|
|
21
|
+
"build:esm": "cross-env BABEL_ENV=esm babel src --root-mode upward --extensions .ts,.tsx --out-dir dist/esm --source-maps",
|
|
22
|
+
"build:cjs": "cross-env BABEL_ENV=cjs babel src --root-mode upward --extensions .ts,.tsx --out-dir dist/cjs --source-maps",
|
|
23
|
+
"build:types": "tsc --emitDeclarationOnly --declaration --declarationDir dist/types",
|
|
24
|
+
"build": "yarn clean && npm-run-all build:*",
|
|
25
|
+
"ncu": "ncu -u"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@os-design/use-event": "^1.0.5"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"react": ">=17"
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "4f41a962d4c0afb57d65e8c3e104215794d3c6a7"
|
|
37
|
+
}
|