@fabricator-ui/react-native 0.0.0-stage → 0.10.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.
Files changed (46) hide show
  1. package/README.md +8 -2
  2. package/dist/LICENSE.md +25 -0
  3. package/dist/NOTICE.md +24 -0
  4. package/dist/lib/data-variants.ts +23 -0
  5. package/dist/lib/density-scale.ts +208 -0
  6. package/dist/lib/fabricator/css-object.ts +2 -0
  7. package/dist/lib/fabricator/density-scale.ts +210 -0
  8. package/dist/lib/fabricator/density-variables.ts +37 -0
  9. package/dist/lib/fabricator/motion-speeds.ts +566 -0
  10. package/dist/lib/fabricator/radius.ts +240 -0
  11. package/dist/lib/feedback.ts +124 -0
  12. package/dist/lib/field-frame.ts +53 -0
  13. package/dist/lib/field-model.ts +112 -0
  14. package/dist/lib/fluid-hover-model.ts +142 -0
  15. package/dist/lib/fluid-hover.tsx +788 -0
  16. package/dist/lib/icon-stroke.ts +38 -0
  17. package/dist/lib/motion-tokens.ts +97 -0
  18. package/dist/lib/motion.ts +292 -0
  19. package/dist/lib/overlay-model.ts +205 -0
  20. package/dist/lib/press-wash.tsx +73 -0
  21. package/dist/lib/render.tsx +53 -0
  22. package/dist/lib/select-model.ts +87 -0
  23. package/dist/lib/sheet-model.ts +101 -0
  24. package/dist/lib/sheet-surface.tsx +429 -0
  25. package/dist/lib/sound-recipes.ts +449 -0
  26. package/dist/lib/spring-model.ts +214 -0
  27. package/dist/lib/surface-color.tsx +27 -0
  28. package/dist/lib/text-children.ts +43 -0
  29. package/dist/styles.css +610 -0
  30. package/dist/ui/badge.tsx +108 -0
  31. package/dist/ui/button.tsx +285 -0
  32. package/dist/ui/card.tsx +365 -0
  33. package/dist/ui/checkbox.tsx +230 -0
  34. package/dist/ui/collapsible.tsx +194 -0
  35. package/dist/ui/density.tsx +79 -0
  36. package/dist/ui/dialog.tsx +232 -0
  37. package/dist/ui/dropdown-menu.tsx +687 -0
  38. package/dist/ui/fabricator-root.tsx +68 -0
  39. package/dist/ui/input.tsx +63 -0
  40. package/dist/ui/label.tsx +22 -0
  41. package/dist/ui/select.tsx +616 -0
  42. package/dist/ui/switch.tsx +177 -0
  43. package/dist/ui/tabs.tsx +272 -0
  44. package/dist/ui/text.tsx +125 -0
  45. package/dist/ui/textarea.tsx +65 -0
  46. package/package.json +54 -4
