Skip to content

Commit ed83baa

Browse files
committed
[fix] Animated support for string transforms
Fix #2528 Fix #2523 Close #2546 Close #2530
1 parent 80e83fa commit ed83baa

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
*/
9+
10+
function parsePixelValues(value: string): string | number {
11+
if (value.indexOf('px') > -1) {
12+
return parseFloat(value);
13+
}
14+
if (!Number.isNaN(Number(value))) {
15+
return Number(value);
16+
}
17+
return value;
18+
}
19+
20+
type TransformArray = Array<{ [key: string]: number | string }>;
21+
22+
export function parseTransform(transformString: string): TransformArray {
23+
if (transformString === 'none') {
24+
return [];
25+
}
26+
27+
const transforms = transformString.trim().split(/\) |\)/);
28+
const transformsArray = [];
29+
30+
transforms.forEach((transform: string) => {
31+
if (!transform) return;
32+
33+
const [nameString, transformValue] = transform.split('(');
34+
const name = nameString.trim();
35+
if (
36+
// Skip 3d entirely for now, since React Native doesn't support most Z axis
37+
name.indexOf('3d') > -1 ||
38+
name === 'scaleZ' ||
39+
name === 'skewZ' ||
40+
name === 'translateZ'
41+
) {
42+
console.error(
43+
`React Strict DOM: transform "${name}" is not supported by React Native`
44+
);
45+
return;
46+
}
47+
48+
const valueArray = transformValue.split(',');
49+
const values = valueArray.map((val) => {
50+
return parsePixelValues(
51+
val.indexOf(')') === val.length - 1 ? val.replace(')', '') : val.trim()
52+
);
53+
});
54+
const value = values.length === 1 ? values[0] : values;
55+
transformsArray.push({ [name]: value });
56+
});
57+
58+
return transformsArray;
59+
}

packages/react-native-web/src/vendor/react-native/Animated/nodes/AnimatedStyle.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import AnimatedWithChildren from './AnimatedWithChildren';
1616
import NativeAnimatedHelper from '../NativeAnimatedHelper';
1717

1818
import StyleSheet from '../../../../exports/StyleSheet';
19+
import { parseTransform } from '../../../../exports/StyleSheet/parseTransform';
1920

2021
const flattenStyle = StyleSheet.flatten;
2122

@@ -24,8 +25,8 @@ function createAnimatedStyle(inputStyle: any): Object {
2425
const animatedStyles = {}
2526
for (const key in style) {
2627
const value = style[key];
27-
if (key === 'transform') {
28-
animatedStyles[key] = new AnimatedTransform(value);
28+
if (key === 'transform' && value != null) {
29+
animatedStyles[key] = new AnimatedTransform(typeof value === 'string' ? parseTransform(value) : value);
2930
}
3031
else if (value instanceof AnimatedNode) {
3132
animatedStyles[key] = value;

0 commit comments

Comments
 (0)