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: trap focus in add-to-thread popover (#9842)

Adam Eivy05e364782047
diff --git a/client/src/components/threads/AddToThreadButton.jsx b/client/src/components/threads/AddToThreadButton.jsx
index 8bc71848ac..f5aa0c9894 100644
--- a/client/src/components/threads/AddToThreadButton.jsx
+++ b/client/src/components/threads/AddToThreadButton.jsx
@@ -17,6 +17,8 @@ import toast from '../ui/Toast';
import EntityCombobox from '../EntityCombobox';
import useClickOutside from '../../hooks/useClickOutside';
import usePopoverPosition, { VIEWPORT_PADDING } from '../../hooks/usePopoverPosition';
+import useEscapeKey from '../../hooks/useEscapeKey.js';
+import useFocusTrap from '../../hooks/useFocusTrap.js';
import { THREAD_ACTIVE_STATUSES } from '../../lib/brainThreads';
export default function AddToThreadButton({
@@ -87,8 +89,16 @@ export default function AddToThreadButton({
position: 'below',
contentDeps: [comboboxItems.length, loading, attaching]
});
+ const menuVisible = open && Boolean(menuStyle);
useClickOutside([triggerRef, popoverRef], open, () => setOpen(false));
+ useEscapeKey(open, () => { setOpen(false); triggerRef.current?.focus(); });
+ useFocusTrap(menuVisible, popoverRef);
+ useEffect(() => {
+ if (menuVisible) {
+ popoverRef.current?.querySelector('[role="combobox"]')?.focus();
+ }
+ }, [menuVisible, popoverRef]);
const handlePick = async (item) => {
if (!computedRef || attaching) return;
@@ -160,12 +170,6 @@ export default function AddToThreadButton({
width: menuStyle?.width ?? '320px',
visibility: menuStyle ? 'visible' : 'hidden',
}}
- onKeyDown={(e) => {
- if (e.key === 'Escape') {
- e.stopPropagation();
- setOpen(false);
- }
- }}
className="z-50 bg-port-card border border-port-border rounded-lg shadow-xl p-3 space-y-2 text-white"
>
<div className="flex items-center justify-between gap-2 pb-1 border-b border-port-border">
diff --git a/client/src/components/threads/AddToThreadButton.test.jsx b/client/src/components/threads/AddToThreadButton.test.jsx
index 83630bc85c..43af1c2aa7 100644
--- a/client/src/components/threads/AddToThreadButton.test.jsx
+++ b/client/src/components/threads/AddToThreadButton.test.jsx
@@ -70,6 +70,27 @@ describe('AddToThreadButton', () => {
});
});
+ it('focuses the search input, traps Tab, and returns focus after Escape', async () => {
+ render(<AddToThreadButton threadRef={sampleRef} />);
+ const trigger = screen.getByRole('button', { name: 'Add to Brain thread' });
+ trigger.focus();
+ fireEvent.click(trigger);
+
+ const input = await screen.findByRole('combobox');
+ await waitFor(() => expect(input).toHaveFocus());
+
+ const close = screen.getByRole('button', { name: 'Close' });
+ close.focus();
+ fireEvent.keyDown(close, { key: 'Tab', shiftKey: true });
+ expect(input).toHaveFocus();
+
+ fireEvent.keyDown(input, { key: 'Escape' });
+ await waitFor(() => {
+ expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
+ expect(trigger).toHaveFocus();
+ });
+ });
+
it('attaches to an existing thread when selected from combobox', async () => {
const onAttached = vi.fn();
render(<AddToThreadButton threadRef={sampleRef} onAttached={onAttached} />);
diff --git a/client/src/hooks/useFocusTrap.js b/client/src/hooks/useFocusTrap.js
index 91a5f8c11a..09d238ec3e 100644
--- a/client/src/hooks/useFocusTrap.js
+++ b/client/src/hooks/useFocusTrap.js
@@ -25,11 +25,11 @@ import { pointerFocusSuppressedElement } from '../lib/a11yKeyboard.js';
// so without this filter the trap's computed last element diverges from the
// browser's real tab order and Tab fails to wrap, letting focus escape.
const FOCUSABLE_SELECTOR = [
- 'a[href]',
- 'button:not([disabled])',
- 'textarea:not([disabled])',
- 'input:not([disabled]):not([type="hidden"])',
- 'select:not([disabled])',
+ 'a[href]:not([tabindex="-1"])',
+ 'button:not([disabled]):not([tabindex="-1"])',
+ 'textarea:not([disabled]):not([tabindex="-1"])',
+ 'input:not([disabled]):not([type="hidden"]):not([tabindex="-1"])',
+ 'select:not([disabled]):not([tabindex="-1"])',
'[tabindex]:not([tabindex="-1"])',
].join(',');
diff --git a/client/src/hooks/useFocusTrap.test.jsx b/client/src/hooks/useFocusTrap.test.jsx
index 00fd026d1a..0abc208da1 100644
--- a/client/src/hooks/useFocusTrap.test.jsx
+++ b/client/src/hooks/useFocusTrap.test.jsx
@@ -50,6 +50,18 @@ function InputFocusDialog() {
);
}
+function NegativeTabIndexDialog() {
+ const ref = useRef(null);
+ useFocusTrap(true, ref);
+ return (
+ <div ref={ref} data-testid="dialog">
+ <button>first</button>
+ <button tabIndex={-1}>virtual option</button>
+ <button>last</button>
+ </div>
+ );
+}
+
function Harness({ active }) {
return (
<>
@@ -147,6 +159,13 @@ describe('useFocusTrap', () => {
expect(document.activeElement).toBe(screen.getByText('last'));
});
+ it('excludes tabindex=-1 controls when finding Tab wrap boundaries', () => {
+ render(<NegativeTabIndexDialog />);
+ screen.getByText('first').focus();
+ fireEvent.keyDown(screen.getByTestId('dialog'), { key: 'Tab', shiftKey: true });
+ expect(document.activeElement).toBe(screen.getByText('last'));
+ });
+
it('restores focus to the previously-focused element on deactivation', () => {
const opener = document.createElement('button');
document.body.appendChild(opener);