Skip to content

Commit 83960f8

Browse files
authored
fix(ui): dismiss tooltips when their trigger is pressed (#63834)
1 parent f91b88e commit 83960f8

13 files changed

Lines changed: 372 additions & 166 deletions

File tree

Lines changed: 127 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,169 @@
11
import { expect, story } from "../../stories/playwright/story"
22

3-
story("opens a neighbouring tooltip without the open delay", async ({ mount, page }) => {
3+
story.beforeEach(async ({ page }) => {
4+
await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") })
5+
await page.clock.pauseAt(new Date("2026-01-01T00:00:01Z"))
6+
})
7+
8+
story("cancels a pending hover on click and opens again on re-entry", async ({ mount, page }) => {
9+
const root = await mount("ui-tooltip--with-button")
10+
11+
const trigger = root.getByRole("button", { name: "Add" })
12+
13+
const tooltip = page.getByRole("tooltip", { name: "Add one to the count" })
14+
15+
await trigger.hover()
16+
await page.clock.runFor(300)
17+
await trigger.click()
18+
await expect(root.getByRole("status")).toHaveText("Count: 1")
19+
await expect(tooltip).toBeHidden()
20+
await page.clock.runFor(600)
21+
await expect(tooltip).toBeHidden()
22+
23+
await page.mouse.move(0, 0)
24+
await trigger.hover()
25+
await page.clock.runFor(600)
26+
await expect(tooltip).toBeVisible()
27+
})
28+
29+
story("dismisses an open tooltip on pointerdown before the button activates", async ({ mount, page }) => {
30+
const root = await mount("ui-tooltip--with-button")
31+
32+
const tooltip = page.getByRole("tooltip", { name: "Add one to the count" })
33+
34+
await root.getByRole("button", { name: "Add" }).hover()
35+
await page.clock.runFor(600)
36+
await expect(tooltip).toBeVisible()
37+
await page.mouse.down()
38+
await expect(tooltip).toBeHidden()
39+
await expect(root.getByRole("status")).toHaveText("Count: 0")
40+
await page.mouse.up()
41+
await expect(root.getByRole("status")).toHaveText("Count: 1")
42+
await page.clock.runFor(600)
43+
await expect(tooltip).toBeHidden()
44+
})
45+
46+
story("shows the wrapped button's tooltip on Tab and Shift+Tab, even after a click", async ({ mount, page }) => {
47+
const root = await mount("ui-tooltip--with-button")
48+
49+
const trigger = root.getByRole("button", { name: "Add" })
50+
51+
const tooltip = page.getByRole("tooltip", { name: "Add one to the count" })
52+
53+
await page.keyboard.press("Tab")
54+
await expect(trigger).toBeFocused()
55+
await expect(tooltip).toBeVisible()
56+
57+
await trigger.click()
58+
await expect(tooltip).toBeHidden()
59+
await page.keyboard.press("Tab")
60+
await expect(root.getByRole("button", { name: "Reset" })).toBeFocused()
61+
await page.keyboard.press("Shift+Tab")
62+
await expect(trigger).toBeFocused()
63+
await expect(tooltip).toBeVisible()
64+
await page.keyboard.press("Escape")
65+
await expect(tooltip).toBeHidden()
66+
await expect(trigger).toBeFocused()
67+
})
68+
69+
story("restored focus does not open a tooltip after Tab leaves the window", async ({ mount, page }) => {
70+
const root = await mount("ui-tooltip--with-button")
71+
72+
await page.keyboard.down("Tab")
73+
await expect(root.getByRole("button", { name: "Add" })).toBeFocused()
74+
// The page loses focus before receiving Tab's keyup.
75+
await page.evaluate(() => window.dispatchEvent(new FocusEvent("blur")))
76+
await root.getByRole("button", { name: "Reset" }).focus()
77+
await expect(page.getByRole("tooltip", { name: "Reset the count" })).toBeHidden()
78+
await page.keyboard.up("Tab")
79+
})
80+
81+
story.describe("touch", () => {
82+
story.use({ hasTouch: true, isMobile: true, viewport: { width: 390, height: 844 } })
83+
84+
story("tapping the button preserves the tooltip's hover state", async ({ mount, page }) => {
85+
const root = await mount("ui-tooltip--with-button")
86+
87+
const trigger = root.getByRole("button", { name: "Add" })
88+
89+
const tooltip = page.getByRole("tooltip", { name: "Add one to the count" })
90+
91+
expect(await page.evaluate(() => matchMedia("(hover: none)").matches)).toBe(true)
92+
await trigger.tap()
93+
await expect(root.getByRole("status")).toHaveText("Count: 1")
94+
await expect(tooltip).toBeHidden()
95+
await page.clock.runFor(600)
96+
await expect(tooltip).toBeHidden()
97+
98+
await trigger.hover()
99+
await page.clock.runFor(600)
100+
await expect(tooltip).toBeVisible()
101+
await trigger.tap()
102+
await expect(root.getByRole("status")).toHaveText("Count: 2")
103+
await expect(tooltip).toBeVisible()
104+
})
105+
})
106+
107+
story("waits 600 ms on first hover and opens a neighbouring tooltip immediately", async ({ mount, page }) => {
4108
const root = await mount("ui-tooltip--appearances")
109+
110+
const tooltip = page.getByRole("tooltip", { name: "Standard tooltip" })
111+
5112
await root.getByText("Standard", { exact: true }).hover()
6-
await expect(page.getByRole("tooltip", { name: "Standard tooltip" })).toBeVisible()
113+
await page.clock.runFor(599)
114+
await expect(tooltip).toBeHidden()
115+
await page.clock.runFor(1)
116+
await expect(tooltip).toBeVisible()
7117
// With timers frozen, the next tooltip can only open through the skip-delay window that closing this one starts.
8-
await page.clock.install({ time: new Date("2026-01-01T00:00:00Z") })
9-
await page.clock.pauseAt(new Date("2026-01-01T00:00:01Z"))
10118
await root.getByText("Compact", { exact: true }).hover()
11119
await expect(page.getByRole("tooltip", { name: "Compact tooltip" })).toBeVisible()
12120
})
13121

14122
story("keeps a menu trigger's tooltip hidden when the menu closes under a still pointer", async ({ mount, page }) => {
15123
const root = await mount("ui-tooltip--with-menu")
16-
await page.clock.install()
124+
17125
const trigger = root.getByRole("button", { name: "Actions" })
126+
18127
const tooltip = page.getByRole("tooltip", { name: "More actions" })
128+
19129
await trigger.hover()
130+
await page.clock.runFor(600)
20131
await expect(tooltip).toBeVisible()
21132
await trigger.click()
22133
await expect(page.getByRole("menu")).toBeVisible()
23134
await expect(tooltip).toBeHidden()
24135
await page.keyboard.press("Escape")
25136
await expect(page.getByRole("menu")).toBeHidden()
137+
// The menu restores focus in a zero-delay task after unmounting.
138+
await page.clock.runFor(0)
26139
await expect(trigger).toBeFocused()
27-
// The menu's closing layer hands the still pointer a fresh pointerenter, which would open the tooltip 400 ms later.
28-
await page.clock.runFor(1000)
140+
// The menu's closing layer can hand the still pointer a fresh pointerenter.
141+
await page.clock.runFor(600)
29142
await expect(tooltip).toBeHidden()
30143
await page.mouse.move(0, 0)
31144
await trigger.hover()
145+
await page.clock.runFor(600)
32146
await expect(tooltip).toBeVisible()
33147
})
34148

35149
story("opens a menu trigger's tooltip on the next hover after the menu closes", async ({ mount, page }) => {
36150
const root = await mount("ui-tooltip--with-menu")
151+
37152
const trigger = root.getByRole("button", { name: "Actions" })
153+
38154
const tooltip = page.getByRole("tooltip", { name: "More actions" })
155+
39156
await trigger.hover()
157+
await page.clock.runFor(600)
40158
await expect(tooltip).toBeVisible()
41159
await trigger.click()
42160
await expect(page.getByRole("menu")).toBeVisible()
43161
await page.mouse.move(0, 0)
44162
await page.keyboard.press("Escape")
45163
await expect(page.getByRole("menu")).toBeHidden()
164+
await page.clock.runFor(0)
46165
await expect(trigger).toBeFocused()
47166
await trigger.hover()
167+
await page.clock.runFor(600)
48168
await expect(tooltip).toBeVisible()
49169
})

‎packages/app/e2e/regression/btw-sidebar.spec.ts‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ test("keeps many long-titled side tabs usable at a narrow desktop width", async
163163
})
164164

165165
test("answers /btw in the side panel without admitting a prompt", async ({ page }) => {
166+
await page.clock.install()
166167
await page.context().grantPermissions(["clipboard-read", "clipboard-write"])
167168
const generations: { sessionID: string; prompt: string }[] = []
168169
const prompts: unknown[] = []
@@ -228,12 +229,27 @@ test("answers /btw in the side panel without admitting a prompt", async ({ page
228229
await expect(panel.getByRole("button", { name: "Revert to here", exact: true })).toHaveCount(0)
229230
await expect(panel.getByRole("button", { name: "Copy answer", exact: true })).toHaveCount(1)
230231
await expect(panel.locator('[data-slot="text-part-meta"]')).toContainText(/\d+s/)
231-
await panel.locator('[data-component="user-message"]').hover()
232-
await panel.getByRole("button", { name: "Copy message", exact: true }).click()
233-
await expect(panel.getByRole("button", { name: "Copied", exact: true })).toHaveCount(1)
234-
await panel.locator('[data-component="text-part"]').hover()
235-
await panel.getByRole("button", { name: "Copy answer", exact: true }).click()
236-
await expect(panel.getByRole("button", { name: "Copied", exact: true })).toHaveCount(2)
232+
const feedback = page.getByRole("tooltip", { name: "Copied", exact: true })
233+
234+
for (const [component, label] of [
235+
["user-message", "Copy message"],
236+
["text-part", "Copy answer"],
237+
]) {
238+
const row = panel.locator(`[data-component="${component}"]`)
239+
240+
const button = row.getByRole("button", { name: label, exact: true })
241+
242+
await row.hover()
243+
await button.click()
244+
await expect(feedback).toBeVisible()
245+
await page.clock.fastForward(1_000)
246+
await expect(feedback).toHaveCSS("opacity", "1")
247+
await expect(row.getByRole("button", { name: "Copied", exact: true })).toBeVisible()
248+
await page.clock.fastForward(1_000)
249+
await expect(button).toBeVisible()
250+
await expect(page.getByRole("tooltip")).toBeHidden()
251+
}
252+
237253
expect(generations).toHaveLength(1)
238254
expect(generations[0]?.sessionID).toBe(main.id)
239255
expect(generations[0]?.prompt).toContain("how does the retry loop work?")

‎packages/gui-plugins/src/btw/panel.tsx‎

Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { createMemo, Match, on, Show, Switch } from "solid-js"
2-
import { createStore } from "solid-js/store"
1+
import { createMemo, Match, Show, Switch } from "solid-js"
32
import { Button } from "@opencode/ui/button"
43
import { useI18n } from "@opencode/ui/context/i18n"
54
import { Icon } from "@opencode/ui/icon"
65
import { IconButton } from "@opencode/ui/icon-button"
76
import { TextShimmer } from "@opencode/ui/text-shimmer"
87
import { Tooltip } from "@opencode/ui/tooltip"
98
import { showToast } from "@opencode/ui/toast"
9+
import { createCopyFeedback } from "@opencode/session-ui/copy"
1010
import { Markdown } from "@opencode/session-ui/markdown"
1111
import { usePlugin, type MountedSession } from "@opencode/plugin/gui"
1212
import type { BtwFailure } from "./machine"
@@ -15,7 +15,6 @@ import type { BtwModel } from "./model"
1515
export default function SessionBtwPanel(props: { btw: BtwModel; session: MountedSession; id: string }) {
1616
const ctx = usePlugin()
1717
const i18n = useI18n()
18-
const system = ctx.system
1918
const view = createMemo(() => props.btw.view(props.session, props.id))
2019

2120
const chat = createMemo(() => {
@@ -39,7 +38,6 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
3938
})
4039

4140
const question = () => chat()?.question
42-
const answer = () => answered()?.answer
4341

4442
const failureDetail = (value: BtwFailure) => {
4543
if (value.reason === "empty") return ctx.t("error.empty")
@@ -51,12 +49,6 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
5149
return undefined
5250
}
5351

54-
// New primitive tokens clear the copied mark when text changes without being proxied by the store.
55-
const shownQuestion = createMemo(on(question, () => Symbol()))
56-
const shownAnswer = createMemo(on(answer, () => Symbol()))
57-
const [copied, setCopied] = createStore<{ question?: symbol; answer?: symbol }>({})
58-
const isQuestionCopied = () => copied.question === shownQuestion()
59-
const isAnswerCopied = () => copied.answer === shownAnswer()
6052
const timefmt = createMemo(() => new Intl.DateTimeFormat(i18n.locale(), { timeStyle: "short" }))
6153
const numfmt = createMemo(() => new Intl.NumberFormat(i18n.locale()))
6254

@@ -111,26 +103,6 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
111103

112104
const answerMeta = createMemo(() => [agentLabel(), modelLabel(), durationLabel()].filter(Boolean).join(" \u00B7 "))
113105

114-
const copyQuestion = () => {
115-
const value = question()
116-
117-
if (!value) return
118-
void system.copy(value).then(
119-
() => setCopied("question", shownQuestion()),
120-
() => showToast({ title: ctx.t("common.requestFailed") }),
121-
)
122-
}
123-
124-
const copy = () => {
125-
const value = answer()
126-
127-
if (!value) return
128-
void system.copy(value).then(
129-
() => setCopied("answer", shownAnswer()),
130-
() => showToast({ title: ctx.t("common.requestFailed") }),
131-
)
132-
}
133-
134106
return (
135107
<div class="flex h-full min-h-0 flex-col bg-v2-background-bg-base" data-slot="session-btw-panel">
136108
<div class="min-h-0 flex-1 overflow-y-auto" data-scrollable>
@@ -160,21 +132,11 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
160132
</span>
161133
</Show>
162134
</span>
163-
<Tooltip
164-
appearance="compact"
165-
value={isQuestionCopied() ? i18n.t("ui.message.copied") : i18n.t("ui.message.copyMessage")}
166-
placement="top"
167-
gutter={4}
168-
>
169-
<IconButton
170-
size="normal"
171-
variant="ghost-muted"
172-
icon={<Icon name={isQuestionCopied() ? "check" : "outline-copy"} size="small" />}
173-
aria-label={isQuestionCopied() ? i18n.t("ui.message.copied") : i18n.t("ui.message.copyMessage")}
174-
onMouseDown={(event) => event.preventDefault()}
175-
onClick={copyQuestion}
176-
/>
177-
</Tooltip>
135+
<CopyButton
136+
text={question()}
137+
label={i18n.t("ui.message.copyMessage")}
138+
copiedLabel={i18n.t("ui.message.copied")}
139+
/>
178140
</div>
179141
</Show>
180142
</div>
@@ -211,21 +173,7 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
211173
<Markdown text={reply().answer} class="text-14-regular" />
212174
</div>
213175
<div data-slot="text-part-copy-wrapper">
214-
<Tooltip
215-
appearance="compact"
216-
value={isAnswerCopied() ? ctx.t("common.copied") : ctx.t("copy")}
217-
placement="top"
218-
gutter={4}
219-
>
220-
<IconButton
221-
size="normal"
222-
variant="ghost-muted"
223-
icon={<Icon name={isAnswerCopied() ? "check" : "outline-copy"} size="small" />}
224-
aria-label={isAnswerCopied() ? ctx.t("common.copied") : ctx.t("copy")}
225-
onMouseDown={(event) => event.preventDefault()}
226-
onClick={copy}
227-
/>
228-
</Tooltip>
176+
<CopyButton text={reply().answer} label={ctx.t("copy")} copiedLabel={ctx.t("common.copied")} />
229177
<Show when={answerMeta()}>
230178
<span data-slot="text-part-meta" class="text-12-regular text-text-weak cursor-default">
231179
{answerMeta()}
@@ -241,3 +189,41 @@ export default function SessionBtwPanel(props: { btw: BtwModel; session: Mounted
241189
</div>
242190
)
243191
}
192+
193+
function CopyButton(props: { text?: string; label: string; copiedLabel: string }) {
194+
const ctx = usePlugin()
195+
196+
const system = ctx.system
197+
198+
return (
199+
<Show when={props.text} keyed>
200+
{(text) => {
201+
const copy = createCopyFeedback({
202+
write: system.copy,
203+
onError: () => showToast({ title: ctx.t("common.requestFailed") }),
204+
})
205+
206+
const label = () => (copy.copied() ? props.copiedLabel : props.label)
207+
208+
return (
209+
<Tooltip
210+
appearance="compact"
211+
value={label()}
212+
placement="top"
213+
gutter={4}
214+
forceOpen={copy.copied() || undefined}
215+
>
216+
<IconButton
217+
size="normal"
218+
variant="ghost-muted"
219+
icon={<Icon name={copy.copied() ? "check" : "outline-copy"} size="small" />}
220+
aria-label={label()}
221+
onMouseDown={(event) => event.preventDefault()}
222+
onClick={() => copy.copy(text)}
223+
/>
224+
</Tooltip>
225+
)
226+
}}
227+
</Show>
228+
)
229+
}

‎packages/session-ui/component-tests/session-shell.spec.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ story("keeps Copied for two seconds after the latest click", async ({ mount, pag
8686
// The first click's reset falls due here, while the second click's feedback still runs.
8787
await page.clock.runFor(900)
8888
await expect(result.getByRole("button", { name: "Copied" })).toBeVisible()
89+
await expect(page.getByRole("tooltip", { name: "Copied" })).toBeVisible()
8990
await page.clock.runFor(1_200)
9091
await expect(result.getByRole("button", { name: "Copy output", exact: true })).toBeVisible()
92+
await expect(page.getByRole("tooltip")).toBeHidden()
9193
})

0 commit comments

Comments
 (0)