@opencode/ui 0.0.0-dev-703 → 0.0.0-dev-735

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.
@@ -0,0 +1,303 @@
1
+ /**
2
+ * Every tooltip trigger runs this machine, so it never allocates: each reachable state, event and transition is a
3
+ * frozen module singleton, and an event that changes nothing returns the state it was given.
4
+ */
5
+ export type TooltipState =
6
+ /** Kobalte is not mounted yet. Most triggers are never hovered, so it mounts on the first hover or focus. */
7
+ | { readonly status: "unmounted"; readonly disabled: boolean }
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 }
11
+ /**
12
+ * A popup the trigger opened (a menu, a popover) hides the tooltip. While it is open, a modal layer turns pointer
13
+ * events off for the page, so the trigger's own enter and leave events lie and `hovered` follows the pointer's
14
+ * position instead. Once it closes over a hovered trigger, the tooltip stays hidden until the pointer moves off the
15
+ * trigger or the trigger itself takes focus.
16
+ */
17
+ | { readonly status: "suppressed"; readonly popup: "open"; readonly hovered: boolean; readonly disabled: boolean }
18
+ | { readonly status: "suppressed"; readonly popup: "closed"; readonly disabled: boolean }
19
+ /** The caller sets `forceOpen`. `expanded` tracks the popup so releasing control can suppress the tooltip. */
20
+ | {
21
+ readonly status: "controlled"
22
+ readonly open: boolean
23
+ readonly hovered: boolean
24
+ readonly expanded: boolean
25
+ readonly disabled: boolean
26
+ }
27
+ | { readonly status: "disposed" }
28
+
29
+ export type TooltipEvent =
30
+ | { readonly type: "props.synced"; readonly forceOpen: boolean | undefined; readonly disabled: boolean }
31
+ /** The pointer entered or left the trigger. Touch never hovers. */
32
+ | { readonly type: "pointer.entered" }
33
+ | { readonly type: "pointer.left" }
34
+ /** Where a pointer move landed while a popup suppresses the tooltip, measured against the trigger's box. */
35
+ | { readonly type: "pointer.moved"; readonly inside: boolean }
36
+ /** The trigger element itself took focus. */
37
+ | { readonly type: "focused" }
38
+ | { readonly type: "trigger.pressed" }
39
+ /** The press ended, after the click it caused, if any. */
40
+ | { readonly type: "press.ended" }
41
+ /** The microtask after the pointer left, once Kobalte has seen the tooltip open and started its skip-delay window. */
42
+ | { readonly type: "leave.settled" }
43
+ | { readonly type: "popup.changed"; readonly expanded: boolean }
44
+ | { readonly type: "kobalte.changed"; readonly open: boolean }
45
+ | { readonly type: "disposed" }
46
+
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"
49
+
50
+ export type TooltipTransition = {
51
+ readonly state: TooltipState
52
+ readonly effects: readonly TooltipEffect[]
53
+ }
54
+
55
+ const settled = new Map<TooltipState, TooltipTransition>()
56
+
57
+ const NO_EFFECTS: readonly TooltipEffect[] = Object.freeze([])
58
+
59
+ function freeze<S extends TooltipState>(state: S) {
60
+ Object.freeze(state)
61
+ settled.set(state, Object.freeze({ state, effects: NO_EFFECTS }))
62
+
63
+ return state
64
+ }
65
+
66
+ const bit = (value: boolean) => (value ? 1 : 0)
67
+
68
+ const UNMOUNTED = [false, true].map((disabled) => freeze({ status: "unmounted", disabled }))
69
+
70
+ const IDLE = [0, 1, 2, 3].map((key) => freeze({ status: "idle", hovered: !!(key & 1), disabled: !!(key & 2) }))
71
+
72
+ const OPEN = [0, 1, 2, 3].map((key) => freeze({ status: "open", hovered: !!(key & 1), pressing: !!(key & 2) }))
73
+
74
+ const POPUP_OPEN = [0, 1, 2, 3].map((key) =>
75
+ freeze({ status: "suppressed", popup: "open", hovered: !!(key & 1), disabled: !!(key & 2) }),
76
+ )
77
+
78
+ const POPUP_CLOSED = [false, true].map((disabled) => freeze({ status: "suppressed", popup: "closed", disabled }))
79
+
80
+ const CONTROLLED = Array.from({ length: 16 }, (_, key) =>
81
+ freeze({
82
+ status: "controlled",
83
+ open: !!(key & 1),
84
+ hovered: !!(key & 2),
85
+ expanded: !!(key & 4),
86
+ disabled: !!(key & 8),
87
+ }),
88
+ )
89
+
90
+ const DISPOSED = freeze({ status: "disposed" })
91
+
92
+ const unmounted = (disabled: boolean) => UNMOUNTED[bit(disabled)]
93
+
94
+ const idle = (hovered: boolean, disabled: boolean) => IDLE[bit(hovered) | (bit(disabled) << 1)]
95
+
96
+ const open = (hovered: boolean, pressing: boolean) => OPEN[bit(hovered) | (bit(pressing) << 1)]
97
+
98
+ const popupOpen = (hovered: boolean, disabled: boolean) => POPUP_OPEN[bit(hovered) | (bit(disabled) << 1)]
99
+
100
+ const popupClosed = (disabled: boolean) => POPUP_CLOSED[bit(disabled)]
101
+
102
+ const controlled = (open: boolean, hovered: boolean, expanded: boolean, disabled: boolean) =>
103
+ CONTROLLED[bit(open) | (bit(hovered) << 1) | (bit(expanded) << 2) | (bit(disabled) << 3)]
104
+
105
+ const REPLAY: readonly TooltipEffect[] = Object.freeze(["replay"])
106
+
107
+ const REPLAY_HOVER: TooltipTransition = Object.freeze({ state: idle(true, false), effects: REPLAY })
108
+
109
+ const REPLAY_FOCUS: TooltipTransition = Object.freeze({ state: idle(false, false), effects: REPLAY })
110
+
111
+ const LEAVING: TooltipTransition = Object.freeze({
112
+ state: open(false, false),
113
+ effects: Object.freeze<TooltipEffect[]>(["schedule-leave"]),
114
+ })
115
+
116
+ export const POINTER_ENTERED: TooltipEvent = Object.freeze({ type: "pointer.entered" })
117
+
118
+ export const POINTER_LEFT: TooltipEvent = Object.freeze({ type: "pointer.left" })
119
+
120
+ export const POINTER_INSIDE: TooltipEvent = Object.freeze({ type: "pointer.moved", inside: true })
121
+
122
+ export const POINTER_OUTSIDE: TooltipEvent = Object.freeze({ type: "pointer.moved", inside: false })
123
+
124
+ export const FOCUSED: TooltipEvent = Object.freeze({ type: "focused" })
125
+
126
+ export const TRIGGER_PRESSED: TooltipEvent = Object.freeze({ type: "trigger.pressed" })
127
+
128
+ export const PRESS_ENDED: TooltipEvent = Object.freeze({ type: "press.ended" })
129
+
130
+ export const LEAVE_SETTLED: TooltipEvent = Object.freeze({ type: "leave.settled" })
131
+
132
+ export const POPUP_EXPANDED: TooltipEvent = Object.freeze({ type: "popup.changed", expanded: true })
133
+
134
+ export const POPUP_COLLAPSED: TooltipEvent = Object.freeze({ type: "popup.changed", expanded: false })
135
+
136
+ export const KOBALTE_OPENED: TooltipEvent = Object.freeze({ type: "kobalte.changed", open: true })
137
+
138
+ export const KOBALTE_CLOSED: TooltipEvent = Object.freeze({ type: "kobalte.changed", open: false })
139
+
140
+ export const DISPOSED_EVENT: TooltipEvent = Object.freeze({ type: "disposed" })
141
+
142
+ const PROPS = [undefined, false, true].flatMap((forceOpen) =>
143
+ [false, true].map((disabled): TooltipEvent => Object.freeze({ type: "props.synced", forceOpen, disabled })),
144
+ )
145
+
146
+ export function syncPropsEvent(forceOpen: boolean | undefined, disabled: boolean) {
147
+ return PROPS[(forceOpen === undefined ? 0 : forceOpen ? 4 : 2) + bit(disabled)]
148
+ }
149
+
150
+ export function createTooltipState(forceOpen: boolean | undefined, disabled: boolean): TooltipState {
151
+ if (forceOpen === undefined) return unmounted(disabled)
152
+
153
+ return controlled(forceOpen, false, false, disabled)
154
+ }
155
+
156
+ export function transitionTooltip(state: TooltipState, event: TooltipEvent): TooltipTransition {
157
+ const result = next(state, event)
158
+
159
+ if ("state" in result) return result
160
+
161
+ return settled.get(result)!
162
+ }
163
+
164
+ /** Kobalte is mounted: the trigger owns its listeners and the popup observer runs. */
165
+ export function selectTooltipMounted(state: TooltipState) {
166
+ return state.status !== "unmounted" && state.status !== "disposed"
167
+ }
168
+
169
+ export function selectTooltipOpen(state: TooltipState) {
170
+ if (state.status === "open") return true
171
+
172
+ if (state.status === "controlled") return state.open
173
+
174
+ return false
175
+ }
176
+
177
+ export function selectTooltipForceOpen(state: TooltipState) {
178
+ return state.status === "controlled" ? state.open : undefined
179
+ }
180
+
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
184
+ }
185
+
186
+ /** A popup suppresses the tooltip, so the driver reports where the pointer moves. */
187
+ export function selectTooltipTracking(state: TooltipState) {
188
+ return state.status === "suppressed"
189
+ }
190
+
191
+ function next(state: TooltipState, event: TooltipEvent): TooltipState | TooltipTransition {
192
+ if (state.status === "disposed") return state
193
+
194
+ if (event.type === "disposed") return DISPOSED
195
+
196
+ if (event.type === "props.synced") return sync(state, event.forceOpen, event.disabled)
197
+
198
+ if (state.status === "unmounted") {
199
+ if (state.disabled) return state
200
+
201
+ if (event.type === "pointer.entered") return REPLAY_HOVER
202
+
203
+ if (event.type === "focused") return REPLAY_FOCUS
204
+
205
+ return state
206
+ }
207
+
208
+ if (state.status === "idle") {
209
+ if (event.type === "pointer.entered") return idle(true, state.disabled)
210
+
211
+ if (event.type === "pointer.left") return idle(false, state.disabled)
212
+
213
+ if (event.type === "popup.changed" && event.expanded) return popupOpen(state.hovered, state.disabled)
214
+
215
+ if (event.type === "kobalte.changed" && event.open && !state.disabled) return open(state.hovered, false)
216
+
217
+ return state
218
+ }
219
+
220
+ if (state.status === "open") {
221
+ if (event.type === "pointer.entered") return open(true, state.pressing)
222
+
223
+ if (event.type === "pointer.left") return state.hovered || state.pressing ? LEAVING : state
224
+
225
+ if (event.type === "leave.settled") return state.hovered ? state : idle(false, false)
226
+
227
+ if (event.type === "trigger.pressed") return open(state.hovered, true)
228
+
229
+ if (event.type === "press.ended") return open(state.hovered, false)
230
+
231
+ if (event.type === "popup.changed" && event.expanded) return popupOpen(state.hovered, false)
232
+
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
+ }
237
+
238
+ return state
239
+ }
240
+
241
+ if (state.status === "suppressed") {
242
+ if (event.type === "popup.changed" && event.expanded) {
243
+ return popupOpen(state.popup === "closed" || state.hovered, state.disabled)
244
+ }
245
+
246
+ if (state.popup === "open") {
247
+ if (event.type === "pointer.moved") return popupOpen(event.inside, state.disabled)
248
+
249
+ if (event.type === "popup.changed") {
250
+ return state.hovered ? popupClosed(state.disabled) : idle(false, state.disabled)
251
+ }
252
+
253
+ return state
254
+ }
255
+
256
+ if (event.type === "pointer.moved" && !event.inside) return idle(false, state.disabled)
257
+
258
+ if (event.type === "focused") return idle(true, state.disabled)
259
+
260
+ return state
261
+ }
262
+
263
+ if (event.type === "pointer.entered") return controlled(state.open, true, state.expanded, state.disabled)
264
+
265
+ if (event.type === "pointer.left") return controlled(state.open, false, state.expanded, state.disabled)
266
+
267
+ if (event.type === "popup.changed") return controlled(state.open, state.hovered, event.expanded, state.disabled)
268
+
269
+ return state
270
+ }
271
+
272
+ function sync(state: TooltipState, forceOpen: boolean | undefined, disabled: boolean) {
273
+ if (state.status === "disposed") return state
274
+
275
+ if (forceOpen !== undefined) {
276
+ if (state.status === "unmounted") return controlled(forceOpen, false, false, disabled)
277
+
278
+ if (state.status === "controlled") return controlled(forceOpen, state.hovered, state.expanded, disabled)
279
+
280
+ if (state.status === "suppressed" && state.popup === "open") {
281
+ return controlled(forceOpen, state.hovered, true, disabled)
282
+ }
283
+
284
+ if (state.status === "suppressed") return controlled(forceOpen, true, false, disabled)
285
+
286
+ return controlled(forceOpen, state.hovered, false, disabled)
287
+ }
288
+
289
+ if (state.status === "unmounted") return unmounted(disabled)
290
+
291
+ if (state.status === "idle") return idle(state.hovered, disabled)
292
+
293
+ if (state.status === "open") return disabled ? idle(state.hovered, true) : state
294
+
295
+ if (state.status === "suppressed" && state.popup === "open") return popupOpen(state.hovered, disabled)
296
+
297
+ if (state.status === "suppressed") return popupClosed(disabled)
298
+
299
+ // Releasing control closes the tooltip; it opens again on the next hover or focus.
300
+ if (state.expanded) return popupOpen(state.hovered, disabled)
301
+
302
+ return idle(state.hovered, disabled)
303
+ }
@@ -1,8 +1,32 @@
1
1
  import { Content, Portal, Root, Trigger } from "@kobalte/core/tooltip"
