|
| 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 | +} |
0 commit comments