Commit 891bda0c authored by chikara-chan's avatar chikara-chan Committed by 杨奕

feat: keep focusing inside the popup by `tab` key

parent d41c0c4b
...@@ -149,19 +149,41 @@ const PopupManager = { ...@@ -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;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment