tdesign-react
Version:
TDesign Component for React
48 lines (44 loc) • 1.3 kB
JavaScript
/**
* tdesign v1.16.6
* (c) 2026 tdesign
* @license MIT
*/
import { useRef, useCallback, useEffect } from 'react';
var dialogStack = [];
var useDialogEsc = function useDialogEsc(visible, dialog) {
var addedToStackRef = useRef(false);
var focusTopDialog = useCallback(function () {
var lastDialog = dialogStack[dialogStack.length - 1];
if (lastDialog !== null && lastDialog !== void 0 && lastDialog.current) {
lastDialog.current.focus();
}
}, []);
useEffect(function () {
if (visible && dialog !== null && dialog !== void 0 && dialog.current && !addedToStackRef.current) {
dialogStack.push(dialog);
addedToStackRef.current = true;
dialog.current.focus();
}
});
useEffect(function () {
if (!visible && addedToStackRef.current) {
var index = dialogStack.indexOf(dialog);
if (index > -1) {
dialogStack.splice(index, 1);
}
addedToStackRef.current = false;
}
return function () {
for (var i = dialogStack.length - 1; i >= 0; i--) {
if (dialogStack[i].current === null) {
dialogStack.splice(i, 1);
}
}
};
}, [visible, dialog]);
return {
focusTopDialog: focusTopDialog
};
};
export { useDialogEsc as default };
//# sourceMappingURL=useDialogEsc.js.map