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: forward Tab from a tabbable initial focus target reaches the next control (#9537)

Adam Eivy9521f170677d
diff --git a/client/src/hooks/useFocusTrap.js b/client/src/hooks/useFocusTrap.js
index edfa5bdde0..91a5f8c11a 100644
--- a/client/src/hooks/useFocusTrap.js
+++ b/client/src/hooks/useFocusTrap.js
@@ -124,12 +124,15 @@ export default function useFocusTrap(active, containerRef, { initialFocusRef } =
// `focusable`, and without this Shift+Tab from there fell through to the
// browser and walked straight out of the modal.
//
+ // Only when it is absent from `focusable`: a tabbable initial input must
+ // let forward Tab reach its next sibling and wrap only at the boundaries.
+ //
// 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);
+ const adrift = (activeEl === steerTarget && !focusable.includes(steerTarget)) || !container.contains(activeEl);
if (e.shiftKey) {
if (activeEl === first || adrift) {
e.preventDefault();
diff --git a/client/src/hooks/useFocusTrap.test.jsx b/client/src/hooks/useFocusTrap.test.jsx
index f4503c70f2..00fd026d1a 100644
--- a/client/src/hooks/useFocusTrap.test.jsx
+++ b/client/src/hooks/useFocusTrap.test.jsx
@@ -36,6 +36,20 @@ function HeaderFocusDialog() {
);
}
+// Initial focus aimed at an ordinary TABBABLE input (a capture box) followed by
+// a button — the CmdK capture shape.
+function InputFocusDialog() {
+ const ref = useRef(null);
+ const inputRef = useRef(null);
+ useFocusTrap(true, ref, { initialFocusRef: inputRef });
+ return (
+ <div ref={ref} data-testid="dialog">
+ <input ref={inputRef} aria-label="thought" />
+ <button>capture</button>
+ </div>
+ );
+}
+
function Harness({ active }) {
return (
<>
@@ -73,6 +87,24 @@ describe('useFocusTrap', () => {
});
});
+ describe('with an initialFocusRef aimed at a tabbable input', () => {
+ it('lets forward Tab reach the next control', () => {
+ render(<InputFocusDialog />);
+ expect(document.activeElement).toBe(screen.getByLabelText('thought'));
+ const dialog = screen.getByTestId('dialog');
+ expect(fireEvent.keyDown(dialog, { key: 'Tab' })).toBe(true); // not prevented → browser advances
+ screen.getByText('capture').focus();
+ fireEvent.keyDown(dialog, { key: 'Tab' });
+ expect(document.activeElement).toBe(screen.getByLabelText('thought'));
+ });
+
+ it('wraps Shift+Tab from the first control to the last', () => {
+ render(<InputFocusDialog />);
+ fireEvent.keyDown(screen.getByTestId('dialog'), { key: 'Tab', shiftKey: true });
+ expect(document.activeElement).toBe(screen.getByText('capture'));
+ });
+ });
+
// 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).