mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {useState} from 'react'
2import {AnimatedRef, measure, MeasuredDimensions} from 'react-native-reanimated'
3
4export type HandleRef = {
5 (node: any): void
6 current: null | number
7}
8
9// This is a lighterweight alternative to `useAnimatedRef()` for imperative UI thread actions.
10// Render it like <View ref={ref} />, then pass `ref.current` to `measureHandle()` and such.
11export function useHandleRef(): HandleRef {
12 return useState(() => {
13 const ref = (node: any) => {
14 if (node) {
15 ref.current =
16 node._nativeTag ??
17 node.__nativeTag ??
18 node.canonical?.nativeTag ??
19 null
20 } else {
21 ref.current = null
22 }
23 }
24 ref.current = null
25 return ref
26 })[0] as HandleRef
27}
28
29// When using this version, you need to read ref.current on the JS thread, and pass it to UI.
30export function measureHandle(
31 current: number | null,
32): MeasuredDimensions | null {
33 'worklet'
34 if (current !== null) {
35 return measure((() => current) as AnimatedRef<any>)
36 } else {
37 return null
38 }
39}