fix nil value in reanimated style crashing at runtime (#14855)

This commit is contained in:
Parvesh Monu 2023-01-23 15:55:24 +05:30 committed by GitHub
parent dc9454defa
commit d3667ad683
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 16 deletions

View File

@ -2,12 +2,42 @@ import { useDerivedValue, interpolate } from 'react-native-reanimated';
// Generic Worklets // Generic Worklets
// 1. kebab-case styles are not working for worklets
// so we have to convert kebab case styles into camel case styles
// 2. remove keys with nil value, else useAnimatedStyle will throw an error
// https://github.com/status-im/status-mobile/issues/14756
export function applyAnimationsToStyle(animations, style) { export function applyAnimationsToStyle(animations, style) {
return function() { return function() {
'worklet' 'worklet'
var animatedStyle = {} var animatedStyle = {}
// Normal Style
for (var key in style) {
if (key == "transform") {
var transforms = style[key];
var filteredTransforms = []
for (var transform of transforms) {
var transformKey = Object.keys(transform)[0];
var transformValue = transform[transformKey];
if(transformValue !== null) {
filteredTransforms.push(
{[transformKey.replace(/-./g, x=>x[1].toUpperCase())]: transformValue}
);
}
}
animatedStyle[key] = filteredTransforms;
} else {
var value = style[key];
if (value !== null) {
animatedStyle[key.replace(/-./g, x=>x[1].toUpperCase())] = value;
}
}
}
// Animations
for (var key in animations) { for (var key in animations) {
if (key == "transform") { if (key == "transform") {
var transforms = animations[key]; var transforms = animations[key];
@ -15,18 +45,24 @@ export function applyAnimationsToStyle(animations, style) {
for (var transform of transforms) { for (var transform of transforms) {
var transformKey = Object.keys(transform)[0]; var transformKey = Object.keys(transform)[0];
animatedTransforms.push({ var transformValue = transform[transformKey].value;
[transformKey]: transform[transformKey].value if (transformValue !== null) {
}) animatedTransforms.push(
{[transformKey.replace(/-./g, x=>x[1].toUpperCase())]: transformValue}
);
}
} }
animatedStyle[key] = animatedTransforms; animatedStyle[key] = animatedTransforms;
} else { } else {
animatedStyle[key] = animations[key].value; var animatedValue = animations[key].value;
if (animatedValue !== null) {
animatedStyle[key.replace(/-./g, x=>x[1].toUpperCase())] = animatedValue;
}
} }
} }
return Object.assign(animatedStyle, style); return animatedStyle;
}; };
}; };

View File

@ -14,7 +14,6 @@
SlideInUp SlideInUp
SlideOutUp SlideOutUp
LinearTransition)] LinearTransition)]
[camel-snake-kebab.core :as csk]
[reagent.core :as reagent] [reagent.core :as reagent]
[utils.collection])) [utils.collection]))
@ -62,7 +61,7 @@
(defn set-shared-value (defn set-shared-value
[anim val] [anim val]
(when anim (when (and anim (some? val))
(set! (.-value anim) val))) (set! (.-value anim) val)))
;; Worklets ;; Worklets
@ -79,17 +78,10 @@
(clj->js extrapolation)))) (clj->js extrapolation))))
;;;; Component Animations ;;;; Component Animations
;; kebab-case styles are not working for worklets
;; so first convert kebab case styles into camel case styles
(defn apply-animations-to-style (defn apply-animations-to-style
[animations style] [animations style]
(let [animations (utils.collection/map-keys csk/->camelCaseString animations) (use-animated-style
style (apply dissoc (.applyAnimationsToStyle ^js worklet-factory (clj->js animations) (clj->js style))))
(utils.collection/map-keys csk/->camelCaseString style)
(keys animations))]
(use-animated-style
(.applyAnimationsToStyle ^js worklet-factory (clj->js animations) (clj->js style)))))
;; Animators ;; Animators
(defn animate-shared-value-with-timing (defn animate-shared-value-with-timing

View File

@ -9,6 +9,7 @@
(set/rename-keys visibility-status-update (set/rename-keys visibility-status-update
{:publicKey :public-key {:publicKey :public-key
:statusType :status-type})) :statusType :status-type}))
(defn <-rpc-settings (defn <-rpc-settings
[settings] [settings]
(-> settings (-> settings