@stacksjs/components 0.2.917 → 0.2.921
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/RenderSurface-z9zr4d75.stx +137 -0
- package/dist/{Tooltip-1cnzascv.stx → Tooltip-e3g8qntr.stx} +49 -30
- package/dist/index.d.ts +1 -0
- package/dist/index.js +23 -23
- package/dist/render-surface/render-surface.js +145 -0
- package/dist/ui/render-surface/RenderSurface.d.ts +45 -0
- package/dist/ui/render-surface/index.d.ts +17 -0
- package/dist/ui/render-surface/render-surface.d.ts +94 -0
- package/dist/ui/tooltip/Tooltip.d.ts +2 -1
- package/dist/ui/tooltip/index.d.ts +1 -0
- package/dist/utils/tooltip-position.d.ts +2 -0
- package/package.json +8 -3
- package/src/index.ts +1 -0
- package/src/ui/render-surface/README.md +69 -0
- package/src/ui/render-surface/RenderSurface.stx +137 -0
- package/src/ui/render-surface/index.ts +27 -0
- package/src/ui/render-surface/render-surface.ts +244 -0
- package/src/ui/tooltip/Tooltip.stx +49 -30
- package/src/ui/tooltip/index.ts +2 -0
- package/src/utils/tooltip-position.ts +52 -2
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Frames for a `<RenderSurface>`: a box shared TypeScript draws into, frame
|
|
3
|
+
* by frame, the same on a phone and in a browser (stacksjs/stx#2108).
|
|
4
|
+
*
|
|
5
|
+
* A frame is `width * height` 0xAARRGGBB words, row by row: what a CPU
|
|
6
|
+
* graphics core produces. On a native screen `presentFrame` hands them to the
|
|
7
|
+
* host's own `craft.renderSurface.present`, which copies them where the script
|
|
8
|
+
* runs and draws them with Metal (craft-native/craft#578). In a browser the
|
|
9
|
+
* component's `<canvas>` takes them. Either way the frame is scaled to the
|
|
10
|
+
* box, `nearest` or `linear`, so a game can draw below the display's
|
|
11
|
+
* resolution.
|
|
12
|
+
*
|
|
13
|
+
* import { startRenderLoop } from '@stacksjs/components/render-surface'
|
|
14
|
+
*
|
|
15
|
+
* <RenderSurface class="flex-1" @ready="loop = startRenderLoop($event.surface, paint)"
|
|
16
|
+
* @restored="loop.wake()" @error="report($event)" />
|
|
17
|
+
*
|
|
18
|
+
* This module never defines `craft.renderSurface`. The host installs it
|
|
19
|
+
* before the screen's script runs, and it is a function rather than a
|
|
20
|
+
* message: a frame sent as a message would be serialized on every frame.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type RenderSurfaceFilter = 'nearest' | 'linear'
|
|
24
|
+
|
|
25
|
+
/** What `ready`, `resize` and `restored` carry: the box in points, and in pixels. */
|
|
26
|
+
export interface RenderSurfaceMetrics {
|
|
27
|
+
/** The surface's name: what `presentFrame` is given. */
|
|
28
|
+
surface: string
|
|
29
|
+
width: number
|
|
30
|
+
height: number
|
|
31
|
+
scale: number
|
|
32
|
+
pixelWidth: number
|
|
33
|
+
pixelHeight: number
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** What `error` carries. `UNAVAILABLE`: this device cannot draw one. `DUPLICATE`: the name is taken. */
|
|
37
|
+
export interface RenderSurfaceError {
|
|
38
|
+
surface: string
|
|
39
|
+
code: 'UNAVAILABLE' | 'DUPLICATE' | string
|
|
40
|
+
message: string
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Where a frame goes: the host's surface, a canvas, or nowhere. */
|
|
44
|
+
export type RenderSurfaceHost = 'native' | 'canvas' | 'none'
|
|
45
|
+
|
|
46
|
+
/** The largest frame, in pixels a side, that a host takes. */
|
|
47
|
+
export const RENDER_SURFACE_MAX_SIDE = 8192
|
|
48
|
+
|
|
49
|
+
interface CanvasSurface {
|
|
50
|
+
canvas: HTMLCanvasElement
|
|
51
|
+
context: CanvasRenderingContext2D
|
|
52
|
+
opaque: boolean
|
|
53
|
+
image: ImageData | null
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface NativeRenderSurface {
|
|
57
|
+
present: (surface: string, pixels: Uint32Array, width: number, height: number) => boolean
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
type AnyGlobal = typeof globalThis & {
|
|
61
|
+
craft?: { renderSurface?: NativeRenderSurface, capabilities?: Record<string, boolean> }
|
|
62
|
+
stx?: { _host?: { isNative?: boolean } }
|
|
63
|
+
__stxRenderSurfaces?: Map<string, CanvasSurface>
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const root = globalThis as AnyGlobal
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* The page's canvases, by name. Kept on the global: the component's own copy
|
|
70
|
+
* of this module and an app's import of `@stacksjs/components/render-surface`
|
|
71
|
+
* are two modules, and one name has to mean one canvas to both.
|
|
72
|
+
*/
|
|
73
|
+
function canvases(): Map<string, CanvasSurface> {
|
|
74
|
+
root.__stxRenderSurfaces ??= new Map()
|
|
75
|
+
return root.__stxRenderSurfaces
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function surfaceError(code: string, message: string): Error & { code: string } {
|
|
79
|
+
return Object.assign(new Error(message), { code })
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function onNativeScreen(): boolean {
|
|
83
|
+
return Boolean(root.stx?._host?.isNative)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Where a frame would go on this page: `native` on a screen whose host has a
|
|
88
|
+
* Metal GPU, `canvas` in a browser, `none` on a native host without the
|
|
89
|
+
* capability. Read before mounting to decide whether to offer a surface.
|
|
90
|
+
*/
|
|
91
|
+
export function renderSurfaceHost(): RenderSurfaceHost {
|
|
92
|
+
if (onNativeScreen()) {
|
|
93
|
+
const present = root.craft?.renderSurface?.present
|
|
94
|
+
return typeof present === 'function' && root.craft?.capabilities?.renderSurface === true ? 'native' : 'none'
|
|
95
|
+
}
|
|
96
|
+
if (typeof document === 'undefined') return 'none'
|
|
97
|
+
return 'canvas'
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function checkFrame(surface: string, pixels: Uint32Array, width: number, height: number): void {
|
|
101
|
+
if (typeof surface !== 'string' || !surface)
|
|
102
|
+
throw new TypeError('presentFrame needs the surface\'s name')
|
|
103
|
+
if (!(pixels instanceof Uint32Array))
|
|
104
|
+
throw new TypeError('presentFrame takes a Uint32Array of 0xAARRGGBB words')
|
|
105
|
+
for (const [side, value] of [['width', width], ['height', height]] as const) {
|
|
106
|
+
if (!Number.isInteger(value) || value < 1 || value > RENDER_SURFACE_MAX_SIDE)
|
|
107
|
+
throw new TypeError(`presentFrame's ${side} must be a whole number from 1 to ${RENDER_SURFACE_MAX_SIDE}, not ${value}`)
|
|
108
|
+
}
|
|
109
|
+
if (pixels.length !== width * height)
|
|
110
|
+
throw new TypeError(`presentFrame was given ${pixels.length} words for a ${width} x ${height} frame (${width * height})`)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Draw one frame. True when the surface took it; false while it is not ready
|
|
115
|
+
* or is lost (wait for `ready` or `restored`, then draw a whole frame again:
|
|
116
|
+
* nothing is kept across a loss). Throws a TypeError for a wrong array or
|
|
117
|
+
* size, and an Error whose `code` is NOT_FOUND for a name no mounted surface
|
|
118
|
+
* holds (from its removal on) or UNAVAILABLE where nothing can draw one.
|
|
119
|
+
*/
|
|
120
|
+
export function presentFrame(surface: string, pixels: Uint32Array, width: number, height: number): boolean {
|
|
121
|
+
if (onNativeScreen()) {
|
|
122
|
+
const host = root.craft?.renderSurface
|
|
123
|
+
if (!host || typeof host.present !== 'function')
|
|
124
|
+
throw surfaceError('UNAVAILABLE', 'This device cannot draw a RenderSurface')
|
|
125
|
+
return host.present(surface, pixels, width, height)
|
|
126
|
+
}
|
|
127
|
+
checkFrame(surface, pixels, width, height)
|
|
128
|
+
const target = canvases().get(surface)
|
|
129
|
+
if (!target)
|
|
130
|
+
throw surfaceError('NOT_FOUND', `No RenderSurface named "${surface}" is on the page`)
|
|
131
|
+
drawToCanvas(target, pixels, width, height)
|
|
132
|
+
return true
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 0xAARRGGBB words into the canvas's RGBA bytes, at the frame's own size. CSS scales it to the box. */
|
|
136
|
+
function drawToCanvas(target: CanvasSurface, pixels: Uint32Array, width: number, height: number): void {
|
|
137
|
+
const { canvas, context, opaque } = target
|
|
138
|
+
if (canvas.width !== width) canvas.width = width
|
|
139
|
+
if (canvas.height !== height) canvas.height = height
|
|
140
|
+
if (!target.image || target.image.width !== width || target.image.height !== height)
|
|
141
|
+
target.image = context.createImageData(width, height)
|
|
142
|
+
const bytes = target.image.data
|
|
143
|
+
for (let i = 0, j = 0; i < pixels.length; i++, j += 4) {
|
|
144
|
+
const word = pixels[i]!
|
|
145
|
+
let a = word >>> 24
|
|
146
|
+
let r = (word >>> 16) & 0xFF
|
|
147
|
+
let g = (word >>> 8) & 0xFF
|
|
148
|
+
let b = word & 0xFF
|
|
149
|
+
if (opaque) {
|
|
150
|
+
a = 0xFF
|
|
151
|
+
}
|
|
152
|
+
else if (a > 0 && a < 0xFF) {
|
|
153
|
+
// A surface that is not opaque takes premultiplied words, as the host
|
|
154
|
+
// does; ImageData holds straight alpha.
|
|
155
|
+
r = Math.min(0xFF, Math.round(r * 0xFF / a))
|
|
156
|
+
g = Math.min(0xFF, Math.round(g * 0xFF / a))
|
|
157
|
+
b = Math.min(0xFF, Math.round(b * 0xFF / a))
|
|
158
|
+
}
|
|
159
|
+
bytes[j] = r
|
|
160
|
+
bytes[j + 1] = g
|
|
161
|
+
bytes[j + 2] = b
|
|
162
|
+
bytes[j + 3] = a
|
|
163
|
+
}
|
|
164
|
+
context.putImageData(target.image, 0, 0)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The component's half in a browser: put a canvas under a name, or say why
|
|
169
|
+
* not. One live surface per name, as on a native screen: a second answers
|
|
170
|
+
* DUPLICATE and the first keeps drawing.
|
|
171
|
+
*/
|
|
172
|
+
export function claimCanvasSurface(surface: string, canvas: HTMLCanvasElement, options: { opaque?: boolean } = {}): { release: () => void } | { error: RenderSurfaceError } {
|
|
173
|
+
if (canvases().has(surface))
|
|
174
|
+
return { error: { surface, code: 'DUPLICATE', message: `Another RenderSurface named "${surface}" is already on the page` } }
|
|
175
|
+
let context: CanvasRenderingContext2D | null = null
|
|
176
|
+
try { context = canvas.getContext('2d', { alpha: options.opaque === false }) }
|
|
177
|
+
catch { context = null }
|
|
178
|
+
if (!context)
|
|
179
|
+
return { error: { surface, code: 'UNAVAILABLE', message: 'This browser cannot draw a RenderSurface: it has no 2D canvas' } }
|
|
180
|
+
const entry: CanvasSurface = { canvas, context, opaque: options.opaque !== false, image: null }
|
|
181
|
+
canvases().set(surface, entry)
|
|
182
|
+
return {
|
|
183
|
+
release: () => {
|
|
184
|
+
if (canvases().get(surface) === entry) canvases().delete(surface)
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export interface RenderFrame {
|
|
190
|
+
pixels: Uint32Array
|
|
191
|
+
width: number
|
|
192
|
+
height: number
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export interface RenderLoop {
|
|
196
|
+
/** Stop asking for frames. */
|
|
197
|
+
stop: () => void
|
|
198
|
+
/** Resume after `present` answered false: call it from `ready` and `restored`. */
|
|
199
|
+
wake: () => void
|
|
200
|
+
/** Whether the loop is waiting for `wake`. */
|
|
201
|
+
readonly waiting: boolean
|
|
202
|
+
/** Whether the loop has ended: stopped, or its surface was removed. */
|
|
203
|
+
readonly ended: boolean
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Draw on every display frame. `draw` returns the frame to show, or nothing
|
|
208
|
+
* to skip one. While the surface answers false (not ready yet, or lost) the
|
|
209
|
+
* loop draws nothing until `wake()`. Its surface being removed ends it
|
|
210
|
+
* quietly: that is how a loop learns its screen went. Any other error ends
|
|
211
|
+
* it and goes to `onError`, or is thrown when there is none.
|
|
212
|
+
*/
|
|
213
|
+
export function startRenderLoop(surface: string, draw: (time: number) => RenderFrame | null | undefined | void, options: { onError?: (error: unknown) => void } = {}): RenderLoop {
|
|
214
|
+
const frame: (callback: (time: number) => void) => unknown = typeof requestAnimationFrame === 'function'
|
|
215
|
+
? requestAnimationFrame
|
|
216
|
+
: callback => setTimeout(() => callback(Date.now()), 16)
|
|
217
|
+
let ended = false
|
|
218
|
+
let waiting = false
|
|
219
|
+
const tick = (time: number): void => {
|
|
220
|
+
if (ended) return
|
|
221
|
+
if (!waiting) {
|
|
222
|
+
try {
|
|
223
|
+
const next = draw(time)
|
|
224
|
+
if (next && !presentFrame(surface, next.pixels, next.width, next.height))
|
|
225
|
+
waiting = true
|
|
226
|
+
}
|
|
227
|
+
catch (error) {
|
|
228
|
+
ended = true
|
|
229
|
+
if ((error as { code?: string })?.code === 'NOT_FOUND') return
|
|
230
|
+
if (options.onError) options.onError(error)
|
|
231
|
+
else throw error
|
|
232
|
+
return
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
frame(tick)
|
|
236
|
+
}
|
|
237
|
+
frame(tick)
|
|
238
|
+
return {
|
|
239
|
+
stop: () => { ended = true },
|
|
240
|
+
wake: () => { waiting = false },
|
|
241
|
+
get waiting() { return waiting },
|
|
242
|
+
get ended() { return ended },
|
|
243
|
+
}
|
|
244
|
+
}
|
|
@@ -5,6 +5,7 @@ export const delay = $num($props.delay) || 0
|
|
|
5
5
|
export const className = $props.className || ''
|
|
6
6
|
export const show = $bool($props.show)
|
|
7
7
|
export const disabled = $bool($props.disabled)
|
|
8
|
+
export const pressToToggle = $bool($props.pressToToggle)
|
|
8
9
|
|
|
9
10
|
/*
|
|
10
11
|
* The id that makes the tooltip reachable from its trigger.
|
|
@@ -55,7 +56,7 @@ export const arrowClass = `absolute w-0 h-0 border-4 border-transparent ${arrowC
|
|
|
55
56
|
</script>
|
|
56
57
|
|
|
57
58
|
<script client>
|
|
58
|
-
import { measureTooltipPosition, trackNativePopup } from '../../utils/tooltip-position'
|
|
59
|
+
import { measureTooltipPosition, trackBrowserPopup, trackNativePopup } from '../../utils/tooltip-position'
|
|
59
60
|
import { portalElement } from '../../utils/element-portal'
|
|
60
61
|
const nativePopup = !!window.stx._host.isNative
|
|
61
62
|
const mounted = state(false)
|
|
@@ -76,9 +77,11 @@ const arrowStyle = state('')
|
|
|
76
77
|
// fallback duplicated this entire setup for every different hint on a page.
|
|
77
78
|
const tooltipContent = useReactiveProp('content', '')
|
|
78
79
|
const isDisabled = useReactiveProp('disabled', {{ disabled }})
|
|
80
|
+
const pressToToggle = useReactiveProp('pressToToggle', {{ pressToToggle }})
|
|
79
81
|
// Reactive prop binding — parent can drive `show` via signals. See #1704.
|
|
80
82
|
const isVisible = useReactiveProp('show', {{ show }})
|
|
81
83
|
let timeoutId = null
|
|
84
|
+
let pointerExitTimeout = null
|
|
82
85
|
let positionGeneration = 0
|
|
83
86
|
let destroyed = false
|
|
84
87
|
|
|
@@ -110,35 +113,52 @@ async function positionTooltip() {
|
|
|
110
113
|
arrowStyle.set('transform:none;translate:none;border-color:transparent;' + arrows[point.side] + ';')
|
|
111
114
|
}
|
|
112
115
|
|
|
113
|
-
// Measurement follows rendering. Reposition on scroll, including scrolling
|
|
114
|
-
// ancestors, and resize; the composable removes these listeners on teardown.
|
|
115
|
-
effect(() => {
|
|
116
|
-
++positionGeneration
|
|
117
|
-
if (!nativePopup && isVisible() && !isDisabled()) nextTick(positionTooltip)
|
|
118
|
-
})
|
|
119
|
-
useEventListener('resize', positionTooltip)
|
|
120
|
-
useEventListener('scroll', positionTooltip, { capture: true, passive: true })
|
|
121
|
-
|
|
122
116
|
function showTooltip() {
|
|
123
|
-
if (isDisabled()) return
|
|
117
|
+
if (isDisabled() || pressToToggle()) return
|
|
118
|
+
cancelPointerExit()
|
|
119
|
+
if (isVisible()) return
|
|
124
120
|
if (timeoutId) clearTimeout(timeoutId)
|
|
125
121
|
if (delay > 0) {
|
|
126
|
-
timeoutId = setTimeout(() => {
|
|
122
|
+
timeoutId = setTimeout(() => {
|
|
123
|
+
timeoutId = null
|
|
124
|
+
if (!destroyed && !isDisabled() && !pressToToggle()) isVisible.set(true)
|
|
125
|
+
}, delay)
|
|
127
126
|
}
|
|
128
127
|
else {
|
|
129
128
|
isVisible.set(true)
|
|
130
129
|
}
|
|
131
130
|
}
|
|
132
131
|
|
|
132
|
+
function cancelPointerExit() {
|
|
133
|
+
if (pointerExitTimeout === null) return
|
|
134
|
+
clearTimeout(pointerExitTimeout)
|
|
135
|
+
pointerExitTimeout = null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function leaveTooltip() {
|
|
139
|
+
if (pressToToggle()) return
|
|
140
|
+
if (!isVisible()) { hideTooltip(); return }
|
|
141
|
+
cancelPointerExit()
|
|
142
|
+
// A portaled panel has an eight-pixel gap from its trigger. Let a pointer
|
|
143
|
+
// cross that gap without losing the text it was approaching to read.
|
|
144
|
+
pointerExitTimeout = setTimeout(hideTooltip, 160)
|
|
145
|
+
}
|
|
146
|
+
|
|
133
147
|
function toggleTouch(event) {
|
|
134
148
|
// A shared Button emits click before the original press bubbles here.
|
|
135
149
|
// Only the physical press toggles; otherwise one tap opens then closes.
|
|
136
150
|
if (event && Array.isArray(event.__stx_emit_args)) return
|
|
137
|
-
if (
|
|
151
|
+
if (isDisabled()) return
|
|
152
|
+
if (pressToToggle()) {
|
|
153
|
+
if (isVisible()) hideTooltip()
|
|
154
|
+
else isVisible.set(true)
|
|
155
|
+
}
|
|
156
|
+
else if (nativePopup) isVisible() ? hideTooltip() : showTooltip()
|
|
138
157
|
}
|
|
139
158
|
|
|
140
159
|
function hideTooltip() {
|
|
141
160
|
++positionGeneration
|
|
161
|
+
cancelPointerExit()
|
|
142
162
|
if (timeoutId) {
|
|
143
163
|
clearTimeout(timeoutId)
|
|
144
164
|
timeoutId = null
|
|
@@ -146,16 +166,6 @@ function hideTooltip() {
|
|
|
146
166
|
isVisible.set(false)
|
|
147
167
|
}
|
|
148
168
|
|
|
149
|
-
/*
|
|
150
|
-
* Escape dismisses it (WCAG 2.1 SC 1.4.13).
|
|
151
|
-
*
|
|
152
|
-
* The rule is that content shown on hover or focus can be dismissed WITHOUT
|
|
153
|
-
* moving the pointer or the focus -- so a keyboard listener on the trigger is
|
|
154
|
-
* not enough when the tooltip was opened by hovering and focus is somewhere
|
|
155
|
-
* else entirely. The listener is global for that reason, and does nothing
|
|
156
|
-
* unless this tooltip is the one showing. useEventListener drops it on
|
|
157
|
-
* teardown, so a page of tooltips does not leak one apiece.
|
|
158
|
-
*/
|
|
159
169
|
/*
|
|
160
170
|
* While it shows, the tooltip is a popup layer on the page's overlay stack, so
|
|
161
171
|
* Escape dismisses it first: a tooltip over a dialog's field goes, and the
|
|
@@ -165,13 +175,17 @@ function hideTooltip() {
|
|
|
165
175
|
let stopPositioning = null
|
|
166
176
|
effect(() => {
|
|
167
177
|
if (!mounted()) return
|
|
178
|
+
++positionGeneration
|
|
168
179
|
if (stopPositioning) { stopPositioning(); stopPositioning = null }
|
|
169
180
|
if (isVisible() && !isDisabled()) {
|
|
170
181
|
try {
|
|
171
182
|
if (nativePopup && !window.craft?.capabilities?.viewMeasure)
|
|
172
183
|
throw new Error('Native Tooltip requires viewMeasure support')
|
|
173
184
|
window.stx.overlays.open(activeTooltipId, { modal: false, root: nativePopup ? panelRef.value : null, panel: null, onEscape: hideTooltip, onBackdrop: hideTooltip })
|
|
174
|
-
|
|
185
|
+
const trigger = triggerRef.value
|
|
186
|
+
const panel = panelRef.value
|
|
187
|
+
if (nativePopup) stopPositioning = trackNativePopup(trigger, panel, preferredSide, style => panelStyle.set(style))
|
|
188
|
+
else if (trigger && panel) stopPositioning = trackBrowserPopup(trigger, panel, positionTooltip)
|
|
175
189
|
}
|
|
176
190
|
catch (error) {
|
|
177
191
|
console.warn('[stx:tooltip] ' + ((error && error.message) || String(error)))
|
|
@@ -184,6 +198,7 @@ effect(() => {
|
|
|
184
198
|
onDestroy(() => {
|
|
185
199
|
destroyed = true
|
|
186
200
|
++positionGeneration
|
|
201
|
+
cancelPointerExit()
|
|
187
202
|
if (timeoutId) clearTimeout(timeoutId)
|
|
188
203
|
if (stopPositioning) stopPositioning()
|
|
189
204
|
window.stx.overlays.close(activeTooltipId)
|
|
@@ -211,9 +226,9 @@ onDestroy(() => {
|
|
|
211
226
|
function describeTrigger() {
|
|
212
227
|
const wrapper = triggerRef.value
|
|
213
228
|
if (!wrapper) return
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
const target = wrapper.firstElementChild || wrapper
|
|
229
|
+
// A slotted component can wrap its actual control in a scope element.
|
|
230
|
+
const control = !nativePopup && wrapper.querySelector('button, a[href], input:not([type="hidden"]), select, textarea, summary, [role="button"], [role="link"], [tabindex]')
|
|
231
|
+
const target = control || wrapper.firstElementChild || wrapper
|
|
217
232
|
// A repeated trigger can inherit the template instance's description. Replace
|
|
218
233
|
// only our own previous id, preserving descriptions supplied by the caller.
|
|
219
234
|
const previous = target.getAttribute('data-stx-tooltip-description')
|
|
@@ -241,7 +256,11 @@ onMount(() => {
|
|
|
241
256
|
// its reactive bindings and refs, unlike passing them through another scope.
|
|
242
257
|
nextTick(() => {
|
|
243
258
|
const panel = panelRef.value
|
|
244
|
-
if (
|
|
259
|
+
if (nativePopup || destroyed || !panel?.isConnected) return
|
|
260
|
+
// Keep a tooltip opened in a modal with that modal. The shared overlay
|
|
261
|
+
// stack makes its body siblings inert, including a panel portaled there.
|
|
262
|
+
const modal = triggerRef.value?.closest('dialog, [aria-modal="true"]')
|
|
263
|
+
restorePanel = portalElement(panel, modal || panel.ownerDocument.body)
|
|
245
264
|
})
|
|
246
265
|
})
|
|
247
266
|
onDestroy(() => { if (restorePanel) restorePanel() })
|
|
@@ -258,7 +277,7 @@ onDestroy(() => { if (restorePanel) restorePanel() })
|
|
|
258
277
|
x-ref="triggerRef"
|
|
259
278
|
@click="toggleTouch($event)"
|
|
260
279
|
@mouseenter="showTooltip()"
|
|
261
|
-
@mouseleave="
|
|
280
|
+
@mouseleave="leaveTooltip()"
|
|
262
281
|
@focusin="showTooltip()"
|
|
263
282
|
@focusout="hideTooltip()"
|
|
264
283
|
>
|
|
@@ -276,7 +295,7 @@ onDestroy(() => { if (restorePanel) restorePanel() })
|
|
|
276
295
|
class="{{ tooltipClasses }}"
|
|
277
296
|
role="tooltip"
|
|
278
297
|
@mouseenter="showTooltip()"
|
|
279
|
-
@mouseleave="
|
|
298
|
+
@mouseleave="leaveTooltip()"
|
|
280
299
|
>
|
|
281
300
|
<span class="block" style="max-height:calc(100vh - 32px);overflow-y:auto;" :text="tooltipContent">{{ content }}</span>
|
|
282
301
|
<span :show="!nativePopup" class="{{ arrowClass }}" :style="arrowStyle"></span>
|
package/src/ui/tooltip/index.ts
CHANGED
|
@@ -4,6 +4,8 @@ export interface TooltipProps {
|
|
|
4
4
|
content: string
|
|
5
5
|
position?: 'top' | 'bottom' | 'left' | 'right'
|
|
6
6
|
delay?: number
|
|
7
|
+
/** Toggle a dedicated help trigger on activation instead of hover/focus. */
|
|
8
|
+
pressToToggle?: boolean
|
|
7
9
|
show?: boolean
|
|
8
10
|
disabled?: boolean
|
|
9
11
|
className?: string
|
|
@@ -38,8 +38,58 @@ export async function measureTooltipPosition(trigger: HTMLElement | NativeToolti
|
|
|
38
38
|
const viewport = trigger.ownerDocument.defaultView
|
|
39
39
|
if (!rect.width || !rect.height || !viewport) return null
|
|
40
40
|
const origin = panel.offsetParent?.getBoundingClientRect()
|
|
41
|
-
const
|
|
42
|
-
|
|
41
|
+
const visible = viewport.visualViewport
|
|
42
|
+
const x = visible?.offsetLeft ?? 0
|
|
43
|
+
const y = visible?.offsetTop ?? 0
|
|
44
|
+
const anchor = trigger.getBoundingClientRect()
|
|
45
|
+
const point = tooltipPosition(
|
|
46
|
+
{ left: anchor.left - x, right: anchor.right - x, top: anchor.top - y, bottom: anchor.bottom - y },
|
|
47
|
+
rect,
|
|
48
|
+
{ width: visible?.width || viewport.innerWidth, height: visible?.height || viewport.innerHeight },
|
|
49
|
+
preferred,
|
|
50
|
+
)
|
|
51
|
+
return { ...point, left: point.left + x - (origin?.left ?? 0), top: point.top + y - (origin?.top ?? 0) }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Track an open browser popup, reading layout once per rendering frame. */
|
|
55
|
+
export function trackBrowserPopup(trigger: HTMLElement, panel: HTMLElement, place: () => void): () => void {
|
|
56
|
+
const view = trigger.ownerDocument.defaultView
|
|
57
|
+
if (!view) return () => {}
|
|
58
|
+
const viewport = view.visualViewport
|
|
59
|
+
let active = true
|
|
60
|
+
let frame: number | undefined
|
|
61
|
+
const scrollOptions = { capture: true, passive: true }
|
|
62
|
+
|
|
63
|
+
const schedule = (): void => {
|
|
64
|
+
if (!active || frame !== undefined) return
|
|
65
|
+
frame = view.requestAnimationFrame(() => {
|
|
66
|
+
frame = undefined
|
|
67
|
+
if (!trigger.isConnected || !panel.isConnected) stop()
|
|
68
|
+
else place()
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const observer = view.ResizeObserver ? new view.ResizeObserver(schedule) : undefined
|
|
73
|
+
|
|
74
|
+
const stop = (): void => {
|
|
75
|
+
if (!active) return
|
|
76
|
+
active = false
|
|
77
|
+
if (frame !== undefined) view.cancelAnimationFrame(frame)
|
|
78
|
+
view.removeEventListener('scroll', schedule, scrollOptions)
|
|
79
|
+
view.removeEventListener('resize', schedule)
|
|
80
|
+
viewport?.removeEventListener('scroll', schedule)
|
|
81
|
+
viewport?.removeEventListener('resize', schedule)
|
|
82
|
+
observer?.disconnect()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
view.addEventListener('scroll', schedule, scrollOptions)
|
|
86
|
+
view.addEventListener('resize', schedule)
|
|
87
|
+
viewport?.addEventListener('scroll', schedule, { passive: true })
|
|
88
|
+
viewport?.addEventListener('resize', schedule)
|
|
89
|
+
observer?.observe(trigger)
|
|
90
|
+
observer?.observe(panel)
|
|
91
|
+
schedule()
|
|
92
|
+
return stop
|
|
43
93
|
}
|
|
44
94
|
|
|
45
95
|
/**
|