Skip to content

The agent that grows with you

feat(desktop): keyboard-first overlay primitives

Brooklyn Nicholson913882cdbbc2
diff --git a/apps/desktop/src/components/ui/keyboard-first.test.ts b/apps/desktop/src/components/ui/keyboard-first.test.ts
new file mode 100644
index 00000000000..326bdbf91d7
--- /dev/null
+++ b/apps/desktop/src/components/ui/keyboard-first.test.ts
@@ -0,0 +1,69 @@
+import { act, renderHook } from '@testing-library/react'
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { onReleaseTypingFocus, releaseTypingFocus, usePointerQuiet } from './keyboard-first'
+
+const move = () => act(() => void window.dispatchEvent(new MouseEvent('mousemove', { bubbles: true })))
+const scroll = () => act(() => void window.dispatchEvent(new WheelEvent('wheel', { bubbles: true })))
+
+afterEach(() => {
+ vi.restoreAllMocks()
+})
+
+describe('usePointerQuiet', () => {
+ it('starts quiet so a list opening under a parked cursor cannot hover-select', () => {
+ const { result } = renderHook(() => usePointerQuiet())
+
+ expect(result.current).toBe(true)
+ })
+
+ it('wakes on real pointer movement and stays awake', () => {
+ const { result } = renderHook(() => usePointerQuiet())
+
+ move()
+ expect(result.current).toBe(false)
+
+ // A later re-render must not re-arm the guard mid-interaction.
+ move()
+ expect(result.current).toBe(false)
+ })
+
+ it('treats a scroll as a hand on the mouse', () => {
+ const { result } = renderHook(() => usePointerQuiet())
+
+ scroll()
+ expect(result.current).toBe(false)
+ })
+
+ it('re-arms for the next overlay (each open mounts a fresh guard)', () => {
+ const first = renderHook(() => usePointerQuiet())
+
+ move()
+ expect(first.result.current).toBe(false)
+ first.unmount()
+
+ expect(renderHook(() => usePointerQuiet()).result.current).toBe(true)
+ })
+
+ it('drops its listeners on unmount', () => {
+ const remove = vi.spyOn(window, 'removeEventListener')
+
+ renderHook(() => usePointerQuiet()).unmount()
+
+ expect(remove.mock.calls.map(([type]) => type)).toEqual(expect.arrayContaining(['mousemove', 'wheel']))
+ })
+})
+
+describe('releaseTypingFocus', () => {
+ it('notifies subscribers, and stops once unsubscribed', () => {
+ const handler = vi.fn()
+ const off = onReleaseTypingFocus(handler)
+
+ releaseTypingFocus()
+ expect(handler).toHaveBeenCalledTimes(1)
+
+ off()
+ releaseTypingFocus()
+ expect(handler).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/apps/desktop/src/components/ui/keyboard-first.ts b/apps/desktop/src/components/ui/keyboard-first.ts
new file mode 100644
index 00000000000..17253744485
--- /dev/null
+++ b/apps/desktop/src/components/ui/keyboard-first.ts
@@ -0,0 +1,75 @@
+import { useEffect, useState } from 'react'
+
+/**
+ * KEYBOARD-FIRST OVERLAYS — the shared contract for anything you open with a
+ * hotkey and drive from a search field: the composer model menu, the ⌘K
+ * palette, every cmdk picker.
+ *
+ * Both halves exist because a mouse that is merely PRESENT is not a mouse the
+ * user is using.
+ */
+
+const RELEASE_EVENT = 'hermes:release-typing-focus'
+
+/**
+ * True while the pointer must be treated as absent.
+ *
+ * A list that opens under a parked cursor, or that re-flows under one as the
+ * query filters it, fires pointerenter on whatever row happens to slide beneath
+ * — and hover-selecting menus (Radix) or hover-highlighting lists take that as
+ * intent, stealing the row the user typed toward. Nothing about that came from
+ * the user.
+ *
+ * So the pointer stays inert until it actually MOVES (or scrolls, which is also
+ * a hand on the mouse). One real movement hands hover back for the rest of the
+ * overlay's life. Spread the result as `pointer-events-none` on the list — the
+ * blunt instrument is the right one here, because it suppresses the synthetic
+ * enter events at the source rather than racing them.
+ *
+ * Mount-scoped: overlays mount when they open, so "quiet until moved" needs no
+ * knowledge of whether a hotkey or a click opened it.
+ */
+export function usePointerQuiet(): boolean {
+ const [quiet, setQuiet] = useState(true)
+
+ useEffect(() => {
+ const wake = () => setQuiet(false)
+ const options = { capture: true } as const
+
+ window.addEventListener('mousemove', wake, options)
+ window.addEventListener('wheel', wake, options)
+
+ return () => {
+ window.removeEventListener('mousemove', wake, options)
+ window.removeEventListener('wheel', wake, options)
+ }
+ }, [])
+
+ return quiet
+}
+
+/**
+ * Hand the keyboard back to wherever the user was typing.
+ *
+ * Dismissing a keyboard-driven overlay ends its claim on focus. Radix returns
+ * focus to the TRIGGER on close, which for the model menu is a toolbar button —
+ * so committing with Enter left the next keystroke going nowhere instead of
+ * into the message you were about to write. Call this on close; the composer
+ * bus (subscribed once in `useKeybinds`) does the focusing, so this primitive
+ * stays ignorant of what "typing" means on any given surface.
+ */
+export function releaseTypingFocus(): void {
+ if (typeof window !== 'undefined') {
+ window.dispatchEvent(new CustomEvent(RELEASE_EVENT))
+ }
+}
+
+export function onReleaseTypingFocus(handler: () => void): () => void {
+ if (typeof window === 'undefined') {
+ return () => undefined
+ }
+
+ window.addEventListener(RELEASE_EVENT, handler)
+
+ return () => window.removeEventListener(RELEASE_EVENT, handler)
+}