2
- import { createComputed, createEffect, Match, onCleanup, Show, splitProps, Switch, untrack, type JSX } from "solid-js"
2
+ import { createComputed, createSignal, onCleanup, onMount, Show, splitProps, untrack, type JSX } from "solid-js"
3
3
  import type { ComponentProps } from "solid-js"
4
- import { createStore } from "solid-js/store"
5
4
  import { spread } from "solid-js/web"
5
+ import {
6
+ createTooltipState,
7
+ DISPOSED_EVENT,
8
+ FOCUSED,
9
+ KOBALTE_CLOSED,
10
+ KOBALTE_OPENED,
11
+ LEAVE_SETTLED,
12
+ POINTER_ENTERED,
13
+ POINTER_INSIDE,
14
+ POINTER_LEFT,
15
+ POINTER_OUTSIDE,
16
+ POPUP_COLLAPSED,
17
+ POPUP_EXPANDED,
18
+ PRESS_ENDED,
19
+ selectTooltipForceOpen,
20
+ selectTooltipMounted,
21
+ selectTooltipOpen,
22
+ selectTooltipPressing,
23
+ selectTooltipTracking,
24
+ syncPropsEvent,
25
+ transitionTooltip,
26
+ TRIGGER_PRESSED,
27
+ type TooltipEvent,
28
+ type TooltipState,
29
+ } from "./machine"
6
30
  import "./tooltip.css"
