Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: let Drawer honour an inner widget's Escape and join Modal's Esc stack

[._.]/ Adam Eivy27ad6fa7ce7f
diff --git a/client/src/components/Drawer.jsx b/client/src/components/Drawer.jsx
index 03d9d987e8..c312893843 100644
--- a/client/src/components/Drawer.jsx
+++ b/client/src/components/Drawer.jsx
@@ -1,9 +1,10 @@
-import { useEffect, useRef, useState } from 'react';
+import { useRef, useState } from 'react';
import { createPortal } from 'react-dom';
import { X } from 'lucide-react';
import { useScrollLock } from '../hooks/useScrollLock';
import useFocusTrap from '../hooks/useFocusTrap.js';
import TabPills from './ui/TabPills';
+import { useEscLayer } from './ui/Modal';
// Right-side slide-in panel for the "settings over a feature page" pattern.
// Mobile (<sm): full width. Desktop: a `size` bracket (see SIZE below). Backdrop
@@ -83,12 +84,13 @@ export default function Drawer({
const currentTab = hasTabs ? (activeTab ?? internalTab) : undefined;
const changeTab = onTabChange || setInternalTab;
- useEffect(() => {
- if (!open || !closeOnEsc) return;
- const onKey = (e) => { if (e.key === 'Escape') onClose(); };
- window.addEventListener('keydown', onKey);
- return () => window.removeEventListener('keydown', onKey);
- }, [open, onClose, closeOnEsc]);
+ // Esc goes through Modal's shared stack rather than a private window
+ // listener: the top-most layer alone receives it (a drawer over a modal, or
+ // a modal over a drawer, closes innermost-first), and an inner widget that
+ // consumed the key via preventDefault() (EntityCombobox, native <select>)
+ // leaves the drawer — and the form it holds — open. A closeOnEsc=false
+ // drawer still registers so the keystroke is absorbed at this layer.
+ useEscLayer(open, () => { if (closeOnEsc) onClose(); });
if (!open) return null;
diff --git a/client/src/components/Drawer.test.jsx b/client/src/components/Drawer.test.jsx
index 8d9092e667..c0b00af9f3 100644
--- a/client/src/components/Drawer.test.jsx
+++ b/client/src/components/Drawer.test.jsx
@@ -1,6 +1,7 @@
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent, within } from '@testing-library/react';
import Drawer from './Drawer';
+import Modal from './ui/Modal';
const TABS = [
{ id: 'general', label: 'General' },
@@ -74,6 +75,59 @@ describe('Drawer', () => {
expect(onClose).not.toHaveBeenCalled();
});
+ it('leaves the drawer open when an inner widget consumed Escape via preventDefault()', () => {
+ // EntityCombobox / native <select> close their own popover on Esc and
+ // call preventDefault(); the drawer (and the form it holds) must survive.
+ const onClose = vi.fn();
+ render(
+ <Drawer open onClose={onClose} title="X">
+ <input aria-label="popover" onKeyDown={(e) => { if (e.key === 'Escape') e.preventDefault(); }} />
+ <input aria-label="plain" />
+ </Drawer>
+ );
+ fireEvent.keyDown(screen.getByLabelText('popover'), { key: 'Escape' });
+ expect(onClose).not.toHaveBeenCalled();
+ // A plain Escape from a field that doesn't own the key still closes.
+ fireEvent.keyDown(screen.getByLabelText('plain'), { key: 'Escape' });
+ expect(onClose).toHaveBeenCalledTimes(1);
+ });
+
+ it('shares the Esc stack with Modal so nested surfaces close innermost-first', () => {
+ const closeDrawer = vi.fn();
+ const closeModal = vi.fn();
+ // The confirm modal opens from inside the already-open drawer, as a
+ // user action would — it lands on top of the stack.
+ const tree = (modalOpen) => (
+ <Drawer open onClose={closeDrawer} title="X">
+ <Modal open={modalOpen} onClose={closeModal} ariaLabel="confirm"><p>sure?</p></Modal>
+ </Drawer>
+ );
+ const { rerender } = render(tree(false));
+ rerender(tree(true));
+ fireEvent.keyDown(window, { key: 'Escape' });
+ expect(closeModal).toHaveBeenCalledTimes(1);
+ expect(closeDrawer).not.toHaveBeenCalled();
+
+ rerender(tree(false));
+ fireEvent.keyDown(window, { key: 'Escape' });
+ expect(closeDrawer).toHaveBeenCalledTimes(1);
+ expect(closeModal).toHaveBeenCalledTimes(1);
+ });
+
+ it('absorbs Escape at a closeOnEsc=false drawer instead of dismissing the layer beneath', () => {
+ const closeDrawer = vi.fn();
+ const closeModal = vi.fn();
+ render(
+ <>
+ <Modal open onClose={closeModal} ariaLabel="base"><p>base</p></Modal>
+ <Drawer open onClose={closeDrawer} title="X" closeOnEsc={false}>b</Drawer>
+ </>
+ );
+ fireEvent.keyDown(window, { key: 'Escape' });
+ expect(closeDrawer).not.toHaveBeenCalled();
+ expect(closeModal).not.toHaveBeenCalled();
+ });
+
it('fires onClose from the header close button', () => {
const onClose = vi.fn();
render(<Drawer open onClose={onClose} title="X">b</Drawer>);
diff --git a/client/src/components/ui/Modal.jsx b/client/src/components/ui/Modal.jsx
index 10b693cd26..955a477deb 100644
--- a/client/src/components/ui/Modal.jsx
+++ b/client/src/components/ui/Modal.jsx
@@ -61,7 +61,9 @@
* keystroke from reaching the modal beneath it. Inner widgets that consume
* Esc themselves (native <select> closing a dropdown, custom popovers) can
* call event.preventDefault() — Modal honours `defaultPrevented` and skips
- * the close in that case.
+ * the close in that case. The stack is shared with `Drawer` through the
+ * exported `useEscLayer` hook, so a drawer over a modal (or vice versa)
+ * closes innermost-first.
*/
import { useEffect, useId, useRef } from 'react';
@@ -223,6 +225,35 @@ function popModal(id) {
if (idx >= 0) modalStack.splice(idx, 1);
}
+/**
+ * Register an open overlay on the shared Esc stack. Any layered surface that
+ * owns the keyboard while open (Modal, Drawer) joins the SAME stack so nested
+ * surfaces close innermost-first and an inner widget's `preventDefault()`
+ * on Esc is honoured uniformly.
+ *
+ * `onEsc` is read through a ref: handler identity may change every render
+ * (inline arrows) but the stack push/pop must only happen when `open`
+ * toggles, or an unrelated render of one layer would pop+re-push it above a
+ * layer that opened later. Pass a handler that returns without dispatching
+ * when the surface opts out of Esc dismissal — the layer still registers so it
+ * absorbs the keystroke instead of letting it fall through to the layer below.
+ */
+export function useEscLayer(open, onEsc) {
+ const layerId = useId();
+ const onEscRef = useRef(onEsc);
+ onEscRef.current = onEsc;
+ useEffect(() => {
+ if (!open) return;
+ const id = layerId;
+ escHandlers.set(id, () => onEscRef.current?.());
+ pushModal(id);
+ return () => {
+ escHandlers.delete(id);
+ popModal(id);
+ };
+ }, [open, layerId]);
+}
+
export default function Modal({
open,
onClose,
@@ -248,45 +279,16 @@ export default function Modal({
// focusable descendant — usually a header Close button, which is wrong for a
// dialog whose own content owns the keyboard: pass `initialFocusRef` to aim it.
useFocusTrap(open, dialogRef, { initialFocusRef });
- // `useId()` is React's render-safe stable id source — survives StrictMode's
- // double-invoke without allocating extra ids the way a `modalIdSeq++`
- // module-scope counter would.
- const modalId = useId();
- // Latest handler refs — kept fresh on every render without re-running the
- // stack-registration effect. This decouples handler identity (which
- // changes every render when callers use inline arrow functions, e.g.
- // DeployPanel during streaming output) from stack push/pop, which must
- // only happen when `open` toggles. Otherwise an unrelated render of one
- // modal would pop+re-push it and accidentally move it above another
- // modal that opened later.
- const onCloseRef = useRef(onClose);
- const onEscRef = useRef(onEsc);
- const closeOnEscRef = useRef(closeOnEsc);
- onCloseRef.current = onClose;
- onEscRef.current = onEsc;
- closeOnEscRef.current = closeOnEsc;
-
- // Push/pop only on open toggle + unmount. Handler indirection through
- // refs lets us absorb prop changes (new onClose / onEsc / closeOnEsc)
- // without touching the stack. The dispatched handler is one of:
+ // The dispatched handler is one of:
// - onEsc, if provided (caller wants custom Esc semantics — e.g.
// LayoutEditor cancels an inline mode instead of closing).
// - onClose, if closeOnEsc is true (the common case).
- // - undefined → no dispatch, but Esc is still swallowed at this layer
+ // - nothing → no dispatch, but Esc is still swallowed at this layer
// so it can't fall through to an underlying modal.
- useEffect(() => {
- if (!open) return;
- const id = modalId;
- escHandlers.set(id, () => {
- if (onEscRef.current) { onEscRef.current(); return; }
- if (closeOnEscRef.current) onCloseRef.current?.();
- });
- pushModal(id);
- return () => {
- escHandlers.delete(id);
- popModal(id);
- };
- }, [open]);
+ useEscLayer(open, () => {
+ if (onEsc) { onEsc(); return; }
+ if (closeOnEsc) onClose?.();
+ });
if (!open) return null;