@hozo/behaviors 0.1.0
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/LICENSE +21 -0
- package/README.md +80 -0
- package/dist/dismissable-layer.d.ts +17 -0
- package/dist/dismissable-layer.d.ts.map +1 -0
- package/dist/dismissable-layer.js +67 -0
- package/dist/dismissable-layer.js.map +1 -0
- package/dist/dismissable-layer.native.d.ts +28 -0
- package/dist/dismissable-layer.native.d.ts.map +1 -0
- package/dist/dismissable-layer.native.js +38 -0
- package/dist/dismissable-layer.native.js.map +1 -0
- package/dist/floating-geometry.d.ts +63 -0
- package/dist/floating-geometry.d.ts.map +1 -0
- package/dist/floating-geometry.js +191 -0
- package/dist/floating-geometry.js.map +1 -0
- package/dist/floating-positioner.d.ts +24 -0
- package/dist/floating-positioner.d.ts.map +1 -0
- package/dist/floating-positioner.js +110 -0
- package/dist/floating-positioner.js.map +1 -0
- package/dist/floating-positioner.native.d.ts +43 -0
- package/dist/floating-positioner.native.d.ts.map +1 -0
- package/dist/floating-positioner.native.js +82 -0
- package/dist/floating-positioner.native.js.map +1 -0
- package/dist/focus-scope.d.ts +31 -0
- package/dist/focus-scope.d.ts.map +1 -0
- package/dist/focus-scope.js +96 -0
- package/dist/focus-scope.js.map +1 -0
- package/dist/focus-scope.native.d.ts +33 -0
- package/dist/focus-scope.native.d.ts.map +1 -0
- package/dist/focus-scope.native.js +25 -0
- package/dist/focus-scope.native.js.map +1 -0
- package/dist/hover-geometry.d.ts +64 -0
- package/dist/hover-geometry.d.ts.map +1 -0
- package/dist/hover-geometry.js +146 -0
- package/dist/hover-geometry.js.map +1 -0
- package/dist/hover-trigger.d.ts +64 -0
- package/dist/hover-trigger.d.ts.map +1 -0
- package/dist/hover-trigger.js +174 -0
- package/dist/hover-trigger.js.map +1 -0
- package/dist/hover-trigger.native.d.ts +45 -0
- package/dist/hover-trigger.native.d.ts.map +1 -0
- package/dist/hover-trigger.native.js +101 -0
- package/dist/hover-trigger.native.js.map +1 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/index.native.d.ts +12 -0
- package/dist/index.native.d.ts.map +1 -0
- package/dist/index.native.js +12 -0
- package/dist/index.native.js.map +1 -0
- package/dist/live-region.d.ts +19 -0
- package/dist/live-region.d.ts.map +1 -0
- package/dist/live-region.js +51 -0
- package/dist/live-region.js.map +1 -0
- package/dist/live-region.native.d.ts +31 -0
- package/dist/live-region.native.d.ts.map +1 -0
- package/dist/live-region.native.js +38 -0
- package/dist/live-region.native.js.map +1 -0
- package/dist/native.d.ts +5 -0
- package/dist/native.d.ts.map +1 -0
- package/dist/native.js +4 -0
- package/dist/native.js.map +1 -0
- package/dist/portal.d.ts +26 -0
- package/dist/portal.d.ts.map +1 -0
- package/dist/portal.js +41 -0
- package/dist/portal.js.map +1 -0
- package/dist/portal.native.d.ts +44 -0
- package/dist/portal.native.d.ts.map +1 -0
- package/dist/portal.native.js +79 -0
- package/dist/portal.native.js.map +1 -0
- package/dist/roving-focus-rules.d.ts +13 -0
- package/dist/roving-focus-rules.d.ts.map +1 -0
- package/dist/roving-focus-rules.js +49 -0
- package/dist/roving-focus-rules.js.map +1 -0
- package/dist/roving-focus.d.ts +27 -0
- package/dist/roving-focus.d.ts.map +1 -0
- package/dist/roving-focus.js +52 -0
- package/dist/roving-focus.js.map +1 -0
- package/dist/roving-focus.native.d.ts +36 -0
- package/dist/roving-focus.native.d.ts.map +1 -0
- package/dist/roving-focus.native.js +28 -0
- package/dist/roving-focus.native.js.map +1 -0
- package/dist/text-child.d.ts +4 -0
- package/dist/text-child.d.ts.map +1 -0
- package/dist/text-child.js +17 -0
- package/dist/text-child.js.map +1 -0
- package/dist/text-child.native.d.ts +14 -0
- package/dist/text-child.native.d.ts.map +1 -0
- package/dist/text-child.native.js +42 -0
- package/dist/text-child.native.js.map +1 -0
- package/dist/typeahead.d.ts +36 -0
- package/dist/typeahead.d.ts.map +1 -0
- package/dist/typeahead.js +97 -0
- package/dist/typeahead.js.map +1 -0
- package/package.json +82 -0
- package/src/dismissable-layer.native.tsx +72 -0
- package/src/dismissable-layer.tsx +106 -0
- package/src/floating-geometry.ts +306 -0
- package/src/floating-positioner.native.tsx +155 -0
- package/src/floating-positioner.tsx +173 -0
- package/src/focus-scope.native.tsx +53 -0
- package/src/focus-scope.tsx +143 -0
- package/src/hover-geometry.ts +182 -0
- package/src/hover-trigger.native.tsx +179 -0
- package/src/hover-trigger.tsx +303 -0
- package/src/index.native.ts +77 -0
- package/src/index.ts +77 -0
- package/src/live-region.native.tsx +61 -0
- package/src/live-region.tsx +74 -0
- package/src/native.ts +15 -0
- package/src/portal.native.tsx +130 -0
- package/src/portal.tsx +68 -0
- package/src/roving-focus-rules.ts +79 -0
- package/src/roving-focus.native.tsx +81 -0
- package/src/roving-focus.tsx +118 -0
- package/src/text-child.native.tsx +44 -0
- package/src/text-child.ts +19 -0
- package/src/typeahead.ts +120 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { type CSSProperties, type ReactNode, useEffect, useRef } from 'react'
|
|
2
|
+
|
|
3
|
+
export interface FocusCandidate {
|
|
4
|
+
autofocus?: boolean
|
|
5
|
+
focusable?: boolean
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Resolves which candidate receives focus when the scope activates.
|
|
10
|
+
* 1. Explicit autofocus control (if focusable)
|
|
11
|
+
* 2. First focusable descendant
|
|
12
|
+
* 3. null -> focuses the scope container itself
|
|
13
|
+
*/
|
|
14
|
+
export function initialFocusIndex(candidates: readonly FocusCandidate[]): number | null {
|
|
15
|
+
const requested = candidates.findIndex((c) => c.autofocus && c.focusable)
|
|
16
|
+
if (requested !== -1) return requested
|
|
17
|
+
const first = candidates.findIndex((c) => c.focusable)
|
|
18
|
+
return first === -1 ? null : first
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Checks whether focus can be safely restored to opener element.
|
|
23
|
+
* Must be connected to DOM and not disabled to prevent focus dropping to document.body.
|
|
24
|
+
*/
|
|
25
|
+
export function shouldRestoreFocus(opener: FocusCandidate | null | undefined): boolean {
|
|
26
|
+
return opener?.focusable === true
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface FocusScopeProps {
|
|
30
|
+
children?: ReactNode
|
|
31
|
+
trapped?: boolean
|
|
32
|
+
autoFocus?: boolean
|
|
33
|
+
restoreFocus?: boolean
|
|
34
|
+
className?: string
|
|
35
|
+
style?: CSSProperties
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const FOCUSABLE_SELECTOR =
|
|
39
|
+
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])'
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Universal `<FocusScope>` component for Web.
|
|
43
|
+
* Traps Tab key navigation, moves initial focus, and safely restores focus on unmount.
|
|
44
|
+
*/
|
|
45
|
+
export function FocusScope({
|
|
46
|
+
children,
|
|
47
|
+
trapped = true,
|
|
48
|
+
autoFocus = true,
|
|
49
|
+
restoreFocus = true,
|
|
50
|
+
className,
|
|
51
|
+
style,
|
|
52
|
+
}: FocusScopeProps) {
|
|
53
|
+
const containerRef = useRef<HTMLDivElement>(null)
|
|
54
|
+
const openerRef = useRef<HTMLElement | null>(null)
|
|
55
|
+
|
|
56
|
+
useEffect(() => {
|
|
57
|
+
if (typeof document === 'undefined') return
|
|
58
|
+
|
|
59
|
+
// Capture the trigger element that opened this scope
|
|
60
|
+
if (restoreFocus && document.activeElement instanceof HTMLElement) {
|
|
61
|
+
openerRef.current = document.activeElement
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const container = containerRef.current
|
|
65
|
+
if (!container) return
|
|
66
|
+
|
|
67
|
+
if (autoFocus) {
|
|
68
|
+
const focusables = Array.from(
|
|
69
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR),
|
|
70
|
+
).filter((el) => el.offsetParent !== null || el.getClientRects().length > 0)
|
|
71
|
+
|
|
72
|
+
const candidates: FocusCandidate[] = focusables.map((el) => ({
|
|
73
|
+
autofocus: el.hasAttribute('autofocus'),
|
|
74
|
+
focusable: !el.hasAttribute('disabled') && el.tabIndex !== -1,
|
|
75
|
+
}))
|
|
76
|
+
|
|
77
|
+
const targetIdx = initialFocusIndex(candidates)
|
|
78
|
+
if (targetIdx !== null && focusables[targetIdx]) {
|
|
79
|
+
focusables[targetIdx].focus()
|
|
80
|
+
} else {
|
|
81
|
+
// Fallback: focus container itself (tabIndex="-1") so screen readers announce dialog role/label
|
|
82
|
+
container.focus()
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
87
|
+
if (!trapped || event.key !== 'Tab') return
|
|
88
|
+
|
|
89
|
+
const focusables = Array.from(
|
|
90
|
+
container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR),
|
|
91
|
+
).filter((el) => el.offsetParent !== null || el.getClientRects().length > 0)
|
|
92
|
+
|
|
93
|
+
if (focusables.length === 0) {
|
|
94
|
+
event.preventDefault()
|
|
95
|
+
container.focus()
|
|
96
|
+
return
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const first = focusables[0]
|
|
100
|
+
const last = focusables[focusables.length - 1]
|
|
101
|
+
|
|
102
|
+
if (event.shiftKey) {
|
|
103
|
+
if (document.activeElement === first || document.activeElement === container) {
|
|
104
|
+
event.preventDefault()
|
|
105
|
+
last?.focus()
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
if (document.activeElement === last) {
|
|
109
|
+
event.preventDefault()
|
|
110
|
+
first?.focus()
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
container.addEventListener('keydown', handleKeyDown)
|
|
116
|
+
|
|
117
|
+
return () => {
|
|
118
|
+
container.removeEventListener('keydown', handleKeyDown)
|
|
119
|
+
if (restoreFocus) {
|
|
120
|
+
const opener = openerRef.current
|
|
121
|
+
if (
|
|
122
|
+
opener &&
|
|
123
|
+
shouldRestoreFocus({
|
|
124
|
+
focusable: opener.isConnected && !opener.hasAttribute('disabled'),
|
|
125
|
+
})
|
|
126
|
+
) {
|
|
127
|
+
opener.focus()
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}, [trapped, autoFocus, restoreFocus])
|
|
132
|
+
|
|
133
|
+
return (
|
|
134
|
+
<div
|
|
135
|
+
ref={containerRef}
|
|
136
|
+
tabIndex={-1}
|
|
137
|
+
className={className}
|
|
138
|
+
style={{ outline: 'none', ...style }}
|
|
139
|
+
>
|
|
140
|
+
{children}
|
|
141
|
+
</div>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import type { BasePlacement, Rect } from './floating-geometry.ts'
|
|
2
|
+
|
|
3
|
+
export interface Point {
|
|
4
|
+
x: number
|
|
5
|
+
y: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type Polygon = Point[]
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Determines whether a point lies within a polygon using the Ray-Casting algorithm.
|
|
12
|
+
* Used for Safe Polygon calculation between trigger and floating content.
|
|
13
|
+
*/
|
|
14
|
+
export function isPointInPolygon(point: Point, polygon: Polygon): boolean {
|
|
15
|
+
if (polygon.length < 3) return false
|
|
16
|
+
const { x, y } = point
|
|
17
|
+
let inside = false
|
|
18
|
+
|
|
19
|
+
for (let i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
|
|
20
|
+
const p1 = polygon[i]
|
|
21
|
+
const p2 = polygon[j]
|
|
22
|
+
if (!p1 || !p2) continue
|
|
23
|
+
|
|
24
|
+
const xi = p1.x
|
|
25
|
+
const yi = p1.y
|
|
26
|
+
const xj = p2.x
|
|
27
|
+
const yj = p2.y
|
|
28
|
+
|
|
29
|
+
const intersect = yi > y !== yj > y && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi
|
|
30
|
+
if (intersect) inside = !inside
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return inside
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Computes a safe polygon connecting the anchor to the floating element.
|
|
38
|
+
* Moving the pointer within this polygon will prevent the floating content from closing prematurely.
|
|
39
|
+
*/
|
|
40
|
+
export function computeSafePolygon(
|
|
41
|
+
anchorRect: Rect,
|
|
42
|
+
floatingRect: Rect,
|
|
43
|
+
placement: BasePlacement,
|
|
44
|
+
buffer = 4,
|
|
45
|
+
): Polygon {
|
|
46
|
+
switch (placement) {
|
|
47
|
+
case 'bottom': {
|
|
48
|
+
// Anchor is above floating element
|
|
49
|
+
return [
|
|
50
|
+
{ x: anchorRect.x - buffer, y: anchorRect.y + anchorRect.height },
|
|
51
|
+
{ x: anchorRect.x + anchorRect.width + buffer, y: anchorRect.y + anchorRect.height },
|
|
52
|
+
{
|
|
53
|
+
x: floatingRect.x + floatingRect.width + buffer,
|
|
54
|
+
y: floatingRect.y + floatingRect.height + buffer,
|
|
55
|
+
},
|
|
56
|
+
{ x: floatingRect.x - buffer, y: floatingRect.y + floatingRect.height + buffer },
|
|
57
|
+
]
|
|
58
|
+
}
|
|
59
|
+
case 'top': {
|
|
60
|
+
// Anchor is below floating element
|
|
61
|
+
return [
|
|
62
|
+
{ x: anchorRect.x + anchorRect.width + buffer, y: anchorRect.y },
|
|
63
|
+
{ x: anchorRect.x - buffer, y: anchorRect.y },
|
|
64
|
+
{ x: floatingRect.x - buffer, y: floatingRect.y - buffer },
|
|
65
|
+
{ x: floatingRect.x + floatingRect.width + buffer, y: floatingRect.y - buffer },
|
|
66
|
+
]
|
|
67
|
+
}
|
|
68
|
+
case 'right': {
|
|
69
|
+
// Anchor is to the left of floating element
|
|
70
|
+
return [
|
|
71
|
+
{ x: anchorRect.x + anchorRect.width, y: anchorRect.y - buffer },
|
|
72
|
+
{ x: anchorRect.x + anchorRect.width, y: anchorRect.y + anchorRect.height + buffer },
|
|
73
|
+
{
|
|
74
|
+
x: floatingRect.x + floatingRect.width + buffer,
|
|
75
|
+
y: floatingRect.y + floatingRect.height + buffer,
|
|
76
|
+
},
|
|
77
|
+
{ x: floatingRect.x + floatingRect.width + buffer, y: floatingRect.y - buffer },
|
|
78
|
+
]
|
|
79
|
+
}
|
|
80
|
+
case 'left': {
|
|
81
|
+
// Anchor is to the right of floating element
|
|
82
|
+
return [
|
|
83
|
+
{ x: anchorRect.x, y: anchorRect.y + anchorRect.height + buffer },
|
|
84
|
+
{ x: anchorRect.x, y: anchorRect.y - buffer },
|
|
85
|
+
{ x: floatingRect.x - buffer, y: floatingRect.y - buffer },
|
|
86
|
+
{ x: floatingRect.x - buffer, y: floatingRect.y + floatingRect.height + buffer },
|
|
87
|
+
]
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* State machine for Tooltip Delay Grouping (Warmup / Grace period).
|
|
94
|
+
* When one tooltip opens, subsequent tooltips within the group open instantly (delay = 0ms).
|
|
95
|
+
*/
|
|
96
|
+
export interface DelayGroupConfig {
|
|
97
|
+
/** Delay in milliseconds before opening an un-warmed tooltip. Default: 700ms. */
|
|
98
|
+
openDelay?: number
|
|
99
|
+
/** Delay in milliseconds before closing when pointer leaves. Default: 300ms. */
|
|
100
|
+
closeDelay?: number
|
|
101
|
+
/** Grace duration in ms after a tooltip closes where the group remains 'warm'. Default: 300ms. */
|
|
102
|
+
skipDelayDuration?: number
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export class DelayGroupMachine {
|
|
106
|
+
private openDelay: number
|
|
107
|
+
private closeDelay: number
|
|
108
|
+
private skipDelayDuration: number
|
|
109
|
+
private activeId: string | null = null
|
|
110
|
+
private isWarm = false
|
|
111
|
+
private cooldownTimer: ReturnType<typeof setTimeout> | null = null
|
|
112
|
+
|
|
113
|
+
constructor(config: DelayGroupConfig = {}) {
|
|
114
|
+
this.openDelay = config.openDelay ?? 700
|
|
115
|
+
this.closeDelay = config.closeDelay ?? 300
|
|
116
|
+
this.skipDelayDuration = config.skipDelayDuration ?? 300
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Returns whether the group is currently warm (i.e. delays should be skipped).
|
|
121
|
+
*/
|
|
122
|
+
getIsWarm(): boolean {
|
|
123
|
+
return this.isWarm
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Gets the effective open delay for a tooltip.
|
|
128
|
+
* Returns 0 if warm, or standard openDelay otherwise.
|
|
129
|
+
*/
|
|
130
|
+
getEffectiveOpenDelay(): number {
|
|
131
|
+
return this.isWarm ? 0 : this.openDelay
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Gets the close delay.
|
|
136
|
+
*/
|
|
137
|
+
getCloseDelay(): number {
|
|
138
|
+
return this.closeDelay
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Called when a tooltip in the group opens.
|
|
143
|
+
*/
|
|
144
|
+
onOpen(id: string) {
|
|
145
|
+
if (this.cooldownTimer) {
|
|
146
|
+
clearTimeout(this.cooldownTimer)
|
|
147
|
+
this.cooldownTimer = null
|
|
148
|
+
}
|
|
149
|
+
this.activeId = id
|
|
150
|
+
this.isWarm = true
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Called when a tooltip in the group closes.
|
|
155
|
+
* Keeps warm state for `skipDelayDuration` before going cold.
|
|
156
|
+
*/
|
|
157
|
+
onClose(id: string, onCooldownExpired?: () => void) {
|
|
158
|
+
if (this.activeId === id) {
|
|
159
|
+
this.activeId = null
|
|
160
|
+
}
|
|
161
|
+
if (this.cooldownTimer) {
|
|
162
|
+
clearTimeout(this.cooldownTimer)
|
|
163
|
+
}
|
|
164
|
+
this.cooldownTimer = setTimeout(() => {
|
|
165
|
+
this.isWarm = false
|
|
166
|
+
this.cooldownTimer = null
|
|
167
|
+
onCooldownExpired?.()
|
|
168
|
+
}, this.skipDelayDuration)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Disposes of any pending cooldown timer.
|
|
173
|
+
*/
|
|
174
|
+
dispose() {
|
|
175
|
+
if (this.cooldownTimer) {
|
|
176
|
+
clearTimeout(this.cooldownTimer)
|
|
177
|
+
this.cooldownTimer = null
|
|
178
|
+
}
|
|
179
|
+
this.isWarm = false
|
|
180
|
+
this.activeId = null
|
|
181
|
+
}
|
|
182
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createContext,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
useCallback,
|
|
5
|
+
useContext,
|
|
6
|
+
useEffect,
|
|
7
|
+
useId,
|
|
8
|
+
useMemo,
|
|
9
|
+
useRef,
|
|
10
|
+
useState,
|
|
11
|
+
} from 'react'
|
|
12
|
+
import { DelayGroupMachine } from './hover-geometry.ts'
|
|
13
|
+
|
|
14
|
+
// Context for Delay Grouping across multiple tooltips on Native
|
|
15
|
+
const TooltipGroupContext = createContext<DelayGroupMachine | null>(null)
|
|
16
|
+
|
|
17
|
+
export interface TooltipGroupProviderProps {
|
|
18
|
+
children: ReactNode
|
|
19
|
+
openDelay?: number
|
|
20
|
+
closeDelay?: number
|
|
21
|
+
skipDelayDuration?: number
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Native implementation of TooltipGroupProvider.
|
|
26
|
+
*/
|
|
27
|
+
export function TooltipGroupProvider({
|
|
28
|
+
children,
|
|
29
|
+
openDelay = 700,
|
|
30
|
+
closeDelay = 300,
|
|
31
|
+
skipDelayDuration = 300,
|
|
32
|
+
}: TooltipGroupProviderProps) {
|
|
33
|
+
const machine = useMemo(
|
|
34
|
+
() => new DelayGroupMachine({ openDelay, closeDelay, skipDelayDuration }),
|
|
35
|
+
[openDelay, closeDelay, skipDelayDuration],
|
|
36
|
+
)
|
|
37
|
+
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
return () => machine.dispose()
|
|
40
|
+
}, [machine])
|
|
41
|
+
|
|
42
|
+
return <TooltipGroupContext.Provider value={machine}>{children}</TooltipGroupContext.Provider>
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function useTooltipGroup(): DelayGroupMachine | null {
|
|
46
|
+
return useContext(TooltipGroupContext)
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface UseHoverTriggerOptions {
|
|
50
|
+
open?: boolean
|
|
51
|
+
defaultOpen?: boolean
|
|
52
|
+
onOpenChange?: (open: boolean) => void
|
|
53
|
+
openDelay?: number
|
|
54
|
+
closeDelay?: number
|
|
55
|
+
disabled?: boolean
|
|
56
|
+
hoverableContent?: boolean
|
|
57
|
+
openOnLongPress?: boolean
|
|
58
|
+
contentId?: string
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface NativeTriggerProps {
|
|
62
|
+
onHoverIn: () => void
|
|
63
|
+
onHoverOut: () => void
|
|
64
|
+
onLongPress: () => void
|
|
65
|
+
accessibilityHint?: string
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface NativeContentProps {
|
|
69
|
+
onHoverIn: () => void
|
|
70
|
+
onHoverOut: () => void
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface UseHoverTriggerReturn {
|
|
74
|
+
isOpen: boolean
|
|
75
|
+
setIsOpen: (open: boolean) => void
|
|
76
|
+
triggerProps: NativeTriggerProps
|
|
77
|
+
contentProps: NativeContentProps
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Universal hook for hover and long-press tooltips on React Native.
|
|
82
|
+
*/
|
|
83
|
+
export function useHoverTrigger({
|
|
84
|
+
open: controlledOpen,
|
|
85
|
+
defaultOpen = false,
|
|
86
|
+
onOpenChange,
|
|
87
|
+
openDelay: customOpenDelay,
|
|
88
|
+
closeDelay: customCloseDelay,
|
|
89
|
+
disabled = false,
|
|
90
|
+
openOnLongPress = true,
|
|
91
|
+
contentId,
|
|
92
|
+
}: UseHoverTriggerOptions = {}): UseHoverTriggerReturn {
|
|
93
|
+
const generatedId = useId()
|
|
94
|
+
const id = contentId ?? generatedId
|
|
95
|
+
|
|
96
|
+
const group = useTooltipGroup()
|
|
97
|
+
const [uncontrolledOpen, setUncontrolledOpen] = useState(defaultOpen)
|
|
98
|
+
const isControlled = controlledOpen !== undefined
|
|
99
|
+
const isOpen = isControlled ? controlledOpen : uncontrolledOpen
|
|
100
|
+
|
|
101
|
+
const openTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
102
|
+
const closeTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
103
|
+
|
|
104
|
+
const clearTimers = useCallback(() => {
|
|
105
|
+
if (openTimerRef.current) {
|
|
106
|
+
clearTimeout(openTimerRef.current)
|
|
107
|
+
openTimerRef.current = null
|
|
108
|
+
}
|
|
109
|
+
if (closeTimerRef.current) {
|
|
110
|
+
clearTimeout(closeTimerRef.current)
|
|
111
|
+
closeTimerRef.current = null
|
|
112
|
+
}
|
|
113
|
+
}, [])
|
|
114
|
+
|
|
115
|
+
const changeOpen = useCallback(
|
|
116
|
+
(nextOpen: boolean) => {
|
|
117
|
+
if (disabled) return
|
|
118
|
+
if (!isControlled) {
|
|
119
|
+
setUncontrolledOpen(nextOpen)
|
|
120
|
+
}
|
|
121
|
+
onOpenChange?.(nextOpen)
|
|
122
|
+
if (nextOpen) {
|
|
123
|
+
group?.onOpen(id)
|
|
124
|
+
} else {
|
|
125
|
+
group?.onClose(id)
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
[disabled, isControlled, onOpenChange, group, id],
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
const effectiveOpenDelay = group?.getEffectiveOpenDelay() ?? customOpenDelay ?? 700
|
|
132
|
+
const effectiveCloseDelay = group?.getCloseDelay() ?? customCloseDelay ?? 300
|
|
133
|
+
|
|
134
|
+
const handleHoverIn = useCallback(() => {
|
|
135
|
+
if (disabled) return
|
|
136
|
+
clearTimers()
|
|
137
|
+
|
|
138
|
+
if (effectiveOpenDelay === 0) {
|
|
139
|
+
changeOpen(true)
|
|
140
|
+
} else {
|
|
141
|
+
openTimerRef.current = setTimeout(() => {
|
|
142
|
+
changeOpen(true)
|
|
143
|
+
}, effectiveOpenDelay)
|
|
144
|
+
}
|
|
145
|
+
}, [disabled, clearTimers, effectiveOpenDelay, changeOpen])
|
|
146
|
+
|
|
147
|
+
const handleHoverOut = useCallback(() => {
|
|
148
|
+
if (disabled) return
|
|
149
|
+
clearTimers()
|
|
150
|
+
|
|
151
|
+
closeTimerRef.current = setTimeout(() => {
|
|
152
|
+
changeOpen(false)
|
|
153
|
+
}, effectiveCloseDelay)
|
|
154
|
+
}, [disabled, clearTimers, effectiveCloseDelay, changeOpen])
|
|
155
|
+
|
|
156
|
+
const handleLongPress = useCallback(() => {
|
|
157
|
+
if (disabled || !openOnLongPress) return
|
|
158
|
+
clearTimers()
|
|
159
|
+
changeOpen(true)
|
|
160
|
+
}, [disabled, openOnLongPress, clearTimers, changeOpen])
|
|
161
|
+
|
|
162
|
+
useEffect(() => {
|
|
163
|
+
return () => clearTimers()
|
|
164
|
+
}, [clearTimers])
|
|
165
|
+
|
|
166
|
+
return {
|
|
167
|
+
isOpen,
|
|
168
|
+
setIsOpen: changeOpen,
|
|
169
|
+
triggerProps: {
|
|
170
|
+
onHoverIn: handleHoverIn,
|
|
171
|
+
onHoverOut: handleHoverOut,
|
|
172
|
+
onLongPress: handleLongPress,
|
|
173
|
+
},
|
|
174
|
+
contentProps: {
|
|
175
|
+
onHoverIn: handleHoverIn,
|
|
176
|
+
onHoverOut: handleHoverOut,
|
|
177
|
+
},
|
|
178
|
+
}
|
|
179
|
+
}
|