@hanzogui/use-escape-keydown 2.0.0-rc.41-hanzoai.5
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/LICENSE +21 -0
- package/dist/cjs/index.cjs +58 -0
- package/dist/cjs/index.native.js +65 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/esm/index.js +22 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +22 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +26 -0
- package/dist/esm/index.native.js.map +1 -0
- package/package.json +45 -0
- package/src/index.ts +35 -0
- package/types/index.d.ts +7 -0
- package/types/index.d.ts.map +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Radix
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __export = (target, all) => {
|
|
8
|
+
for (var name in all) __defProp(target, name, {
|
|
9
|
+
get: all[name],
|
|
10
|
+
enumerable: true
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
var __copyProps = (to, from, except, desc) => {
|
|
14
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
16
|
+
get: () => from[key],
|
|
17
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
23
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
24
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
25
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
26
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
27
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
28
|
+
value: mod,
|
|
29
|
+
enumerable: true
|
|
30
|
+
}) : target, mod));
|
|
31
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
32
|
+
value: true
|
|
33
|
+
}), mod);
|
|
34
|
+
var index_exports = {};
|
|
35
|
+
__export(index_exports, {
|
|
36
|
+
useEscapeKeydown: () => useEscapeKeydown
|
|
37
|
+
});
|
|
38
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
|
+
var import_use_callback_ref = require("@hanzogui/use-callback-ref");
|
|
40
|
+
var import_react = __toESM(require("react"), 1);
|
|
41
|
+
function useEscapeKeydown(onEscapeKeyDownProp, ownerDocument = globalThis?.document) {
|
|
42
|
+
const onEscapeKeyDown = (0, import_use_callback_ref.useCallbackRef)(onEscapeKeyDownProp);
|
|
43
|
+
import_react.default.useEffect(() => {
|
|
44
|
+
const handleKeyDown = event => {
|
|
45
|
+
if (event.key === "Escape") {
|
|
46
|
+
onEscapeKeyDown(event);
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
ownerDocument.addEventListener("keydown",
|
|
50
|
+
// @ts-expect-error
|
|
51
|
+
handleKeyDown);
|
|
52
|
+
return () => {
|
|
53
|
+
ownerDocument.removeEventListener("keydown",
|
|
54
|
+
// @ts-expect-error
|
|
55
|
+
handleKeyDown);
|
|
56
|
+
};
|
|
57
|
+
}, [onEscapeKeyDown, ownerDocument]);
|
|
58
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __export = (target, all) => {
|
|
10
|
+
for (var name in all) __defProp(target, name, {
|
|
11
|
+
get: all[name],
|
|
12
|
+
enumerable: true
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
18
|
+
get: () => from[key],
|
|
19
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return to;
|
|
23
|
+
};
|
|
24
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
25
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
26
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
27
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
28
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
29
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
30
|
+
value: mod,
|
|
31
|
+
enumerable: true
|
|
32
|
+
}) : target, mod));
|
|
33
|
+
var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
|
|
34
|
+
value: true
|
|
35
|
+
}), mod);
|
|
36
|
+
var index_exports = {};
|
|
37
|
+
__export(index_exports, {
|
|
38
|
+
useEscapeKeydown: () => useEscapeKeydown
|
|
39
|
+
});
|
|
40
|
+
module.exports = __toCommonJS(index_exports);
|
|
41
|
+
var import_use_callback_ref = require("@hanzogui/use-callback-ref");
|
|
42
|
+
var import_react = __toESM(require("react"), 1);
|
|
43
|
+
function useEscapeKeydown(onEscapeKeyDownProp) {
|
|
44
|
+
var ownerDocument = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : function () {
|
|
45
|
+
var _globalThis;
|
|
46
|
+
return (_globalThis = globalThis) === null || _globalThis === void 0 ? void 0 : _globalThis.document;
|
|
47
|
+
}();
|
|
48
|
+
var onEscapeKeyDown = (0, import_use_callback_ref.useCallbackRef)(onEscapeKeyDownProp);
|
|
49
|
+
import_react.default.useEffect(function () {
|
|
50
|
+
var handleKeyDown = function (event) {
|
|
51
|
+
if (event.key === "Escape") {
|
|
52
|
+
onEscapeKeyDown(event);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
ownerDocument.addEventListener("keydown",
|
|
56
|
+
// @ts-expect-error
|
|
57
|
+
handleKeyDown);
|
|
58
|
+
return function () {
|
|
59
|
+
ownerDocument.removeEventListener("keydown",
|
|
60
|
+
// @ts-expect-error
|
|
61
|
+
handleKeyDown);
|
|
62
|
+
};
|
|
63
|
+
}, [onEscapeKeyDown, ownerDocument]);
|
|
64
|
+
}
|
|
65
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["__toCommonJS","mod","__copyProps","__defProp","value","index_exports","__export","useEscapeKeydown","module","exports","import_use_callback_ref","require","import_react","__toESM","onEscapeKeyDownProp","ownerDocument","arguments","length","_globalThis","globalThis","document","onEscapeKeyDown","useCallbackRef","default","useEffect","handleKeyDown","event","key","addEventListener","removeEventListener"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,YAAA,GAAAC,GAAA,IAAAC,WAAA,CAAAC,SAAA;EAAAC,KAAA;AAAA,IAAAH,GAAA;AAAA,IAAAI,aAAA;AAAAC,QAAA,CAAAD,aAAA;EAAAE,gBAAA,EAAAA,CAAA,KAAAA;AAAA;AACAC,MAAA,CAAAC,OAAA,GAAAT,YAAA,CAA+BK,aAAA;AAC/B,IAAAK,uBAAkB,GAAAC,OAAA;AAKX,IAAAC,YAAS,GAAAC,OACd,CAAAF,OAAA,aACA;AAEA,SAAMJ,iBAAAO,mBAAkB;EAExB,IAAAC,aAAA,GAAAC,SAAM,CAAAC,MAAU,GAAM,KAAAD,SAAA,iBAAAA,SAAA;IACpB,IAAAE,WAAM;IACJ,OAAI,CAAAA,WAAM,GAAQC,UAAU,cAAAD,WAAA,uBAAAA,WAAA,CAAAE,QAAA;EAC1B;EAAqB,IACvBC,eAAA,OAAAX,uBAAA,CAAAY,cAAA,EAAAR,mBAAA;EAAAF,YACF,CAAAW,OAAA,CAAAC,SAAA;IAEA,IAAAC,aAAc,YAAAA,CAAAC,KAAA;MACZ,IAAAA,KAAA,CAAAC,GAAA;QAAAN,eAAA,CAAAK,KAAA;MAEA;IACF;IAEAX,aAAa,CAAAa,gBAAA,CACX;IAAc;IACZH,aAAA;IAEA,OACF;MACFV,aAAA,CAAAc,mBAAA,CACE;MACN","ignoreList":[]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useCallbackRef } from "@hanzogui/use-callback-ref";
|
|
2
|
+
import React from "react";
|
|
3
|
+
function useEscapeKeydown(onEscapeKeyDownProp, ownerDocument = globalThis?.document) {
|
|
4
|
+
const onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp);
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
const handleKeyDown = event => {
|
|
7
|
+
if (event.key === "Escape") {
|
|
8
|
+
onEscapeKeyDown(event);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
ownerDocument.addEventListener("keydown",
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
handleKeyDown);
|
|
14
|
+
return () => {
|
|
15
|
+
ownerDocument.removeEventListener("keydown",
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
handleKeyDown);
|
|
18
|
+
};
|
|
19
|
+
}, [onEscapeKeyDown, ownerDocument]);
|
|
20
|
+
}
|
|
21
|
+
export { useEscapeKeydown };
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallbackRef","React","useEscapeKeydown","onEscapeKeyDownProp","ownerDocument","globalThis","document","onEscapeKeyDown","useEffect","handleKeyDown","event","key","addEventListener","removeEventListener"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AACA,SAASA,cAAA,QAAsB;AAC/B,OAAOC,KAAA,MAAW;AAKX,SAASC,iBACdC,mBAAA,EACAC,aAAA,GAA0BC,UAAA,EAAYC,QAAA,EAChC;EACN,MAAMC,eAAA,GAAkBP,cAAA,CAAeG,mBAAmB;EAE1DF,KAAA,CAAMO,SAAA,CAAU,MAAM;IACpB,MAAMC,aAAA,GAAiBC,KAAA,IAA+B;MACpD,IAAIA,KAAA,CAAMC,GAAA,KAAQ,UAAU;QAC1BJ,eAAA,CAAgBG,KAAK;MACvB;IACF;IAEAN,aAAA,CAAcQ,gBAAA,CACZ;IAAA;IAEAH,aACF;IAEA,OAAO,MAAM;MACXL,aAAA,CAAcS,mBAAA,CACZ;MAAA;MAEAJ,aACF;IACF;EACF,GAAG,CAACF,eAAA,EAAiBH,aAAa,CAAC;AACrC","ignoreList":[]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { useCallbackRef } from "@hanzogui/use-callback-ref";
|
|
2
|
+
import React from "react";
|
|
3
|
+
function useEscapeKeydown(onEscapeKeyDownProp, ownerDocument = globalThis?.document) {
|
|
4
|
+
const onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp);
|
|
5
|
+
React.useEffect(() => {
|
|
6
|
+
const handleKeyDown = event => {
|
|
7
|
+
if (event.key === "Escape") {
|
|
8
|
+
onEscapeKeyDown(event);
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
ownerDocument.addEventListener("keydown",
|
|
12
|
+
// @ts-expect-error
|
|
13
|
+
handleKeyDown);
|
|
14
|
+
return () => {
|
|
15
|
+
ownerDocument.removeEventListener("keydown",
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
handleKeyDown);
|
|
18
|
+
};
|
|
19
|
+
}, [onEscapeKeyDown, ownerDocument]);
|
|
20
|
+
}
|
|
21
|
+
export { useEscapeKeydown };
|
|
22
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallbackRef","React","useEscapeKeydown","onEscapeKeyDownProp","ownerDocument","globalThis","document","onEscapeKeyDown","useEffect","handleKeyDown","event","key","addEventListener","removeEventListener"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AACA,SAASA,cAAA,QAAsB;AAC/B,OAAOC,KAAA,MAAW;AAKX,SAASC,iBACdC,mBAAA,EACAC,aAAA,GAA0BC,UAAA,EAAYC,QAAA,EAChC;EACN,MAAMC,eAAA,GAAkBP,cAAA,CAAeG,mBAAmB;EAE1DF,KAAA,CAAMO,SAAA,CAAU,MAAM;IACpB,MAAMC,aAAA,GAAiBC,KAAA,IAA+B;MACpD,IAAIA,KAAA,CAAMC,GAAA,KAAQ,UAAU;QAC1BJ,eAAA,CAAgBG,KAAK;MACvB;IACF;IAEAN,aAAA,CAAcQ,gBAAA,CACZ;IAAA;IAEAH,aACF;IAEA,OAAO,MAAM;MACXL,aAAA,CAAcS,mBAAA,CACZ;MAAA;MAEAJ,aACF;IACF;EACF,GAAG,CAACF,eAAA,EAAiBH,aAAa,CAAC;AACrC","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useCallbackRef } from "@hanzogui/use-callback-ref";
|
|
2
|
+
import React from "react";
|
|
3
|
+
function useEscapeKeydown(onEscapeKeyDownProp) {
|
|
4
|
+
var ownerDocument = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : function () {
|
|
5
|
+
var _globalThis;
|
|
6
|
+
return (_globalThis = globalThis) === null || _globalThis === void 0 ? void 0 : _globalThis.document;
|
|
7
|
+
}();
|
|
8
|
+
var onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp);
|
|
9
|
+
React.useEffect(function () {
|
|
10
|
+
var handleKeyDown = function (event) {
|
|
11
|
+
if (event.key === "Escape") {
|
|
12
|
+
onEscapeKeyDown(event);
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
ownerDocument.addEventListener("keydown",
|
|
16
|
+
// @ts-expect-error
|
|
17
|
+
handleKeyDown);
|
|
18
|
+
return function () {
|
|
19
|
+
ownerDocument.removeEventListener("keydown",
|
|
20
|
+
// @ts-expect-error
|
|
21
|
+
handleKeyDown);
|
|
22
|
+
};
|
|
23
|
+
}, [onEscapeKeyDown, ownerDocument]);
|
|
24
|
+
}
|
|
25
|
+
export { useEscapeKeydown };
|
|
26
|
+
//# sourceMappingURL=index.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["useCallbackRef","React","useEscapeKeydown","onEscapeKeyDownProp","ownerDocument","arguments","length","_globalThis","globalThis","document","onEscapeKeyDown","useEffect","handleKeyDown","event","key","addEventListener","removeEventListener"],"sources":["../../src/index.ts"],"sourcesContent":[null],"mappings":"AACA,SAASA,cAAA,QAAsB;AAC/B,OAAOC,KAAA,MAAW;AAKX,SAASC,iBACdC,mBAAA,EACA;EAEA,IAAAC,aAAM,GAAAC,SAAkB,CAAAC,MAAA,GAAe,KAAAD,SAAA,QAAmB,SAAAA,SAAA;IAE1D,IAAME,WAAU;IACd,OAAM,CAAAA,WAAA,GAAgBC,UAAC,MAA+B,QAAAD,WAAA,uBAAAA,WAAA,CAAAE,QAAA;EACpD;EACE,IAAAC,eAAA,GAAgBV,cAAK,CAAAG,mBAAA;EAAAF,KACvB,CAAAU,SAAA;IACF,IAAAC,aAAA,YAAAA,CAAAC,KAAA;MAEA,IAAAA,KAAA,CAAAC,GAAc;QACZJ,eAAA,CAAAG,KAAA;MAAA;IAAA;IAGFT,aAAA,CAAAW,gBAAA,CAEA,SAAO;IACL;IAAcH,aACZ;IAAA,OAEA;MACFR,aAAA,CAAAY,mBAAA,CACF;MACE;MACNJ,a","ignoreList":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hanzogui/use-escape-keydown",
|
|
3
|
+
"version": "2.0.0-rc.41-hanzoai.5",
|
|
4
|
+
"files": [
|
|
5
|
+
"src",
|
|
6
|
+
"types",
|
|
7
|
+
"dist"
|
|
8
|
+
],
|
|
9
|
+
"type": "module",
|
|
10
|
+
"sideEffects": false,
|
|
11
|
+
"main": "dist/cjs",
|
|
12
|
+
"module": "dist/esm",
|
|
13
|
+
"types": "./types/index.d.ts",
|
|
14
|
+
"exports": {
|
|
15
|
+
"./package.json": "./package.json",
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./types/index.d.ts",
|
|
18
|
+
"react-native": "./dist/esm/index.native.js",
|
|
19
|
+
"browser": "./dist/esm/index.mjs",
|
|
20
|
+
"module": "./dist/esm/index.mjs",
|
|
21
|
+
"import": "./dist/esm/index.mjs",
|
|
22
|
+
"require": "./dist/cjs/index.cjs",
|
|
23
|
+
"default": "./dist/esm/index.mjs"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "hanzogui-build",
|
|
31
|
+
"watch": "hanzogui-build --watch",
|
|
32
|
+
"clean": "hanzogui-build clean",
|
|
33
|
+
"clean:build": "hanzogui-build clean:build"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@hanzogui/use-callback-ref": "2.0.0-rc.41-hanzoai.5"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@hanzogui/build": "2.0.0-rc.41-hanzoai.5",
|
|
40
|
+
"react": ">=19"
|
|
41
|
+
},
|
|
42
|
+
"peerDependencies": {
|
|
43
|
+
"react": ">=19"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// via radix-ui
|
|
2
|
+
import { useCallbackRef } from '@hanzogui/use-callback-ref'
|
|
3
|
+
import React from 'react'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Listens for when the escape key is down
|
|
7
|
+
*/
|
|
8
|
+
export function useEscapeKeydown(
|
|
9
|
+
onEscapeKeyDownProp?: React.KeyboardEventHandler,
|
|
10
|
+
ownerDocument: Document = globalThis?.document
|
|
11
|
+
): void {
|
|
12
|
+
const onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp)
|
|
13
|
+
|
|
14
|
+
React.useEffect(() => {
|
|
15
|
+
const handleKeyDown = (event: React.KeyboardEvent) => {
|
|
16
|
+
if (event.key === 'Escape') {
|
|
17
|
+
onEscapeKeyDown(event)
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
ownerDocument.addEventListener(
|
|
22
|
+
'keydown',
|
|
23
|
+
// @ts-expect-error
|
|
24
|
+
handleKeyDown
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
return () => {
|
|
28
|
+
ownerDocument.removeEventListener(
|
|
29
|
+
'keydown',
|
|
30
|
+
// @ts-expect-error
|
|
31
|
+
handleKeyDown
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
}, [onEscapeKeyDown, ownerDocument])
|
|
35
|
+
}
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"mappings": "AAEA,OAAO,WAAW;;;;AAKlB,OAAO,iBAAS,iBACd,sBAAsB,MAAM,sBAC5B,gBAAe",
|
|
3
|
+
"names": [],
|
|
4
|
+
"sources": [
|
|
5
|
+
"src/index.ts"
|
|
6
|
+
],
|
|
7
|
+
"version": 3,
|
|
8
|
+
"sourcesContent": [
|
|
9
|
+
"// via radix-ui\nimport { useCallbackRef } from '@hanzogui/use-callback-ref'\nimport React from 'react'\n\n/**\n * Listens for when the escape key is down\n */\nexport function useEscapeKeydown(\n onEscapeKeyDownProp?: React.KeyboardEventHandler,\n ownerDocument: Document = globalThis?.document\n): void {\n const onEscapeKeyDown = useCallbackRef(onEscapeKeyDownProp)\n\n React.useEffect(() => {\n const handleKeyDown = (event: React.KeyboardEvent) => {\n if (event.key === 'Escape') {\n onEscapeKeyDown(event)\n }\n }\n\n ownerDocument.addEventListener(\n 'keydown',\n // @ts-expect-error\n handleKeyDown\n )\n\n return () => {\n ownerDocument.removeEventListener(\n 'keydown',\n // @ts-expect-error\n handleKeyDown\n )\n }\n }, [onEscapeKeyDown, ownerDocument])\n}\n"
|
|
10
|
+
]
|
|
11
|
+
}
|