Skip to content

Commit

Permalink
Skip the entering transition on the first render of ButtonSolid (#…
Browse files Browse the repository at this point in the history
…374)

## Short description
This PR adds a skip of the `entering` transition on the first render of
`ButtonSolid`

## List of changes proposed in this pull request
- Restore previous code to avoid `entering` transition

### Preview

The `entering` transition in the first render we'd like to remove (play
at **0.25/0.5×**)


https://github.com/user-attachments/assets/b394559b-555e-455c-a4ca-37f7495bac2f




## How to test
Go to the **Buttons** page in the example app and check if there's any
difference between `main` and this branch
  • Loading branch information
dmnplb authored Dec 20, 2024
1 parent 783f4b9 commit a2b813e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 18 additions & 3 deletions src/components/buttons/ButtonSolid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ComponentProps, useCallback } from "react";
import React, { ComponentProps, useCallback, useEffect, useRef } from "react";
import {
GestureResponderEvent,
Pressable,
Expand Down Expand Up @@ -186,6 +186,17 @@ export const ButtonSolid = React.forwardRef<View, ButtonSolidProps>(
[isExperimental]
);

/* Prevent the component from triggering the `isEntering' transition
on the on the first render. Solution from this discussion:
https://github.com/software-mansion/react-native-reanimated/discussions/2513
*/
const isMounted = useRef(false);

useEffect(() => {
// eslint-disable-next-line functional/immutable-data
isMounted.current = true;
}, []);

// Interpolate animation values from `isPressed` values
const pressedAnimationStyle = useAnimatedStyle(() => {
// Link color states to the pressed states
Expand Down Expand Up @@ -255,7 +266,9 @@ export const ButtonSolid = React.forwardRef<View, ButtonSolidProps>(
{loading && (
<Animated.View
style={buttonStyles.buttonInner}
entering={enterTransitionInnerContentSmall}
entering={
isMounted.current ? enterTransitionInnerContentSmall : undefined
}
exiting={exitTransitionInnerContent}
>
<LoadingSpinner color={foregroundColor} />
Expand All @@ -271,7 +284,9 @@ export const ButtonSolid = React.forwardRef<View, ButtonSolidProps>(
reverse flex property to invert the position */
iconPosition === "end" && { flexDirection: "row-reverse" }
]}
entering={enterTransitionInnerContent}
entering={
isMounted.current ? enterTransitionInnerContent : undefined
}
>
{icon && (
<Icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ exports[`Test Buttons Components - Experimental Enabled ButtonSolid Snapshot 1`]
}
>
<View
entering={[Function]}
style={
[
{
Expand Down Expand Up @@ -1038,7 +1037,6 @@ exports[`Test Buttons Components ButtonSolid Snapshot 1`] = `
}
>
<View
entering={[Function]}
style={
[
{
Expand Down

0 comments on commit a2b813e

Please sign in to comment.