@opencode/ui 0.0.0-dev-1288 → 0.0.0-dev-1290

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.
@@ -18,6 +18,8 @@ export declare function RedirectPasteForm(props: {
18
18
  submitting: boolean;
19
19
  /** Focuses the field on mount, for a dialog where pasting is the main action. */
20
20
  autofocus?: boolean;
21
+ /** Leaves submitting to Enter and the caller's own button. */
22
+ hideSubmit?: boolean;
21
23
  onInput: (value: string) => void;
22
24
  onSubmit: () => void;
23
25
  }): import("solid-js").JSX.Element;
@@ -0,0 +1,48 @@
1
+ /**
2
+ * A menu opens at most one submenu at a time. A submenu stays open while focus crosses the other items of its menu, so
3
+ * the next submenu replaces it in the same frame, and the pointer can still reach it, instead of every move closing it
4
+ * and leaving the menu without one until the next opens.
5
+ */
6
+ export type SubmenuState = {
7
+ readonly status: "closed";
8
+ } | {
9
+ readonly status: "open";
10
+ readonly sub: string;
11
+ }
12
+ /** Focus left the trigger for another item of the menu; the submenu closes unless focus returns or another opens. */
13
+ | {
14
+ readonly status: "leaving";
15
+ readonly sub: string;
16
+ } | {
17
+ readonly status: "disposed";
18
+ };
19
+ export type SubmenuEvent =
20
+ /** Kobalte asked to open or close one submenu: a hover, click or key opened it, or Escape, ArrowLeft or a pick closed it. */
21
+ {
22
+ readonly type: "requested";
23
+ readonly sub: string;
24
+ readonly open: boolean;
25
+ }
26
+ /** Focus moved to another item of the menu that holds the open submenu's trigger, or to that menu itself. */
27
+ | {
28
+ readonly type: "focus.left";
29
+ }
30
+ /** Focus came back to the open submenu's trigger, or moved into the submenu. */
31
+ | {
32
+ readonly type: "focus.returned";
33
+ }
34
+ /** The delay that the given `leaving` state started ran out. */
35
+ | {
36
+ readonly type: "leave.elapsed";
37
+ readonly state: SubmenuState;
38
+ } | {
39
+ readonly type: "disposed";
40
+ };
41
+ export declare const SUBMENU_CLOSED: SubmenuState;
42
+ export declare const SUBMENU_FOCUS_LEFT: SubmenuEvent;
43
+ export declare const SUBMENU_FOCUS_RETURNED: SubmenuEvent;
44
+ export declare const SUBMENU_DISPOSED: SubmenuEvent;
45
+ export declare function submenuRequested(sub: string, open: boolean): SubmenuEvent;
46
+ export declare function submenuLeaveElapsed(state: SubmenuState): SubmenuEvent;
47
+ export declare function transitionSubmenu(state: SubmenuState, event: SubmenuEvent): SubmenuState;
48
+ export declare function selectSubmenuOpen(state: SubmenuState, sub: string): boolean;
@@ -11,12 +11,14 @@ export type TooltipState =
11
11
  readonly status: "idle";
12
12
  readonly hovered: boolean;
13
13
  readonly disabled: boolean;
14
- }
15
- /** `pressing` lasts from a press on the trigger until that press ends, so its click keeps the tooltip open. */
16
- | {
14
+ } | {
17
15
  readonly status: "open";
18
16
  readonly hovered: boolean;
19
- readonly pressing: boolean;
17
+ }
18
+ /** A press keeps the tooltip hidden until the next hover or keyboard focus. */
19
+ | {
20
+ readonly status: "dismissed";
21
+ readonly disabled: boolean;
20
22
  }
21
23
  /**
22
24
  * A popup the trigger opened (a menu, a popover) hides the tooltip. While it is open, a modal layer turns pointer
@@ -60,16 +62,12 @@ export type TooltipEvent = {
60
62
  readonly type: "pointer.moved";
61
63
  readonly inside: boolean;
62
64
  }
63
- /** The trigger element itself took focus. */
65
+ /** The trigger received focus independently of a pointer press. */
64
66
  | {
65
67
  readonly type: "focused";
66
68
  } | {
67
69
  readonly type: "trigger.pressed";
68
70
  }
69
- /** The press ended, after the click it caused, if any. */
70
- | {
71
- readonly type: "press.ended";
72
- }
73
71
  /** The microtask after the pointer left, once Kobalte has seen the tooltip open and started its skip-delay window. */
