Skip to content

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

TypeScript

feat(desktop): keep palette arrow selection visible

Josué1fd7071e95b5
diff --git a/desktop-app/ui/src/components/CommandPalette/CommandPalette.navigation.test.tsx b/desktop-app/ui/src/components/CommandPalette/CommandPalette.navigation.test.tsx
new file mode 100644
index 000000000..84a72cba4
--- /dev/null
+++ b/desktop-app/ui/src/components/CommandPalette/CommandPalette.navigation.test.tsx
@@ -0,0 +1,47 @@
+// @vitest-environment jsdom
+import { afterEach, describe, expect, it, vi } from 'vitest'
+import { fireEvent, render, screen, waitFor } from '@testing-library/react'
+import { CommandPalette } from '.'
+
+const scrollIntoView = vi.fn()
+
+afterEach(() => {
+ vi.restoreAllMocks()
+ scrollIntoView.mockReset()
+})
+
+describe('CommandPalette arrow navigation', () => {
+ it('scrolls each newly selected command into the visible list area', async () => {
+ Object.defineProperty(HTMLElement.prototype, 'scrollIntoView', {
+ configurable: true,
+ value: scrollIntoView,
+ })
+
+ render(
+ <CommandPalette
+ platform="darwin"
+ isEligible={() => true}
+ onClose={vi.fn()}
+ onExecute={vi.fn()}
+ />
+ )
+
+ const input = screen.getByRole('textbox', { name: 'Search commands' })
+ const options = screen.getAllByRole('option')
+ await waitFor(() => expect(options[0]?.getAttribute('aria-selected')).toBe('true'))
+ scrollIntoView.mockClear()
+
+ fireEvent.keyDown(input, { key: 'ArrowDown' })
+ await waitFor(() => {
+ expect(options[1]?.getAttribute('aria-selected')).toBe('true')
+ expect(scrollIntoView).toHaveBeenLastCalledWith({ block: 'nearest' })
+ })
+
+ scrollIntoView.mockClear()
+ fireEvent.keyDown(input, { key: 'ArrowUp' })
+ await waitFor(() => {
+ expect(options[0]?.getAttribute('aria-selected')).toBe('true')
+ expect(scrollIntoView).toHaveBeenLastCalledWith({ block: 'nearest' })
+ })
+ })
+})
diff --git a/desktop-app/ui/src/components/CommandPalette/CommandPaletteList.tsx b/desktop-app/ui/src/components/CommandPalette/CommandPaletteList.tsx
new file mode 100644
index 000000000..d44117270
--- /dev/null
+++ b/desktop-app/ui/src/components/CommandPalette/CommandPaletteList.tsx
@@ -0,0 +1,72 @@
+import { useEffect, useRef } from 'react'
+import { Button } from '@components/Common'
+import {
+ type DesktopCommandDefinition,
+ type DesktopCommandId,
+ type DesktopShortcutPlatform,
+ formatDesktopShortcut,
+} from '../../../../src/desktopCommands'
+
+type CommandPaletteListProps = {
+ commands: readonly DesktopCommandDefinition[]
+ isEligible: (commandId: DesktopCommandId) => boolean
+ onExecute: (commandId: DesktopCommandId) => void
+ platform: DesktopShortcutPlatform
+ selectedId: DesktopCommandId | null
+}
+
+export function CommandPaletteList({
+ commands,
+ isEligible,
+ onExecute,
+ platform,
+ selectedId,
+}: CommandPaletteListProps) {
+ const itemRefs = useRef(new Map<DesktopCommandId, HTMLButtonElement>())
+
+ useEffect(() => {
+ if (!selectedId) return
+ const selectedItem = itemRefs.current.get(selectedId)
+ if (typeof selectedItem?.scrollIntoView === 'function') {
+ selectedItem.scrollIntoView({ block: 'nearest' })
+ }
+ }, [selectedId])
+
+ return (
+ <div className="command-palette__list" role="listbox" aria-label="Desktop commands">
+ {commands.length ? (
+ commands.map(command => {
+ const commandId = command.id as DesktopCommandId
+ const eligible = isEligible(commandId)
+ const selected = eligible && selectedId === commandId
+ return (
+ <Button
+ ref={element => {
+ if (element) itemRefs.current.set(commandId, element)
+ else itemRefs.current.delete(commandId)
+ }}
+ aria-selected={selected}
+ className={`command-palette__item${selected ? ' is-selected' : ''}`}
+ color="neutral"
+ disabled={!eligible}
+ key={command.id}
+ onClick={() => onExecute(commandId)}
+ role="option"
+ variant="ghost"
+ >
+ <span className="command-palette__item-copy">
+ <strong>{command.label}</strong>
+ <span>{command.description}</span>
+ </span>
+ {command.defaultBinding ? (
+ <kbd>{formatDesktopShortcut(command.defaultBinding, platform)}</kbd>
+ ) : null}
+ </Button>
+ )
+ })
+ ) : (
+ <p className="command-palette__empty">No matching commands.</p>
+ )}
+ </div>
+ )
+}
diff --git a/desktop-app/ui/src/components/CommandPalette/index.tsx b/desktop-app/ui/src/components/CommandPalette/index.tsx
index 7ef5133c5..0172f66d1 100644
--- a/desktop-app/ui/src/components/CommandPalette/index.tsx
+++ b/desktop-app/ui/src/components/CommandPalette/index.tsx
@@ -1,10 +1,7 @@
import { useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
-import { Button, TextInput } from '@components/Common'
-import {
- DESKTOP_COMMANDS,
- type DesktopCommandId,
- formatDesktopShortcut,
-} from '../../../../src/desktopCommands'
+import { TextInput } from '@components/Common'
+import { DESKTOP_COMMANDS, type DesktopCommandId } from '../../../../src/desktopCommands'
+import { CommandPaletteList } from './CommandPaletteList'
import type { CommandPaletteProps } from './types'
export function CommandPalette({
@@ -129,37 +126,13 @@ export function CommandPalette({
value={query}
/>
</div>
- <div className="command-palette__list" role="listbox" aria-label="Desktop commands">
- {commands.length ? (
- commands.map(command => {
- const commandId = command.id as DesktopCommandId
- const eligible = isEligible(commandId)
- const selected = eligible && selectedId === commandId
- return (
- <Button
- aria-selected={selected}
- className={`command-palette__item${selected ? ' is-selected' : ''}`}
- color="neutral"
- disabled={!eligible}
- key={command.id}
- onClick={() => execute(commandId)}
- role="option"
- variant="ghost"
- >
- <span className="command-palette__item-copy">
- <strong>{command.label}</strong>
- <span>{command.description}</span>
- </span>
- {command.defaultBinding ? (
- <kbd>{formatDesktopShortcut(command.defaultBinding, platform)}</kbd>
- ) : null}
- </Button>
- )
- })
- ) : (
- <p className="command-palette__empty">No matching commands.</p>
- )}
- </div>
+ <CommandPaletteList
+ commands={commands}
+ isEligible={isEligible}
+ onExecute={execute}
+ platform={platform}
+ selectedId={selectedId}
+ />
</div>
</div>
)