Skip to content

The agent that grows with you

fix(desktop): one tap focuses the Webapp composer on iPhone, with casing and correction

Tekniumd7ef3d91e61d
diff --git a/apps/desktop/src/app/chat/composer/index.tsx b/apps/desktop/src/app/chat/composer/index.tsx
index 13e4102c416..606eb48b55f 100644
--- a/apps/desktop/src/app/chat/composer/index.tsx
+++ b/apps/desktop/src/app/chat/composer/index.tsx
@@ -24,7 +24,7 @@ import { chatMessageText } from '@/lib/chat-messages'
import { sanitizeComposerInput } from '@/lib/composer-input-sanitize'
import { DATA_IMAGE_URL_RE } from '@/lib/embedded-images'
import { triggerHaptic } from '@/lib/haptics'
-import { isMacPlatform } from '@/lib/platform'
+import { $touchPointer } from '@/lib/touch-interaction'
import { useStoreSelector, useStoresSelector } from '@/lib/use-session-slice'
import { cn } from '@/lib/utils'
import { sessionCompacting } from '@/store/compaction'
@@ -104,6 +104,7 @@ import { ComposerStatusStack } from './status-stack'
import { CodingStatusRow } from './status-stack/coding-row'
import { StatusDrawerContent, StatusDrawerToggle } from './status-stack/drawer'
import { extractClipboardImageBlobs, openDirectiveScope } from './text-utils'
+import { composerTextEntryProps, touchTapFocusHandlers } from './touch-text-entry'
import { ComposerTriggerPopover } from './trigger-popover'
import type { ChatBarProps } from './types'
import { isRedoShortcut, isUndoShortcut } from './undo-history'
@@ -158,6 +159,8 @@ export function ChatBar({
onTranscribeAudio
}: ChatBarProps) {
const hudMode = useStore($hudMode)
+ const touchPointer = useStore($touchPointer)
+ const tapFocus = useMemo(() => (touchPointer ? touchTapFocusHandlers() : undefined), [touchPointer])
const hudWindowing = window.hermesDesktop?.hud?.windowing
const hudNativeDrag = hudMode && hudWindowing?.nativeDrag === true
@@ -1166,10 +1169,8 @@ export function ChatBar({
<div
aria-disabled={inputDisabled ? true : undefined}
aria-label={t.composer.message}
- autoCapitalize="off"
- // Chromium's macOS text-replacement path shares the autocorrect gate.
- // Keeping spellcheck off below still excludes smart quotes and dashes.
- autoCorrect={isMacPlatform() ? 'on' : 'off'}
+ {...composerTextEntryProps(touchPointer)}
+ {...tapFocus}
className={cn(
'min-h-[1.625rem] min-h-(--composer-input-min-height) max-h-(--composer-input-max-height) cursor-text overflow-y-auto whitespace-pre-wrap break-words [overflow-wrap:anywhere] bg-transparent pb-1 pr-1 pt-1 leading-normal text-foreground outline-none disabled:cursor-not-allowed',
'**:data-ref-text:cursor-default',
@@ -1233,7 +1234,6 @@ export function ChatBar({
onPaste={handlePaste}
ref={editorRef}
role="textbox"
- spellCheck={false}
suppressContentEditableWarning
/>
<ComposerDirectiveActions editorRef={editorRef} />
diff --git a/apps/desktop/src/app/chat/composer/touch-text-entry.test.tsx b/apps/desktop/src/app/chat/composer/touch-text-entry.test.tsx
new file mode 100644
index 00000000000..3a179bcc7de
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/touch-text-entry.test.tsx
@@ -0,0 +1,58 @@
+import { cleanup, fireEvent, render } from '@testing-library/react'
+import { useMemo } from 'react'
+import { afterEach, describe, expect, it } from 'vitest'
+
+import { composerTextEntryProps, touchTapFocusHandlers } from './touch-text-entry'
+
+afterEach(cleanup)
+
+// The composer editor's wiring in index.tsx and user-edit-composer.tsx: the
+// text-entry props and, on a touch host only, the tap-focus handlers.
+function Editor({ touch }: { touch: boolean }) {
+ const tapFocus = useMemo(() => (touch ? touchTapFocusHandlers() : undefined), [touch])
+
+ return (
+ <div
+ {...composerTextEntryProps(touch)}
+ {...tapFocus}
+ contentEditable
+ data-testid="editor"
+ role="textbox"
+ suppressContentEditableWarning
+ />
+ )
+}
+
+const point = (x: number, y: number) => ({ clientX: x, clientY: y, identifier: 0 })
+
+function tap(editor: HTMLElement, from: [number, number], to: [number, number] = from) {
+ fireEvent.touchStart(editor, { touches: [point(...from)], changedTouches: [point(...from)] })
+ fireEvent.touchEnd(editor, { touches: [], changedTouches: [point(...to)] })
+}
+
+describe('composer text entry on a touch host (#145166)', () => {
+ it('one still tap focuses the editor, which offers casing, correction and spelling', () => {
+ const editor = render(<Editor touch />).getByTestId('editor')
+
+ expect(editor.getAttribute('autocapitalize')).toBe('sentences')
+ expect(editor.getAttribute('autocorrect')).toBe('on')
+ expect(editor.getAttribute('spellcheck')).toBe('true')
+
+ // A swipe across the composer is a scroll, not a tap: focus stays put.
+ tap(editor, [10, 10], [10, 60])
+ expect(editor.ownerDocument.activeElement).not.toBe(editor)
+
+ tap(editor, [10, 10], [12, 11])
+ expect(editor.ownerDocument.activeElement).toBe(editor)
+ })
+
+ it('desktop keeps the attributes off and leaves taps to the browser', () => {
+ const editor = render(<Editor touch={false} />).getByTestId('editor')
+
+ expect(editor.getAttribute('autocapitalize')).toBe('off')
+ expect(editor.getAttribute('spellcheck')).toBe('false')
+
+ tap(editor, [10, 10])
+ expect(editor.ownerDocument.activeElement).not.toBe(editor)
+ })
+})
diff --git a/apps/desktop/src/app/chat/composer/touch-text-entry.ts b/apps/desktop/src/app/chat/composer/touch-text-entry.ts
new file mode 100644
index 00000000000..95579a4b12c
--- /dev/null
+++ b/apps/desktop/src/app/chat/composer/touch-text-entry.ts
@@ -0,0 +1,70 @@
+import type { TouchEvent as ReactTouchEvent } from 'react'
+
+import { isMacPlatform } from '@/lib/platform'
+
+import { placeCaretEnd } from './rich-editor'
+
+// A tap, not a scroll, drag or long press (which iOS hands to selection).
+const TAP_MAX_MOVE_PX = 10
+const TAP_MAX_MS = 500
+
+/** Text-entry attributes for the composer editor. Desktop keeps them off: the
+ * composer takes paths, code and commands, and Chromium's macOS
+ * text-replacement path shares the autocorrect gate (spellcheck stays off, so
+ * smart quotes and dashes stay out). A touch host types prose on a soft
+ * keyboard and gets the platform's casing, correction and spelling. */
+export function composerTextEntryProps(touch: boolean) {
+ return touch
+ ? ({ autoCapitalize: 'sentences', autoCorrect: 'on', spellCheck: true } as const)
+ : ({ autoCapitalize: 'off', autoCorrect: isMacPlatform() ? 'on' : 'off', spellCheck: false } as const)
+}
+
+function placeCaretAt(editor: HTMLElement, x: number, y: number) {
+ const range = document.caretRangeFromPoint?.(x, y)
+ const selection = window.getSelection()
+
+ if (range && selection && editor.contains(range.startContainer)) {
+ selection.removeAllRanges()
+ selection.addRange(range)
+ } else {
+ placeCaretEnd(editor)
+ }
+}
+
+/** Focus the editor from the touchend of a short, still tap. iPhone Safari
+ * drops the synthetic click of a single tap on the composer (a long press
+ * still works), so the editor never gets focus or the keyboard. A focus()
+ * inside touchend runs in the user gesture, so iOS opens the keyboard. An
+ * already focused editor is left to the browser, which moves the caret. */
+export function touchTapFocusHandlers() {
+ let start: { at: number; x: number; y: number } | null = null
+
+ return {
+ onTouchEnd(event: ReactTouchEvent<HTMLElement>) {
+ const touch = event.changedTouches[0]
+ const editor = event.currentTarget
+ const began = start
+ start = null
+
+ if (!began || !touch || editor.getAttribute('contenteditable') === 'false' || document.activeElement === editor) {
+ return
+ }
+
+ const still = Math.hypot(touch.clientX - began.x, touch.clientY - began.y) <= TAP_MAX_MOVE_PX
+
+ if (!still || event.timeStamp - began.at > TAP_MAX_MS) {
+ return
+ }
+
+ editor.focus()
+ placeCaretAt(editor, touch.clientX, touch.clientY)
+ },
+ onTouchStart(event: ReactTouchEvent<HTMLElement>) {
+ const touch = event.touches.length === 1 ? event.touches[0] : undefined
+ start = touch ? { at: event.timeStamp, x: touch.clientX, y: touch.clientY } : null
+ },
+ onTouchCancel() {
+ start = null
+ }
+ }
+}
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx b/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
index 5de07aa71d7..74b055c1861 100644
--- a/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/user-edit-composer.tsx
@@ -1,5 +1,6 @@
import type { Unstable_TriggerAdapter, Unstable_TriggerItem } from '@assistant-ui/core'
import { ComposerPrimitive, useAui, useAuiState } from '@assistant-ui/react'
+import { useStore } from '@nanostores/react'
import {
type ClipboardEvent,
type FC,
@@ -9,6 +10,7 @@ import {
type DragEvent as ReactDragEvent,
useCallback,
useEffect,
+ useMemo,
useRef,
useState
} from 'react'
@@ -48,6 +50,7 @@ import {
RICH_INPUT_SLOT
} from '@/app/chat/composer/rich-editor'
import { detectTrigger, openDirectiveScope, textBeforeCaret, type TriggerState } from '@/app/chat/composer/text-utils'
+import { composerTextEntryProps, touchTapFocusHandlers } from '@/app/chat/composer/touch-text-entry'
import { ComposerTriggerPopover } from '@/app/chat/composer/trigger-popover'
import { isRedoShortcut, isUndoShortcut } from '@/app/chat/composer/undo-history'
import { chipTypedUrlOnSpace, linkifyUrls } from '@/app/chat/composer/url-refs'
@@ -76,7 +79,7 @@ import { sanitizeComposerInput } from '@/lib/composer-input-sanitize'
import { DATA_IMAGE_URL_RE } from '@/lib/embedded-images'
import { triggerHaptic } from '@/lib/haptics'
import { Loader2Icon } from '@/lib/icons'
-import { isMacPlatform } from '@/lib/platform'
+import { $touchPointer } from '@/lib/touch-interaction'
import { cn } from '@/lib/utils'
import type { ComposerAttachment } from '@/store/composer'
import { notify, notifyError, readableError } from '@/store/notifications'
@@ -100,6 +103,8 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
const textDirection = useForcedTextDirection()
const rootRef = useRef<HTMLDivElement | null>(null)
const editorRef = useRef<HTMLDivElement | null>(null)
+ const touchPointer = useStore($touchPointer)
+ const tapFocus = useMemo(() => (touchPointer ? touchTapFocusHandlers() : undefined), [touchPointer])
// Capture the original draft immediately before the first edit. The runtime
// may hydrate composer.text after this component's first render, so taking a
// mount-time snapshot can incorrectly classify every later blur as dirty.
@@ -900,9 +905,9 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
>
<div
aria-label={copy.editMessage}
- autoCapitalize="off"
- // Match the main composer: allow macOS replacements, not spellcheck.
- autoCorrect={isMacPlatform() ? 'on' : 'off'}
+ // The main composer's text entry: off on desktop, the platform's on touch.
+ {...composerTextEntryProps(touchPointer)}
+ {...tapFocus}
className={cn(
'ui-prompt-input-editor__input max-h-48 w-full resize-none overflow-y-auto bg-transparent p-0 pr-7 text-[length:var(--conversation-text-font-size)] text-foreground/95 outline-none',
'**:data-ref-text:cursor-default',
@@ -933,7 +938,6 @@ export const UserEditComposer: FC<UserEditComposerProps> = ({ cwd, gateway, sess
onPaste={handlePaste}
ref={editorRef}
role="textbox"
- spellCheck={false}
suppressContentEditableWarning
/>
<ComposerDirectiveActions editorRef={editorRef} />