Skip to content

The agent that grows with you

fix(desktop): clamp long sticky prompts on touch, with an expand toggle

BearHuddleston2cf594898610
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message-touch-clamp.test.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message-touch-clamp.test.tsx
new file mode 100644
index 00000000000..3bcf368f939
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/user-message-touch-clamp.test.tsx
@@ -0,0 +1,121 @@
+import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
+
+import { $touchPointer } from '@/lib/touch-interaction'
+
+import {
+ assistantMessage,
+ stubThreadEnvironment,
+ stubThreadViewportSize,
+ ThreadRuntime,
+ userMessage
+} from '../test-utils'
+
+import { Thread } from '.'
+
+const PROMPT = 'a long prompt with pasted image notes'
+
+/** ResizeObservers whose deliveries the test drives, since jsdom has no layout. */
+const observers = new Map<Element, Set<ResizeObserverCallback>>()
+
+class DrivenResizeObserver {
+ private readonly callback: ResizeObserverCallback
+
+ constructor(callback: ResizeObserverCallback) {
+ this.callback = callback
+ }
+
+ disconnect() {
+ for (const callbacks of observers.values()) {
+ callbacks.delete(this.callback)
+ }
+ }
+
+ observe(element: Element) {
+ observers.set(element, (observers.get(element) ?? new Set()).add(this.callback))
+ }
+
+ unobserve(element: Element) {
+ observers.get(element)?.delete(this.callback)
+ }
+}
+
+stubThreadEnvironment()
+vi.stubGlobal('ResizeObserver', DrivenResizeObserver)
+stubThreadViewportSize()
+
+/** Report a laid-out height for the innermost observed box holding the prompt. */
+function layOutPrompt(blockSize: number) {
+ const text = screen.getByText(PROMPT)
+ const holders = [...observers.keys()].filter(element => observers.get(element)?.size && element.contains(text))
+ const box = holders.find(element => !holders.some(other => other !== element && element.contains(other)))
+
+ expect(box).toBeTruthy()
+
+ const entry = { target: box, borderBoxSize: [{ blockSize, inlineSize: 600 }] } as unknown as ResizeObserverEntry
+
+ act(() => {
+ for (const callback of observers.get(box!) ?? []) {
+ callback([entry], {} as ResizeObserver)
+ }
+ })
+
+ return box!.parentElement!
+}
+
+function renderTouchThread() {
+ render(
+ <ThreadRuntime messages={[userMessage('user-1', PROMPT), assistantMessage()]}>
+ <Thread />
+ </ThreadRuntime>
+ )
+}
+
+beforeEach(() => {
+ window.document.documentElement.dataset.hermesDesktopHost = 'browser'
+ $touchPointer.set(true)
+})
+
+afterEach(() => {
+ cleanup()
+ $touchPointer.set(false)
+ delete window.document.documentElement.dataset.hermesDesktopHost
+})
+
+describe('touch user bubble clamp', () => {
+ // A tablet has no hover and no tap-to-edit, and the bubble is sticky: an
+ // unclamped long prompt pinned at the top of the transcript hid the reply.
+ it('clamps a long prompt and lets an explicit toggle hold it open', async () => {
+ renderTouchThread()
+ await screen.findByText(PROMPT)
+
+ const body = layOutPrompt(400)
+
+ expect(body.classList.contains('sticky-human-clamp')).toBe(true)
+ expect(body.dataset.clamped).toBe('true')
+
+ const toggle = screen.getByRole('button', { name: 'Expand message' })
+
+ expect(toggle.getAttribute('aria-expanded')).toBe('false')
+
+ fireEvent.click(toggle)
+
+ expect(toggle.getAttribute('aria-expanded')).toBe('true')
+ expect(body.classList.contains('sticky-human-clamp')).toBe(false)
+
+ fireEvent.click(toggle)
+
+ expect(toggle.getAttribute('aria-expanded')).toBe('false')
+ expect(body.classList.contains('sticky-human-clamp')).toBe(true)
+ })
+
+ it('offers no toggle for a prompt that fits the clamp', async () => {
+ renderTouchThread()
+ await screen.findByText(PROMPT)
+
+ const body = layOutPrompt(20)
+
+ expect(body.dataset.clamped).toBeUndefined()
+ expect(screen.queryByRole('button', { name: 'Expand message' })).toBeNull()
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
index 0feeb17411e..06807a3ca9d 100644
--- a/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
+++ b/apps/desktop/src/components/assistant-ui/thread/user-message.tsx
@@ -334,15 +334,28 @@ interface TouchUserBubbleProps {
children: ReactNode
className: string
editable: boolean
+ expandable: boolean
+ expanded: boolean
+ onToggleExpanded: () => void
showRestore: boolean
showStop: boolean
}
/** Touch bubble: the prose stays plain selectable text, so a long press keeps
* the platform's selection handles and Copy, and editing is an explicit button
- * below it instead of a tap on the bubble. The Edit row leaves room for the
- * Stop/Restore cluster pinned over its end. */
-function TouchUserBubble({ children, className, editable, showRestore, showStop }: TouchUserBubbleProps) {
+ * below it instead of a tap on the bubble. A clamped prompt gets an explicit
+ * expand toggle in the same row. The row leaves room for the Stop/Restore
+ * cluster pinned over its end. */
+function TouchUserBubble({
+ children,
+ className,
+ editable,
+ expandable,
+ expanded,
+ onToggleExpanded,
+ showRestore,
+ showStop
+}: TouchUserBubbleProps) {
const copy = useI18n().t.assistant.thread
return (
@@ -351,6 +364,21 @@ function TouchUserBubble({ children, className, editable, showRestore, showStop
{children}
</div>
<div className={cn('flex justify-end', (showStop || showRestore) && 'pr-11')}>
+ {expandable && (
+ <Button
+ aria-expanded={expanded}
+ aria-label={copy.expandMessage}
+ data-slot="aui_user-touch-expand"
+ onClick={() => {
+ triggerHaptic('selection')
+ onToggleExpanded()
+ }}
+ size="icon"
+ variant="ghost"
+ >
+ <Codicon name={expanded ? 'chevron-up' : 'chevron-down'} />
+ </Button>
+ )}
{editable && (
<ActionBarPrimitive.Edit asChild>
<Button
@@ -374,14 +402,15 @@ function TouchUserBubble({ children, className, editable, showRestore, showStop
* clamp lifts on hover / focus (see styles.css). We measure the *unclamped*
* inner wrapper so the ResizeObserver only fires on real content / width
* changes, not on every frame while the outer max-height animates open.
- * Touch has no hover to lift it, so a touch bubble is never clamped. */
+ * Touch and watch windows open no editor on tap, so there an explicit
+ * `expanded` toggle lifts the clamp instead. */
function useUserBubbleClamp(readOnly: boolean, touch: boolean) {
const clampInnerRef = useRef<HTMLDivElement | null>(null)
const [bodyClamped, setBodyClamped] = useState(false)
const lastClampHeightRef = useRef(-1)
const lineHeightRef = useRef(0)
const [expanded, setExpanded] = useState(false)
- const clampActive = !touch && !(readOnly && expanded)
+ const clampActive = !(expanded && (readOnly || touch))
const measureClamp = useCallback((entries: readonly ResizeObserverEntry[]) => {
const inner = clampInnerRef.current
@@ -590,6 +619,9 @@ export const UserMessage: FC<{
<TouchUserBubble
className={bubbleClassName}
editable={!readOnly}
+ expandable={bodyClamped}
+ expanded={expanded}
+ onToggleExpanded={() => setExpanded(value => !value)}
showRestore={showRestore}
showStop={showStop}
>