@@ -0,0 +1,788 @@
1
+ import * as React from "react"
2
+ import { useEffectEvent as useLatestCallback } from "@rn-primitives/hooks"
3
+ import { cn } from "cn"
4
+ import { ScrollView, View, type LayoutChangeEvent } from "react-native"
5
+ import { Gesture, GestureDetector } from "react-native-gesture-handler"
6
+ import Animated, {
7
+ Easing,
8
+ ReduceMotion,
9
+ useAnimatedStyle,
10
+ useSharedValue,
11
+ withDelay,
12
+ withSpring,
13
+ withTiming,
14
+ type SharedValue,
15
+ } from "react-native-reanimated"
16
+ import { scheduleOnRN } from "react-native-worklets"
17
+
18
+ import {
19
+ getRectEdgeSprings,
20
+ type SpringConfig,
21
+ } from "./spring-model"
22
+ import { useFeedback } from "./feedback"
23
+ import {
24
+ createFluidHoverStore,
25
+ getFadeOutDelay,
26
+ isNearRows,
27
+ pickNearestRow,
28
+ sameRect,
29
+ shouldGlide,
30
+ type FluidAxis,
31
+ type FluidRow,
32
+ type Rect,
33
+ } from "./fluid-hover-model"
34
+ import { useMotion, useMotionSprings } from "./motion"
35
+
36
+ // A drag this far along the row's axis starts tracking, and across it gives way
37
+ // to whatever is underneath, so a strip inside a list still scrolls.
38
+ const TRACK_SLOP = 6
39
+ const CROSS_SLOP = 12
40
+ // Holding this long starts tracking in a list that also scrolls: a flick scrolls,
41
+ // and a slow tap stays a tap, since tracking taking over cancels the press.
42
+ const HOLD_TO_TRACK = 400
43
+ // Beyond this far from the rows, nothing is lit and letting go does nothing.
44
+ const RELEASE_SLOP = 32
45
+ // Rows in a list that scrolls wait this long to light, as iOS tables do: a finger
46
+ // that's starting a scroll doesn't flash the row it landed on. A tap still shows.
47
+ const SCROLL_PRESS_DELAY = 90
48
+ // The selection steps back while another row is lit: "current", against "next".
49
+ const DIMMED_SELECTION = 0.85
50
+
51
+ type OverlayValues = {
52
+ x: SharedValue<number>
53
+ y: SharedValue<number>
54
+ right: SharedValue<number>
55
+ bottom: SharedValue<number>
56
+ opacity: SharedValue<number>
57
+ }
58
+
59
+ type MeasurableView = {
60
+ measureLayout: (
61
+ relativeTo: MeasurableView,
62
+ onSuccess: (x: number, y: number, width: number, height: number) => void,
63
+ onFail?: () => void
64
+ ) => void
65
+ }
66
+
67
+ function isMeasurable(target: unknown): target is MeasurableView {
68
+ return (
69
+ typeof target === "object" && target !== null && "measureLayout" in target
70
+ )
71
+ }
72
+
73
+ function useOverlayValues(): OverlayValues {
74
+ const x = useSharedValue(0)
75
+ const y = useSharedValue(0)
76
+ const right = useSharedValue(0)
77
+ const bottom = useSharedValue(0)
78
+ const opacity = useSharedValue(0)
79
+ return React.useMemo(
80
+ () => ({ x, y, right, bottom, opacity }),
81
+ [x, y, right, bottom, opacity]
82
+ )
83
+ }
84
+
85
+ type EdgeTravel = { lead: SpringConfig; trail: SpringConfig }
86
+
87
+ function placeOverlay(
88
+ overlay: OverlayValues,
89
+ rect: Rect,
90
+ travel: EdgeTravel | null
91
+ ) {
92
+ "worklet"
93
+ const right = rect.left + rect.width
94
+ const bottom = rect.top + rect.height
95
+ if (travel) {
96
+ const springs = getRectEdgeSprings(
97
+ {
98
+ left: overlay.x.get(),
99
+ top: overlay.y.get(),
100
+ right: overlay.right.get(),
101
+ bottom: overlay.bottom.get(),
102
+ },
103
+ { left: rect.left, top: rect.top, right, bottom },
104
+ travel.lead,
105
+ travel.trail
106
+ )
107
+ overlay.x.set(withSpring(rect.left, { ...springs.left, mass: 1 }))
108
+ overlay.y.set(withSpring(rect.top, { ...springs.top, mass: 1 }))
109
+ overlay.right.set(withSpring(right, { ...springs.right, mass: 1 }))
110
+ overlay.bottom.set(withSpring(bottom, { ...springs.bottom, mass: 1 }))
111
+ return
112
+ }
113
+ overlay.x.set(rect.left)
114
+ overlay.y.set(rect.top)
115
+ overlay.right.set(right)
116
+ overlay.bottom.set(bottom)
117
+ }
118
+
119
+ export type FluidHoverOptions = {
120
+ /** The key of the row that gets the sliding selection background. */
121
+ selected?: string
122
+ /** Which way the rows run: y for lists, x for strips, xy for grids. */
123
+ axis?: FluidAxis
124
+ /**
125
+ * What a drag does. `drag` follows the finger at once, for strips and lists
126
+ * that don't scroll. `hold` follows it after a short hold, so a list that
127
+ * scrolls still scrolls. `none` leaves a drag to whatever is underneath.
128
+ */
129
+ track?: "none" | "drag" | "hold"
130
+ /**
131
+ * The highlight previews the next selection, as tabs' does: the selection dims
132
+ * while another row is lit, and the highlight hides over the selected row.
133
+ */
134
+ previewsSelection?: boolean
135
+ }
136
+
137
+ type RowOptions = { disabled?: boolean; onActivate?: () => void }
138
+
139
+ export function useFluidHover({
140
+ selected,
141
+ axis = "y",
142
+ track = "none",
143
+ previewsSelection = false,
144
+ }: FluidHoverOptions = {}) {
145
+ const glide = useMotion("glide")
146
+ const feedback = useFeedback()
147
+ const container = React.useRef<MeasurableView | null>(null)
148
+ const rects = React.useRef(new Map<string, Rect>())
149
+ const hosts = React.useRef(new Map<string, MeasurableView>())
150
+ const remeasureFrame = React.useRef<number | null>(null)
151
+ const rowOptions = React.useRef(new Map<string, RowOptions>())
152
+ const selectionRect = React.useRef<Rect | null>(null)
153
+ const pressedAt = React.useRef(0)
154
+ const rows = useSharedValue<FluidRow[]>([])
155
+ const litKey = useSharedValue<string | null>(null)
156
+ const isTracking = useSharedValue(false)
157
+ const hover = useOverlayValues()
158
+ const selection = useOverlayValues()
159
+ const [lit, setCurrentLit] = React.useState<string | null>(null)
160
+ const [litStore] = React.useState(createFluidHoverStore)
161
+ const setLit = React.useCallback(
162
+ (key: string | null) => {
163
+ litStore.set(key)
164
+ setCurrentLit(key)
165
+ },
166
+ [litStore]
167
+ )
168
+
169
+ // Reduce Motion and motion off drop the travel; the fades stay.
170
+ const springs = useMotionSprings()
171
+ const edgeTravel = React.useMemo(
172
+ () =>
173
+ springs.isReduced || !springs.springs
174
+ ? null
175
+ : { lead: springs.springs.lead, trail: springs.springs.trail },
176
+ [springs]
177
+ )
178
+ // Reduce Motion drops the travel and keeps the fade, which Reanimated would
179
+ // otherwise jump to its end.
180
+ const fadeIn = React.useMemo(
181
+ () => ({
182
+ duration: glide.enterMs,
183
+ easing: Easing.linear,
184
+ reduceMotion: ReduceMotion.Never,
185
+ }),
186
+ [glide.enterMs]
187
+ )
188
+ const fadeOut = React.useMemo(
189
+ () => ({
190
+ duration: glide.exitMs,
191
+ easing: Easing.linear,
192
+ reduceMotion: ReduceMotion.Never,
193
+ }),
194
+ [glide.exitMs]
195
+ )
196
+
197
+ const syncRows = React.useCallback(() => {
198
+ rows.set(
199
+ Array.from(rects.current, ([key, rect]) => ({
200
+ key,
201
+ rect,
202
+ disabled: rowOptions.current.get(key)?.disabled,
203
+ }))
204
+ )
205
+ }, [rows])
206
+
207
+ // The selection appears at once, so a page never opens with its pill fading in,
208
+ // and stretches between rows on the edge springs.
209
+ const syncSelection = useLatestCallback(
210
+ (selectedKey: string | undefined = selected) => {
211
+ const rect = selectedKey ? (rects.current.get(selectedKey) ?? null) : null
212
+ const previous = selectionRect.current
213
+ if (sameRect(previous, rect)) return
214
+ selectionRect.current = rect
215
+ if (!rect) {
216
+ selection.opacity.set(withTiming(0, fadeOut))
217
+ return
218
+ }
219
+ placeOverlay(
220
+ selection,
221
+ rect,
222
+ shouldGlide(previous, rect) ? edgeTravel : null
223
+ )
224
+ selection.opacity.set(1)
225
+ }
226
+ )
227
+
228
+ React.useEffect(() => syncSelection(selected), [selected, syncSelection])
229
+
230
+ const storeRect = React.useCallback(
231
+ (key: string, rect: Rect) => {
232
+ if (sameRect(rects.current.get(key) ?? null, rect)) return
233
+ rects.current.set(key, rect)
234
+ syncRows()
235
+ syncSelection()
236
+ },
237
+ [syncRows, syncSelection]
238
+ )
239
+
240
+ const measureRow = React.useCallback(
241
+ (key: string, host: MeasurableView, onDone?: () => void) => {
242
+ const root = container.current
243
+ if (!root) {
244
+ onDone?.()
245
+ return
246
+ }
247
+ host.measureLayout(
248
+ root,
249
+ (left, top, width, height) => {
250
+ storeRect(key, { top, left, width, height })
251
+ onDone?.()
252
+ },
253
+ () => onDone?.()
254
+ )
255
+ },
256
+ [storeRect]
257
+ )
258
+
259
+ // A row's own layout doesn't change when one above it hides or grows, so a
260
+ // change to any row measures them all, once per frame.
261
+ const remeasureRows = React.useCallback(() => {
262
+ if (remeasureFrame.current !== null) return
263
+ remeasureFrame.current = requestAnimationFrame(() => {
264
+ remeasureFrame.current = null
265
+ for (const [key, host] of hosts.current) measureRow(key, host)
266
+ })
267
+ }, [measureRow])
268
+
269
+ React.useEffect(
270
+ () => () => {
271
+ if (remeasureFrame.current !== null) {
272
+ cancelAnimationFrame(remeasureFrame.current)
273
+ }
274
+ },
275
+ []
276
+ )
277
+
278
+ const register = React.useCallback(
279
+ (key: string, options: RowOptions) => {
280
+ rowOptions.current.set(key, options)
281
+ syncRows()
282
+ },
283
+ [syncRows]
284
+ )
285
+
286
+ const unregister = React.useCallback(
287
+ (key: string) => {
288
+ rowOptions.current.delete(key)
289
+ rects.current.delete(key)
290
+ hosts.current.delete(key)
291
+ syncRows()
292
+ syncSelection()
293
+ remeasureRows()
294
+ },
295
+ [remeasureRows, syncRows, syncSelection]
296
+ )
297
+
298
+ React.useEffect(() => {
299
+ if (!previewsSelection || !selectionRect.current) return
300
+ selection.opacity.set(
301
+ withTiming(
302
+ lit !== null && lit !== selected ? DIMMED_SELECTION : 1,
303
+ fadeIn
304
+ )
305
+ )
306
+ }, [fadeIn, lit, previewsSelection, selected, selection])
307
+
308
+ const light = React.useCallback(
309
+ (key: string) => {
310
+ if (rowOptions.current.get(key)?.disabled) return
311
+ const rect = rects.current.get(key)
312
+ if (!rect) return
313
+ pressedAt.current = Date.now()
314
+ setLit(key)
315
+ // Still fading from the last press: glide from there. Otherwise appear.
316
+ placeOverlay(hover, rect, hover.opacity.get() > 0.01 ? edgeTravel : null)
317
+ hover.opacity.set(
318
+ withTiming(previewsSelection && key === selected ? 0 : 1, fadeIn)
319
+ )
320
+ },
321
+ [fadeIn, edgeTravel, hover, previewsSelection, selected, setLit]
322
+ )
323
+
324
+ const release = React.useCallback(() => {
325
+ setLit(null)
326
+ const delay = getFadeOutDelay(Date.now() - pressedAt.current, glide.enterMs)
327
+ hover.opacity.set(withDelay(delay, withTiming(0, fadeOut)))
328
+ }, [fadeOut, glide.enterMs, hover, setLit])
329
+
330
+ const getItemProps = React.useCallback(
331
+ (key: string) => ({
332
+ onLayout: (event: LayoutChangeEvent) => {
333
+ const { x, y, width, height } = event.nativeEvent.layout
334
+ const target = event.target
335
+ if (!container.current || !isMeasurable(target)) {
336
+ storeRect(key, { top: y, left: x, width, height })
337
+ return
338
+ }
339
+ hosts.current.set(key, target)
340
+ remeasureRows()
341
+ },
342
+ onPressIn: () => {
343
+ // Fresh: the row may have moved since it was last measured.
344
+ const host = hosts.current.get(key)
345
+ if (!host) {
346
+ light(key)
347
+ return
348
+ }
349
+ measureRow(key, host, () => light(key))
350
+ },
351
+ onPressOut: () => {
352
+ // The gesture took over this touch, and owns the highlight until it ends.
353
+ if (!isTracking.get()) release()
354
+ },
355
+ onHoverIn: () => light(key),
356
+ onHoverOut: () => release(),
357
+ }),
358
+ [isTracking, light, measureRow, release, remeasureRows, storeRect]
359
+ )
360
+
361
+ const activate = useLatestCallback((key: string) => {
362
+ rowOptions.current.get(key)?.onActivate?.()
363
+ })
364
+
365
+ const handleTrackChange = useLatestCallback((key: string | null) => {
366
+ setLit(key)
367
+ if (key) feedback("tick")
368
+ })
369
+
370
+ const gesture = React.useMemo(() => {
371
+ const trackTo = (point: { x: number; y: number }) => {
372
+ "worklet"
373
+ const list = rows.get()
374
+ const next = isNearRows(list, point, RELEASE_SLOP)
375
+ ? pickNearestRow(list, point, axis)
376
+ : null
377
+ if (next === litKey.get()) return
378
+ litKey.set(next)
379
+ scheduleOnRN(handleTrackChange, next)
380
+ const rect = next ? list.find((row) => row.key === next)?.rect : null
381
+ if (!rect) {
382
+ hover.opacity.set(withTiming(0, fadeOut))
383
+ return
384
+ }
385
+ placeOverlay(hover, rect, hover.opacity.get() > 0.01 ? edgeTravel : null)
386
+ hover.opacity.set(
387
+ withTiming(previewsSelection && next === selected ? 0 : 1, fadeIn)
388
+ )
389
+ }
390
+ const slop: [number, number] = [-TRACK_SLOP, TRACK_SLOP]
391
+ const crossSlop: [number, number] = [-CROSS_SLOP, CROSS_SLOP]
392
+ const base = Gesture.Pan()
393
+ .enabled(track !== "none")
394
+ .shouldCancelWhenOutside(false)
395
+ const configured =
396
+ track === "hold"
397
+ ? base.activateAfterLongPress(HOLD_TO_TRACK)
398
+ : axis === "x"
399
+ ? base.activeOffsetX(slop).failOffsetY(crossSlop)
400
+ : axis === "y"
401
+ ? base.activeOffsetY(slop).failOffsetX(crossSlop)
402
+ : base.minDistance(TRACK_SLOP)
403
+ return configured
404
+ .onStart((event) => {
405
+ "worklet"
406
+ isTracking.set(true)
407
+ trackTo({ x: event.x, y: event.y })
408
+ })
409
+ .onUpdate((event) => {
410
+ "worklet"
411
+ trackTo({ x: event.x, y: event.y })
412
+ })
413
+ .onEnd((_event, success) => {
414
+ "worklet"
415
+ const key = litKey.get()
416
+ if (success && key) scheduleOnRN(activate, key)
417
+ })
418
+ .onFinalize(() => {
419
+ "worklet"
420
+ if (!isTracking.get()) return
421
+ isTracking.set(false)
422
+ litKey.set(null)
423
+ scheduleOnRN(handleTrackChange, null)
424
+ hover.opacity.set(withTiming(0, fadeOut))
425
+ })
426
+ }, [
427
+ activate,
428
+ axis,
429
+ fadeIn,
430
+ fadeOut,
431
+ edgeTravel,
432
+ handleTrackChange,
433
+ hover,
434
+ isTracking,
435
+ litKey,
436
+ previewsSelection,
437
+ rows,
438
+ selected,
439
+ track,
440
+ ])
441
+
442
+ const attach = React.useCallback((node: unknown) => {
443
+ container.current = isMeasurable(node) ? node : null
444
+ }, [])
445
+
446
+ const pressDelay = track === "drag" ? 0 : SCROLL_PRESS_DELAY
447
+ const rowContext = React.useMemo(
448
+ () => ({
449
+ getItemProps,
450
+ register,
451
+ unregister,
452
+ remeasure: remeasureRows,
453
+ pressDelay,
454
+ litStore,
455
+ }),
456
+ [getItemProps, register, unregister, remeasureRows, pressDelay, litStore]
457
+ )
458
+
459
+ return React.useMemo(
460
+ () => ({
461
+ /** Ref callback for the container that holds the rows. */
462
+ attach,
463
+ /** Wraps the container through FluidHoverArea. */
464
+ gesture,
465
+ isTrackable: track !== "none",
466
+ pressDelay,
467
+ rowContext,
468
+ getItemProps,
469
+ register,
470
+ unregister,
471
+ /** Measures every row again, for when something moved them. */
472
+ remeasure: remeasureRows,
473
+ /** The lit row's key, for what styles a lit row. */
474
+ lit,
475
+ hover,
476
+ selection,
477
+ }),
478
+ [
479
+ attach,
480
+ gesture,
481
+ getItemProps,
482
+ hover,
483
+ lit,
484
+ register,
485
+ remeasureRows,
486
+ selection,
487
+ pressDelay,
488
+ rowContext,
489
+ track,
490
+ unregister,
491
+ ]
492
+ )
493
+ }
494
+
495
+ type FluidHover = ReturnType<typeof useFluidHover>
496
+
497
+ const FluidHoverContext = React.createContext<FluidHover["rowContext"] | null>(
498
+ null
499
+ )
500
+
501
+ /** Lets the rows inside find their container. */
502
+ function FluidHoverProvider({
503
+ fluid,
504
+ children,
505
+ }: {
506
+ fluid: FluidHover
507
+ children?: React.ReactNode
508
+ }) {
509
+ return (
510
+ <FluidHoverContext value={fluid.rowContext}>{children}</FluidHoverContext>
511
+ )
512
+ }
513
+
514
+ const subscribeNever = () => () => {}
515
+
516
+ /** Also lets a table row follow the highlight on its next row. */
517
+ function useFluidRowLit(key: string) {
518
+ const store = React.use(FluidHoverContext)?.litStore
519
+ const subscribe = React.useCallback(
520
+ (listener: () => void) =>
521
+ store ? store.subscribe(key, listener) : subscribeNever(),
522
+ [key, store]
523
+ )
524
+ return React.useSyncExternalStore(subscribe, () => store?.get() === key)
525
+ }
526
+
527
+ /**
528
+ * A row in the nearest container. Spread `rowProps` on its Pressable. Outside a
529
+ * container it passes the handlers through, so the row still works alone.
530
+ */
531
+ function useFluidRow<
532
+ TLayout extends LayoutChangeEvent = LayoutChangeEvent,
533
+ TPress = unknown,
534
+ THover = unknown,
535
+ >({
536
+ itemKey,
537
+ disabled,
538
+ onActivate,
539
+ onLayout,
540
+ onPressIn,
541
+ onPressOut,
542
+ onHoverIn,
543
+ onHoverOut,
544
+ }: {
545
+ /** A stable key, when the row has one. Otherwise it gets its own. */
546
+ itemKey?: string
547
+ disabled?: boolean | null
548
+ /** What letting go over this row does, after a drag. */
549
+ onActivate?: () => void
550
+ onLayout?: ((event: TLayout) => void) | null
551
+ onPressIn?: ((event: TPress) => void) | null
552
+ onPressOut?: ((event: TPress) => void) | null
553
+ onHoverIn?: ((event: THover) => void) | null
554
+ onHoverOut?: ((event: THover) => void) | null
555
+ } = {}) {
556
+ const fluid = React.use(FluidHoverContext)
557
+ const id = React.useId()
558
+ const key = itemKey ?? id
559
+ const isLit = useFluidRowLit(key)
560
+ const activate = useLatestCallback(() => onActivate?.())
561
+ const isDisabled = Boolean(disabled)
562
+ const register = fluid?.register
563
+ const unregister = fluid?.unregister
564
+ React.useEffect(() => {
565
+ register?.(key, { disabled: isDisabled, onActivate: activate })
566
+ }, [activate, isDisabled, key, register])
567
+ React.useEffect(() => () => unregister?.(key), [key, unregister])
568
+ const item = fluid?.getItemProps(key)
569
+ return {
570
+ key,
571
+ isLit,
572
+ /** Measures the rows again, when this one moved without its layout changing. */
573
+ remeasure: fluid?.remeasure,
574
+ rowProps: {
575
+ unstable_pressDelay: fluid?.pressDelay,
576
+ onLayout: (event: TLayout) => {
577
+ item?.onLayout(event)
578
+ onLayout?.(event)
579
+ },
580
+ onPressIn: (event: TPress) => {
581
+ item?.onPressIn()
582
+ onPressIn?.(event)
583
+ },
584
+ onPressOut: (event: TPress) => {
585
+ item?.onPressOut()
586
+ onPressOut?.(event)
587
+ },
588
+ onHoverIn: (event: THover) => {
589
+ item?.onHoverIn()
590
+ onHoverIn?.(event)
591
+ },
592
+ onHoverOut: (event: THover) => {
593
+ item?.onHoverOut()
594
+ onHoverOut?.(event)
595
+ },
596
+ },
597
+ }
598
+ }
599
+
600
+ /** Measures the nearest container's rows again, for what moves them all. */
601
+ function useFluidRemeasure() {
602
+ return React.use(FluidHoverContext)?.remeasure
603
+ }
604
+
605
+ /** Follows the finger, where the container's `track` asks for it. */
606
+ function FluidHoverArea({
607
+ fluid,
608
+ children,
609
+ }: {
610
+ fluid: FluidHover
611
+ children: React.ReactElement
612
+ }) {
613
+ if (!fluid.isTrackable) return children
614
+ return <GestureDetector gesture={fluid.gesture}>{children}</GestureDetector>
615
+ }
616
+
617
+ function FluidHoverOverlay({
618
+ overlay,
619
+ className,
620
+ style,
621
+ ...props
622
+ }: {
623
+ overlay: OverlayValues
624
+ } & React.ComponentProps<typeof Animated.View>) {
625
+ const animatedStyle = useAnimatedStyle(() => ({
626
+ opacity: overlay.opacity.value,
627
+ width: Math.max(0, overlay.right.get() - overlay.x.get()),
628
+ height: Math.max(0, overlay.bottom.get() - overlay.y.get()),
629
+ transform: [
630
+ { translateX: overlay.x.get() },
631
+ { translateY: overlay.y.get() },
632
+ ],
633
+ }))
634
+ return (
635
+ <Animated.View
636
+ pointerEvents="none"
637
+ importantForAccessibility="no-hide-descendants"
638
+ accessibilityElementsHidden
639
+ className={cn("absolute top-0 left-0", className)}
640
+ style={[animatedStyle, style]}
641
+ {...props}
642
+ />
643
+ )
644
+ }
645
+
646
+ /** The hover highlight. Render it as the first child of the container. */
647
+ function FluidHoverHighlight({
648
+ hover,
649
+ className,
650
+ ...props
651
+ }: { hover: OverlayValues } & React.ComponentProps<typeof Animated.View>) {
652
+ return (
653
+ <FluidHoverOverlay
654
+ overlay={hover}
655
+ data-slot="fluid-hover-highlight"
656
+ className={cn("bg-hover rounded-(--radius-control) corner-curve", className)}
657
+ {...props}
658
+ />
659
+ )
660
+ }
661
+
662
+ /** The selection background. Render it before the highlight. */
663
+ function FluidHoverSelection({
664
+ selection,
665
+ className,
666
+ ...props
667
+ }: { selection: OverlayValues } & React.ComponentProps<typeof Animated.View>) {
668
+ return (
669
+ <FluidHoverOverlay
670
+ overlay={selection}
671
+ data-slot="fluid-hover-selection"
672
+ className={cn("bg-active rounded-(--radius-control) corner-curve", className)}
673
+ {...props}
674
+ />
675
+ )
676
+ }
677
+
678
+ /**
679
+ * The usual container: a view that holds the rows, its highlight and its finger.
680
+ * Rows inside use `useFluidRow`.
681
+ */
682
+ function FluidHoverList({
683
+ axis,
684
+ track,
685
+ selected,
686
+ highlightClassName,
687
+ selectionClassName,
688
+ className,
689
+ children,
690
+ ...props
691
+ }: FluidHoverOptions & {
692
+ highlightClassName?: string
693
+ selectionClassName?: string
694
+ } & React.ComponentProps<typeof View>) {
695
+ const fluid = useFluidHover({ axis, track, selected })
696
+ const { attach } = fluid
697
+ const attachContainer = React.useCallback(
698
+ (node: unknown) => attach(node),
699
+ [attach]
700
+ )
701
+ return (
702
+ <FluidHoverProvider fluid={fluid}>
703
+ <FluidHoverArea fluid={fluid}>
704
+ <View
705
+ ref={attachContainer}
706
+ data-slot="fluid-hover-list"
707
+ className={cn("relative", className)}
708
+ {...props}
709
+ >
710
+ {selected === undefined ? null : (
711
+ <FluidHoverSelection
712
+ selection={fluid.selection}
713
+ className={selectionClassName}
714
+ />
715
+ )}
716
+ <FluidHoverHighlight
717
+ hover={fluid.hover}
718
+ className={highlightClassName}
719
+ />
720
+ {children}
721
+ </View>
722
+ </FluidHoverArea>
723
+ </FluidHoverProvider>
724
+ )
725
+ }
726
+
727
+ /**
728
+ * A list in a scroll view, for a menu or a picker: a drag follows the finger while
729
+ * everything fits, and takes a short hold once it scrolls, so a flick still does.
730
+ * `ScrollComponent` takes a sheet's own scroll view.
731
+ */
732
+ function FluidHoverScrollList({
733
+ ScrollComponent = ScrollView,
734
+ selected,
735
+ listClassName,
736
+ highlightClassName,
737
+ selectionClassName,
738
+ onLayout,
739
+ onContentSizeChange,
740
+ children,
741
+ ...props
742
+ }: Pick<FluidHoverOptions, "selected"> & {
743
+ ScrollComponent?: React.ComponentType<React.ComponentProps<typeof ScrollView>>
744
+ listClassName?: string
745
+ highlightClassName?: string
746
+ selectionClassName?: string
747
+ } & React.ComponentProps<typeof ScrollView>) {
748
+ const [viewport, setViewport] = React.useState(0)
749
+ const [content, setContent] = React.useState(0)
750
+ return (
751
+ <ScrollComponent
752
+ alwaysBounceVertical={false}
753
+ keyboardShouldPersistTaps="handled"
754
+ onLayout={(event) => {
755
+ setViewport(event.nativeEvent.layout.height)
756
+ onLayout?.(event)
757
+ }}
758
+ onContentSizeChange={(width, height) => {
759
+ setContent(height)
760
+ onContentSizeChange?.(width, height)
761
+ }}
762
+ {...props}
763
+ >
764
+ <FluidHoverList
765
+ axis="y"
766
+ track={content > viewport + 1 ? "hold" : "drag"}
767
+ selected={selected}
768
+ className={listClassName}
769
+ highlightClassName={highlightClassName}
770
+ selectionClassName={selectionClassName}
771
+ >
772
+ {children}
773
+ </FluidHoverList>
774
+ </ScrollComponent>
775
+ )
776
+ }
777
+
778
+ export {
779
+ FluidHoverArea,
780
+ FluidHoverHighlight,
781
+ FluidHoverList,
782
+ FluidHoverProvider,
783
+ FluidHoverScrollList,
784
+ FluidHoverSelection,
785
+ useFluidRemeasure,
786
+ useFluidRowLit,
787
+ useFluidRow,
788
+ }