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(desktop): keep composer selection on release outside input (#104162)

Michael Reichenbachdb9ba2fb6ec0
diff --git a/products/desktop/docs/TESTING.md b/products/desktop/docs/TESTING.md
index 0279d5f8c262..44028c01681b 100644
--- a/products/desktop/docs/TESTING.md
+++ b/products/desktop/docs/TESTING.md
@@ -57,6 +57,13 @@ Run the focus and submission checks with:
pnpm --filter @posthog/ui test src/features/sessions/components/CommentComposer.integration.test.tsx
```
+## Composer text selection
+
+In the new-session and session composers, select text and release the mouse outside the editor.
+The selection must remain visible, including when you release over the surrounding padding.
+A plain click on that padding must still focus the editor.
+Buttons and menus must keep their own actions.
+
## Profile pictures
In Settings > Account, check a Gravatar with a transparent background. The picture must not show initials behind it.
diff --git a/products/desktop/packages/ui/src/features/message-editor/components/PromptInput.tsx b/products/desktop/packages/ui/src/features/message-editor/components/PromptInput.tsx
index dc636e74b031..d252ccb94afd 100644
--- a/products/desktop/packages/ui/src/features/message-editor/components/PromptInput.tsx
+++ b/products/desktop/packages/ui/src/features/message-editor/components/PromptInput.tsx
@@ -12,6 +12,7 @@ import { SHORTCUTS } from "@posthog/ui/features/command/keyboard-shortcuts";
import type { PromptRecallHandler } from "@posthog/ui/features/sessions/components/chat-thread/composerPromptRecall";
import { cycleModeOption } from "@posthog/ui/features/sessions/sessionStore";
import { useSettingsStore } from "@posthog/ui/features/settings/settingsStore";
+import { shouldFocusOnBackgroundClick } from "@posthog/ui/utils/backgroundClick";
import { hasOpenOverlay } from "@posthog/ui/utils/overlay";
import { Flex, Text, Tooltip } from "@radix-ui/themes";
import { EditorContent } from "@tiptap/react";
@@ -36,6 +37,8 @@ import { SlotMachineSubmit } from "./SlotMachineSubmit";
export type { EditorHandle };
+const COMPOSER_SELF_HANDLED_SELECTOR = 'button, [role="menu"], .ProseMirror';
+
// How long the send button holds its own busy state when the surface never
// reports one — long enough to register as a press, short enough that a send
// the surface silently refuses doesn't strand the spinner.
@@ -374,11 +377,11 @@ export const PromptInput = forwardRef<EditorHandle, PromptInputProps>(
const handleContainerClick = useCallback(
(e: React.MouseEvent) => {
- const target = e.target as HTMLElement;
if (
- !target.closest("button") &&
- !target.closest('[role="menu"]') &&
- !target.closest(".ProseMirror")
+ shouldFocusOnBackgroundClick(
+ e.target as HTMLElement,
+ COMPOSER_SELF_HANDLED_SELECTOR,
+ )
) {
focus();
}
diff --git a/products/desktop/packages/ui/src/features/task-detail/components/TaskInput.tsx b/products/desktop/packages/ui/src/features/task-detail/components/TaskInput.tsx
index a3873f662b1e..fe2ee15dfb61 100644
--- a/products/desktop/packages/ui/src/features/task-detail/components/TaskInput.tsx
+++ b/products/desktop/packages/ui/src/features/task-detail/components/TaskInput.tsx
@@ -67,6 +67,7 @@ import { DotPatternBackground } from "../../../primitives/DotPatternBackground";
import { toast } from "../../../primitives/toast";
import { useActiveRepoStore } from "../../../shell/activeRepoStore";
import { pendingTaskPromptStoreApi } from "../../../shell/pendingTaskPromptStore";
+import { shouldFocusOnBackgroundClick } from "../../../utils/backgroundClick";
import { FOCUSABLE_SELECTOR } from "../../../utils/overlay";
import { useAuthStateValue } from "../../auth/store";
import { AutoresearchComposerControls } from "../../autoresearch/AutoresearchComposerControls";
@@ -1393,7 +1394,11 @@ export function TaskInput({
const handleContainerClick = useCallback((e: React.MouseEvent) => {
if (!e.currentTarget.contains(e.target as Node)) return;
- if ((e.target as HTMLElement).closest(FOCUSABLE_SELECTOR)) return;
+ if (
+ !shouldFocusOnBackgroundClick(e.target as HTMLElement, FOCUSABLE_SELECTOR)
+ ) {
+ return;
+ }
editorRef.current?.focus();
}, []);
diff --git a/products/desktop/packages/ui/src/utils/backgroundClick.test.ts b/products/desktop/packages/ui/src/utils/backgroundClick.test.ts
new file mode 100644
index 000000000000..1451862faf92
--- /dev/null
+++ b/products/desktop/packages/ui/src/utils/backgroundClick.test.ts
@@ -0,0 +1,51 @@
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { shouldFocusOnBackgroundClick } from "./backgroundClick";
+
+const IGNORE_SELECTOR = 'button, [contenteditable="true"]';
+
+function mockSelection(isCollapsed: boolean | null): void {
+ vi.spyOn(window, "getSelection").mockReturnValue(
+ isCollapsed === null ? null : ({ isCollapsed } as Selection),
+ );
+}
+
+describe("shouldFocusOnBackgroundClick", () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ it.each([
+ {
+ gesture: "a plain click on background chrome",
+ tagName: "div",
+ isCollapsed: true,
+ expected: true,
+ },
+ {
+ gesture: "releasing a drag-select outside the input",
+ tagName: "div",
+ isCollapsed: false,
+ expected: false,
+ },
+ {
+ gesture: "a click on an element that answers clicks itself",
+ tagName: "button",
+ isCollapsed: true,
+ expected: false,
+ },
+ {
+ gesture: "a click with no selection object at all",
+ tagName: "div",
+ isCollapsed: null,
+ expected: true,
+ },
+ ])("returns $expected for $gesture", ({ tagName, isCollapsed, expected }) => {
+ const target = document.createElement(tagName);
+ document.body.replaceChildren(target);
+ mockSelection(isCollapsed);
+
+ expect(shouldFocusOnBackgroundClick(target, IGNORE_SELECTOR)).toBe(
+ expected,
+ );
+ });
+});
diff --git a/products/desktop/packages/ui/src/utils/backgroundClick.ts b/products/desktop/packages/ui/src/utils/backgroundClick.ts
new file mode 100644
index 000000000000..29ec14dd0ad1
--- /dev/null
+++ b/products/desktop/packages/ui/src/utils/backgroundClick.ts
@@ -0,0 +1,8 @@
+// A drag selection can end on container chrome, so preserve any live selection.
+export function shouldFocusOnBackgroundClick(
+ target: HTMLElement,
+ ignoreSelector: string,
+): boolean {
+ if (target.closest(ignoreSelector)) return false;
+ return window.getSelection()?.isCollapsed !== false;
+}