mirror of https://github.com/ElemeFE/element
feat: keep focusing inside the popup by `tab` key
parent
d41c0c4ba9
commit
891bda0c57
|
@ -149,19 +149,41 @@ const PopupManager = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
!Vue.prototype.$isServer && window.addEventListener('keydown', function(event) {
|
|
||||||
if (event.keyCode === 27) { // ESC
|
const getTopPopup = function() {
|
||||||
if (PopupManager.modalStack.length > 0) {
|
if (Vue.prototype.$isServer) return;
|
||||||
const topItem = PopupManager.modalStack[PopupManager.modalStack.length - 1];
|
if (PopupManager.modalStack.length > 0) {
|
||||||
if (!topItem) return;
|
const topPopup = PopupManager.modalStack[PopupManager.modalStack.length - 1];
|
||||||
const instance = PopupManager.getInstance(topItem.id);
|
if (!topPopup) return;
|
||||||
if (instance.closeOnPressEscape) {
|
const instance = PopupManager.getInstance(topPopup.id);
|
||||||
instance.handleClose
|
|
||||||
? instance.handleClose()
|
return instance;
|
||||||
: (instance.handleAction ? instance.handleAction('cancel') : instance.close());
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!Vue.prototype.$isServer) {
|
||||||
|
// handle `esc` key when the popup is shown
|
||||||
|
window.addEventListener('keydown', function(event) {
|
||||||
|
if (event.keyCode === 27) {
|
||||||
|
const topPopup = getTopPopup();
|
||||||
|
|
||||||
|
if (topPopup && topPopup.closeOnPressEscape) {
|
||||||
|
topPopup.handleClose
|
||||||
|
? topPopup.handleClose()
|
||||||
|
: (topPopup.handleAction ? topPopup.handleAction('cancel') : topPopup.close());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
});
|
|
||||||
|
// keep focusing inside the popup by `tab` key
|
||||||
|
document.addEventListener('focusin', function(event) {
|
||||||
|
const topPopup = getTopPopup();
|
||||||
|
|
||||||
|
if (topPopup && !topPopup.$el.contains(event.target)) {
|
||||||
|
event.stopPropagation();
|
||||||
|
topPopup.$el.focus();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default PopupManager;
|
export default PopupManager;
|
||||||
|
|
Loading…
Reference in New Issue