Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

fix(desktop): keep command palette input focused on open

Josué813b23a169b8
diff --git a/desktop-app/ui/src/components/CommandPalette/CommandPalette.focus.test.tsx b/desktop-app/ui/src/components/CommandPalette/CommandPalette.focus.test.tsx
new file mode 100644
index 000000000..7c1e94744
--- /dev/null
+++ b/desktop-app/ui/src/components/CommandPalette/CommandPalette.focus.test.tsx
@@ -0,0 +1,37 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { cleanup, render, screen } from '@testing-library/react'
+import { CommandPalette } from '.'
+
+afterEach(cleanup)
+
+function nextAnimationFrame(): Promise<void> {
+ return new Promise(resolve => requestAnimationFrame(() => resolve()))
+}
+
+describe('CommandPalette focus ownership', () => {
+ it('reclaims focus once when native focus settles after the palette commits', async () => {
+ const focusThief = document.createElement('button')
+ document.body.append(focusThief)
+
+ render(
+ <CommandPalette
+ platform="darwin"
+ isEligible={() => true}
+ onClose={vi.fn()}
+ onExecute={vi.fn()}
+ />
+ )
+
+ const input = screen.getByRole('textbox', { name: 'Search commands' })
+ expect(document.activeElement).toBe(input)
+
+ focusThief.focus()
+ expect(document.activeElement).toBe(focusThief)
+
+ await nextAnimationFrame()
+ expect(document.activeElement).toBe(input)
+
+ focusThief.remove()
+ })
+})
diff --git a/desktop-app/ui/src/components/CommandPalette/index.tsx b/desktop-app/ui/src/components/CommandPalette/index.tsx
index b755699b3..7ef5133c5 100644
--- a/desktop-app/ui/src/components/CommandPalette/index.tsx
+++ b/desktop-app/ui/src/components/CommandPalette/index.tsx
@@ -1,4 +1,4 @@
-import { useEffect, useMemo, useRef, useState } from 'react'
+import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
import { Button, TextInput } from '@components/Common'
import {
DESKTOP_COMMANDS,
@@ -34,10 +34,15 @@ export function CommandPalette({
}, [query])
const eligibleCommands = commands.filter(command => isEligible(command.id as DesktopCommandId))
- useEffect(() => {
+ useLayoutEffect(() => {
previousFocusRef.current = document.activeElement as HTMLElement | null
- inputRef.current?.focus()
+ const input = inputRef.current
+ input?.focus()
+ const verificationFrame = requestAnimationFrame(() => {
+ if (input?.isConnected && document.activeElement !== input) input.focus()
+ })
return () => {
+ cancelAnimationFrame(verificationFrame)
const previous = previousFocusRef.current
if (restoreFocusOnUnmountRef.current) {
requestAnimationFrame(() => {