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

address review (claude): capture focus-restore target at render time so autoFocus modals restore correctly

[._.]/ Adam Eivya15234b22ddc
diff --git a/client/src/hooks/useFocusTrap.js b/client/src/hooks/useFocusTrap.js
index 41f84be2e9..3f14c15ccf 100644
--- a/client/src/hooks/useFocusTrap.js
+++ b/client/src/hooks/useFocusTrap.js
@@ -1,4 +1,4 @@
-import { useEffect } from 'react';
+import { useEffect, useRef } from 'react';
// Keyboard focus management for modal surfaces (dialogs, drawers, lightboxes).
// When `active` flips true it:
@@ -36,15 +36,38 @@ function getFocusable(container) {
}
export default function useFocusTrap(active, containerRef, { initialFocusRef } = {}) {
+ // Capture the element to return focus to at the RENDER where `active` flips
+ // true — before the dialog commits to the DOM and any child `autoFocus`
+ // fires. Capturing in the effect below (which runs after commit) would grab
+ // the modal's own auto-focused input instead of the control that opened it,
+ // breaking restoration for autoFocus modals like ResumeAgentModal.
+ const restoreRef = useRef(null);
+ const wasActive = useRef(false);
+ if (active && !wasActive.current) {
+ restoreRef.current = typeof document !== 'undefined' ? document.activeElement : null;
+ }
+ wasActive.current = active;
+
useEffect(() => {
if (!active) return;
const container = containerRef.current;
if (!container) return;
- const previouslyFocused = document.activeElement;
+ const previouslyFocused = restoreRef.current;
const focusInitial = () => {
- const target = initialFocusRef?.current || getFocusable(container)[0];
+ if (initialFocusRef?.current) {
+ initialFocusRef.current.focus();
+ return;
+ }
+ // Respect focus a child already claimed — React applies a child's
+ // `autoFocus` during commit, before this passive effect runs, so if
+ // focus is already inside the dialog leave it there rather than yanking
+ // it to the first focusable (which would defeat the author's autoFocus).
+ if (container.contains(document.activeElement) && document.activeElement !== container) {
+ return;
+ }
+ const target = getFocusable(container)[0];
if (target) {
target.focus();
} else {
@@ -57,7 +80,7 @@ export default function useFocusTrap(active, containerRef, { initialFocusRef } =
focusInitial();
const onKeyDown = (e) => {
- if (e.key !== 'Tab') return;
+ if (e.key !== 'Tab' || e.defaultPrevented) return;
const focusable = getFocusable(container);
if (focusable.length === 0) {
e.preventDefault();
@@ -81,6 +104,11 @@ export default function useFocusTrap(active, containerRef, { initialFocusRef } =
container.addEventListener('keydown', onKeyDown);
return () => {
container.removeEventListener('keydown', onKeyDown);
+ // Drop the fallback tabindex we may have added so the container doesn't
+ // linger as a programmatic focus target after close.
+ if (container.getAttribute('tabindex') === '-1') {
+ container.removeAttribute('tabindex');
+ }
// Restore focus to the pre-open element so keyboard users return to where
// they were. Guard: it may have been removed from the DOM while open.
if (previouslyFocused && typeof previouslyFocused.focus === 'function') {
diff --git a/client/src/hooks/useFocusTrap.test.jsx b/client/src/hooks/useFocusTrap.test.jsx
index 0513df1389..7d6d25c1bc 100644
--- a/client/src/hooks/useFocusTrap.test.jsx
+++ b/client/src/hooks/useFocusTrap.test.jsx
@@ -65,6 +65,54 @@ describe('useFocusTrap', () => {
opener.remove();
});
+ it('respects a child autoFocus instead of moving to the first focusable', () => {
+ function AutoFocusDialog({ active }) {
+ const ref = useRef(null);
+ useFocusTrap(active, ref);
+ return (
+ <div ref={ref} data-testid="dialog">
+ <button>close</button>
+ <input data-testid="field" autoFocus />
+ </div>
+ );
+ }
+ render(<AutoFocusDialog active />);
+ // React applies the input's autoFocus during commit; the trap must not
+ // yank focus to the leading close button.
+ expect(document.activeElement).toBe(screen.getByTestId('field'));
+ });
+
+ it('restores focus to the opener even when a child auto-focuses', () => {
+ function AutoFocusDialog() {
+ const ref = useRef(null);
+ useFocusTrap(true, ref);
+ return (
+ <div ref={ref}>
+ <input data-testid="field" autoFocus />
+ </div>
+ );
+ }
+ function AutoHarness({ active }) {
+ return (
+ <>
+ <button data-testid="opener">opener</button>
+ {active && <AutoFocusDialog />}
+ </>
+ );
+ }
+ const { rerender } = render(<AutoHarness active={false} />);
+ screen.getByTestId('opener').focus();
+ expect(document.activeElement).toBe(screen.getByTestId('opener'));
+
+ rerender(<AutoHarness active />);
+ expect(document.activeElement).toBe(screen.getByTestId('field'));
+
+ rerender(<AutoHarness active={false} />);
+ // The pre-open element was captured at render time (before the child's
+ // autoFocus fired), so focus returns to the opener, not the input.
+ expect(document.activeElement).toBe(screen.getByTestId('opener'));
+ });
+
it('focuses the container itself when there is nothing focusable inside', () => {
function Empty() {
const ref = useRef(null);