@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.
- package/dist/components/image-flight-machine.d.ts +135 -0
- package/dist/components/image-zoom-machine.d.ts +95 -0
- package/dist/components/image-zoom.d.ts +6 -3
- package/dist/overlays/tooltip/machine.d.ts +146 -0
- package/package.json +3 -3
- package/src/components/image-flight-machine.ts +247 -0
- package/src/components/image-preview.css +5 -2
- package/src/components/image-preview.tsx +390 -252
- package/src/components/image-zoom-machine.ts +191 -0
- package/src/components/image-zoom.ts +22 -14
- package/src/overlays/tooltip/machine.ts +303 -0
- package/src/overlays/tooltip/tooltip.tsx +160 -133
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/** The element a pan captures the pointer on: the preview's body for an image, the borrowed video for a video. */
|
|
2
|
+
export type PanTarget = "viewport" | "video"
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The press under way. A `backdrop-press` began on the backdrop around the image and stayed put, so the click that ends
|
|
6
|
+
* it closes the preview. A pan captures the pointer, so its click lands on the backdrop wherever it began: `onBackdrop`
|
|
7
|
+
* says whether it began there, and `moved` whether it has moved, so the click that ends a moved pan neither closes the
|
|
8
|
+
* preview nor plays the video.
|
|
9
|
+
*/
|
|
10
|
+
export type PreviewZoomGesture =
|
|
11
|
+
| { readonly type: "idle" }
|
|
12
|
+
| { readonly type: "backdrop-press" }
|
|
13
|
+
| {
|
|
14
|
+
readonly type: "panning"
|
|
15
|
+
readonly pointerId: number
|
|
16
|
+
readonly target: PanTarget
|
|
17
|
+
readonly onBackdrop: boolean
|
|
18
|
+
readonly moved: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* `gliding` is an eased zoom under way on the frame loop. Scale and offsets live on springs in the driver, not here: a
|
|
23
|
+
* frame of a glide or a pan writes no state. A press does not stop a glide, so a third click of a double-click zoom
|
|
24
|
+
* leaves it running; the hand moving does.
|
|
25
|
+
*/
|
|
26
|
+
export type PreviewZoomState =
|
|
27
|
+
| { readonly status: "still" | "gliding"; readonly gesture: PreviewZoomGesture }
|
|
28
|
+
| { readonly status: "disposed" }
|
|
29
|
+
|
|
30
|
+
/** `zoomed` is whether the image shows above fit once the event's change applies. */
|
|
31
|
+
export type PreviewZoomEvent =
|
|
32
|
+
| {
|
|
33
|
+
readonly type: "pointer.down"
|
|
34
|
+
readonly pointerId: number
|
|
35
|
+
readonly button: number
|
|
36
|
+
/** `content` is anywhere in the preview but its body, such as the band around it or the controls. */
|
|
37
|
+
readonly target: PanTarget | "content"
|
|
38
|
+
readonly onBackdrop: boolean
|
|
39
|
+
readonly zoomed: boolean
|
|
40
|
+
}
|
|
41
|
+
| { readonly type: "pointer.moved"; readonly pointerId: number }
|
|
42
|
+
| { readonly type: "pointer.up"; readonly pointerId: number }
|
|
43
|
+
/** `pointercancel`, `lostpointercapture`, or a move with the primary button already up: the pan ends unreleased. */
|
|
44
|
+
| { readonly type: "pointer.cancel"; readonly pointerId: number }
|
|
45
|
+
| { readonly type: "backdrop.click"; readonly onBackdrop: boolean }
|
|
46
|
+
/** Pinch, wheel, drag or a resize set the view directly. */
|
|
47
|
+
| { readonly type: "follow"; readonly zoomed: boolean }
|
|
48
|
+
| { readonly type: "glide.start" }
|
|
49
|
+
| { readonly type: "glide.settled"; readonly zoomed: boolean }
|
|
50
|
+
| { readonly type: "disposed" }
|
|
51
|
+
|
|
52
|
+
export type PreviewZoomEffect =
|
|
53
|
+
| { readonly type: "pointer.capture"; readonly target: PanTarget; readonly pointerId: number }
|
|
54
|
+
| { readonly type: "pointer.release"; readonly target: PanTarget; readonly pointerId: number }
|
|
55
|
+
| { readonly type: "glide.loop.start" }
|
|
56
|
+
| { readonly type: "glide.loop.stop" }
|
|
57
|
+
/** Swallows the click that ends a moved pan over the video, so it does not play or pause it. */
|
|
58
|
+
| { readonly type: "video.suppress-click" }
|
|
59
|
+
| { readonly type: "dialog.close" }
|
|
60
|
+
|
|
61
|
+
export type PreviewZoomTransition = {
|
|
62
|
+
readonly state: PreviewZoomState
|
|
63
|
+
readonly effects: readonly PreviewZoomEffect[]
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const IDLE: PreviewZoomGesture = Object.freeze({ type: "idle" })
|
|
67
|
+
|
|
68
|
+
const BACKDROP_PRESS: PreviewZoomGesture = Object.freeze({ type: "backdrop-press" })
|
|
69
|
+
|
|
70
|
+
const NONE: readonly PreviewZoomEffect[] = Object.freeze([])
|
|
71
|
+
|
|
72
|
+
export const INITIAL_PREVIEW_ZOOM_STATE: PreviewZoomState = Object.freeze({ status: "still", gesture: IDLE })
|
|
73
|
+
|
|
74
|
+
const DISPOSED: PreviewZoomState = Object.freeze({ status: "disposed" })
|
|
75
|
+
|
|
76
|
+
export function transitionPreviewZoom(state: PreviewZoomState, event: PreviewZoomEvent): PreviewZoomTransition {
|
|
77
|
+
if (state.status === "disposed") return unchanged(state)
|
|
78
|
+
const gliding = state.status === "gliding"
|
|
79
|
+
const pan = state.gesture.type === "panning" ? state.gesture : undefined
|
|
80
|
+
|
|
81
|
+
if (event.type === "disposed") {
|
|
82
|
+
return { state: DISPOSED, effects: [...release(pan), ...(gliding ? [{ type: "glide.loop.stop" } as const] : [])] }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (event.type === "pointer.down") {
|
|
86
|
+
// A second pointer leaves the pan alone; the same pointer pressing again means its release never arrived.
|
|
87
|
+
if (pan && pan.pointerId !== event.pointerId) return unchanged(state)
|
|
88
|
+
|
|
89
|
+
if (event.button === 0 && event.zoomed && event.target !== "content") {
|
|
90
|
+
return {
|
|
91
|
+
state: {
|
|
92
|
+
status: state.status,
|
|
93
|
+
gesture: {
|
|
94
|
+
type: "panning",
|
|
95
|
+
pointerId: event.pointerId,
|
|
96
|
+
target: event.target,
|
|
97
|
+
onBackdrop: event.onBackdrop,
|
|
98
|
+
moved: false,
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
effects: [{ type: "pointer.capture", target: event.target, pointerId: event.pointerId }],
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return gesture(state, event.button === 0 && event.onBackdrop ? BACKDROP_PRESS : IDLE)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (event.type === "pointer.moved") {
|
|
109
|
+
if (pan?.pointerId !== event.pointerId || pan.moved) return unchanged(state)
|
|
110
|
+
|
|
111
|
+
return gesture(state, { ...pan, onBackdrop: false, moved: true })
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (event.type === "pointer.up") {
|
|
115
|
+
if (pan?.pointerId !== event.pointerId) return unchanged(state)
|
|
116
|
+
|
|
117
|
+
if (pan.moved && pan.target === "video") {
|
|
118
|
+
return { state: { status: state.status, gesture: IDLE }, effects: [{ type: "video.suppress-click" }] }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return gesture(state, !pan.moved && pan.onBackdrop ? BACKDROP_PRESS : IDLE)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
if (event.type === "pointer.cancel") {
|
|
125
|
+
if (pan?.pointerId !== event.pointerId) return unchanged(state)
|
|
126
|
+
|
|
127
|
+
return { state: { status: state.status, gesture: IDLE }, effects: release(pan) }
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (event.type === "backdrop.click") {
|
|
131
|
+
if (state.gesture.type !== "backdrop-press") return unchanged(state)
|
|
132
|
+
|
|
133
|
+
return {
|
|
134
|
+
state: { status: state.status, gesture: IDLE },
|
|
135
|
+
effects: event.onBackdrop ? [{ type: "dialog.close" }] : NONE,
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// An eased zoom takes over from a pan, so a pan never outlives a zoom back to fit.
|
|
140
|
+
if (event.type === "glide.start") {
|
|
141
|
+
if (gliding && !pan) return unchanged(state)
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
state: { status: "gliding", gesture: pan ? IDLE : state.gesture },
|
|
145
|
+
effects: [...release(pan), ...(gliding ? [] : [{ type: "glide.loop.start" } as const])],
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (event.type === "glide.settled" && !gliding) return unchanged(state)
|
|
150
|
+
// The hand takes over from an eased zoom; a pan ends once the image is back at fit.
|
|
151
|
+
const ends = pan && !event.zoomed ? pan : undefined
|
|
152
|
+
|
|
153
|
+
if (!gliding && !ends) return unchanged(state)
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
state: { status: "still", gesture: ends ? IDLE : state.gesture },
|
|
157
|
+
effects: [...(gliding ? [{ type: "glide.loop.stop" } as const] : []), ...release(ends)],
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Whether a pan holds the pointer, which shows the grabbing cursor. */
|
|
162
|
+
export function selectDragging(state: PreviewZoomState) {
|
|
163
|
+
return state.status !== "disposed" && state.gesture.type === "panning"
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Whether `pointerId` is the pointer the pan under way follows. */
|
|
167
|
+
export function selectPanPointer(state: PreviewZoomState, pointerId: number) {
|
|
168
|
+
return state.status !== "disposed" && state.gesture.type === "panning" && state.gesture.pointerId === pointerId
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function gesture(state: Exclude<PreviewZoomState, { status: "disposed" }>, next: PreviewZoomGesture) {
|
|
172
|
+
if (state.gesture === next) return unchanged(state)
|
|
173
|
+
|
|
174
|
+
return { state: { status: state.status, gesture: next }, effects: NONE }
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function release(pan: Extract<PreviewZoomGesture, { type: "panning" }> | undefined): readonly PreviewZoomEffect[] {
|
|
178
|
+
if (!pan) return NONE
|
|
179
|
+
|
|
180
|
+
return [{ type: "pointer.release", target: pan.target, pointerId: pan.pointerId }]
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Every pointer move of a pan dispatches events that change nothing; answering them with the transition already built
|
|
184
|
+
// for that state keeps the hot path allocation-free.
|
|
185
|
+
let last: PreviewZoomTransition = Object.freeze({ state: DISPOSED, effects: NONE })
|
|
186
|
+
|
|
187
|
+
function unchanged(state: PreviewZoomState) {
|
|
188
|
+
if (last.state !== state) last = Object.freeze({ state, effects: NONE })
|
|
189
|
+
|
|
190
|
+
return last
|
|
191
|
+
}
|
|
@@ -59,26 +59,25 @@ export function detailScale(fitRatio: number) {
|
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
/**
|
|
62
|
-
* The `object-view-box` that shows
|
|
63
|
-
* as a video under its controls. The inset is in the media's natural pixels.
|
|
62
|
+
* The `object-view-box` that shows the view at `scale`, `x` and `y` of media fitted at `size`, for media that must zoom
|
|
63
|
+
* inside its own box, such as a video under its controls. The inset is in the media's natural pixels. It takes the view
|
|
64
|
+
* as numbers, so painting a frame of a glide allocates nothing but the string.
|
|
64
65
|
*/
|
|
65
66
|
export function viewBox(
|
|
66
|
-
|
|
67
|
+
scale: number,
|
|
68
|
+
x: number,
|
|
69
|
+
y: number,
|
|
67
70
|
size: { width: number; height: number },
|
|
68
71
|
natural: { width: number; height: number },
|
|
69
72
|
) {
|
|
70
|
-
if (
|
|
73
|
+
if (scale <= 1) return "none"
|
|
71
74
|
|
|
72
|
-
const
|
|
75
|
+
const top = px(edge(y, size.height, scale) * natural.height)
|
|
76
|
+
const right = px(edge(-x, size.width, scale) * natural.width)
|
|
77
|
+
const bottom = px(edge(-y, size.height, scale) * natural.height)
|
|
78
|
+
const left = px(edge(x, size.width, scale) * natural.width)
|
|
73
79
|
|
|
74
|
-
|
|
75
|
-
edge(view.y, size.height) * natural.height,
|
|
76
|
-
edge(-view.x, size.width) * natural.width,
|
|
77
|
-
edge(-view.y, size.height) * natural.height,
|
|
78
|
-
edge(view.x, size.width) * natural.width,
|
|
79
|
-
]
|
|
80
|
-
|
|
81
|
-
return `inset(${inset.map((value) => `${Math.max(0, Math.round(value * 100) / 100)}px`).join(" ")})`
|
|
80
|
+
return `inset(${top} ${right} ${bottom} ${left})`
|
|
82
81
|
}
|
|
83
82
|
|
|
84
83
|
/** The zoom factor for one wheel event, bounded so a coarse mouse-wheel notch is a step rather than a jump. */
|
|
@@ -86,13 +85,22 @@ export function wheelZoomFactor(deltaY: number) {
|
|
|
86
85
|
return Math.exp(-clamp(deltaY, -30, 30) / 100)
|
|
87
86
|
}
|
|
88
87
|
|
|
89
|
-
|
|
88
|
+
/** Keeps one axis of a zoomed image of `size` covering a `viewport` it overflows, and centered in one it fits. */
|
|
89
|
+
export function clampAxis(offset: number, size: number, viewport: number) {
|
|
90
90
|
const limit = Math.max(0, (size - viewport) / 2)
|
|
91
91
|
|
|
92
92
|
// Adding zero turns -0 into 0.
|
|
93
93
|
return clamp(offset, -limit, limit) + 0
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
+
function edge(offset: number, fitted: number, scale: number) {
|
|
97
|
+
return 0.5 - 0.5 / scale - offset / (scale * fitted)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function px(value: number) {
|
|
101
|
+
return `${Math.max(0, Math.round(value * 100) / 100)}px`
|
|
102
|
+
}
|
|
103
|
+
|
|
96
104
|
function clamp(value: number, min: number, max: number) {
|
|
97
105
|
return Math.min(Math.max(value, min), max)
|
|
98
106
|
}
|
|
@@ -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
|
+
}
|