Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(frontend): keep stacked dialog focus on top layer

pulse-triage[bot]5a5232a04814
diff --git a/frontend-modern/src/components/shared/__tests__/Dialog.test.tsx b/frontend-modern/src/components/shared/__tests__/Dialog.test.tsx
index 6bd47aedb1..02ac841ff1 100644
--- a/frontend-modern/src/components/shared/__tests__/Dialog.test.tsx
+++ b/frontend-modern/src/components/shared/__tests__/Dialog.test.tsx
@@ -116,6 +116,84 @@ describe('Dialog', () => {
expect(last).toHaveFocus();
});
+ it('gives Escape, focus trapping, and backdrop dismissal to only the topmost dialog', async () => {
+ const [innerOpen, setInnerOpen] = createSignal(false);
+ const onOuterClose = vi.fn();
+ const onInnerClose = vi.fn(() => setInnerOpen(false));
+ render(() => (
+ <>
+ <Dialog isOpen={true} onClose={onOuterClose} ariaLabel="Outer dialog">
+ <button type="button" onClick={() => setInnerOpen(true)}>
+ Open inner dialog
+ </button>
+ </Dialog>
+ <Dialog isOpen={innerOpen()} onClose={onInnerClose} ariaLabel="Inner dialog">
+ <button type="button">Inner first</button>
+ <button type="button">Inner last</button>
+ </Dialog>
+ </>
+ ));
+
+ await Promise.resolve();
+ const outerAction = screen.getByRole('button', { name: 'Open inner dialog' });
+ outerAction.focus();
+ fireEvent.click(outerAction);
+ await Promise.resolve();
+ expect(screen.getByRole('button', { name: 'Inner first' })).toHaveFocus();
+
+ const innerLast = screen.getByRole('button', { name: 'Inner last' });
+ innerLast.focus();
+ fireEvent.keyDown(document, { key: 'Tab' });
+ expect(screen.getByRole('button', { name: 'Inner first' })).toHaveFocus();
+ expect(outerAction).not.toHaveFocus();
+
+ const [outerBackdrop, innerBackdrop] = Array.from(
+ document.querySelectorAll<HTMLElement>('[data-dialog-backdrop]'),
+ );
+ fireEvent.click(outerBackdrop);
+ expect(onOuterClose).not.toHaveBeenCalled();
+ expect(onInnerClose).not.toHaveBeenCalled();
+
+ fireEvent.click(innerBackdrop);
+ expect(onInnerClose).toHaveBeenCalledTimes(1);
+ expect(onOuterClose).not.toHaveBeenCalled();
+ expect(outerAction).toHaveFocus();
+
+ fireEvent.keyDown(document, { key: 'Escape' });
+ expect(onInnerClose).toHaveBeenCalledTimes(1);
+ expect(onOuterClose).toHaveBeenCalledTimes(1);
+ });
+
+ it('does not let an underlying dialog cleanup steal focus from the topmost dialog', async () => {
+ const [outerOpen, setOuterOpen] = createSignal(true);
+ const [innerOpen, setInnerOpen] = createSignal(false);
+ render(() => (
+ <>
+ <Dialog isOpen={outerOpen()} onClose={() => setOuterOpen(false)} ariaLabel="Outer dialog">
+ <button type="button" onClick={() => setInnerOpen(true)}>
+ Open inner dialog
+ </button>
+ </Dialog>
+ <Dialog isOpen={innerOpen()} onClose={() => setInnerOpen(false)} ariaLabel="Inner dialog">
+ <button type="button">Inner action</button>
+ </Dialog>
+ </>
+ ));
+
+ await Promise.resolve();
+ const outerAction = screen.getByRole('button', { name: 'Open inner dialog' });
+ outerAction.focus();
+ fireEvent.click(outerAction);
+ await Promise.resolve();
+
+ const innerAction = screen.getByRole('button', { name: 'Inner action' });
+ expect(innerAction).toHaveFocus();
+ setOuterOpen(false);
+
+ expect(innerAction).toHaveFocus();
+ expect(document.body.style.overflow).toBe('hidden');
+ });
+
it('honors an explicitly requested initial focus target', async () => {
render(() => (
<Dialog isOpen={true} onClose={() => undefined}>
diff --git a/frontend-modern/src/components/shared/useDialogState.ts b/frontend-modern/src/components/shared/useDialogState.ts
index 70001da148..ed3a4d8e8c 100644
--- a/frontend-modern/src/components/shared/useDialogState.ts
+++ b/frontend-modern/src/components/shared/useDialogState.ts
@@ -12,46 +12,55 @@ interface DialogStateOptions {
let openDialogCount = 0;
let previousBodyOverflow = '';
const [dialogStackDepth, setDialogStackDepth] = createSignal(0);
+const dialogStack: symbol[] = [];
export function dialogStackHasBlockingDialog() {
return dialogStackDepth() > 0;
}
-function lockBodyScroll() {
+function lockBodyScroll(dialogId: symbol) {
if (typeof document === 'undefined') return;
if (openDialogCount === 0) {
previousBodyOverflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
}
- openDialogCount += 1;
+ dialogStack.push(dialogId);
+ openDialogCount = dialogStack.length;
setDialogStackDepth(openDialogCount);
}
-function unlockBodyScroll() {
+function unlockBodyScroll(dialogId: symbol) {
if (typeof document === 'undefined') return;
- openDialogCount = Math.max(0, openDialogCount - 1);
+ const stackIndex = dialogStack.lastIndexOf(dialogId);
+ if (stackIndex !== -1) dialogStack.splice(stackIndex, 1);
+ openDialogCount = dialogStack.length;
setDialogStackDepth(openDialogCount);
if (openDialogCount === 0) {
document.body.style.overflow = previousBodyOverflow;
}
}
+function isTopmostDialog(dialogId: symbol) {
+ return dialogStack[dialogStack.length - 1] === dialogId;
+}
+
export function useDialogState(options: DialogStateOptions): {
handleBackdropClick: () => void;
layout: Accessor<DialogLayout>;
setPanelRef: (el: HTMLDivElement) => void;
} {
let panelRef: HTMLDivElement | undefined;
+ const dialogId = Symbol('dialog');
createEffect(() => {
if (!options.isOpen || typeof document === 'undefined') return;
const previousFocus =
document.activeElement instanceof HTMLElement ? document.activeElement : null;
- lockBodyScroll();
+ lockBodyScroll(dialogId);
queueMicrotask(() => {
- if (!panelRef) return;
+ if (!panelRef || !document.contains(panelRef) || !isTopmostDialog(dialogId)) return;
const focusable = getDialogFocusableElements(panelRef);
const requestedInitialFocus = focusable.find((element) => element.hasAttribute('autofocus'));
if (requestedInitialFocus) {
@@ -66,7 +75,7 @@ export function useDialogState(options: DialogStateOptions): {
});
const onKeyDown = (event: KeyboardEvent) => {
- if (!panelRef) return;
+ if (!panelRef || !isTopmostDialog(dialogId)) return;
if (event.key === 'Escape') {
event.preventDefault();
event.stopImmediatePropagation();
@@ -103,9 +112,10 @@ export function useDialogState(options: DialogStateOptions): {
document.addEventListener('keydown', onKeyDown);
onCleanup(() => {
+ const shouldRestoreFocus = isTopmostDialog(dialogId);
document.removeEventListener('keydown', onKeyDown);
- unlockBodyScroll();
- if (previousFocus && document.contains(previousFocus)) {
+ unlockBodyScroll(dialogId);
+ if (shouldRestoreFocus && previousFocus && document.contains(previousFocus)) {
previousFocus.focus({ preventScroll: true });
}
});
@@ -113,7 +123,7 @@ export function useDialogState(options: DialogStateOptions): {
return {
handleBackdropClick: () => {
- if (options.closeOnBackdrop ?? true) {
+ if (isTopmostDialog(dialogId) && (options.closeOnBackdrop ?? true)) {
options.onClose();
}
},