-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement React Native Reanimated V2
- Loading branch information
1 parent
62a3ed6
commit 87f6095
Showing
6 changed files
with
355 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"presets": [ | ||
"module:metro-react-native-babel-preset" | ||
], | ||
"plugins": [ | ||
"react-native-reanimated/plugin" | ||
] | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { withTiming, withDelay } from 'react-native-reanimated'; | ||
|
||
// Worklets | ||
|
||
export function applyAnimationsToStyle(animations, style) { | ||
return function() { | ||
'worklet' | ||
|
||
var animatedStyle = {} | ||
|
||
for (var key in animations) { | ||
if (key == "transform") { | ||
var transforms = animations[key]; | ||
var animatedTransforms = [] | ||
|
||
for (var transform of transforms) { | ||
var transformKey = Object.keys(transform)[0]; | ||
animatedTransforms.push({ | ||
[transformKey]: transform[transformKey].value | ||
}) | ||
} | ||
|
||
animatedStyle[key] = animatedTransforms; | ||
} else { | ||
animatedStyle[key] = animations[key].value; | ||
} | ||
} | ||
|
||
return Object.assign(animatedStyle, style); | ||
}; | ||
}; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(ns quo2.reanimated | ||
(:require ["react-native" :as rn] | ||
[reagent.core :as reagent] | ||
[clojure.string :as string] | ||
["react-native-reanimated" :default reanimated | ||
:refer (enableLayoutAnimations useSharedValue useAnimatedStyle withTiming Easing Keyframe)])) | ||
|
||
;; We can Remove manually enabling layout animations, | ||
;; once react-native-reanimated is updated to 2.5.0 | ||
(defonce enable-layout-animation (enableLayoutAnimations true)) | ||
|
||
;; Animated Components | ||
(def create-animated-component (comp reagent/adapt-react-class (.-createAnimatedComponent reanimated))) | ||
|
||
(def view (reagent/adapt-react-class (.-View reanimated))) | ||
(def image (reagent/adapt-react-class (.-Image reanimated))) | ||
(def touchable-opacity (create-animated-component (.-TouchableOpacity ^js rn))) | ||
|
||
;; Hooks | ||
(def use-shared-value useSharedValue) | ||
(def use-animated-style useAnimatedStyle) | ||
|
||
;; Animations | ||
(def with-timing withTiming) | ||
(def key-frame Keyframe) | ||
|
||
;; Easings | ||
(def bezier (.-bezier ^js Easing)) | ||
|
||
(def easings {:easing1 (bezier 0.25 0.1 0.25 1) ;; TODO - rename easing functions, (design team input) | ||
:easing2 (bezier 0 0.3 0.6 0.9)}) | ||
|
||
;; Helper functions | ||
(defn get-shared-value [anim] | ||
(.-value anim)) | ||
|
||
(defn set-shared-value [anim val] | ||
(set! (.-value anim) val)) | ||
|
||
(defn kebab-case->camelCase [k] | ||
(let [words (string/split (name k) #"-")] | ||
(->> (map string/capitalize (rest words)) | ||
(apply str (first words)) | ||
keyword))) | ||
|
||
(defn map-keys [f m] | ||
(->> (map (fn [[k v]] [(f k) v]) m) | ||
(into {}))) | ||
|
||
;; Worklets | ||
(def worklet-factory (js/require "../js/worklet_factory.js")) | ||
|
||
;;;; Component Animations | ||
|
||
;; kebab-case styles are not working for worklets | ||
;; so first converting kebab case styles into camel case styles | ||
(defn apply-animations-to-style [animations style] | ||
(let [animations (map-keys kebab-case->camelCase animations) | ||
style (apply dissoc (map-keys kebab-case->camelCase style) (keys animations))] | ||
(use-animated-style | ||
(.applyAnimationsToStyle ^js worklet-factory (clj->js animations) (clj->js style))))) | ||
|
||
;; Animators | ||
(defn animate-shared-value-with-timing [anim val duration easing] | ||
(set-shared-value anim (with-timing val (clj->js {:duration duration | ||
:easing (get easings easing)})))) |
Oops, something went wrong.