@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,306 @@
|
|
|
1
|
+
export interface Rect {
|
|
2
|
+
x: number
|
|
3
|
+
y: number
|
|
4
|
+
width: number
|
|
5
|
+
height: number
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface Viewport {
|
|
9
|
+
width: number
|
|
10
|
+
height: number
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type BasePlacement = 'top' | 'bottom' | 'left' | 'right'
|
|
14
|
+
export type Alignment = 'start' | 'center' | 'end'
|
|
15
|
+
export type Placement = BasePlacement | `${BasePlacement}-start` | `${BasePlacement}-end`
|
|
16
|
+
|
|
17
|
+
export interface ComputePositionOptions {
|
|
18
|
+
/** Desired placement relative to the anchor. Default: 'bottom' */
|
|
19
|
+
placement?: Placement
|
|
20
|
+
/** Distance along the main axis between anchor and floating element in pixels. Default: 8 */
|
|
21
|
+
offset?: number
|
|
22
|
+
/** Offset along the cross axis in pixels. Default: 0 */
|
|
23
|
+
crossAxisOffset?: number
|
|
24
|
+
/** Whether to flip placement if floating element overflows viewport. Default: true */
|
|
25
|
+
flip?: boolean
|
|
26
|
+
/** Whether to shift floating element to keep it within viewport. Default: true */
|
|
27
|
+
shift?: boolean
|
|
28
|
+
/** Minimum padding from the viewport edge in pixels. Default: 8 */
|
|
29
|
+
viewportPadding?: number
|
|
30
|
+
/** Minimum padding from the floating edge to arrow center in pixels. Default: 4 */
|
|
31
|
+
arrowPadding?: number
|
|
32
|
+
/** Whether the floating element width should match the anchor width. Default: false */
|
|
33
|
+
matchAnchorWidth?: boolean
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface PositionResult {
|
|
37
|
+
x: number
|
|
38
|
+
y: number
|
|
39
|
+
placement: Placement
|
|
40
|
+
basePlacement: BasePlacement
|
|
41
|
+
alignment: Alignment
|
|
42
|
+
flipped: boolean
|
|
43
|
+
shifted: boolean
|
|
44
|
+
/** Whether the anchor element is currently completely outside the viewport (hidden by scroll) */
|
|
45
|
+
referenceHidden: boolean
|
|
46
|
+
/** The anchor element's measured width in pixels */
|
|
47
|
+
anchorWidth: number
|
|
48
|
+
/** Maximum dimensions available for the floating element before overflowing viewport */
|
|
49
|
+
availableDimensions: {
|
|
50
|
+
width: number
|
|
51
|
+
height: number
|
|
52
|
+
}
|
|
53
|
+
arrow?: {
|
|
54
|
+
x?: number
|
|
55
|
+
y?: number
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function parsePlacement(placement: Placement): {
|
|
60
|
+
basePlacement: BasePlacement
|
|
61
|
+
alignment: Alignment
|
|
62
|
+
} {
|
|
63
|
+
const [base, align] = placement.split('-') as [BasePlacement, Alignment | undefined]
|
|
64
|
+
return {
|
|
65
|
+
basePlacement: base,
|
|
66
|
+
alignment: align ?? 'center',
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function getOppositeBase(base: BasePlacement): BasePlacement {
|
|
71
|
+
switch (base) {
|
|
72
|
+
case 'top':
|
|
73
|
+
return 'bottom'
|
|
74
|
+
case 'bottom':
|
|
75
|
+
return 'top'
|
|
76
|
+
case 'left':
|
|
77
|
+
return 'right'
|
|
78
|
+
case 'right':
|
|
79
|
+
return 'left'
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function calculateRawCoords(
|
|
84
|
+
anchor: Rect,
|
|
85
|
+
floating: Rect,
|
|
86
|
+
base: BasePlacement,
|
|
87
|
+
align: Alignment,
|
|
88
|
+
offset: number,
|
|
89
|
+
crossAxisOffset: number,
|
|
90
|
+
): { x: number; y: number } {
|
|
91
|
+
let x = 0
|
|
92
|
+
let y = 0
|
|
93
|
+
|
|
94
|
+
if (base === 'top') {
|
|
95
|
+
y = anchor.y - floating.height - offset
|
|
96
|
+
} else if (base === 'bottom') {
|
|
97
|
+
y = anchor.y + anchor.height + offset
|
|
98
|
+
} else if (base === 'left') {
|
|
99
|
+
x = anchor.x - floating.width - offset
|
|
100
|
+
} else if (base === 'right') {
|
|
101
|
+
x = anchor.x + anchor.width + offset
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (base === 'top' || base === 'bottom') {
|
|
105
|
+
if (align === 'start') {
|
|
106
|
+
x = anchor.x + crossAxisOffset
|
|
107
|
+
} else if (align === 'end') {
|
|
108
|
+
x = anchor.x + anchor.width - floating.width + crossAxisOffset
|
|
109
|
+
} else {
|
|
110
|
+
x = anchor.x + (anchor.width - floating.width) / 2 + crossAxisOffset
|
|
111
|
+
}
|
|
112
|
+
} else {
|
|
113
|
+
if (align === 'start') {
|
|
114
|
+
y = anchor.y + crossAxisOffset
|
|
115
|
+
} else if (align === 'end') {
|
|
116
|
+
y = anchor.y + anchor.height - floating.height + crossAxisOffset
|
|
117
|
+
} else {
|
|
118
|
+
y = anchor.y + (anchor.height - floating.height) / 2 + crossAxisOffset
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
return { x, y }
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Pure geometric calculation for positioning floating elements relative to an anchor.
|
|
127
|
+
* Supports Popper/Floating-UI equivalent placement, flip, shift, size limits, and arrow calculation.
|
|
128
|
+
*/
|
|
129
|
+
export function computePosition(
|
|
130
|
+
anchor: Rect,
|
|
131
|
+
floating: Rect,
|
|
132
|
+
viewport: Viewport,
|
|
133
|
+
options: ComputePositionOptions = {},
|
|
134
|
+
): PositionResult {
|
|
135
|
+
const {
|
|
136
|
+
placement = 'bottom',
|
|
137
|
+
offset = 8,
|
|
138
|
+
crossAxisOffset = 0,
|
|
139
|
+
flip = true,
|
|
140
|
+
shift = true,
|
|
141
|
+
viewportPadding = 8,
|
|
142
|
+
arrowPadding = 4,
|
|
143
|
+
matchAnchorWidth = false,
|
|
144
|
+
} = options
|
|
145
|
+
|
|
146
|
+
const effectiveFloating = matchAnchorWidth ? { ...floating, width: anchor.width } : floating
|
|
147
|
+
|
|
148
|
+
const parsed = parsePlacement(placement)
|
|
149
|
+
let currentBase = parsed.basePlacement
|
|
150
|
+
const currentAlign = parsed.alignment
|
|
151
|
+
let flipped = false
|
|
152
|
+
|
|
153
|
+
let coords = calculateRawCoords(
|
|
154
|
+
anchor,
|
|
155
|
+
effectiveFloating,
|
|
156
|
+
currentBase,
|
|
157
|
+
currentAlign,
|
|
158
|
+
offset,
|
|
159
|
+
crossAxisOffset,
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
// Check collision for flipping
|
|
163
|
+
if (flip) {
|
|
164
|
+
let overflows = false
|
|
165
|
+
if (currentBase === 'top') {
|
|
166
|
+
overflows = coords.y < viewportPadding
|
|
167
|
+
} else if (currentBase === 'bottom') {
|
|
168
|
+
overflows = coords.y + effectiveFloating.height > viewport.height - viewportPadding
|
|
169
|
+
} else if (currentBase === 'left') {
|
|
170
|
+
overflows = coords.x < viewportPadding
|
|
171
|
+
} else if (currentBase === 'right') {
|
|
172
|
+
overflows = coords.x + effectiveFloating.width > viewport.width - viewportPadding
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
if (overflows) {
|
|
176
|
+
const oppositeBase = getOppositeBase(currentBase)
|
|
177
|
+
const oppositeCoords = calculateRawCoords(
|
|
178
|
+
anchor,
|
|
179
|
+
effectiveFloating,
|
|
180
|
+
oppositeBase,
|
|
181
|
+
currentAlign,
|
|
182
|
+
offset,
|
|
183
|
+
crossAxisOffset,
|
|
184
|
+
)
|
|
185
|
+
|
|
186
|
+
// Measure spaces
|
|
187
|
+
let currentSpace = 0
|
|
188
|
+
let oppositeSpace = 0
|
|
189
|
+
|
|
190
|
+
if (currentBase === 'top') {
|
|
191
|
+
currentSpace = anchor.y
|
|
192
|
+
oppositeSpace = viewport.height - (anchor.y + anchor.height)
|
|
193
|
+
} else if (currentBase === 'bottom') {
|
|
194
|
+
currentSpace = viewport.height - (anchor.y + anchor.height)
|
|
195
|
+
oppositeSpace = anchor.y
|
|
196
|
+
} else if (currentBase === 'left') {
|
|
197
|
+
currentSpace = anchor.x
|
|
198
|
+
oppositeSpace = viewport.width - (anchor.x + anchor.width)
|
|
199
|
+
} else if (currentBase === 'right') {
|
|
200
|
+
currentSpace = viewport.width - (anchor.x + anchor.width)
|
|
201
|
+
oppositeSpace = anchor.x
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Flip if opposite side has more space or fits
|
|
205
|
+
if (oppositeSpace > currentSpace) {
|
|
206
|
+
currentBase = oppositeBase
|
|
207
|
+
coords = oppositeCoords
|
|
208
|
+
flipped = true
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
let finalX = coords.x
|
|
214
|
+
let finalY = coords.y
|
|
215
|
+
let shifted = false
|
|
216
|
+
|
|
217
|
+
// Shift along cross-axis to stay within viewport
|
|
218
|
+
if (shift) {
|
|
219
|
+
const minX = viewportPadding
|
|
220
|
+
const maxX = Math.max(
|
|
221
|
+
viewportPadding,
|
|
222
|
+
viewport.width - effectiveFloating.width - viewportPadding,
|
|
223
|
+
)
|
|
224
|
+
const minY = viewportPadding
|
|
225
|
+
const maxY = Math.max(
|
|
226
|
+
viewportPadding,
|
|
227
|
+
viewport.height - effectiveFloating.height - viewportPadding,
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
const clampedX = Math.min(Math.max(finalX, minX), maxX)
|
|
231
|
+
const clampedY = Math.min(Math.max(finalY, minY), maxY)
|
|
232
|
+
|
|
233
|
+
if (clampedX !== finalX || clampedY !== finalY) {
|
|
234
|
+
shifted = true
|
|
235
|
+
finalX = clampedX
|
|
236
|
+
finalY = clampedY
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// Calculate arrow position (relative to floating element top-left)
|
|
241
|
+
let arrow: { x?: number; y?: number } | undefined
|
|
242
|
+
if (currentBase === 'top' || currentBase === 'bottom') {
|
|
243
|
+
const anchorCenter = anchor.x + anchor.width / 2
|
|
244
|
+
const rawArrowX = anchorCenter - finalX
|
|
245
|
+
const minArrowX = arrowPadding
|
|
246
|
+
const maxArrowX = Math.max(arrowPadding, effectiveFloating.width - arrowPadding)
|
|
247
|
+
arrow = {
|
|
248
|
+
x: Math.min(Math.max(rawArrowX, minArrowX), maxArrowX),
|
|
249
|
+
}
|
|
250
|
+
} else {
|
|
251
|
+
const anchorCenter = anchor.y + anchor.height / 2
|
|
252
|
+
const rawArrowY = anchorCenter - finalY
|
|
253
|
+
const minArrowY = arrowPadding
|
|
254
|
+
const maxArrowY = Math.max(arrowPadding, effectiveFloating.height - arrowPadding)
|
|
255
|
+
arrow = {
|
|
256
|
+
y: Math.min(Math.max(rawArrowY, minArrowY), maxArrowY),
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Calculate available dimensions for the floating content
|
|
261
|
+
let availableWidth = Math.max(0, viewport.width - 2 * viewportPadding)
|
|
262
|
+
let availableHeight = Math.max(0, viewport.height - 2 * viewportPadding)
|
|
263
|
+
|
|
264
|
+
if (currentBase === 'top') {
|
|
265
|
+
availableHeight = Math.max(0, anchor.y - offset - viewportPadding)
|
|
266
|
+
} else if (currentBase === 'bottom') {
|
|
267
|
+
availableHeight = Math.max(
|
|
268
|
+
0,
|
|
269
|
+
viewport.height - (anchor.y + anchor.height) - offset - viewportPadding,
|
|
270
|
+
)
|
|
271
|
+
} else if (currentBase === 'left') {
|
|
272
|
+
availableWidth = Math.max(0, anchor.x - offset - viewportPadding)
|
|
273
|
+
} else if (currentBase === 'right') {
|
|
274
|
+
availableWidth = Math.max(
|
|
275
|
+
0,
|
|
276
|
+
viewport.width - (anchor.x + anchor.width) - offset - viewportPadding,
|
|
277
|
+
)
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Detect whether the anchor reference is hidden (scrolled completely off-screen)
|
|
281
|
+
const referenceHidden =
|
|
282
|
+
anchor.y + anchor.height < 0 ||
|
|
283
|
+
anchor.y > viewport.height ||
|
|
284
|
+
anchor.x + anchor.width < 0 ||
|
|
285
|
+
anchor.x > viewport.width
|
|
286
|
+
|
|
287
|
+
const finalPlacement =
|
|
288
|
+
currentAlign === 'center' ? currentBase : (`${currentBase}-${currentAlign}` as Placement)
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
x: Math.round(finalX),
|
|
292
|
+
y: Math.round(finalY),
|
|
293
|
+
placement: finalPlacement,
|
|
294
|
+
basePlacement: currentBase,
|
|
295
|
+
alignment: currentAlign,
|
|
296
|
+
flipped,
|
|
297
|
+
shifted,
|
|
298
|
+
referenceHidden,
|
|
299
|
+
anchorWidth: Math.round(anchor.width),
|
|
300
|
+
availableDimensions: {
|
|
301
|
+
width: Math.round(availableWidth),
|
|
302
|
+
height: Math.round(availableHeight),
|
|
303
|
+
},
|
|
304
|
+
arrow,
|
|
305
|
+
}
|
|
306
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
type ComponentRef,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
type RefObject,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react'
|
|
10
|
+
import { type StyleProp, View, type ViewStyle } from 'react-native'
|
|
11
|
+
import {
|
|
12
|
+
type ComputePositionOptions,
|
|
13
|
+
computePosition,
|
|
14
|
+
type PositionResult,
|
|
15
|
+
} from './floating-geometry.ts'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* What React Native hands back for a `<View>`, rather than a shape of our
|
|
19
|
+
* own that happens to have `measureInWindow` on it.
|
|
20
|
+
*
|
|
21
|
+
* The hand-rolled one could not be passed to a `<View ref>` -- this file
|
|
22
|
+
* did exactly that, and the erasure around it hid the mismatch.
|
|
23
|
+
*/
|
|
24
|
+
type MeasurableRef = RefObject<ComponentRef<typeof View> | null>
|
|
25
|
+
|
|
26
|
+
export interface UseFloatingPositionOptions extends ComputePositionOptions {
|
|
27
|
+
anchorRef: MeasurableRef
|
|
28
|
+
floatingRef: MeasurableRef
|
|
29
|
+
enabled?: boolean
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Universal React hook for floating element positioning on React Native.
|
|
34
|
+
*/
|
|
35
|
+
export function useFloatingPosition({
|
|
36
|
+
anchorRef,
|
|
37
|
+
floatingRef,
|
|
38
|
+
enabled = true,
|
|
39
|
+
placement = 'bottom',
|
|
40
|
+
offset = 8,
|
|
41
|
+
crossAxisOffset = 0,
|
|
42
|
+
flip = true,
|
|
43
|
+
shift = true,
|
|
44
|
+
viewportPadding = 8,
|
|
45
|
+
arrowPadding = 4,
|
|
46
|
+
}: UseFloatingPositionOptions): PositionResult | null {
|
|
47
|
+
const [position, setPosition] = useState<PositionResult | null>(null)
|
|
48
|
+
|
|
49
|
+
const update = useCallback(() => {
|
|
50
|
+
if (!enabled) return
|
|
51
|
+
const anchor = anchorRef.current
|
|
52
|
+
if (!anchor?.measureInWindow) return
|
|
53
|
+
|
|
54
|
+
anchor.measureInWindow((ax, ay, aw, ah) => {
|
|
55
|
+
const floating = floatingRef.current
|
|
56
|
+
if (floating?.measureInWindow) {
|
|
57
|
+
floating.measureInWindow((_fx, _fy, fw, fh) => {
|
|
58
|
+
// Assume standard device screen dimensions fallback
|
|
59
|
+
const viewport = { width: 400, height: 800 }
|
|
60
|
+
const result = computePosition(
|
|
61
|
+
{ x: ax, y: ay, width: aw, height: ah },
|
|
62
|
+
{ x: 0, y: 0, width: fw, height: fh },
|
|
63
|
+
viewport,
|
|
64
|
+
{
|
|
65
|
+
placement,
|
|
66
|
+
offset,
|
|
67
|
+
crossAxisOffset,
|
|
68
|
+
flip,
|
|
69
|
+
shift,
|
|
70
|
+
viewportPadding,
|
|
71
|
+
arrowPadding,
|
|
72
|
+
},
|
|
73
|
+
)
|
|
74
|
+
setPosition(result)
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
}, [
|
|
79
|
+
enabled,
|
|
80
|
+
anchorRef,
|
|
81
|
+
floatingRef,
|
|
82
|
+
placement,
|
|
83
|
+
offset,
|
|
84
|
+
crossAxisOffset,
|
|
85
|
+
flip,
|
|
86
|
+
shift,
|
|
87
|
+
viewportPadding,
|
|
88
|
+
arrowPadding,
|
|
89
|
+
])
|
|
90
|
+
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!enabled) return
|
|
93
|
+
update()
|
|
94
|
+
}, [enabled, update])
|
|
95
|
+
|
|
96
|
+
return position
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface FloatingPositionerProps extends ComputePositionOptions {
|
|
100
|
+
/**
|
|
101
|
+
* Tailwind classes, the same prop the Web half takes.
|
|
102
|
+
*
|
|
103
|
+
* On a tag the compiler lowers it is gone by runtime, replaced by a
|
|
104
|
+
* `StyleSheet` entry. Anywhere else it is carried and ignored here --
|
|
105
|
+
* this side has no CSS engine to resolve a class list against -- and
|
|
106
|
+
* the type still has to accept it, because an app is type-checked
|
|
107
|
+
* against the source the compiler reads rather than its output.
|
|
108
|
+
*/
|
|
109
|
+
className?: string
|
|
110
|
+
children?: ReactNode | ((position: PositionResult | null) => ReactNode)
|
|
111
|
+
anchorRef: MeasurableRef
|
|
112
|
+
style?: StyleProp<ViewStyle>
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Universal `<FloatingPositioner>` component for React Native.
|
|
117
|
+
*/
|
|
118
|
+
export function FloatingPositioner({
|
|
119
|
+
children,
|
|
120
|
+
anchorRef,
|
|
121
|
+
style,
|
|
122
|
+
...options
|
|
123
|
+
}: FloatingPositionerProps) {
|
|
124
|
+
const floatingRef = useRef<ComponentRef<typeof View> | null>(null)
|
|
125
|
+
|
|
126
|
+
const position = useFloatingPosition({
|
|
127
|
+
anchorRef,
|
|
128
|
+
floatingRef,
|
|
129
|
+
...options,
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
const positionStyles = position
|
|
133
|
+
? {
|
|
134
|
+
position: 'absolute' as const,
|
|
135
|
+
left: position.x,
|
|
136
|
+
top: position.y,
|
|
137
|
+
width: options.matchAnchorWidth ? position.anchorWidth : undefined,
|
|
138
|
+
opacity: position.referenceHidden ? 0 : 1,
|
|
139
|
+
}
|
|
140
|
+
: {
|
|
141
|
+
position: 'absolute' as const,
|
|
142
|
+
left: -9999,
|
|
143
|
+
top: -9999,
|
|
144
|
+
opacity: 0,
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return React.createElement(
|
|
148
|
+
View,
|
|
149
|
+
{
|
|
150
|
+
ref: floatingRef,
|
|
151
|
+
style: [positionStyles, style],
|
|
152
|
+
},
|
|
153
|
+
typeof children === 'function' ? children(position) : children,
|
|
154
|
+
)
|
|
155
|
+
}
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type CSSProperties,
|
|
3
|
+
type ReactNode,
|
|
4
|
+
type RefObject,
|
|
5
|
+
useCallback,
|
|
6
|
+
useEffect,
|
|
7
|
+
useRef,
|
|
8
|
+
useState,
|
|
9
|
+
} from 'react'
|
|
10
|
+
import {
|
|
11
|
+
type ComputePositionOptions,
|
|
12
|
+
computePosition,
|
|
13
|
+
type PositionResult,
|
|
14
|
+
} from './floating-geometry.ts'
|
|
15
|
+
|
|
16
|
+
export interface UseFloatingPositionOptions extends ComputePositionOptions {
|
|
17
|
+
anchorRef: RefObject<HTMLElement | null>
|
|
18
|
+
floatingRef: RefObject<HTMLElement | null>
|
|
19
|
+
enabled?: boolean
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Universal React hook for floating element positioning on Web.
|
|
24
|
+
* Automatically recalculates position on scroll, resize, and element dimension changes.
|
|
25
|
+
*/
|
|
26
|
+
export function useFloatingPosition({
|
|
27
|
+
anchorRef,
|
|
28
|
+
floatingRef,
|
|
29
|
+
enabled = true,
|
|
30
|
+
placement = 'bottom',
|
|
31
|
+
offset = 8,
|
|
32
|
+
crossAxisOffset = 0,
|
|
33
|
+
flip = true,
|
|
34
|
+
shift = true,
|
|
35
|
+
viewportPadding = 8,
|
|
36
|
+
arrowPadding = 4,
|
|
37
|
+
matchAnchorWidth = false,
|
|
38
|
+
}: UseFloatingPositionOptions): PositionResult | null {
|
|
39
|
+
const [position, setPosition] = useState<PositionResult | null>(null)
|
|
40
|
+
|
|
41
|
+
const update = useCallback(() => {
|
|
42
|
+
if (!enabled || typeof window === 'undefined') return
|
|
43
|
+
const anchor = anchorRef.current
|
|
44
|
+
const floating = floatingRef.current
|
|
45
|
+
if (!anchor || !floating) return
|
|
46
|
+
|
|
47
|
+
const anchorDomRect = anchor.getBoundingClientRect()
|
|
48
|
+
const floatingDomRect = floating.getBoundingClientRect()
|
|
49
|
+
|
|
50
|
+
const anchorRect = {
|
|
51
|
+
x: anchorDomRect.left,
|
|
52
|
+
y: anchorDomRect.top,
|
|
53
|
+
width: anchorDomRect.width,
|
|
54
|
+
height: anchorDomRect.height,
|
|
55
|
+
}
|
|
56
|
+
const floatingRect = {
|
|
57
|
+
x: floatingDomRect.left,
|
|
58
|
+
y: floatingDomRect.top,
|
|
59
|
+
width: floatingDomRect.width,
|
|
60
|
+
height: floatingDomRect.height,
|
|
61
|
+
}
|
|
62
|
+
const viewport = {
|
|
63
|
+
width: window.innerWidth,
|
|
64
|
+
height: window.innerHeight,
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const result = computePosition(anchorRect, floatingRect, viewport, {
|
|
68
|
+
placement,
|
|
69
|
+
offset,
|
|
70
|
+
crossAxisOffset,
|
|
71
|
+
flip,
|
|
72
|
+
shift,
|
|
73
|
+
viewportPadding,
|
|
74
|
+
arrowPadding,
|
|
75
|
+
matchAnchorWidth,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
setPosition(result)
|
|
79
|
+
}, [
|
|
80
|
+
enabled,
|
|
81
|
+
anchorRef,
|
|
82
|
+
floatingRef,
|
|
83
|
+
placement,
|
|
84
|
+
offset,
|
|
85
|
+
crossAxisOffset,
|
|
86
|
+
flip,
|
|
87
|
+
shift,
|
|
88
|
+
viewportPadding,
|
|
89
|
+
arrowPadding,
|
|
90
|
+
matchAnchorWidth,
|
|
91
|
+
])
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
if (!enabled || typeof window === 'undefined') return
|
|
95
|
+
|
|
96
|
+
update()
|
|
97
|
+
|
|
98
|
+
const handleScrollOrResize = () => update()
|
|
99
|
+
window.addEventListener('resize', handleScrollOrResize)
|
|
100
|
+
window.addEventListener('scroll', handleScrollOrResize, true)
|
|
101
|
+
|
|
102
|
+
let resizeObserver: ResizeObserver | null = null
|
|
103
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
104
|
+
resizeObserver = new ResizeObserver(() => update())
|
|
105
|
+
if (anchorRef.current) resizeObserver.observe(anchorRef.current)
|
|
106
|
+
if (floatingRef.current) resizeObserver.observe(floatingRef.current)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return () => {
|
|
110
|
+
window.removeEventListener('resize', handleScrollOrResize)
|
|
111
|
+
window.removeEventListener('scroll', handleScrollOrResize, true)
|
|
112
|
+
resizeObserver?.disconnect()
|
|
113
|
+
}
|
|
114
|
+
}, [enabled, update, anchorRef, floatingRef])
|
|
115
|
+
|
|
116
|
+
return position
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface FloatingPositionerProps extends ComputePositionOptions {
|
|
120
|
+
children?: ReactNode | ((position: PositionResult | null) => ReactNode)
|
|
121
|
+
anchorRef: RefObject<HTMLElement | null>
|
|
122
|
+
className?: string
|
|
123
|
+
style?: CSSProperties
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Universal `<FloatingPositioner>` component for Web.
|
|
128
|
+
* Popper/Floating-UI equivalent positioning container that attaches to an anchor element.
|
|
129
|
+
*/
|
|
130
|
+
export function FloatingPositioner({
|
|
131
|
+
children,
|
|
132
|
+
anchorRef,
|
|
133
|
+
className,
|
|
134
|
+
style,
|
|
135
|
+
...options
|
|
136
|
+
}: FloatingPositionerProps) {
|
|
137
|
+
const floatingRef = useRef<HTMLDivElement>(null)
|
|
138
|
+
const position = useFloatingPosition({
|
|
139
|
+
anchorRef,
|
|
140
|
+
floatingRef,
|
|
141
|
+
...options,
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
const positionStyles: CSSProperties = position
|
|
145
|
+
? {
|
|
146
|
+
position: 'fixed',
|
|
147
|
+
left: `${position.x}px`,
|
|
148
|
+
top: `${position.y}px`,
|
|
149
|
+
width: options.matchAnchorWidth ? `${position.anchorWidth}px` : undefined,
|
|
150
|
+
willChange: 'transform',
|
|
151
|
+
display: position.referenceHidden ? 'none' : undefined,
|
|
152
|
+
}
|
|
153
|
+
: {
|
|
154
|
+
position: 'fixed',
|
|
155
|
+
left: '-9999px',
|
|
156
|
+
top: '-9999px',
|
|
157
|
+
visibility: 'hidden',
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return (
|
|
161
|
+
<div
|
|
162
|
+
ref={floatingRef}
|
|
163
|
+
className={className}
|
|
164
|
+
style={{ ...positionStyles, ...style }}
|
|
165
|
+
data-placement={position?.placement}
|
|
166
|
+
data-flipped={position?.flipped ? '' : undefined}
|
|
167
|
+
data-shifted={position?.shifted ? '' : undefined}
|
|
168
|
+
data-reference-hidden={position?.referenceHidden ? '' : undefined}
|
|
169
|
+
>
|
|
170
|
+
{typeof children === 'function' ? children(position) : children}
|
|
171
|
+
</div>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import React, { type ReactNode } from 'react'
|
|
2
|
+
import { type StyleProp, View, type ViewStyle } from 'react-native'
|
|
3
|
+
|
|
4
|
+
export interface FocusCandidate {
|
|
5
|
+
autofocus?: boolean
|
|
6
|
+
focusable?: boolean
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function initialFocusIndex(candidates: readonly FocusCandidate[]): number | null {
|
|
10
|
+
const requested = candidates.findIndex((c) => c.autofocus && c.focusable)
|
|
11
|
+
if (requested !== -1) return requested
|
|
12
|
+
const first = candidates.findIndex((c) => c.focusable)
|
|
13
|
+
return first === -1 ? null : first
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function shouldRestoreFocus(opener: FocusCandidate | null | undefined): boolean {
|
|
17
|
+
return opener?.focusable === true
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface FocusScopeProps {
|
|
21
|
+
/**
|
|
22
|
+
* Tailwind classes, the same prop the Web half takes.
|
|
23
|
+
*
|
|
24
|
+
* On a tag the compiler lowers it is gone by runtime, replaced by a
|
|
25
|
+
* `StyleSheet` entry. Anywhere else it is carried and ignored here --
|
|
26
|
+
* this side has no CSS engine to resolve a class list against -- and
|
|
27
|
+
* the type still has to accept it, because an app is type-checked
|
|
28
|
+
* against the source the compiler reads rather than its output.
|
|
29
|
+
*/
|
|
30
|
+
className?: string
|
|
31
|
+
children?: ReactNode
|
|
32
|
+
trapped?: boolean
|
|
33
|
+
autoFocus?: boolean
|
|
34
|
+
restoreFocus?: boolean
|
|
35
|
+
style?: StyleProp<ViewStyle>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Universal `<FocusScope>` component for React Native.
|
|
40
|
+
* Renders an accessible trapping container for modal dialogs and overlays.
|
|
41
|
+
*/
|
|
42
|
+
export function FocusScope({ children, trapped = true, style, ...props }: FocusScopeProps) {
|
|
43
|
+
return React.createElement(
|
|
44
|
+
View,
|
|
45
|
+
{
|
|
46
|
+
accessible: true,
|
|
47
|
+
accessibilityViewIsModal: trapped,
|
|
48
|
+
style,
|
|
49
|
+
...props,
|
|
50
|
+
},
|
|
51
|
+
children,
|
|
52
|
+
)
|
|
53
|
+
}
|