Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(insights): keep click and drag selection in scene title fields (#93943)

Paul D'Ambra2601c7e8535a
diff --git a/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx b/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx
index 882e497bfd67..e0567571ff00 100644
--- a/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx
+++ b/frontend/src/layout/scenes/components/SceneTitleSection.test.tsx
@@ -1,6 +1,6 @@
import '@testing-library/jest-dom'
-import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+import { cleanup, createEvent, fireEvent, render, screen } from '@testing-library/react'
import { SceneName } from './SceneTitleSection'
@@ -37,4 +37,32 @@ describe('SceneName', () => {
rerender(<SceneName name="Generated name" canEdit onChange={jest.fn()} />)
expect(screen.getByText('Generated name')).toBeInTheDocument()
})
+
+ // Guards click and drag text selection: the edit row is wider and taller than the field
+ // inside it, so a press that misses the glyphs by a few pixels lands on the row. Left
+ // unclaimed, some browsers read that press as their own gesture rather than a selection.
+ test('a press on the edit row beside the field focuses the field and is consumed', () => {
+ render(<SceneName name="Paying users" canEdit forceEdit onChange={jest.fn()} />)
+
+ const row = screen.getByTestId('scene-name-edit-row')
+ const textarea = screen.getByRole('textbox')
+ expect(textarea).not.toHaveFocus()
+
+ const press = createEvent.mouseDown(row)
+ fireEvent(row, press)
+
+ expect(textarea).toHaveFocus()
+ expect(press.defaultPrevented).toBe(true)
+ })
+
+ // The counterpart: a press on the field itself must reach the browser untouched,
+ // or it would never start a selection.
+ test('a press on the field itself is left alone', () => {
+ render(<SceneName name="Paying users" canEdit forceEdit onChange={jest.fn()} />)
+
+ const press = createEvent.mouseDown(screen.getByRole('textbox'))
+ fireEvent(screen.getByRole('textbox'), press)
+
+ expect(press.defaultPrevented).toBe(false)
+ })
})
diff --git a/frontend/src/layout/scenes/components/SceneTitleSection.tsx b/frontend/src/layout/scenes/components/SceneTitleSection.tsx
index fca45feca899..26cb54ff583a 100644
--- a/frontend/src/layout/scenes/components/SceneTitleSection.tsx
+++ b/frontend/src/layout/scenes/components/SceneTitleSection.tsx
@@ -469,6 +469,7 @@ export function SceneName({
const [isEditing, setIsEditing] = useState(forceEdit)
const containerRef = useRef<HTMLDivElement>(null)
+ const nameInputRef = useRef<HTMLTextAreaElement>(null)
const textClasses =
'text-lg font-semibold my-0 pl-[var(--button-padding-x-sm)] min-h-[var(--button-height-sm)] leading-[1.4] select-auto'
@@ -519,8 +520,21 @@ export function SceneName({
onChange && canEdit ? (
<>
{isEditing ? (
- <div ref={containerRef} className="flex items-center gap-1 w-full">
+ <div
+ ref={containerRef}
+ className="flex items-center gap-1 w-full"
+ data-attr="scene-name-edit-row"
+ onMouseDown={(e) => {
+ // A press on the row around the field leaves the gesture unclaimed by the page,
+ // which some browsers read as a window drag instead of a text selection.
+ if (e.target === e.currentTarget) {
+ e.preventDefault()
+ nameInputRef.current?.focus()
+ }
+ }}
+ >
<TextareaPrimitive
+ ref={nameInputRef}
variant="default"
name="name"
value={name || ''}
diff --git a/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.test.tsx b/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.test.tsx
new file mode 100644
index 000000000000..6bac7e135b4d
--- /dev/null
+++ b/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.test.tsx
@@ -0,0 +1,50 @@
+import '@testing-library/jest-dom'
+
+import { cleanup, fireEvent, render, screen } from '@testing-library/react'
+
+import { TextareaPrimitive } from './TextareaPrimitive'
+
+describe('TextareaPrimitive', () => {
+ afterEach(() => {
+ cleanup()
+ })
+
+ // Guards click and drag text selection: moving the caret to the end on a focus that
+ // came from a pointer press collapses the selection the press started, so a drag
+ // selects nothing and the gesture is left for the browser to interpret.
+ it('keeps the caret where a pointer press put it', () => {
+ render(<TextareaPrimitive defaultValue="a title to select" />)
+ const textarea = screen.getByRole('textbox') as HTMLTextAreaElement
+
+ fireEvent.pointerDown(textarea)
+ fireEvent.focus(textarea)
+
+ expect(textarea.selectionStart).toBe(0)
+ })
+
+ it('moves the caret to the end when focus arrives without a pointer press', () => {
+ render(<TextareaPrimitive defaultValue="a title to select" />)
+ const textarea = screen.getByRole('textbox') as HTMLTextAreaElement
+
+ fireEvent.focus(textarea)
+
+ expect(textarea.selectionStart).toBe('a title to select'.length)
+ })
+ // The caret rule is layered on top of the caller's own handlers rather than replacing
+ // them: SceneName saves its pending rename on blur, and that must still run.
+ it('still calls the handlers a caller supplies', () => {
+ const onFocus = jest.fn()
+ const onBlur = jest.fn()
+ const onPointerDown = jest.fn()
+ render(<TextareaPrimitive onFocus={onFocus} onBlur={onBlur} onPointerDown={onPointerDown} />)
+ const textarea = screen.getByRole('textbox')
+
+ fireEvent.pointerDown(textarea)
+ fireEvent.focus(textarea)
+ fireEvent.blur(textarea)
+
+ expect(onPointerDown).toHaveBeenCalledTimes(1)
+ expect(onFocus).toHaveBeenCalledTimes(1)
+ expect(onBlur).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.tsx b/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.tsx
index 96d8feea6543..77fa23fcb4a1 100644
--- a/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.tsx
+++ b/frontend/src/lib/ui/TextareaPrimitive/TextareaPrimitive.tsx
@@ -1,4 +1,4 @@
-import { forwardRef } from 'react'
+import { forwardRef, useRef } from 'react'
import TextareaAutosize, { TextareaAutosizeProps } from 'react-textarea-autosize'
import { IconMarkdownFilled } from '@posthog/icons'
@@ -17,18 +17,37 @@ type TextareaPrimitiveProps = TextareaAutosizeProps &
export const TextareaPrimitive = forwardRef<HTMLTextAreaElement, TextareaPrimitiveProps>(
({ className, variant, error, markdown = false, wrapperClassName, ...rest }, ref): JSX.Element => {
- // Ensure cursor is at the end of the textarea when it is focused
+ const focusFollowsPointerRef = useRef(false)
+
+ // Put the cursor at the end when focus arrives without a pointer press, e.g. autofocus or tab.
+ // After a pointer press the caret must stay where it landed, so that click and drag selects text.
function onFocus(e: React.FocusEvent<HTMLTextAreaElement>): void {
- e.currentTarget.setSelectionRange(e.currentTarget.value.length, e.currentTarget.value.length)
+ if (!focusFollowsPointerRef.current) {
+ e.currentTarget.setSelectionRange(e.currentTarget.value.length, e.currentTarget.value.length)
+ }
+ focusFollowsPointerRef.current = false
+ rest.onFocus?.(e)
+ }
+
+ function onPointerDown(e: React.PointerEvent<HTMLTextAreaElement>): void {
+ focusFollowsPointerRef.current = true
+ rest.onPointerDown?.(e)
+ }
+
+ function onBlur(e: React.FocusEvent<HTMLTextAreaElement>): void {
+ focusFollowsPointerRef.current = false
+ rest.onBlur?.(e)
}
return (
<div className={cn('relative flex flex-col gap-0', wrapperClassName)}>
<TextareaAutosize
ref={ref}
- onFocus={onFocus}
aria-label={markdown ? 'Markdown supported' : undefined}
{...rest}
+ onFocus={onFocus}
+ onPointerDown={onPointerDown}
+ onBlur={onBlur}
className={cn(
textInputVariants({ variant, error: !!error, size: 'auto' }),
'resize-y show-scrollbar-on-hover px-[var(--button-padding-x-base)] py-[var(--button-padding-y-base)]',
diff --git a/frontend/src/styles/base.scss b/frontend/src/styles/base.scss
index 09e75a950ff3..8f70887c9da4 100644
--- a/frontend/src/styles/base.scss
+++ b/frontend/src/styles/base.scss
@@ -2157,3 +2157,11 @@
transform: translateY(0);
}
}
+
+// `-webkit-app-region` is an inherited property in Chromium, and a region marked as a drag
+// handle swallows click and drag text selection. Text entry must never become a drag handle.
+input,
+textarea,
+[contenteditable='true'] {
+ -webkit-app-region: no-drag;
+}