fix: make keyboard note navigation predictable

This commit is contained in:
lucaronin
2026-04-16 09:04:13 +02:00
parent 1b3a9e3ecf
commit 57a87e43b3
10 changed files with 511 additions and 199 deletions

View File

@@ -42,11 +42,13 @@ describe('useNoteListKeyboard', () => {
})
it('ArrowDown highlights first item from no selection', () => {
const open = vi.fn()
const { result } = renderHook(() =>
useNoteListKeyboard({ items, selectedNotePath: null, onOpen, enabled: true }),
useNoteListKeyboard({ items, selectedNotePath: null, onOpen: open, enabled: true }),
)
act(() => result.current.handleKeyDown(keyEvent('ArrowDown')))
expect(result.current.highlightedPath).toBe('/a.md')
expect(open).toHaveBeenCalledWith(items[0])
})
it('ArrowDown advances highlight', () => {
@@ -70,11 +72,26 @@ describe('useNoteListKeyboard', () => {
})
it('ArrowUp highlights last item from no selection', () => {
const open = vi.fn()
const { result } = renderHook(() =>
useNoteListKeyboard({ items, selectedNotePath: null, onOpen, enabled: true }),
useNoteListKeyboard({ items, selectedNotePath: null, onOpen: open, enabled: true }),
)
act(() => result.current.handleKeyDown(keyEvent('ArrowUp')))
expect(result.current.highlightedPath).toBe('/c.md')
expect(open).toHaveBeenCalledWith(items[2])
})
it('scrolls the highlighted item into view with nearest-style behavior', () => {
const open = vi.fn()
const { result } = renderHook(() =>
useNoteListKeyboard({ items, selectedNotePath: null, onOpen: open, enabled: true }),
)
const scrollIntoView = vi.fn()
result.current.virtuosoRef.current = { scrollIntoView } as never
act(() => result.current.handleKeyDown(keyEvent('ArrowDown')))
expect(scrollIntoView).toHaveBeenCalledWith({ index: 0, behavior: 'auto' })
})
it('ArrowUp clamps at start of list', () => {