74
72
  | {
75
73
  readonly type: "leave.settled";
@@ -82,8 +80,7 @@ export type TooltipEvent = {
82
80
  } | {
83
81
  readonly type: "disposed";
84
82
  };
85
- /** `replay` forwards the mounting hover or focus to Kobalte, which attached its listeners too late to receive it. */
86
- export type TooltipEffect = "replay" | "schedule-leave";
83
+ export type TooltipEffect = "replay" | "schedule-leave" | "cancel-opening" | "hide";
87
84
  export type TooltipTransition = {
88
85
  readonly state: TooltipState;
89
86
  readonly effects: readonly TooltipEffect[];
@@ -94,7 +91,6 @@ export declare const POINTER_INSIDE: TooltipEvent;
94
91
  export declare const POINTER_OUTSIDE: TooltipEvent;
95
92
  export declare const FOCUSED: TooltipEvent;
96
93
  export declare const TRIGGER_PRESSED: TooltipEvent;
97
- export declare const PRESS_ENDED: TooltipEvent;
98
94
  export declare const LEAVE_SETTLED: TooltipEvent;
99
95
  export declare const POPUP_EXPANDED: TooltipEvent;
100
96
  export declare const POPUP_COLLAPSED: TooltipEvent;
@@ -112,7 +108,9 @@ export declare function selectTooltipMounted(state: TooltipState): state is {
112
108
  } | {
113
109
  readonly status: "open";
114
110
  readonly hovered: boolean;
115
- readonly pressing: boolean;
111
+ } | {
112
+ readonly status: "dismissed";
113
+ readonly disabled: boolean;
116
114
  } | {
117
115
  readonly status: "suppressed";
118
116
  readonly popup: "open";
@@ -131,8 +129,10 @@ export declare function selectTooltipMounted(state: TooltipState): state is {
131
129
  };
132
130
  export declare function selectTooltipOpen(state: TooltipState): boolean;
133
131
  export declare function selectTooltipForceOpen(state: TooltipState): boolean | undefined;
134
- /** A press is in progress, so the driver listens for its end. */
135
- export declare function selectTooltipPressing(state: TooltipState): boolean;
132
+ export declare function selectTooltipDismissed(state: TooltipState): state is {
133
+ readonly status: "dismissed";
134
+ readonly disabled: boolean;
135
+ };
136
136
  /** A popup suppresses the tooltip, so the driver reports where the pointer moves. */
137
137
  export declare function selectTooltipTracking(state: TooltipState): state is {
138
138
  readonly status: "suppressed";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@opencode/ui",
3
- "version": "0.0.0-dev-1288",
3
+ "version": "0.0.0-dev-1290",
4
4
  "type": "module",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -294,7 +294,7 @@
294
294
  "generate:v2-oc2": "bun run script/build-oc2-v2-overrides.ts"
295
295
  },
296
296
  "devDependencies": {
297
- "@hona/strobe": "0.35.0",
297
+ "@hona/strobe": "0.37.0",
298
298
  "@solidjs/meta": "0.29.4",
299
299
  "@tsconfig/node22": "22.0.2",
300
300
  "@types/bun": "1.4.3",
@@ -309,7 +309,7 @@
309
309
  "dependencies": {
310
310
  "@kitlangton/solid-motion": "0.2.2",
311
311
  "@kobalte/core": "0.13.13",
312
- "@opencode/util": "0.0.0-dev-1288",
312
+ "@opencode/util": "0.0.0-dev-1290",
313
313
  "@pierre/diffs": "1.6.1",
314
314
  "@pierre/highlights": "0.2.0",
315
315
  "@solid-primitives/event-listener": "2.4.6",
@@ -1,5 +1,6 @@
1
1
  import { createUniqueId, onMount, Show } from "solid-js"
2
- import { Button } from "@opencode/ui/button"
2
+ import { Icon } from "@opencode/ui/icon"
3
+ import { IconButton } from "@opencode/ui/icon-button"
3
4
  import { TextInput } from "@opencode/ui/text-input"
4
5
  import { useI18n } from "../../context/i18n"
5
6
 
@@ -19,6 +20,8 @@ export function RedirectPasteForm(props: {
19
20
  submitting: boolean
20
21
  /** Focuses the field on mount, for a dialog where pasting is the main action. */
21
22
  autofocus?: boolean
23
+ /** Leaves submitting to Enter and the caller's own button. */
24
+ hideSubmit?: boolean
22
25
  onInput: (value: string) => void
23
26
  onSubmit: () => void
24
27
  }) {
@@ -42,13 +45,14 @@ export function RedirectPasteForm(props: {
42
45
  event.preventDefault()
43
46
  props.onSubmit()
44
47
  }}
45
- class="flex flex-col items-start gap-5 self-stretch"
48
+ class="flex flex-col items-start gap-2 self-stretch"
46
49
  >
47
50
  <label class="flex w-full flex-col gap-2 font-[530] leading-4 text-v2-text-text-base">
48
51
  {i18n.t("ui.redirectPaste.label")}
49
52
  <TextInput
50
53
  ref={input}
51
54
  class="!w-full"
55
+ appearance="large"
52
56
  name="redirect"
53
57
  placeholder={i18n.t("ui.redirectPaste.placeholder")}
54
58
  value={props.value}
@@ -58,18 +62,28 @@ export function RedirectPasteForm(props: {
58
62
  autocomplete="off"
59
63
  spellcheck={false}
60
64
  onInput={(event) => props.onInput(event.currentTarget.value)}
65
+ trailing={
66
+ !props.hideSubmit && (
67
+ <IconButton
68
+ type="submit"
69
+ size="small"
70
+ variant="ghost"
71
+ icon={<Icon name="arrow-right" />}
72
+ disabled={props.locked}
73
+ aria-busy={props.submitting}
74
+ aria-label={i18n.t("ui.common.continue")}
75
+ />
76
+ )
77
+ }
61
78
  />
62
79
  </label>
63
80
  <Show when={error()}>
64
81
  {(message) => (
65
- <div id={errorID} role="alert" class="-mt-4 text-xs text-v2-state-fg-danger">
82
+ <div id={errorID} role="alert" class="text-xs text-v2-state-fg-danger">
66
83
  {message()}
67
84
  </div>
68
85
  )}
69
86
  </Show>
70
- <Button type="submit" variant="contrast" disabled={props.locked} aria-busy={props.submitting}>
71
- {i18n.t("ui.common.continue")}
72
- </Button>
73
87
  </form>
74
88
  )
75
89
  }
@@ -160,6 +160,11 @@
160
160
  }
161
161
  }
162
162
 
163
+ /* Like a native menu, an item stays highlighted while the pointer is in the submenu it opened. */
164
+ [data-component="menu-v2-content"]:not(:has([data-highlighted])) [data-component="menu-v2-item"][data-expanded] {
165
+ background: var(--menu-v2-hover);
166
+ }
167
+
163
168
  [data-slot="menu-v2-separator"] {
164
169
  height: 1px;
165
170
  width: calc(100% + 4px);
@@ -257,6 +262,10 @@
257
262
  }
258
263
  }
259
264
 
265
+ &:not(:has([data-highlighted])) [data-component="menu-v2-item"][data-expanded] {
266
+ background: var(--surface-raised-base-hover);
267
+ }
268
+
260
269
  [data-slot="menu-v2-separator"] {
261
270
  width: calc(100% + 8px);
262
271
  height: 1px;
@@ -1,10 +1,15 @@
1
1
  import { DropdownMenu, type DropdownMenuTriggerProps } from "@kobalte/core/dropdown-menu"
2
2
  import { ContextMenu } from "@kobalte/core/context-menu"
3
3
  import type { PolymorphicProps } from "@kobalte/core/polymorphic"
4
+ import { makeEventListener } from "@solid-primitives/event-listener"
4
5
  import {
5
6
  createComputed,
6
7
  createContext,
8
+ createEffect,
9
+ createMemo,
7
10
  createSignal,
11
+ createUniqueId,
12
+ on,
8
13
  onCleanup,
9
14
  Show,
10
15
  splitProps,
@@ -28,6 +33,18 @@ import {
28
33
  type MenuInputGesture,
29
34
  type MenuState,
30
35
  } from "./machine"
36
+ import {
37
+ selectSubmenuOpen,
38
+ SUBMENU_CLOSED,
39
+ SUBMENU_DISPOSED,
40
+ SUBMENU_FOCUS_LEFT,
41
+ SUBMENU_FOCUS_RETURNED,
42
+ submenuLeaveElapsed,
43
+ submenuRequested,
44
+ transitionSubmenu,
45
+ type SubmenuEvent,
46
+ type SubmenuState,
47
+ } from "./submenu-machine"
31
48
  import "./menu.css"
32
49
 
33
50
  const ChevronRight: Component = () => (
@@ -254,17 +271,112 @@ export interface MenuSubProps extends ComponentProps<typeof DropdownMenu.Sub> {
254
271
  | "right-end"
255
272
  }
256
273
 
274
+ // Longer than Kobalte's 100ms hover delay, so a submenu the pointer rests on replaces the leaving one in the same frame.
275
+ const SUBMENU_LEAVE_DELAY = 150
276
+
277
+ type SubmenuGroup = { state: Accessor<SubmenuState>; dispatch: (event: SubmenuEvent) => void }
278
+
279
+ const SubmenuGroupContext = createContext<SubmenuGroup>()
280
+
281
+ const SubmenuTriggerContext = createContext<(element: HTMLElement) => void>()
282
+
283
+ function createSubmenuGroup(): SubmenuGroup {
284
+ const [state, setState] = createSignal(SUBMENU_CLOSED)
285
+ let leave: ReturnType<typeof setTimeout> | undefined
286
+
287
+ const dispatch = (event: SubmenuEvent) => {
288
+ const next = transitionSubmenu(state(), event)
289
+
290
+ if (next === state()) return
291
+ clearTimeout(leave)
292
+
293
+ if (next.status === "leaving") leave = setTimeout(() => dispatch(submenuLeaveElapsed(next)), SUBMENU_LEAVE_DELAY)
294
+ setState(next)
295
+ }
296
+
297
+ onCleanup(() => {
298
+ clearTimeout(leave)
299
+ dispatch(SUBMENU_DISPOSED)
300
+ })
301
+
302
+ return { state, dispatch }
303
+ }
304
+
305
+ // Each menu surface owns the submenus it lists, and mounts this with its items so every open starts with none open.
306
+ function SubmenuGroupProvider(props: ParentProps) {
307
+ const group = createSubmenuGroup()
308
+
309
+ return <SubmenuGroupContext.Provider value={group}>{props.children}</SubmenuGroupContext.Provider>
310
+ }
311
+
312
+ // The menu surface decides which of its submenus is open. A controlled submenu stays with its owner instead.
257
313
  function MenuSub(props: MenuSubProps) {
258
- return <DropdownMenu.Sub {...props} />
314
+ const [local, rest] = splitProps(props, ["open", "defaultOpen", "onOpenChange"])
315
+ const group = useContext(SubmenuGroupContext) ?? createSubmenuGroup()
316
+ const id = createUniqueId()
317
+ const open = createMemo(() => selectSubmenuOpen(group.state(), id))
318
+ let trigger: HTMLElement | undefined
319
+
320
+ if (local.defaultOpen) group.dispatch(submenuRequested(id, true))
321
+ onCleanup(() => group.dispatch(submenuRequested(id, false)))
322
+
323
+ createEffect(
324
+ on(
325
+ open,
326
+ (value) => {
327
+ if (local.open === undefined) local.onOpenChange?.(value)
328
+ },
329
+ { defer: true },
330
+ ),
331
+ )
332
+
333
+ // Kobalte closes a submenu when focus leaves it, but ignores focus inside a top layer, and every menu is one so a modal
334
+ // dialog keeps it reachable. Focus moves go to the group instead, which waits for the next submenu or the pointer.
335
+ createEffect(() => {
336
+ if (!open()) return
337
+ makeEventListener(
338
+ document,
339
+ "focusin",
340
+ (event) => {
341
+ if (!(event.target instanceof Node) || !trigger) return
342
+
343
+ const left = !trigger.contains(event.target) && !!trigger.closest('[role="menu"]')?.contains(event.target)
344
+ group.dispatch(left ? SUBMENU_FOCUS_LEFT : SUBMENU_FOCUS_RETURNED)
345
+ },
346
+ { capture: true },
347
+ )
348
+ })
349
+
350
+ return (
351
+ <SubmenuTriggerContext.Provider value={(element) => (trigger = element)}>
352
+ <DropdownMenu.Sub
353
+ {...rest}
354
+ open={local.open ?? open()}
355
+ onOpenChange={(next) => {
356
+ if (local.open !== undefined) return local.onOpenChange?.(next)
357
+ group.dispatch(submenuRequested(id, next))
358
+ }}
359
+ />
360
+ </SubmenuTriggerContext.Provider>
361
+ )
259
362
  }
260
363
 
261
364
  function MenuSubTrigger(props: ParentProps<MenuSubTriggerProps>) {
262
365
  const ctx = useMenuContext()
263
- const [s, r] = splitProps(props, ["class", "classList", "children", "shortcut", "badge"])
366
+ const registerTrigger = useContext(SubmenuTriggerContext)
367
+ const [s, r] = splitProps(props, ["class", "classList", "children", "shortcut", "badge", "ref"])
368
+
369
+ const assignRef = (element: HTMLElement) => {
370
+ registerTrigger?.(element)
371
+ const ref = s.ref
372
+
373
+ if (ref instanceof Function) ref(element)
374
+ }
264
375
 
265
376
  return (
266
377
  <DropdownMenu.SubTrigger
267
378
  {...r}
379
+ ref={assignRef}
268
380
  data-component="menu-v2-item"
269
381
  data-appearance={ctx.appearance()}
270
382
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
@@ -278,7 +390,7 @@ function MenuSubTrigger(props: ParentProps<MenuSubTriggerProps>) {
278
390
 
279
391
  function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
280
392
  const ctx = useMenuContext()
281
- const [s, r] = splitProps(props, ["class", "classList"])
393
+ const [s, r] = splitProps(props, ["class", "classList", "children"])
282
394
 
283
395
  return (
284
396
  <DropdownMenu.SubContent
@@ -287,7 +399,9 @@ function MenuSubContent(props: ComponentProps<typeof DropdownMenu.SubContent>) {
287
399
  data-component="menu-v2-content"
288
400
  data-appearance={ctx.appearance()}
289
401
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
290
- />
402
+ >
403
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
404
+ </DropdownMenu.SubContent>
291
405
  )
292
406
  }
293
407
 
@@ -329,7 +443,7 @@ function MenuTrigger<T extends ValidComponent = "button">(props: PolymorphicProp
329
443
 
330
444
  function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
331
445
  const ctx = useMenuContext()
332
- const [s, r] = splitProps(props, ["class", "classList", "onCloseAutoFocus", "onInteractOutside"])
446
+ const [s, r] = splitProps(props, ["class", "classList", "children", "onCloseAutoFocus", "onInteractOutside"])
333
447
 
334
448
  // Kobalte's own rule for when an outside press keeps focus where it landed.
335
449
  const onInteractOutside: NonNullable<typeof s.onInteractOutside> = (event) => {
@@ -366,7 +480,9 @@ function MenuContent(props: ComponentProps<typeof DropdownMenu.Content>) {
366
480
  data-component="menu-v2-content"
367
481
  data-appearance={ctx.appearance()}
368
482
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
369
- />
483
+ >
484
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
485
+ </DropdownMenu.Content>
370
486
  )
371
487
  }
372
488
 
@@ -433,7 +549,7 @@ function MenuContextRoot(props: MenuContextProps) {
433
549
 
434
550
  function MenuContextContent(props: ComponentProps<typeof ContextMenu.Content>) {
435
551
  const ctx = useMenuContext()
436
- const [s, r] = splitProps(props, ["class", "classList"])
552
+ const [s, r] = splitProps(props, ["class", "classList", "children"])
437
553
 
438
554
  return (
439
555
  <ContextMenu.Content
@@ -441,7 +557,9 @@ function MenuContextContent(props: ComponentProps<typeof ContextMenu.Content>) {
441
557
  data-component="menu-v2-content"
442
558
  data-appearance={ctx.appearance()}
443
559
  classList={{ ...s.classList, [s.class ?? ""]: !!s.class }}
444
- />
560
+ >
561
+ <SubmenuGroupProvider>{s.children}</SubmenuGroupProvider>
562
+ </ContextMenu.Content>
445
563
  )
446
564
  }
447
565
 
@@ -0,0 +1,67 @@
1
+ /**
2
+ * A menu opens at most one submenu at a time. A submenu stays open while focus crosses the other items of its menu, so
3
+ * the next submenu replaces it in the same frame, and the pointer can still reach it, instead of every move closing it
4
+ * and leaving the menu without one until the next opens.
5
+ */
6
+ export type SubmenuState =
7
+ | { readonly status: "closed" }
8
+ | { readonly status: "open"; readonly sub: string }
9
+ /** Focus left the trigger for another item of the menu; the submenu closes unless focus returns or another opens. */
10
+ | { readonly status: "leaving"; readonly sub: string }
11
+ | { readonly status: "disposed" }
12
+
13
+ export type SubmenuEvent =
14
+ /** Kobalte asked to open or close one submenu: a hover, click or key opened it, or Escape, ArrowLeft or a pick closed it. */
15
+ | { readonly type: "requested"; readonly sub: string; readonly open: boolean }
16
+ /** Focus moved to another item of the menu that holds the open submenu's trigger, or to that menu itself. */
17
+ | { readonly type: "focus.left" }
18
+ /** Focus came back to the open submenu's trigger, or moved into the submenu. */
19
+ | { readonly type: "focus.returned" }
20
+ /** The delay that the given `leaving` state started ran out. */
21
+ | { readonly type: "leave.elapsed"; readonly state: SubmenuState }
22
+ | { readonly type: "disposed" }
23
+
24
+ export const SUBMENU_CLOSED: SubmenuState = Object.freeze({ status: "closed" })
25
+
26
+ const DISPOSED: SubmenuState = Object.freeze({ status: "disposed" })
27
+
28
+ export const SUBMENU_FOCUS_LEFT: SubmenuEvent = Object.freeze({ type: "focus.left" })
29
+
30
+ export const SUBMENU_FOCUS_RETURNED: SubmenuEvent = Object.freeze({ type: "focus.returned" })
31
+
32
+ export const SUBMENU_DISPOSED: SubmenuEvent = Object.freeze({ type: "disposed" })
33
+
34
+ export function submenuRequested(sub: string, open: boolean): SubmenuEvent {
35
+ return Object.freeze({ type: "requested", sub, open })
36
+ }
37
+
38
+ export function submenuLeaveElapsed(state: SubmenuState): SubmenuEvent {
39
+ return Object.freeze({ type: "leave.elapsed", state })
40
+ }
41
+
42
+ export function transitionSubmenu(state: SubmenuState, event: SubmenuEvent): SubmenuState {
43
+ if (state.status === "disposed") return state
44
+
45
+ if (event.type === "disposed") return DISPOSED
46
+
47
+ if (event.type === "requested") {
48
+ if (event.open) return state.status === "open" && state.sub === event.sub ? state : open(event.sub)
49
+
50
+ return state.status !== "closed" && state.sub === event.sub ? SUBMENU_CLOSED : state
51
+ }
52
+
53
+ if (state.status === "closed") return state
54
+
55
+ // Each move away is a new state that restarts the delay, so the submenu outlives a pointer still crossing items.
56
+ if (event.type === "focus.left") return Object.freeze({ status: "leaving", sub: state.sub })
57
+
58
+ if (event.type === "focus.returned") return state.status === "leaving" ? open(state.sub) : state
59
+
60
+ return event.state === state ? SUBMENU_CLOSED : state
61
+ }
62
+
63
+ export function selectSubmenuOpen(state: SubmenuState, sub: string) {
64
+ return (state.status === "open" || state.status === "leaving") && state.sub === sub
65
+ }
66
+
67
+ const open = (sub: string): SubmenuState => Object.freeze({ status: "open", sub })
@@ -6,8 +6,9 @@ export type TooltipState =
6
6
  /** Kobalte is not mounted yet. Most triggers are never hovered, so it mounts on the first hover or focus. */
7
7
  | { readonly status: "unmounted"; readonly disabled: boolean }
8
8
  | { readonly status: "idle"; readonly hovered: boolean; readonly disabled: boolean }
9
- /** `pressing` lasts from a press on the trigger until that press ends, so its click keeps the tooltip open. */
10
- | { readonly status: "open"; readonly hovered: boolean; readonly pressing: boolean }
9
+ | { readonly status: "open"; readonly hovered: boolean }
10
+ /** A press keeps the tooltip hidden until the next hover or keyboard focus. */
11
+ | { readonly status: "dismissed"; readonly disabled: boolean }
11
12
  /**
12
13
  * A popup the trigger opened (a menu, a popover) hides the tooltip. While it is open, a modal layer turns pointer
13
14
  * events off for the page, so the trigger's own enter and leave events lie and `hovered` follows the pointer's
@@ -33,19 +34,16 @@ export type TooltipEvent =
33
34
  | { readonly type: "pointer.left" }
34
35
  /** Where a pointer move landed while a popup suppresses the tooltip, measured against the trigger's box. */
35
36
  | { readonly type: "pointer.moved"; readonly inside: boolean }
36
- /** The trigger element itself took focus. */
37
+ /** The trigger received focus independently of a pointer press. */
37
38
  | { readonly type: "focused" }
38
39
  | { readonly type: "trigger.pressed" }
39
- /** The press ended, after the click it caused, if any. */
40
- | { readonly type: "press.ended" }
41
40
  /** The microtask after the pointer left, once Kobalte has seen the tooltip open and started its skip-delay window. */
42
41
  | { readonly type: "leave.settled" }
43
42
  | { readonly type: "popup.changed"; readonly expanded: boolean }
44
43
  | { readonly type: "kobalte.changed"; readonly open: boolean }
45
44
  | { readonly type: "disposed" }
46
45
 
47
- /** `replay` forwards the mounting hover or focus to Kobalte, which attached its listeners too late to receive it. */
48
- export type TooltipEffect = "replay" | "schedule-leave"
46
+ export type TooltipEffect = "replay" | "schedule-leave" | "cancel-opening" | "hide"
49
47
 
50
48
  export type TooltipTransition = {
51
49
  readonly state: TooltipState
@@ -69,7 +67,9 @@ const UNMOUNTED = [false, true].map((disabled) => freeze({ status: "unmounted",
69
67
 
70
68
  const IDLE = [0, 1, 2, 3].map((key) => freeze({ status: "idle", hovered: !!(key & 1), disabled: !!(key & 2) }))
71
69
 
72
- const OPEN = [0, 1, 2, 3].map((key) => freeze({ status: "open", hovered: !!(key & 1), pressing: !!(key & 2) }))
70
+ const OPEN = [false, true].map((hovered) => freeze({ status: "open", hovered }))
71
+
72
+ const DISMISSED = [false, true].map((disabled) => freeze({ status: "dismissed", disabled }))
73
73
 
74
74
  const POPUP_OPEN = [0, 1, 2, 3].map((key) =>
75
75
  freeze({ status: "suppressed", popup: "open", hovered: !!(key & 1), disabled: !!(key & 2) }),
@@ -93,7 +93,7 @@ const unmounted = (disabled: boolean) => UNMOUNTED[bit(disabled)]
93
93
 
94
94
  const idle = (hovered: boolean, disabled: boolean) => IDLE[bit(hovered) | (bit(disabled) << 1)]
95
95
 
96
- const open = (hovered: boolean, pressing: boolean) => OPEN[bit(hovered) | (bit(pressing) << 1)]
96
+ const open = (hovered: boolean) => OPEN[bit(hovered)]
97
97
 
98
98
  const popupOpen = (hovered: boolean, disabled: boolean) => POPUP_OPEN[bit(hovered) | (bit(disabled) << 1)]
99
99
 
@@ -102,16 +102,17 @@ const popupClosed = (disabled: boolean) => POPUP_CLOSED[bit(disabled)]
102
102
  const controlled = (open: boolean, hovered: boolean, expanded: boolean, disabled: boolean) =>
103
103
  CONTROLLED[bit(open) | (bit(hovered) << 1) | (bit(expanded) << 2) | (bit(disabled) << 3)]
104
104
 
105
- const REPLAY: readonly TooltipEffect[] = Object.freeze(["replay"])
105
+ function withEffect(state: TooltipState, effect: TooltipEffect): TooltipTransition {
106
+ return Object.freeze({ state, effects: Object.freeze([effect]) })
107
+ }
108
+
109
+ const REPLAY_HOVER = withEffect(idle(true, false), "replay")
106
110
 
107
- const REPLAY_HOVER: TooltipTransition = Object.freeze({ state: idle(true, false), effects: REPLAY })
111
+ const LEAVING = withEffect(open(false), "schedule-leave")
108
112
 
109
- const REPLAY_FOCUS: TooltipTransition = Object.freeze({ state: idle(false, false), effects: REPLAY })
113
+ const CANCEL_OPENING = withEffect(DISMISSED[0], "cancel-opening")
110
114
 
111
- const LEAVING: TooltipTransition = Object.freeze({
112
- state: open(false, false),
113
- effects: Object.freeze<TooltipEffect[]>(["schedule-leave"]),
114
- })
115
+ const HIDE = withEffect(DISMISSED[0], "hide")
115
116
 
116
117
  export const POINTER_ENTERED: TooltipEvent = Object.freeze({ type: "pointer.entered" })
117
118
 
@@ -125,8 +126,6 @@ export const FOCUSED: TooltipEvent = Object.freeze({ type: "focused" })
125
126
 
126
127
  export const TRIGGER_PRESSED: TooltipEvent = Object.freeze({ type: "trigger.pressed" })
127
128
 
128
- export const PRESS_ENDED: TooltipEvent = Object.freeze({ type: "press.ended" })
129
-
130
129
  export const LEAVE_SETTLED: TooltipEvent = Object.freeze({ type: "leave.settled" })
131
130
 
132
131
  export const POPUP_EXPANDED: TooltipEvent = Object.freeze({ type: "popup.changed", expanded: true })
@@ -178,9 +177,8 @@ export function selectTooltipForceOpen(state: TooltipState) {
178
177
  return state.status === "controlled" ? state.open : undefined
179
178
  }
180
179
 
181
- /** A press is in progress, so the driver listens for its end. */
182
- export function selectTooltipPressing(state: TooltipState) {
183
- return state.status === "open" && state.pressing
180
+ export function selectTooltipDismissed(state: TooltipState) {
181
+ return state.status === "dismissed"
184
182
  }
185
183
 
186
184
  /** A popup suppresses the tooltip, so the driver reports where the pointer moves. */
@@ -200,7 +198,7 @@ function next(state: TooltipState, event: TooltipEvent): TooltipState | TooltipT
200
198
 
201
199
  if (event.type === "pointer.entered") return REPLAY_HOVER
202
200
 
203
- if (event.type === "focused") return REPLAY_FOCUS
201
+ if (event.type === "focused") return idle(false, false)
204
202
 
205
203
  return state
206
204
  }
@@ -212,28 +210,35 @@ function next(state: TooltipState, event: TooltipEvent): TooltipState | TooltipT
212
210
 
213
211
  if (event.type === "popup.changed" && event.expanded) return popupOpen(state.hovered, state.disabled)
214
212
 
215
- if (event.type === "kobalte.changed" && event.open && !state.disabled) return open(state.hovered, false)
213
+ if (event.type === "trigger.pressed" && state.hovered && !state.disabled) return CANCEL_OPENING
214
+
215
+ if (event.type === "kobalte.changed" && event.open && !state.disabled) return open(state.hovered)
216
216
 
217
217
  return state
218
218
  }
219
219
 
220
220
  if (state.status === "open") {
221
- if (event.type === "pointer.entered") return open(true, state.pressing)
221
+ if (event.type === "pointer.entered") return open(true)
222
222
 
223
- if (event.type === "pointer.left") return state.hovered || state.pressing ? LEAVING : state
223
+ if (event.type === "pointer.left") return state.hovered ? LEAVING : state
224
224
 
225
225
  if (event.type === "leave.settled") return state.hovered ? state : idle(false, false)
226
226
 
227
- if (event.type === "trigger.pressed") return open(state.hovered, true)
228
-
229
- if (event.type === "press.ended") return open(state.hovered, false)
227
+ if (event.type === "trigger.pressed") return HIDE
230
228
 
231
229
  if (event.type === "popup.changed" && event.expanded) return popupOpen(state.hovered, false)
232
230
 
233
- // Kobalte closes the tooltip on every click of its trigger; the click that ends a press keeps it open.
234
- if (event.type === "kobalte.changed" && !event.open) {
235
- return state.pressing ? open(state.hovered, false) : idle(state.hovered, false)
236
- }
231
+ if (event.type === "kobalte.changed" && !event.open) return idle(state.hovered, false)
232
+
233
+ return state
234
+ }
235
+
236
+ if (state.status === "dismissed") {
237
+ if (event.type === "pointer.left") return idle(false, state.disabled)
238
+
239
+ if (event.type === "focused") return idle(true, state.disabled)
240
+
241
+ if (event.type === "popup.changed" && event.expanded) return popupOpen(true, state.disabled)
237
242
 
238
243
  return state
239
244
  }
@@ -281,7 +286,9 @@ function sync(state: TooltipState, forceOpen: boolean | undefined, disabled: boo
281
286
  return controlled(forceOpen, state.hovered, true, disabled)
282
287
  }
283
288
 
284
- if (state.status === "suppressed") return controlled(forceOpen, true, false, disabled)
289
+ if (state.status === "suppressed" || state.status === "dismissed") {
290
+ return controlled(forceOpen, true, false, disabled)
291
+ }
285
292
 
286
293
  return controlled(forceOpen, state.hovered, false, disabled)
287
294
  }
@@ -292,6 +299,8 @@ function sync(state: TooltipState, forceOpen: boolean | undefined, disabled: boo
292
299
 
293
300
  if (state.status === "open") return disabled ? idle(state.hovered, true) : state
294
301
 
302
+ if (state.status === "dismissed") return DISMISSED[bit(disabled)]
303
+
295
304
  if (state.status === "suppressed" && state.popup === "open") return popupOpen(state.hovered, disabled)
296
305
 
297
306
  if (state.status === "suppressed") return popupClosed(disabled)
@@ -1,4 +1,5 @@
1
- import { Content, Portal, Root, Trigger } from "@kobalte/core/tooltip"
1
+ import { Content, Portal, Root, Trigger, useTooltipContext, type TooltipContextValue } from "@kobalte/core/tooltip"
2
+ import { makeEventListener } from "@solid-primitives/event-listener"
2
3
  import { createComputed, createSignal, onCleanup, onMount, Show, splitProps, untrack, type JSX } from "solid-js"
3
4
  import type { ComponentProps } from "solid-js"
4
5
  import { spread } from "solid-js/web"
@@ -15,11 +16,10 @@ import {
15
16
  POINTER_OUTSIDE,
16
17
  POPUP_COLLAPSED,
17
18
  POPUP_EXPANDED,
18
- PRESS_ENDED,
19
+ selectTooltipDismissed,
19
20
  selectTooltipForceOpen,
20
21
  selectTooltipMounted,
21
22
  selectTooltipOpen,
22
- selectTooltipPressing,
23
23
  selectTooltipTracking,
24
24
  syncPropsEvent,
25
25
  transitionTooltip,
@@ -41,13 +41,46 @@ export interface TooltipProps extends ComponentProps<typeof Root> {
41
41
  }
42
42
 
43
43
  type TriggerHandlers = {
44
- onPointerEnter?: (event: PointerEvent) => void
45
- onFocus?: (event: FocusEvent) => void
44
+ onPointerEnter: (event: PointerEvent) => void
45
+ onFocus: (event: FocusEvent) => void
46
+ onBlur: (event: FocusEvent) => void
47
+ onClick: (event: MouseEvent) => void
46
48
  }
47
49
 
48
50
  // Expanded panels still need tooltips; only an open popup suppresses its trigger's tooltip.
49
51
  const POPUP = '[aria-haspopup]:not([aria-haspopup="false"]):is([aria-expanded="true"], [data-expanded])'
50
52
 
53
+ // A closing popup can restore focus to its button. Only Tab should open the button's tooltip.
54
+ let triggers = 0
55
+
56
+ let tabbing = false
57
+
58
+ function startTab(event: KeyboardEvent) {
59
+ tabbing = event.key === "Tab"
60
+ }
61
+
62
+ function endTab() {
63
+ tabbing = false
64
+ }
65
+
66
+ function listenForTab() {
67
+ if (++triggers === 1) {
68
+ window.addEventListener("keydown", startTab, true)
69
+ window.addEventListener("keyup", endTab, true)
70
+ window.addEventListener("pointerdown", endTab, true)
71
+ window.addEventListener("blur", endTab)
72
+ }
73
+
74
+ onCleanup(() => {
75
+ if (--triggers) return
76
+ window.removeEventListener("keydown", startTab, true)
77
+ window.removeEventListener("keyup", endTab, true)
78
+ window.removeEventListener("pointerdown", endTab, true)
79
+ window.removeEventListener("blur", endTab)
80
+ endTab()
81
+ })
82
+ }
83
+
51
84
  export function Tooltip(props: TooltipProps) {
52
85
  return (
53
86
  <Show when={!props.inactive} fallback={props.children}>
@@ -58,9 +91,13 @@ export function Tooltip(props: TooltipProps) {
58
91
 
59
92
  function ActiveTooltip(props: TooltipProps) {
60
93
  let ref: HTMLDivElement | undefined
94
+
61
95
  let handlers: TriggerHandlers | undefined
96
+
62
97
  let observer: MutationObserver | undefined
63
98
 
99
+ let kobalte: TooltipContextValue | undefined
100
+
64
101
  const [local, others] = splitProps(props, [
65
102
  "children",
66
103
  "appearance",
@@ -79,7 +116,7 @@ function ActiveTooltip(props: TooltipProps) {
79
116
  // open it, or as soon as a caller controls it with forceOpen.
80
117
  const [tooltip, setTooltip] = createSignal(createTooltipState(local.forceOpen, !!others.disabled))
81
118
 
82
- const dispatch = (event: TooltipEvent, source?: PointerEvent | FocusEvent) => {
119
+ const dispatch = (event: TooltipEvent, source?: PointerEvent) => {
83
120
  const previous = untrack(tooltip)
84
121
  const transition = transitionTooltip(previous, event)
85
122
 
@@ -91,10 +128,13 @@ function ActiveTooltip(props: TooltipProps) {
91
128
  for (const effect of transition.effects) {
92
129
  if (effect === "schedule-leave") queueMicrotask(settleLeave)
93
130
 
94
- // Kobalte attached its listeners while this event was being dispatched, too late to receive it.
95
- if (effect === "replay" && source instanceof PointerEvent) handlers?.onPointerEnter?.(source)
131
+ // hideTooltip starts the skip window; cancelling a pending hover shouldn't.
132
+ if (effect === "cancel-opening") kobalte?.cancelOpening()
133
+
134
+ if (effect === "hide") kobalte?.hideTooltip(true)
96
135
 
97
- if (effect === "replay" && source instanceof FocusEvent) handlers?.onFocus?.(source)
136
+ // Kobalte attached its listeners while this event was being dispatched, too late to receive it.
137
+ if (effect === "replay" && source) handlers?.onPointerEnter(source)
98
138
  }
99
139
  }
100
140
 
@@ -102,8 +142,6 @@ function ActiveTooltip(props: TooltipProps) {
102
142
  // tooltips if it still sees this one open.
103
143
  const settleLeave = () => dispatch(LEAVE_SETTLED)
104
144
  const syncPopup = () => dispatch(ref?.querySelector(POPUP) ? POPUP_EXPANDED : POPUP_COLLAPSED)
105
- // The press's click, if any, follows its pointerup in the same task.
106
- const onPressEnd = () => setTimeout(dispatch, 0, PRESS_ENDED)
107
145
 
108
146
  const onPointerMove = (event: PointerEvent) => {
109
147
  if (!ref) return
@@ -117,18 +155,6 @@ function ActiveTooltip(props: TooltipProps) {
117
155
 
118
156
  // Attaches the listeners and observer the state needs and detaches the rest.
119
157
  const reconcile = (previous: TooltipState, state: TooltipState) => {
120
- const pressing = selectTooltipPressing(state)
121
-
122
- if (pressing && !selectTooltipPressing(previous)) {
123
- window.addEventListener("pointerup", onPressEnd, true)
124
- window.addEventListener("pointercancel", onPressEnd, true)
125
- }
126
-
127
- if (!pressing && selectTooltipPressing(previous)) {
128
- window.removeEventListener("pointerup", onPressEnd, true)
129
- window.removeEventListener("pointercancel", onPressEnd, true)
130
- }
131
-
132
158
  const tracking = selectTooltipTracking(state)
133
159
 
134
160
  if (tracking && !selectTooltipTracking(previous)) {
@@ -155,6 +181,7 @@ function ActiveTooltip(props: TooltipProps) {
155
181
  }
156
182
 
157
183
  createComputed(() => dispatch(syncPropsEvent(local.forceOpen, !!others.disabled)))
184
+ onMount(listenForTab)
158
185
  onMount(() => reconcile(untrack(tooltip), untrack(tooltip)))
159
186
  onCleanup(() => dispatch(DISPOSED_EVENT))
160
187
 
@@ -162,19 +189,30 @@ function ActiveTooltip(props: TooltipProps) {
162
189
  // the trigger and its children in place, with their focus and hover.
163
190
  const adopt = (triggerProps: TriggerHandlers) => {
164
191
  handlers = triggerProps
192
+ kobalte = useTooltipContext()
165
193
 
166
194
  if (!ref) return null
195
+ // Tab-close buttons stop pointerdown propagation.
196
+ makeEventListener(
197
+ ref,
198
+ "pointerdown",
199
+ (event) => {
200
+ if (event.pointerType !== "touch") dispatch(TRIGGER_PRESSED)
201
+ },
202
+ true,
203
+ )
204
+
167
205
  // Kobalte owns the open state attributes from here, and spread never removes an attribute it did not set, so a
168
206
  // tooltip that mounts open would keep the closed marker.
169
207
  ref.removeAttribute("data-closed")
170
- spread(ref, triggerProps, false, true)
208
+ const [, rest] = splitProps(triggerProps, ["onFocus", "onBlur", "onClick"])
209
+ spread(ref, rest, false, true)
171
210
 
172
211
  return null
173
212
  }
174
213
 
175
214
  return (
176
215
  <>
177
- {/* These listeners are attached before Kobalte's, so the machine sees each hover and focus first. */}
178
216
  <div
179
217
  ref={(el) => {
180
218
  ref = el
@@ -188,14 +226,23 @@ function ActiveTooltip(props: TooltipProps) {
188
226
  onPointerEnter={(event) => {
189
227
  if (event.pointerType !== "touch") dispatch(POINTER_ENTERED, event)
190
228
  }}
191
- onFocus={(event) => dispatch(FOCUSED, event)}
229
+ onFocusIn={(event) => {
230
+ if (!tabbing && (event.target !== ref || selectTooltipDismissed(untrack(tooltip)))) return
231
+ dispatch(FOCUSED)
232
+ handlers?.onFocus(event)
233
+ }}
234
+ onFocusOut={(event) => handlers?.onBlur(event)}
235
+ onClick={(event) => {
236
+ if (event instanceof PointerEvent && event.pointerType === "touch") return
237
+ handlers?.onClick(event)
238
+ }}
192
239
  >
193
240
  {local.children}
194
241
  </div>
195
242
  <Show when={selectTooltipMounted(tooltip())}>
196
243
  <Root
197
244
  gutter={4}
198
- openDelay={400}
245
+ openDelay={600}
199
246
  skipDelayDuration={300}
200
247
  {...others}
201
248
  closeDelay={0}
@@ -207,7 +254,6 @@ function ActiveTooltip(props: TooltipProps) {
207
254
  tooltip before Kobalte's listener ran, which then skipped the skip-delay window. */}
208
255
  <Trigger
209
256
  as={adopt}
210
- onPointerDown={() => dispatch(TRIGGER_PRESSED)}
211
257
  onPointerLeave={(event: PointerEvent) => {
212
258
  if (event.pointerType !== "touch") dispatch(POINTER_LEFT)
213
259
  }}