-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: CSS transition shorthand property behavior in react-native-web (#…
…7019) ## Summary This PR fixes CSS transition properties being overridden by `react-native-web`. It happened because we didn't filter out CSS props from the style object passed to the component in the `render()` method of the `AnimatedComponent`. I also made a few other changes to ensure that the transition shorthand is validated on web as well and preprocessed in a similar way to the native implementation. ## Example Recordings In the previous implementation, the `transitionTimingFunction` prop specified before the `transition` property shorthand was overriding the `transition` prop easing. Web also shown an error in the console that shorthand props shouldn't be mixed with longhand ones. We don't want this error to show up and want to support usage of both at the same time. ### Web | Before | After | |-|-| | <video src="https://github.com/user-attachments/assets/59edb895-5f5d-4202-bc8c-8080166251a9" /> | <video src="https://github.com/user-attachments/assets/bfb052c0-60e7-4431-a8b8-58c85e43da4c" /> | ### iOS This recording is just for comparison to see that the behavior on web is also correct (the same as on the recording below) after changes implemented in this PR https://github.com/user-attachments/assets/6fb73423-28a0-4822-8a5d-5dc08e7e3bd6 <details> <summary>Source code</summary> ```tsx /* eslint-disable perfectionist/sort-objects */ import React, { useReducer } from 'react'; import { Button, StyleSheet, View } from 'react-native'; import { createAnimatedComponent, steps } from 'react-native-reanimated'; const AnimatedView = createAnimatedComponent(View); export default function EmptyExample() { const [state, toggleState] = useReducer((s) => !s, false); return ( <View style={styles.container}> <AnimatedView style={{ width: state ? 200 : 100, height: state ? 200 : 100, transform: [{ rotate: state ? '45deg' : '0deg' }], transitionTimingFunction: steps(2), // overridden by the shorthand backgroundColor: 'red', transition: '5s cubic-bezier(0, -0.54, 1, -0.5), transform 2s ease-in', transitionDuration: 1000, // overrides the shorthand }} /> <Button title="Toggle width" onPress={toggleState} /> </View> ); } const styles = StyleSheet.create({ container: { alignItems: 'center', flex: 1, justifyContent: 'center', }, }); ``` </details>
- Loading branch information
Showing
25 changed files
with
391 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/react-native-reanimated/src/css/component/utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict'; | ||
import type { | ||
Falsy, | ||
RecursiveArray, | ||
RegisteredStyle, | ||
StyleProp, | ||
} from 'react-native'; | ||
|
||
import type { AnyRecord, CSSStyle } from '../types'; | ||
import { isCSSStyleProp } from '../utils/guards'; | ||
|
||
type BaseStyle = CSSStyle | Falsy | RegisteredStyle<CSSStyle>; | ||
type StyleProps = BaseStyle | RecursiveArray<BaseStyle> | readonly BaseStyle[]; | ||
|
||
function filterNonCSSStylePropsRecursive( | ||
props: StyleProps | ||
): StyleProp<CSSStyle> { | ||
if (Array.isArray(props)) { | ||
return props.map(filterNonCSSStylePropsRecursive); | ||
} | ||
|
||
if (!props) { | ||
return props; | ||
} | ||
|
||
if (typeof props === 'object') { | ||
return Object.entries(props).reduce<AnyRecord>((acc, [key, value]) => { | ||
if (!isCSSStyleProp(key)) { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, {}); | ||
} | ||
|
||
return props; | ||
} | ||
|
||
export function filterNonCSSStyleProps( | ||
props: StyleProp<CSSStyle> | ||
): StyleProp<CSSStyle> { | ||
return filterNonCSSStylePropsRecursive(props); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.