@rainbow-oh/yee-c 0.15.2 → 0.15.3
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/dist/cjs/Dialog/dialog.js +1 -1
- package/dist/cjs/Drawer/drawer.js +1 -1
- package/dist/cjs/ImageViewer/popup.js +1 -1
- package/dist/cjs/hooks/useEsc.d.ts +1 -1
- package/dist/cjs/hooks/useEsc.js +15 -10
- package/dist/cjs/style/themes.css +1 -1
- package/dist/cjs/style/variables.css +1 -1
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/Dialog/dialog.js +3 -2
- package/dist/esm/Drawer/drawer.js +4 -1
- package/dist/esm/ImageViewer/popup.js +4 -1
- package/dist/esm/hooks/useEsc.d.ts +1 -1
- package/dist/esm/hooks/useEsc.js +31 -9
- package/dist/esm/style/themes.css +1 -1
- package/dist/esm/style/variables.css +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/dist/umd/yee-c.min.js +1 -1
- package/package.json +1 -1
- package/src/Dialog/dialog.tsx +3 -2
- package/src/Drawer/drawer.tsx +3 -1
- package/src/ImageViewer/popup.tsx +3 -1
- package/src/hooks/useEsc.test.tsx +90 -0
- package/src/hooks/useEsc.ts +23 -12
- package/src/version.ts +1 -1
|
@@ -160,7 +160,7 @@ var Dialog = (baseprops) => {
|
|
|
160
160
|
});
|
|
161
161
|
(0, import_useLockFocus.default)(contentElement, delayOpen);
|
|
162
162
|
(0, import_useEsc.default)({
|
|
163
|
-
enabled: keyboard,
|
|
163
|
+
enabled: keyboard && open,
|
|
164
164
|
onEsc: () => {
|
|
165
165
|
onCancel == null ? void 0 : onCancel();
|
|
166
166
|
}
|
|
@@ -157,7 +157,7 @@ var Drawer = (baseprops) => {
|
|
|
157
157
|
const bodyId = `${drawerId}-body`;
|
|
158
158
|
(0, import_useLockFocus.default)(contentRef.current, delayOpen);
|
|
159
159
|
(0, import_useEsc.default)({
|
|
160
|
-
enabled: keyboard,
|
|
160
|
+
enabled: keyboard && open,
|
|
161
161
|
onEsc: () => {
|
|
162
162
|
onClose == null ? void 0 : onClose();
|
|
163
163
|
}
|
|
@@ -74,7 +74,7 @@ var Popup = (baseprops, ref) => {
|
|
|
74
74
|
const internalRef = (0, import_react2.useRef)(null);
|
|
75
75
|
const componentRef = ref || internalRef;
|
|
76
76
|
(0, import_useEsc.default)({
|
|
77
|
-
enabled: keyboard,
|
|
77
|
+
enabled: keyboard && open,
|
|
78
78
|
onEsc: () => {
|
|
79
79
|
onClose == null ? void 0 : onClose();
|
|
80
80
|
}
|
package/dist/cjs/hooks/useEsc.js
CHANGED
|
@@ -34,25 +34,30 @@ __export(useEsc_exports, {
|
|
|
34
34
|
module.exports = __toCommonJS(useEsc_exports);
|
|
35
35
|
var import_react = require("react");
|
|
36
36
|
var import_useEvent = __toESM(require("./useEvent"));
|
|
37
|
+
var escSeq = 0;
|
|
38
|
+
var escLayers = [];
|
|
37
39
|
function useEsc({ onEsc, enabled = true } = {}) {
|
|
38
40
|
const handleEsc = (0, import_useEvent.default)(onEsc || (() => {
|
|
39
41
|
}));
|
|
40
|
-
const
|
|
41
|
-
(event) => {
|
|
42
|
-
if (event.key === "Escape") {
|
|
43
|
-
event.preventDefault();
|
|
44
|
-
handleEsc(event);
|
|
45
|
-
}
|
|
46
|
-
},
|
|
47
|
-
[handleEsc]
|
|
48
|
-
);
|
|
42
|
+
const [id] = (0, import_react.useState)(() => ++escSeq);
|
|
49
43
|
(0, import_react.useEffect)(() => {
|
|
50
44
|
if (!enabled) {
|
|
51
45
|
return;
|
|
52
46
|
}
|
|
47
|
+
escLayers.push(id);
|
|
48
|
+
const handleKeyDown = (event) => {
|
|
49
|
+
if (event.key === "Escape" && escLayers[escLayers.length - 1] === id) {
|
|
50
|
+
event.preventDefault();
|
|
51
|
+
handleEsc(event);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
53
54
|
document.addEventListener("keydown", handleKeyDown);
|
|
54
55
|
return () => {
|
|
55
56
|
document.removeEventListener("keydown", handleKeyDown);
|
|
57
|
+
const index = escLayers.lastIndexOf(id);
|
|
58
|
+
if (index !== -1) {
|
|
59
|
+
escLayers.splice(index, 1);
|
|
60
|
+
}
|
|
56
61
|
};
|
|
57
|
-
}, [enabled,
|
|
62
|
+
}, [enabled, handleEsc, id]);
|
|
58
63
|
}
|
package/dist/cjs/version.d.ts
CHANGED
package/dist/cjs/version.js
CHANGED
|
@@ -114,9 +114,10 @@ var Dialog = function Dialog(baseprops) {
|
|
|
114
114
|
// lock focus in the dialog
|
|
115
115
|
useLockFocus(contentElement, delayOpen);
|
|
116
116
|
|
|
117
|
-
// enable esc to close dialog
|
|
117
|
+
// enable esc to close dialog; gated on open so a mounted-but-closed
|
|
118
|
+
// dialog does not occupy the esc layer stack and swallow the top layer
|
|
118
119
|
useEsc({
|
|
119
|
-
enabled: keyboard,
|
|
120
|
+
enabled: keyboard && open,
|
|
120
121
|
onEsc: function onEsc() {
|
|
121
122
|
onCancel === null || onCancel === void 0 || onCancel();
|
|
122
123
|
}
|
|
@@ -136,8 +136,11 @@ var Drawer = function Drawer(baseprops) {
|
|
|
136
136
|
var titleId = "".concat(drawerId, "-title");
|
|
137
137
|
var bodyId = "".concat(drawerId, "-body");
|
|
138
138
|
useLockFocus(contentRef.current, delayOpen);
|
|
139
|
+
|
|
140
|
+
// gated on open so a mounted-but-closed drawer does not occupy the esc
|
|
141
|
+
// layer stack and swallow the top layer
|
|
139
142
|
useEsc({
|
|
140
|
-
enabled: keyboard,
|
|
143
|
+
enabled: keyboard && open,
|
|
141
144
|
onEsc: function onEsc() {
|
|
142
145
|
onClose === null || onClose === void 0 || onClose();
|
|
143
146
|
}
|
|
@@ -37,8 +37,11 @@ var Popup = function Popup(baseprops, ref) {
|
|
|
37
37
|
onClose = props.onClose;
|
|
38
38
|
var internalRef = useRef(null);
|
|
39
39
|
var componentRef = ref || internalRef;
|
|
40
|
+
|
|
41
|
+
// gated on open so a mounted-but-closed viewer does not occupy the esc
|
|
42
|
+
// layer stack and swallow the top layer
|
|
40
43
|
useEsc({
|
|
41
|
-
enabled: keyboard,
|
|
44
|
+
enabled: keyboard && open,
|
|
42
45
|
onEsc: function onEsc() {
|
|
43
46
|
onClose === null || onClose === void 0 || onClose();
|
|
44
47
|
}
|
package/dist/esm/hooks/useEsc.js
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
|
|
1
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
2
|
+
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."); }
|
|
3
|
+
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); }
|
|
4
|
+
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; }
|
|
5
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
6
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
7
|
+
import { useEffect, useState } from 'react';
|
|
2
8
|
import useEvent from "./useEvent";
|
|
9
|
+
// 层级栈:所有 useEsc 监听都挂在 document 上,同一 keydown 会触发全部 handler。
|
|
10
|
+
// 按启用顺序压栈,Esc 只响应最后启用(最上层)的那一个 —— 弹层内再开
|
|
11
|
+
// 筛选/下拉/抽屉时,第一次 Esc 只关最上层,再按才逐层关闭。
|
|
12
|
+
var escSeq = 0;
|
|
13
|
+
var escLayers = [];
|
|
14
|
+
|
|
3
15
|
/**
|
|
4
|
-
*
|
|
16
|
+
* Hook for listening to ESC key press
|
|
5
17
|
*
|
|
6
18
|
* @example
|
|
7
19
|
* ```tsx
|
|
@@ -19,19 +31,29 @@ export default function useEsc() {
|
|
|
19
31
|
_ref$enabled = _ref.enabled,
|
|
20
32
|
enabled = _ref$enabled === void 0 ? true : _ref$enabled;
|
|
21
33
|
var handleEsc = useEvent(onEsc || function () {});
|
|
22
|
-
var
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}, [handleEsc]);
|
|
34
|
+
var _useState = useState(function () {
|
|
35
|
+
return ++escSeq;
|
|
36
|
+
}),
|
|
37
|
+
_useState2 = _slicedToArray(_useState, 1),
|
|
38
|
+
id = _useState2[0];
|
|
28
39
|
useEffect(function () {
|
|
29
40
|
if (!enabled) {
|
|
30
41
|
return;
|
|
31
42
|
}
|
|
43
|
+
escLayers.push(id);
|
|
44
|
+
var handleKeyDown = function handleKeyDown(event) {
|
|
45
|
+
if (event.key === 'Escape' && escLayers[escLayers.length - 1] === id) {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
handleEsc(event);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
32
50
|
document.addEventListener('keydown', handleKeyDown);
|
|
33
51
|
return function () {
|
|
34
52
|
document.removeEventListener('keydown', handleKeyDown);
|
|
53
|
+
var index = escLayers.lastIndexOf(id);
|
|
54
|
+
if (index !== -1) {
|
|
55
|
+
escLayers.splice(index, 1);
|
|
56
|
+
}
|
|
35
57
|
};
|
|
36
|
-
}, [enabled,
|
|
58
|
+
}, [enabled, handleEsc, id]);
|
|
37
59
|
}
|
package/dist/esm/version.d.ts
CHANGED
package/dist/esm/version.js
CHANGED