Keep busy dialogs keyboard reachable after native focus loss
This commit is contained in:
@@ -9,6 +9,18 @@
|
||||
!el.matches(':disabled') && !el.closest('[inert],[hidden]') &&
|
||||
el.getClientRects().length && getComputedStyle(el).visibility !== 'hidden');
|
||||
}
|
||||
function otherDialogHasFocus(state) {
|
||||
const owner = document.activeElement?.closest('[role="dialog"],dialog');
|
||||
return owner && owner !== state.el && !state.el.contains(owner);
|
||||
}
|
||||
function anchorFocus(state) {
|
||||
if (current !== state || !state.el.isConnected || state.el.closest('[inert]') ||
|
||||
state.el.contains(document.activeElement) || otherDialogHasFocus(state)) return;
|
||||
// Disabling/removing the focused control can leave focus on body. Keep the
|
||||
// current presentation reachable even when every busy action is disabled.
|
||||
const safe = focusable(state.el).find(item => item.matches('[data-initial-focus]'));
|
||||
(safe || state.el).focus();
|
||||
}
|
||||
function restoreFocus(trigger) {
|
||||
if (trigger?.isConnected && !trigger.closest('[inert]')) trigger.focus();
|
||||
if (document.activeElement === trigger) return;
|
||||
@@ -28,6 +40,7 @@
|
||||
if (!current || current.id !== id || (generation !== undefined && current.generation !== generation)) return;
|
||||
const state = current; current = null;
|
||||
state.el.removeEventListener('keydown', state.keydown);
|
||||
document.removeEventListener('keydown', state.keydown, true);
|
||||
state.locked.forEach(entry => entry.el.inert = entry.previous);
|
||||
if (!state.hadModalOpen) document.body.classList.remove('modal-open');
|
||||
restoreFocus(state.trigger);
|
||||
@@ -48,7 +61,8 @@
|
||||
if (node.parentElement === document.body) break;
|
||||
}
|
||||
state.keydown = event => {
|
||||
if (current !== state) return;
|
||||
if (current !== state || otherDialogHasFocus(state) ||
|
||||
(event.currentTarget === document && el.contains(event.target))) return;
|
||||
if (event.key === 'Escape') {
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
if (!state.busy) state.receiver.invokeMethodAsync('RequestCloseFromDom', state.generation).catch(() => {});
|
||||
@@ -60,13 +74,20 @@
|
||||
else if (event.shiftKey && (active === first || !items.includes(active))) { event.preventDefault(); last.focus(); }
|
||||
else if (!event.shiftKey && (active === last || !items.includes(active))) { event.preventDefault(); first.focus(); }
|
||||
};
|
||||
// Capture only outside-target gaps before background shortcuts. Inside events
|
||||
// stay with the existing local listener and child-widget Escape handling.
|
||||
current = state; el.addEventListener('keydown', state.keydown);
|
||||
document.addEventListener('keydown', state.keydown, true);
|
||||
document.body.classList.add('modal-open');
|
||||
const initial = focusable(el).find(item => item.matches('[data-initial-focus]'));
|
||||
(initial || el).focus();
|
||||
return true;
|
||||
}
|
||||
function update(id, generation, busy) { if (current?.id === id && current.generation === generation) current.busy = busy; }
|
||||
function update(id, generation, busy) {
|
||||
if (current?.id !== id || current.generation !== generation) return;
|
||||
current.busy = busy;
|
||||
anchorFocus(current);
|
||||
}
|
||||
new MutationObserver(() => { if (current && !current.el.isConnected) deactivate(current.id); })
|
||||
.observe(document.documentElement, { childList: true, subtree: true });
|
||||
window.otDialog = { activate, update, deactivate };
|
||||
|
||||
Reference in New Issue
Block a user