@opencode/ui 0.0.0-dev-724 → 0.0.0-dev-750

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.
@@ -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
+ }