@marimo-team/frontend 0.23.17-dev57 → 0.23.17-dev58

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,7 +23,11 @@ import type { StorageNamespace } from "@/core/storage/types";
23
23
  import { variablesAtom } from "@/core/variables/state";
24
24
  import type { Variables } from "@/core/variables/types";
25
25
  import { openLensTarget } from "../actions";
26
- import { CODE_LENS_HOVER_DELAY_MS, codeLensBundle } from "../extension";
26
+ import {
27
+ CODE_LENS_HOVER_DELAY_MS,
28
+ CODE_LENS_HOVER_GRACE_MS,
29
+ codeLensBundle,
30
+ } from "../extension";
27
31
  import { mountLensPopover } from "../popover";
28
32
 
29
33
  vi.mock("@/core/config/feature-flag", () => ({
@@ -236,9 +240,85 @@ describe("codeLensBundle", () => {
236
240
  );
237
241
 
238
242
  lens.dispatchEvent(new MouseEvent("mouseleave"));
243
+ // Not hidden immediately: the pointer may be on its way into the popover
244
+ expect(v.dom.querySelector(".mo-cm-tooltip")).not.toBeNull();
245
+ vi.advanceTimersByTime(CODE_LENS_HOVER_GRACE_MS);
246
+ expect(v.dom.querySelector(".mo-cm-tooltip")).toBeNull();
247
+ });
248
+
249
+ it("stays open while the pointer is over the popover", async () => {
250
+ seedStore({ tables: [DF_TABLE] });
251
+ const v = await mount("df = load()");
252
+ const lens = lenses(v)[0];
253
+
254
+ lens.dispatchEvent(new MouseEvent("mouseenter"));
255
+ vi.advanceTimersByTime(CODE_LENS_HOVER_DELAY_MS);
256
+ const popover = v.dom.querySelector<HTMLElement>(".mo-cm-tooltip");
257
+ expect(popover).not.toBeNull();
258
+
259
+ // Icon -> popover within the grace period
260
+ lens.dispatchEvent(new MouseEvent("mouseleave"));
261
+ vi.advanceTimersByTime(CODE_LENS_HOVER_GRACE_MS / 2);
262
+ popover?.dispatchEvent(new MouseEvent("mouseenter"));
263
+ vi.advanceTimersByTime(CODE_LENS_HOVER_GRACE_MS * 5);
264
+ expect(v.dom.querySelector(".mo-cm-tooltip")).toBe(popover);
265
+
266
+ // Popover -> icon: no re-show; the popover is kept as-is
267
+ popover?.dispatchEvent(new MouseEvent("mouseleave"));
268
+ lens.dispatchEvent(new MouseEvent("mouseenter"));
269
+ vi.advanceTimersByTime(CODE_LENS_HOVER_DELAY_MS * 2);
270
+ expect(v.dom.querySelector(".mo-cm-tooltip")).toBe(popover);
271
+ expect(mountLensPopover).toHaveBeenCalledTimes(1);
272
+
273
+ // Leaving the popover for good hides it after the grace period
274
+ lens.dispatchEvent(new MouseEvent("mouseleave"));
275
+ popover?.dispatchEvent(new MouseEvent("mouseenter"));
276
+ popover?.dispatchEvent(new MouseEvent("mouseleave"));
277
+ vi.advanceTimersByTime(CODE_LENS_HOVER_GRACE_MS);
278
+ expect(v.dom.querySelector(".mo-cm-tooltip")).toBeNull();
279
+ });
280
+
281
+ it("keeps the popover open across a lens rebuild for the same lens", async () => {
282
+ seedStore({ tables: [DF_TABLE] });
283
+ const v = await mount("df = load()");
284
+ const lens = lenses(v)[0];
285
+
286
+ lens.dispatchEvent(new MouseEvent("mouseenter"));
287
+ vi.advanceTimersByTime(CODE_LENS_HOVER_DELAY_MS);
288
+ const popover = v.dom.querySelector(".mo-cm-tooltip");
289
+ expect(popover).not.toBeNull();
290
+
291
+ // A store update triggers a (debounced) recompute of the same lenses
292
+ seedStore({ tables: [DF_TABLE] });
293
+ vi.advanceTimersByTime(300);
294
+ await Promise.resolve();
295
+ v.dispatch({});
296
+ expect(v.dom.querySelector(".mo-cm-tooltip")).toBe(popover);
297
+
298
+ // ...but a rebuild that drops the hovered lens closes it
299
+ seedStore({ tables: [] });
300
+ vi.advanceTimersByTime(300);
301
+ await Promise.resolve();
302
+ v.dispatch({});
303
+ expect(lenses(v)).toHaveLength(0);
239
304
  expect(v.dom.querySelector(".mo-cm-tooltip")).toBeNull();
240
305
  });
241
306
 
307
+ it("dismisses the editor's own hover tooltips when the icon is entered", async () => {
308
+ seedStore({ tables: [DF_TABLE] });
309
+ const v = await mount("df = load()");
310
+ const lens = lenses(v)[0];
311
+ // CodeMirror's `hoverTooltip` listens for `mouseleave` on `view.dom` to
312
+ // cancel a pending hover and close an open one
313
+ const onLeave = vi.fn();
314
+ v.dom.addEventListener("mouseleave", onLeave);
315
+
316
+ lens.dispatchEvent(new MouseEvent("mouseenter"));
317
+
318
+ expect(onLeave).toHaveBeenCalledTimes(1);
319
+ expect(onLeave.mock.calls[0][0].relatedTarget).toBe(lens);
320
+ });
321
+
242
322
  it("does not show a popover before the hover delay", async () => {
243
323
  seedStore({ tables: [DF_TABLE] });
244
324
  const v = await mount("df = load()");
@@ -29,12 +29,108 @@ import { mountLensPopover } from "./popover";
29
29
  // Delay (in ms) before showing the hover popover, matching the app's
30
30
  // tooltip delay
31
31
  export const CODE_LENS_HOVER_DELAY_MS = 400;
32
+ // Grace period (in ms) after the pointer leaves the icon (or the popover)
33
+ // before hiding, so the pointer can travel from the icon into the popover
34
+ // without it closing underneath
35
+ export const CODE_LENS_HOVER_GRACE_MS = 200;
32
36
 
33
37
  const setCodeLenses = StateEffect.define<CodeLensSpec[]>();
34
38
  const setHoveredLens = StateEffect.define<CodeLensSpec | null>();
35
39
 
36
- // Pending hover timers, keyed by widget element so `destroy` can cancel them
37
- const HOVER_TIMERS = new WeakMap<HTMLElement, number>();
40
+ function specEquals(a: CodeLensSpec, b: CodeLensSpec): boolean {
41
+ return (
42
+ a.pos === b.pos &&
43
+ a.kind === b.kind &&
44
+ a.name === b.name &&
45
+ a.cache?.boundName === b.cache?.boundName &&
46
+ a.cache?.cacheName === b.cache?.cacheName
47
+ );
48
+ }
49
+
50
+ /**
51
+ * Per-view hover state shared by the lens icons and their popover, so the
52
+ * popover stays open while the pointer is over either of them.
53
+ */
54
+ class LensHoverController {
55
+ private readonly view: EditorView;
56
+ private showTimer: number | undefined;
57
+ private hideTimer: number | undefined;
58
+
59
+ constructor(view: EditorView) {
60
+ this.view = view;
61
+ }
62
+
63
+ /** Pointer entered a lens icon */
64
+ enterLens(spec: CodeLensSpec, icon: HTMLElement): void {
65
+ this.clearTimers();
66
+ dismissEditorHoverTooltips(this.view, icon);
67
+ const hovered = this.view.state.field(codeLensHoverField, false);
68
+ if (hovered && specEquals(hovered.spec, spec)) {
69
+ // Already showing this lens (e.g. pointer came back from the popover)
70
+ return;
71
+ }
72
+ this.showTimer = window.setTimeout(() => {
73
+ this.showTimer = undefined;
74
+ this.view.dispatch({ effects: setHoveredLens.of(spec) });
75
+ }, CODE_LENS_HOVER_DELAY_MS);
76
+ }
77
+
78
+ /** Pointer left a lens icon or the popover */
79
+ leave(): void {
80
+ this.clearTimers();
81
+ this.hideTimer = window.setTimeout(() => {
82
+ this.hideTimer = undefined;
83
+ this.hide();
84
+ }, CODE_LENS_HOVER_GRACE_MS);
85
+ }
86
+
87
+ /** Pointer entered the popover */
88
+ enterPopover(): void {
89
+ window.clearTimeout(this.hideTimer);
90
+ this.hideTimer = undefined;
91
+ }
92
+
93
+ hide(): void {
94
+ this.clearTimers();
95
+ if (this.view.state.field(codeLensHoverField, false)) {
96
+ this.view.dispatch({ effects: setHoveredLens.of(null) });
97
+ }
98
+ }
99
+
100
+ destroy(): void {
101
+ this.clearTimers();
102
+ }
103
+
104
+ private clearTimers(): void {
105
+ window.clearTimeout(this.showTimer);
106
+ window.clearTimeout(this.hideTimer);
107
+ this.showTimer = undefined;
108
+ this.hideTimer = undefined;
109
+ }
110
+ }
111
+
112
+ const lensHoverPlugin = ViewPlugin.define(
113
+ (view) => new LensHoverController(view),
114
+ );
115
+
116
+ /**
117
+ * Keeps the editor's own hover tooltips (LSP / documentation hints) from
118
+ * showing for the token the icon sits after.
119
+ *
120
+ * CodeMirror's `hoverTooltip` tracks the last `mousemove` seen on `view.dom`
121
+ * and, once the pointer has rested for its hover delay, opens a tooltip for
122
+ * that position. The icon swallows `mousemove` (see the widget), so a pointer
123
+ * resting on the icon looks to CodeMirror like a pointer resting on the last
124
+ * character it crossed on the way in, and any hover already open for that
125
+ * token never gets closed. A `mouseleave` on `view.dom` is the signal it uses
126
+ * to cancel a pending hover and close an open one, so synthesize that.
127
+ */
128
+ function dismissEditorHoverTooltips(
129
+ view: EditorView,
130
+ relatedTarget: HTMLElement,
131
+ ): void {
132
+ view.dom.dispatchEvent(new MouseEvent("mouseleave", { relatedTarget }));
133
+ }
38
134
 
39
135
  class CodeLensWidget extends WidgetType {
40
136
  private readonly spec: CodeLensSpec;
@@ -45,15 +141,9 @@ class CodeLensWidget extends WidgetType {
45
141
  }
46
142
 
47
143
  override eq(other: CodeLensWidget): boolean {
48
- return (
49
- // `pos` is captured by the DOM hover/click handlers, so a reused widget
50
- // whose anchor moved must not be treated as equal
51
- this.spec.pos === other.spec.pos &&
52
- this.spec.kind === other.spec.kind &&
53
- this.spec.name === other.spec.name &&
54
- this.spec.cache?.boundName === other.spec.cache?.boundName &&
55
- this.spec.cache?.cacheName === other.spec.cache?.cacheName
56
- );
144
+ // `pos` is captured by the DOM hover/click handlers, so a reused widget
145
+ // whose anchor moved must not be treated as equal
146
+ return specEquals(this.spec, other.spec);
57
147
  }
58
148
 
59
149
  override toDOM(view: EditorView): HTMLElement {
@@ -67,23 +157,13 @@ class CodeLensWidget extends WidgetType {
67
157
  element.setAttribute("aria-label", LENS_TOOLTIPS[spec.kind]);
68
158
  // Static, trusted markup (see icons.ts)
69
159
  element.innerHTML = LENS_ICONS[spec.kind];
70
- const hidePopover = () => {
71
- window.clearTimeout(HOVER_TIMERS.get(element));
72
- HOVER_TIMERS.delete(element);
73
- if (view.state.field(codeLensHoverField, false)) {
74
- view.dispatch({ effects: setHoveredLens.of(null) });
75
- }
76
- };
77
- element.onmouseenter = () => {
78
- const timer = window.setTimeout(() => {
79
- view.dispatch({ effects: setHoveredLens.of(spec) });
80
- }, CODE_LENS_HOVER_DELAY_MS);
81
- HOVER_TIMERS.set(element, timer);
82
- };
83
- element.onmouseleave = hidePopover;
160
+ const hover = () => view.plugin(lensHoverPlugin);
161
+ const hidePopover = () => hover()?.hide();
162
+ element.onmouseenter = () => hover()?.enterLens(spec, element);
163
+ element.onmouseleave = () => hover()?.leave();
84
164
  element.onmousemove = (event) => {
85
- // Keep the editor's built-in hover documentation tooltip from also
86
- // triggering over the icon
165
+ // Keep the editor's built-in hover tooltip from re-arming while the
166
+ // pointer is over the icon (see `dismissEditorHoverTooltips`)
87
167
  event.stopPropagation();
88
168
  };
89
169
  element.onmousedown = (event) => {
@@ -108,48 +188,58 @@ class CodeLensWidget extends WidgetType {
108
188
  return element;
109
189
  }
110
190
 
111
- override destroy(dom: HTMLElement): void {
112
- window.clearTimeout(HOVER_TIMERS.get(dom));
113
- HOVER_TIMERS.delete(dom);
114
- }
115
-
116
191
  override ignoreEvent(): boolean {
117
192
  // The widget handles its own events
118
193
  return true;
119
194
  }
120
195
  }
121
196
 
122
- function createLensTooltip(spec: CodeLensSpec): Tooltip {
123
- return {
197
+ interface HoveredLens {
198
+ spec: CodeLensSpec;
199
+ tooltip: Tooltip;
200
+ }
201
+
202
+ function createHoveredLens(spec: CodeLensSpec): HoveredLens {
203
+ const tooltip: Tooltip = {
124
204
  pos: spec.pos,
125
205
  above: true,
126
- create: () => {
206
+ create: (view) => {
127
207
  const dom = document.createElement("div");
128
208
  // Same chrome as the SQL completion/hover popovers
129
209
  dom.classList.add("mo-cm-tooltip", "docs-documentation");
210
+ // The popover is scrollable, so keep it open while the pointer is
211
+ // inside it
212
+ dom.onmouseenter = () => view.plugin(lensHoverPlugin)?.enterPopover();
213
+ dom.onmouseleave = () => view.plugin(lensHoverPlugin)?.leave();
130
214
  const unmount = mountLensPopover(dom, spec);
131
215
  return { dom, resize: false, destroy: unmount };
132
216
  },
133
217
  };
218
+ return { spec, tooltip };
134
219
  }
135
220
 
136
- const codeLensHoverField = StateField.define<Tooltip | null>({
221
+ const codeLensHoverField = StateField.define<HoveredLens | null>({
137
222
  create() {
138
223
  return null;
139
224
  },
140
- update(tooltip, tr) {
225
+ update(hovered, tr) {
141
226
  for (const effect of tr.effects) {
142
227
  if (effect.is(setHoveredLens)) {
143
- return effect.value ? createLensTooltip(effect.value) : null;
228
+ return effect.value ? createHoveredLens(effect.value) : null;
144
229
  }
145
- if (effect.is(setCodeLenses)) {
146
- // Lenses were rebuilt; the hovered widget may be gone
147
- return null;
230
+ if (effect.is(setCodeLenses) && hovered) {
231
+ // Lenses were rebuilt (e.g. after a store update); keep the popover
232
+ // only if the hovered lens is still there, unchanged
233
+ const stillPresent = effect.value.some((spec) =>
234
+ specEquals(spec, hovered.spec),
235
+ );
236
+ return stillPresent ? hovered : null;
148
237
  }
149
238
  }
150
- return tr.docChanged ? null : tooltip;
239
+ return tr.docChanged ? null : hovered;
151
240
  },
152
- provide: (field) => showTooltip.from(field),
241
+ provide: (field) =>
242
+ showTooltip.from(field, (hovered) => hovered?.tooltip ?? null),
153
243
  });
154
244
 
155
245
  const codeLensField = StateField.define<DecorationSet>({
@@ -263,13 +353,19 @@ class CodeLensPlugin {
263
353
  }
264
354
  }
265
355
 
356
+ // Padding around the 12px icon so the hover/click target isn't tiny
357
+ const LENS_HIT_PADDING = "3px";
358
+
266
359
  const codeLensTheme = EditorView.baseTheme({
267
360
  ".mo-code-lens": {
268
361
  display: "inline-flex",
269
362
  verticalAlign: "baseline",
270
363
  // Optically center the icon against the text
271
364
  transform: "translateY(1.5px)",
272
- marginLeft: "0.3em",
365
+ // Enlarge the hit area without affecting layout: the padding is pulled
366
+ // back in with negative vertical margins so the line height is unchanged
367
+ padding: LENS_HIT_PADDING,
368
+ margin: `-${LENS_HIT_PADDING} 0 -${LENS_HIT_PADDING} calc(0.3em - ${LENS_HIT_PADDING})`,
273
369
  cursor: "pointer",
274
370
  opacity: "0.5",
275
371
  },
@@ -290,6 +386,7 @@ export function codeLensBundle(cellId: CellId, enabled = true): Extension {
290
386
  return [
291
387
  codeLensField,
292
388
  codeLensHoverField,
389
+ lensHoverPlugin,
293
390
  ViewPlugin.define(
294
391
  (view) => new CodeLensPlugin(view, cellId, getFeatureFlag("cache_panel")),
295
392
  ),