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: keep focus restoration, video controls, and keyup-only callers intact (#4748)

[._.]/ Adam Eivy8d55f30f3e62
diff --git a/client/src/components/meatspace/post/MorseTrainer.jsx b/client/src/components/meatspace/post/MorseTrainer.jsx
index 71583c74bc..6cd6c4c18b 100644
--- a/client/src/components/meatspace/post/MorseTrainer.jsx
+++ b/client/src/components/meatspace/post/MorseTrainer.jsx
@@ -560,7 +560,14 @@ export default function MorseTrainer({ mode = null, onSelectMode, onExitMode, on
</div>
</div>
- <div className={`grid grid-cols-1 ${showReference ? 'xl:grid-cols-[minmax(0,1fr)_24rem]' : ''} gap-6`}>
+ {/* In send mode the keyer owns the spacebar across BOTH columns — the
+ drill on the left and the practice key / reference tabs on the right —
+ so the guard belongs on the grid that holds them, not on either one.
+ Off send mode nothing owns Space and this is inert. */}
+ <div
+ className={`grid grid-cols-1 ${showReference ? 'xl:grid-cols-[minmax(0,1fr)_24rem]' : ''} gap-6`}
+ {...(mode === 'send' ? noPointerFocusSurfaceProps : null)}
+ >
<div className="space-y-6 min-w-0 max-w-2xl">
<SettingsPanel prefs={prefs} updatePrefs={updatePrefs} onResetProgress={resetProgress} trainingStats={trainingStats} progress={morseProgress} />
{!mode && <ModeGrid onPick={onSelectMode} />}
@@ -1324,9 +1331,7 @@ function SendDrill({ keying, prefs, progress, onExit, onContinue, onSessionCompl
}
return (
- // The keyer owns the spacebar throughout send mode, so no control in here
- // may keep focus from a click and turn the next press into a button press.
- <div className="bg-port-card border border-port-border rounded-lg p-6 space-y-5" {...noPointerFocusSurfaceProps}>
+ <div className="bg-port-card border border-port-border rounded-lg p-6 space-y-5">
<div className="text-center">
<div className="text-xs text-gray-500 uppercase tracking-wide mb-1">Send this</div>
<div className="text-3xl font-mono font-bold text-white tracking-widest">{prompt}</div>
diff --git a/client/src/hooks/useFocusTrap.js b/client/src/hooks/useFocusTrap.js
index c0b1786e5d..bee276d4f4 100644
--- a/client/src/hooks/useFocusTrap.js
+++ b/client/src/hooks/useFocusTrap.js
@@ -1,4 +1,5 @@
import { useEffect, useRef } from 'react';
+import { pointerFocusSuppressedElement } from '../lib/a11yKeyboard.js';
// Keyboard focus management for modal surfaces (dialogs, drawers, lightboxes).
// When `active` flips true it:
@@ -64,7 +65,14 @@ export default function useFocusTrap(active, containerRef, { initialFocusRef } =
const restoreRef = useRef(null);
const wasActive = useRef(false);
if (active && !wasActive.current) {
- restoreRef.current = typeof document !== 'undefined' ? document.activeElement : null;
+ const focused = typeof document !== 'undefined' ? document.activeElement : null;
+ // Nothing focused means the opener may have been a control on a surface that
+ // refuses pointer focus (`noPointerFocusSurfaceProps` — e.g. an OpenWorld HUD
+ // button opening this very drawer). Fall back to it so closing still returns
+ // the user to what they clicked instead of stranding focus on <body>.
+ restoreRef.current = focused && focused !== document.body
+ ? focused
+ : (pointerFocusSuppressedElement() || focused);
}
wasActive.current = active;
@@ -110,18 +118,24 @@ export default function useFocusTrap(active, containerRef, { initialFocusRef } =
const first = focusable[0];
const last = focusable[focusable.length - 1];
const activeEl = document.activeElement;
- // Anything not IN the tab order needs steering, not just the edges and
- // the outside: an `initialFocusRef` aimed at a `tabIndex={-1}` element
- // (or the container's own fallback tabindex) sits inside the dialog but
- // outside `focusable`, and without this Shift+Tab from there fell through
- // to the browser and walked straight out of the modal.
- const inTabOrder = focusable.includes(activeEl);
+ // A target we deliberately focused outside the tab order needs steering
+ // too: an `initialFocusRef` aimed at a `tabIndex={-1}` element (or the
+ // container's own fallback tabindex) sits inside the dialog but outside
+ // `focusable`, and without this Shift+Tab from there fell through to the
+ // browser and walked straight out of the modal.
+ //
+ // Scoped to THAT element on purpose. Other unlisted-but-focusable
+ // descendants — chiefly `<video controls>`, which the selector doesn't
+ // match — must keep falling through, or Tab yanks focus off the video
+ // instead of walking into its native controls.
+ const steerTarget = initialFocusRef?.current || (container.getAttribute('tabindex') === '-1' ? container : null);
+ const adrift = activeEl === steerTarget || !container.contains(activeEl);
if (e.shiftKey) {
- if (activeEl === first || !inTabOrder) {
+ if (activeEl === first || adrift) {
e.preventDefault();
last.focus();
}
- } else if (activeEl === last || !inTabOrder) {
+ } else if (activeEl === last || adrift) {
e.preventDefault();
first.focus();
}
diff --git a/client/src/hooks/useFocusTrap.test.jsx b/client/src/hooks/useFocusTrap.test.jsx
index 7a54a36c0d..f4503c70f2 100644
--- a/client/src/hooks/useFocusTrap.test.jsx
+++ b/client/src/hooks/useFocusTrap.test.jsx
@@ -3,6 +3,7 @@ import { useRef } from 'react';
import { render, screen, fireEvent, cleanup } from '@testing-library/react';
import useFocusTrap from './useFocusTrap.js';
+import { noPointerFocusSurfaceProps } from '../lib/a11yKeyboard.js';
afterEach(cleanup);
@@ -72,6 +73,32 @@ describe('useFocusTrap', () => {
});
});
+ // A surface that owns a key refuses pointer focus for its buttons, so a HUD
+ // button that opens a dialog never becomes document.activeElement. Restoring
+ // to <body> on close would strand the keyboard user (WCAG 2.4.3).
+ it('restores focus to a clicked opener whose pointer focus was suppressed', () => {
+ function PointerHarness({ active }) {
+ return (
+ <div {...noPointerFocusSurfaceProps}>
+ <button data-testid="opener">opener</button>
+ {active && <Dialog active={active} />}
+ </div>
+ );
+ }
+ const { rerender } = render(<PointerHarness active={false} />);
+ const opener = screen.getByTestId('opener');
+
+ // The surface cancels the mousedown, so the opener never takes focus.
+ fireEvent.mouseDown(opener);
+ expect(document.activeElement).not.toBe(opener);
+
+ rerender(<PointerHarness active />);
+ expect(document.activeElement).toBe(screen.getByText('first'));
+
+ rerender(<PointerHarness active={false} />);
+ expect(document.activeElement).toBe(opener);
+ });
+
it('wraps focus from the last element to the first on Tab', () => {
render(<Dialog active />);
const last = screen.getByText('last');
diff --git a/client/src/hooks/useKeyCapture.js b/client/src/hooks/useKeyCapture.js
index 6c2808d4bd..9b11851642 100644
--- a/client/src/hooks/useKeyCapture.js
+++ b/client/src/hooks/useKeyCapture.js
@@ -81,7 +81,10 @@ export default function useKeyCapture({ enabled = true, enabledInDialog = false,
if (isDown) {
if (shouldIgnoreGlobalKey(e, guards)) { ignoredDown.add(e.code || e.key); return; }
ignoredDown.delete(e.code || e.key);
- } else if (ignoredDown.delete(e.code || e.key)) {
+ } else if (hasDown ? ignoredDown.delete(e.code || e.key) : shouldIgnoreGlobalKey(e, guards)) {
+ // Mirroring only works when this hook actually saw the keydown. A
+ // keyup-only caller has no decision to mirror, so it evaluates the
+ // guards directly — otherwise its releases would bypass them entirely.
return;
}
if (ref.current?.(e) !== true) return;
diff --git a/client/src/hooks/useKeyCapture.test.jsx b/client/src/hooks/useKeyCapture.test.jsx
index 732efd9f9e..b3aed3c210 100644
--- a/client/src/hooks/useKeyCapture.test.jsx
+++ b/client/src/hooks/useKeyCapture.test.jsx
@@ -214,6 +214,20 @@ describe('useKeyCapture', () => {
expect(up).not.toHaveBeenCalled();
});
+ // A keyup-only caller has no keydown decision to mirror, so it must evaluate
+ // the guards itself rather than waving every release through.
+ it('guards a keyup-only caller directly, with no keydown to mirror', () => {
+ const up = vi.fn(() => true);
+ const { getByRole, getByLabelText } = render(<Probe onKeyUp={up} />);
+
+ fireEvent.keyUp(getByRole('button', { name: 'act' }), { code: 'Space', key: ' ' });
+ fireEvent.keyUp(getByLabelText('note'), { code: 'Space', key: ' ' });
+ expect(up).not.toHaveBeenCalled();
+
+ fireEvent.keyUp(document.body, { code: 'Space', key: ' ' });
+ expect(up).toHaveBeenCalledTimes(1);
+ });
+
it('reads the latest handler without re-subscribing', () => {
const first = vi.fn(() => true);
const second = vi.fn(() => true);
diff --git a/client/src/lib/a11yKeyboard.js b/client/src/lib/a11yKeyboard.js
index 4eeb4c2e93..f260e88c33 100644
--- a/client/src/lib/a11yKeyboard.js
+++ b/client/src/lib/a11yKeyboard.js
@@ -195,8 +195,30 @@ export function shouldIgnoreGlobalKey(event, { enabledInDialog = false, ignoreRe
* across several child components. One capture-phase handler at the root covers
* all of them, including ones that don't exist yet.
*/
+// The control whose pointer focus was last suppressed. A button that opens a
+// dialog never becomes `document.activeElement`, so `useFocusTrap` would capture
+// `<body>` as the element to restore focus to on close (WCAG 2.4.3) — this is
+// what it falls back to instead. No staleness guard is needed beyond
+// `isConnected`: the fallback is only ever consulted when nothing else holds
+// focus, and anything else taking focus is exactly what makes that untrue.
+let pointerFocusSuppressed = null;
+
export const noPointerFocusSurfaceProps = {
onMouseDownCapture: (event) => {
- if (event.target?.closest?.('button, [role="button"]')) event.preventDefault();
+ const control = event.target?.closest?.('button, [role="button"]');
+ if (!control) return;
+ event.preventDefault();
+ pointerFocusSuppressed = control;
},
};
+
+/**
+ * The control `noPointerFocusSurfaceProps` last kept focus off, if it is still
+ * in the document. Focus restoration consults this when it finds nothing
+ * focused; everything else should read `document.activeElement`.
+ *
+ * @returns {Element | null}
+ */
+export function pointerFocusSuppressedElement() {
+ return pointerFocusSuppressed?.isConnected ? pointerFocusSuppressed : null;
+}