Add useHandleRef as a lighter alternative for useAnimatedRef (#6500)

This commit is contained in:
dan
2024-11-18 22:21:47 +00:00
committed by GitHub
parent 4f0f9eb413
commit 7b6c182723
7 changed files with 75 additions and 54 deletions
+5 -8
View File
@@ -1,11 +1,11 @@
import React, {useRef} from 'react'
import {DimensionValue, Pressable, View} from 'react-native'
import Animated, {AnimatedRef, useAnimatedRef} from 'react-native-reanimated'
import {Image} from 'expo-image'
import {AppBskyEmbedImages} from '@atproto/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {HandleRef, useHandleRef} from '#/lib/hooks/useHandleRef'
import type {Dimensions} from '#/lib/media/types'
import {isNative} from '#/platform/detection'
import {useLargeAltBadgeEnabled} from '#/state/preferences/large-alt-badge'
@@ -68,17 +68,14 @@ export function AutoSizedImage({
image: AppBskyEmbedImages.ViewImage
crop?: 'none' | 'square' | 'constrained'
hideBadge?: boolean
onPress?: (
containerRef: AnimatedRef<React.Component<{}, {}, any>>,
fetchedDims: Dimensions | null,
) => void
onPress?: (containerRef: HandleRef, fetchedDims: Dimensions | null) => void
onLongPress?: () => void
onPressIn?: () => void
}) {
const t = useTheme()
const {_} = useLingui()
const largeAlt = useLargeAltBadgeEnabled()
const containerRef = useAnimatedRef()
const containerRef = useHandleRef()
const fetchedDimsRef = useRef<{width: number; height: number} | null>(null)
let aspectRatio: number | undefined
@@ -109,7 +106,7 @@ export function AutoSizedImage({
const hasAlt = !!image.alt
const contents = (
<Animated.View ref={containerRef} collapsable={false} style={{flex: 1}}>
<View ref={containerRef} collapsable={false} style={{flex: 1}}>
<Image
style={[a.w_full, a.h_full]}
source={image.thumb}
@@ -188,7 +185,7 @@ export function AutoSizedImage({
)}
</View>
) : null}
</Animated.View>
</View>
)
if (cropDisabled) {
+5 -8
View File
@@ -1,11 +1,11 @@
import React from 'react'
import {Pressable, StyleProp, View, ViewStyle} from 'react-native'
import Animated, {AnimatedRef} from 'react-native-reanimated'
import {Image, ImageStyle} from 'expo-image'
import {AppBskyEmbedImages} from '@atproto/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {HandleRef} from '#/lib/hooks/useHandleRef'
import {Dimensions} from '#/lib/media/types'
import {useLargeAltBadgeEnabled} from '#/state/preferences/large-alt-badge'
import {PostEmbedViewContext} from '#/view/com/util/post-embeds/types'
@@ -20,7 +20,7 @@ interface Props {
index: number
onPress?: (
index: number,
containerRefs: AnimatedRef<React.Component<{}, {}, any>>[],
containerRefs: HandleRef[],
fetchedDims: (Dimensions | null)[],
) => void
onLongPress?: EventFunction
@@ -28,7 +28,7 @@ interface Props {
imageStyle?: StyleProp<ImageStyle>
viewContext?: PostEmbedViewContext
insetBorderStyle?: StyleProp<ViewStyle>
containerRefs: AnimatedRef<React.Component<{}, {}, any>>[]
containerRefs: HandleRef[]
thumbDimsRef: React.MutableRefObject<(Dimensions | null)[]>
}
@@ -52,10 +52,7 @@ export function GalleryItem({
const hideBadges =
viewContext === PostEmbedViewContext.FeedEmbedRecordWithMedia
return (
<Animated.View
style={a.flex_1}
ref={containerRefs[index]}
collapsable={false}>
<View style={a.flex_1} ref={containerRefs[index]} collapsable={false}>
<Pressable
onPress={
onPress
@@ -118,6 +115,6 @@ export function GalleryItem({
</Text>
</View>
) : null}
</Animated.View>
</View>
)
}
+7 -7
View File
@@ -1,8 +1,8 @@
import React from 'react'
import {StyleProp, StyleSheet, View, ViewStyle} from 'react-native'
import {AnimatedRef, useAnimatedRef} from 'react-native-reanimated'
import {AppBskyEmbedImages} from '@atproto/api'
import {HandleRef, useHandleRef} from '#/lib/hooks/useHandleRef'
import {PostEmbedViewContext} from '#/view/com/util/post-embeds/types'
import {atoms as a, useBreakpoints} from '#/alf'
import {Dimensions} from '../../lightbox/ImageViewing/@types'
@@ -12,7 +12,7 @@ interface ImageLayoutGridProps {
images: AppBskyEmbedImages.ViewImage[]
onPress?: (
index: number,
containerRefs: AnimatedRef<React.Component<{}, {}, any>>[],
containerRefs: HandleRef[],
fetchedDims: (Dimensions | null)[],
) => void
onLongPress?: (index: number) => void
@@ -43,7 +43,7 @@ interface ImageLayoutGridInnerProps {
images: AppBskyEmbedImages.ViewImage[]
onPress?: (
index: number,
containerRefs: AnimatedRef<React.Component<{}, {}, any>>[],
containerRefs: HandleRef[],
fetchedDims: (Dimensions | null)[],
) => void
onLongPress?: (index: number) => void
@@ -56,10 +56,10 @@ function ImageLayoutGridInner(props: ImageLayoutGridInnerProps) {
const gap = props.gap
const count = props.images.length
const containerRef1 = useAnimatedRef()
const containerRef2 = useAnimatedRef()
const containerRef3 = useAnimatedRef()
const containerRef4 = useAnimatedRef()
const containerRef1 = useHandleRef()
const containerRef2 = useHandleRef()
const containerRef3 = useHandleRef()
const containerRef4 = useHandleRef()
const thumbDimsRef = React.useRef<(Dimensions | null)[]>([])
switch (count) {
+5 -9
View File
@@ -6,13 +6,7 @@ import {
View,
ViewStyle,
} from 'react-native'
import {
AnimatedRef,
measure,
MeasuredDimensions,
runOnJS,
runOnUI,
} from 'react-native-reanimated'
import {MeasuredDimensions, runOnJS, runOnUI} from 'react-native-reanimated'
import {Image} from 'expo-image'
import {
AppBskyEmbedExternal,
@@ -27,6 +21,7 @@ import {
ModerationDecision,
} from '@atproto/api'
import {HandleRef, measureHandle} from '#/lib/hooks/useHandleRef'
import {usePalette} from '#/lib/hooks/usePalette'
import {useLightboxControls} from '#/state/lightbox'
import {useModerationOpts} from '#/state/preferences/moderation-opts'
@@ -163,12 +158,13 @@ export function PostEmbeds({
}
const onPress = (
index: number,
refs: AnimatedRef<React.Component<{}, {}, any>>[],
refs: HandleRef[],
fetchedDims: (Dimensions | null)[],
) => {
const handles = refs.map(r => r.current)
runOnUI(() => {
'worklet'
const rects = refs.map(ref => (ref ? measure(ref) : null))
const rects = handles.map(measureHandle)
runOnJS(_openLightbox)(index, rects, fetchedDims)
})()
}