Improve Cmd+K focus reliability on web

This commit is contained in:
Mohamed Boudra
2026-02-04 12:15:36 +07:00
parent d839716e0c
commit 48cecdb6b5
4 changed files with 99 additions and 15 deletions
@@ -406,12 +406,41 @@ export function AgentInputArea({
if (!req) return;
if (req.agentKey !== `${serverId}:${agentId}`) return;
requestAnimationFrame(() => {
requestAnimationFrame(() => {
messageInputRef.current?.focus();
let cancelled = false;
const deadlineMs = Date.now() + 1500;
const tryFocus = () => {
if (cancelled) return;
const ref = messageInputRef.current;
ref?.focus();
const el = ref?.getNativeElement?.() ?? null;
const active = typeof document !== "undefined" ? document.activeElement : null;
const didFocus = !!el && active === el;
if (didFocus) {
clearFocusChatInputRequest();
return;
}
if (Date.now() >= deadlineMs) {
// Don't keep stealing focus forever; allow other interactions.
clearFocusChatInputRequest();
return;
}
requestAnimationFrame(() => {
requestAnimationFrame(tryFocus);
});
};
requestAnimationFrame(() => {
requestAnimationFrame(tryFocus);
});
return () => {
cancelled = true;
};
}, [agentId, clearFocusChatInputRequest, focusChatInputRequest, serverId]);
// Handle command selection from autocomplete