7
31
 
8
32
  export interface TooltipProps extends ComponentProps<typeof Root> {
@@ -21,19 +45,21 @@ type TriggerHandlers = {
21
45
  onFocus?: (event: FocusEvent) => void
22
46
  }
23
47
 
48
+ // Expanded panels still need tooltips; only an open popup suppresses its trigger's tooltip.
49
+ const POPUP = '[aria-haspopup]:not([aria-haspopup="false"]):is([aria-expanded="true"], [data-expanded])'
50
+
24
51
  export function Tooltip(props: TooltipProps) {
52
+ return (
53
+ <Show when={!props.inactive} fallback={props.children}>
54
+ <ActiveTooltip {...props} />
55
+ </Show>
56
+ )
57
+ }
58
+
59
+ function ActiveTooltip(props: TooltipProps) {
25
60
  let ref: HTMLDivElement | undefined
26
61
  let handlers: TriggerHandlers | undefined
27
-
28
- const [state, setState] = createStore({
29
- open: false,
30
- block: false,
31
- expand: false,
32
- // Kobalte's tooltip costs dozens of computations and listeners per trigger, and most triggers (an action on every
33
- // timeline row, a copy button on every code block) are never hovered. It mounts on the first interaction that
34
- // could open it, or as soon as a caller controls it with forceOpen.
35
- mounted: false,
36
- })
62
+ let observer: MutationObserver | undefined
37
63
 
38
64
  const [local, others] = splitProps(props, [
39
65
  "children",
@@ -48,80 +74,89 @@ export function Tooltip(props: TooltipProps) {
48
74
  "value",
49
75
  ])
50
76
 
51
- const close = () => setState("open", false)
52
- const controlled = () => local.forceOpen !== undefined
77
+ // Kobalte's tooltip costs dozens of computations and listeners per trigger, and most triggers (an action on every
78
+ // timeline row, a copy button on every code block) are never hovered. It mounts on the first interaction that could
79
+ // open it, or as soon as a caller controls it with forceOpen.
80
+ const [tooltip, setTooltip] = createSignal(createTooltipState(local.forceOpen, !!others.disabled))
53
81
 
54
- const inside = () => {
55
- const active = document.activeElement
82
+ const dispatch = (event: TooltipEvent, source?: PointerEvent | FocusEvent) => {
83
+ const previous = untrack(tooltip)
84
+ const transition = transitionTooltip(previous, event)
56
85
 
57
- if (!ref || !active) return false
86
+ if (transition.state !== previous) {
87
+ setTooltip(transition.state)
88
+ reconcile(previous, transition.state)
89
+ }
90
+
91
+ for (const effect of transition.effects) {
92
+ if (effect === "schedule-leave") queueMicrotask(settleLeave)
93
+
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)
58
96
 
59
- return ref.contains(active)
97
+ if (effect === "replay" && source instanceof FocusEvent) handlers?.onFocus?.(source)
98
+ }
60
99
  }
61
100
 
62
- const drop = (expand = state.expand) => {
63
- if (expand || !state.block) return
101
+ // Kobalte's own pointerleave handler runs after this one and only starts the skip-delay window for neighbouring
102
+ // tooltips if it still sees this one open.
103
+ const settleLeave = () => dispatch(LEAVE_SETTLED)
104
+ 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)
64
107
 
65
- if (ref?.matches(":hover")) return
108
+ const onPointerMove = (event: PointerEvent) => {
109
+ if (!ref) return
110
+ const box = ref.getBoundingClientRect()
66
111
 
67
- if (inside()) return
68
- setState("block", false)
69
- }
112
+ const inside =
113
+ event.clientX >= box.left && event.clientX <= box.right && event.clientY >= box.top && event.clientY <= box.bottom
70
114
 
71
- const sync = () => {
72
- // Expanded panels still need tooltips; only an open popup suppresses its trigger's tooltip.
73
- const expand = !!ref?.querySelector(
74
- '[aria-haspopup]:not([aria-haspopup="false"]):is([aria-expanded="true"], [data-expanded])',
75
- )
115
+ dispatch(inside ? POINTER_INSIDE : POINTER_OUTSIDE)
116
+ }
76
117
 
77
- setState("expand", expand)
118
+ // Attaches the listeners and observer the state needs and detaches the rest.
119
+ const reconcile = (previous: TooltipState, state: TooltipState) => {
120
+ const pressing = selectTooltipPressing(state)
78
121
 
79
- if (expand) {
80
- setState("block", true)
81
- close()
122
+ if (pressing && !selectTooltipPressing(previous)) {
123
+ window.addEventListener("pointerup", onPressEnd, true)
124
+ window.addEventListener("pointercancel", onPressEnd, true)
125
+ }
82
126
 
83
- return
127
+ if (!pressing && selectTooltipPressing(previous)) {
128
+ window.removeEventListener("pointerup", onPressEnd, true)
129
+ window.removeEventListener("pointercancel", onPressEnd, true)
84
130
  }
85
131
 
86
- drop(expand)
87
- }
132
+ const tracking = selectTooltipTracking(state)
88
133
 
89
- const leave = () => {
90
- // Kobalte's own pointerleave handler runs after this one and only starts the
91
- // skip-delay window for neighbouring tooltips if it still sees this one open.
92
- queueMicrotask(() => {
93
- if (!inside()) close()
94
- drop()
95
- })
96
- }
134
+ if (tracking && !selectTooltipTracking(previous)) {
135
+ window.addEventListener("pointermove", onPointerMove, { capture: true, passive: true })
136
+ }
97
137
 
98
- const mount = (event: PointerEvent | FocusEvent) => {
99
- if (state.mounted) return
100
- setState("mounted", true)
138
+ if (!tracking && selectTooltipTracking(previous)) window.removeEventListener("pointermove", onPointerMove, true)
101
139
 
102
- // Kobalte attached its listeners while this event was being dispatched, too late to receive it.
103
- if (event instanceof PointerEvent) handlers?.onPointerEnter?.(event)
104
- else handlers?.onFocus?.(event)
105
- }
140
+ if (state.status === "disposed") {
141
+ observer?.disconnect()
106
142
 
107
- createComputed(() => {
108
- if (controlled()) setState("mounted", true)
109
- })
143
+ return
144
+ }
110
145
 
111
- createEffect(() => {
112
- if (!ref || !state.mounted) return
113
- untrack(sync)
114
- const obs = new MutationObserver(sync)
115
- obs.observe(ref, {
146
+ if (!ref || observer || !selectTooltipMounted(state)) return
147
+ observer = new MutationObserver(syncPopup)
148
+ observer.observe(ref, {
116
149
  subtree: true,
117
150
  childList: true,
118
151
  attributes: true,
119
152
  attributeFilter: ["aria-expanded", "data-expanded", "aria-haspopup"],
120
153
  })
121
- onCleanup(() => obs.disconnect())
122
- })
154
+ syncPopup()
155
+ }
123
156
 
124
- let justClickedTrigger = false
157
+ createComputed(() => dispatch(syncPropsEvent(local.forceOpen, !!others.disabled)))
158
+ onMount(() => reconcile(untrack(tooltip), untrack(tooltip)))
159
+ onCleanup(() => dispatch(DISPOSED_EVENT))
125
160
 
126
161
  // Kobalte's trigger takes over the element rendered below instead of rendering one, so mounting the tooltip leaves
127
162
  // the trigger and its children in place, with their focus and hover.
@@ -138,78 +173,70 @@ export function Tooltip(props: TooltipProps) {
138
173
  }
139
174
 
140
175
  return (
141
- <Switch>
142
- <Match when={local.inactive}>{local.children}</Match>
143
- <Match when={true}>
144
- <div
145
- ref={(el) => {
146
- ref = el
147
- // Kobalte's trigger sets this on the element it renders; it sets it again here once it mounts.
148
- el.setAttribute("type", "button")
149
- }}
150
- data-closed=""
151
- tabIndex={local.triggerTabIndex}
152
- data-component="tooltip-v2-trigger"
153
- class={local.class}
154
- onPointerEnter={mount}
155
- onFocus={mount}
156
- onFocusOut={() => requestAnimationFrame(() => drop())}
176
+ <>
177
+ {/* These listeners are attached before Kobalte's, so the machine sees each hover and focus first. */}
178
+ <div
179
+ ref={(el) => {
180
+ ref = el
181
+ // Kobalte's trigger sets this on the element it renders; it sets it again here once it mounts.
182
+ el.setAttribute("type", "button")
183
+ }}
184
+ data-closed=""
185
+ tabIndex={local.triggerTabIndex}
186
+ data-component="tooltip-v2-trigger"
187
+ class={local.class}
188
+ onPointerEnter={(event) => {
189
+ if (event.pointerType !== "touch") dispatch(POINTER_ENTERED, event)
190
+ }}
191
+ onFocus={(event) => dispatch(FOCUSED, event)}
192
+ >
193
+ {local.children}
194
+ </div>
195
+ <Show when={selectTooltipMounted(tooltip())}>
196
+ <Root
197
+ gutter={4}
198
+ openDelay={400}
199
+ skipDelayDuration={300}
200
+ {...others}
201
+ closeDelay={0}
202
+ ignoreSafeArea={local.ignoreSafeArea ?? true}
203
+ open={selectTooltipOpen(tooltip())}
204
+ onOpenChange={(open) => dispatch(open ? KOBALTE_OPENED : KOBALTE_CLOSED)}
157
205
  >
158
- {local.children}
159
- </div>
160
- <Show when={state.mounted}>
161
- <Root
162
- gutter={4}
163
- openDelay={400}
164
- skipDelayDuration={300}
165
- {...others}
166
- closeDelay={0}
167
- ignoreSafeArea={local.ignoreSafeArea ?? true}
168
- open={controlled() ? local.forceOpen : state.open}
169
- onOpenChange={(open) => {
170
- if (controlled()) return
171
-
172
- if (state.block && open) return
173
-
174
- if (justClickedTrigger) {
175
- justClickedTrigger = false
176
-
177
- return
178
- }
179
-
180
- setState("open", open)
206
+ {/* Kobalte calls these inside its own listeners. As separate listeners, the leave microtask would close the
207
+ tooltip before Kobalte's listener ran, which then skipped the skip-delay window. */}
208
+ <Trigger
209
+ as={adopt}
210
+ onPointerDown={() => dispatch(TRIGGER_PRESSED)}
211
+ onPointerLeave={(event: PointerEvent) => {
212
+ if (event.pointerType !== "touch") dispatch(POINTER_LEFT)
181
213
  }}
182
- >
183
- {/* Kobalte calls leave inside its own pointerleave listener. As a separate listener, its microtask would
184
- close the tooltip before Kobalte's listener ran, which then skipped the skip-delay window. */}
185
- <Trigger as={adopt} onPointerLeave={leave} />
186
- <Portal>
187
- <Content
188
- ref={(el) => {
189
- const theme = ref?.closest("[data-theme]")?.getAttribute("data-theme")
190
-
191
- if (theme) el.setAttribute("data-theme", theme)
192
- }}
193
- data-component="tooltip-v2"
194
- data-appearance={local.appearance ?? "compact"}
195
- data-placement={props.placement}
196
- data-force-open={local.forceOpen}
197
- class={local.contentClass}
198
- style={local.contentStyle}
199
- onPointerDownOutside={(e) => {
200
- if (ref === e.target || (e.target instanceof Node && ref?.contains(e.target))) {
201
- justClickedTrigger = true
202
- }
203
-
204
- e.preventDefault()
205
- }}
206
- >
207
- {local.value}
208
- </Content>
209
- </Portal>
210
- </Root>
211
- </Show>
212
- </Match>
213
- </Switch>
214
+ />
215
+ <Portal>
216
+ <Content
217
+ ref={(el) => {
218
+ const theme = ref?.closest("[data-theme]")?.getAttribute("data-theme")
219
+
220
+ if (theme) el.setAttribute("data-theme", theme)
221
+ }}
222
+ data-component="tooltip-v2"
223
+ data-appearance={local.appearance ?? "compact"}
224
+ data-placement={props.placement}
225
+ data-force-open={selectTooltipForceOpen(tooltip())}
226
+ class={local.contentClass}
227
+ style={local.contentStyle}
228
+ onPointerDownOutside={preventDismiss}
229
+ >
230
+ {local.value}
231
+ </Content>
232
+ </Portal>
233
+ </Root>
234
+ </Show>
235
+ </>
214
236
  )
215
237
  }
238
+
239
+ // A press on the trigger is outside the tooltip's layer; the machine decides whether it closes the tooltip.
240
+ function preventDismiss(event: Event) {
241
+ event.preventDefault()
242
+ }