-
Notifications
You must be signed in to change notification settings - Fork 279
Fix fading to avoid white empty white square #5868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ import { | |
| } from 'react-native' | ||
| import Animated, { | ||
| useAnimatedStyle, | ||
| useDerivedValue, | ||
| useSharedValue, | ||
| withTiming | ||
| } from 'react-native-reanimated' | ||
| import Svg, { Path } from 'react-native-svg' | ||
|
|
@@ -42,6 +42,8 @@ export const QrCode: React.FC<Props> = props => { | |
| // Scale the surface to match the container's size: | ||
| const [size, setSize] = React.useState<number>(0) | ||
|
|
||
| const layoutPending = size <= 0 || data == null // loading state includes layout timing to avoid flicker | ||
|
|
||
| const handleLayout = (event: LayoutChangeEvent): void => { | ||
| setSize(event.nativeEvent.layout.height) | ||
| } | ||
|
|
@@ -54,9 +56,14 @@ export const QrCode: React.FC<Props> = props => { | |
| const path = svg.replace(/.*d="([^"]*)".*/, '$1') | ||
|
|
||
| // Handle animation: | ||
| const derivedData = useDerivedValue(() => data) | ||
| const opacity = useSharedValue(0) | ||
|
|
||
| React.useEffect(() => { | ||
| opacity.value = withTiming(data != null ? 1 : 0) | ||
| }, [data, opacity]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: Fade animation completes before QR code view rendersThe opacity animation triggers on |
||
|
|
||
| const fadeStyle = useAnimatedStyle(() => ({ | ||
| opacity: withTiming(derivedData.value != null ? 1 : 0) | ||
| opacity: opacity.value | ||
| })) | ||
|
|
||
| // Create a drawing transform to scale QR cells to device pixels: | ||
|
|
@@ -93,17 +100,18 @@ export const QrCode: React.FC<Props> = props => { | |
| return ( | ||
| <EdgeTouchableWithoutFeedback onPress={onPress}> | ||
| <View style={[styles.container, margin]} onLayout={handleLayout}> | ||
| <ActivityIndicator color={theme.iconTappable} /> | ||
| <Animated.View style={[styles.whiteBox, fadeStyle]}> | ||
| {size <= 0 ? null : ( | ||
| {layoutPending ? ( | ||
| <ActivityIndicator color={theme.iconTappable} /> | ||
| ) : ( | ||
| <Animated.View style={[styles.whiteBox, fadeStyle]}> | ||
| <View style={styles.whiteBoxInner}> | ||
| <Svg height="100%" width="100%" viewBox={viewBox}> | ||
| <Path d={path} fill={theme.qrForegroundColor} /> | ||
| </Svg> | ||
| </View> | ||
| )} | ||
| {icon} | ||
| </Animated.View> | ||
| {icon} | ||
| </Animated.View> | ||
| )} | ||
| </View> | ||
| </EdgeTouchableWithoutFeedback> | ||
| ) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Fade animation completes before QR code view renders
The opacity animation triggers on
datachanges, but theAnimated.Viewonly renders whenlayoutPendingis false (requiring bothsize > 0anddata != null). Ifdatais set before layout completes, the animation runs while theActivityIndicatoris still showing, completing before the QR code view mounts. This causes the QR code to pop in without the intended fade effect, since the animation dependency array doesn't includesizeorlayoutPending.