status-react/src/react_native/reanimated.cljs

142 lines
5.0 KiB
Plaintext
Raw Normal View History

2022-11-08 18:30:17 +01:00
(ns react-native.reanimated
2022-06-28 22:57:14 +05:30
(:require ["react-native" :as rn]
["react-native-linear-gradient" :default LinearGradient]
["react-native-reanimated" :default reanimated :refer
(useSharedValue useAnimatedStyle
withTiming
withDelay
withSpring
withRepeat
Easing
Keyframe
cancelAnimation
SlideInUp
SlideOutUp
LinearTransition)]
2022-12-20 21:52:28 +08:00
[clojure.string :as string]
[reagent.core :as reagent]))
;; Animations
(def slide-in-up-animation SlideInUp)
(def slide-out-up-animation SlideOutUp)
(def linear-transition LinearTransition)
2022-06-28 22:57:14 +05:30
;; 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)))
(def linear-gradient (create-animated-component LinearGradient))
;; Hooks
2022-06-28 22:57:14 +05:30
(def use-shared-value useSharedValue)
(def use-animated-style useAnimatedStyle)
;; Animations
(def with-timing withTiming)
(def with-delay withDelay)
(def with-spring withSpring)
2022-06-28 22:57:14 +05:30
(def key-frame Keyframe)
(def with-repeat withRepeat)
(def cancel-animation cancelAnimation)
2022-06-28 22:57:14 +05:30
;; Easings
(def bezier (.-bezier ^js Easing))
(def easings
{:linear (bezier 0 0 1 1)
:easing1 (bezier 0.25 0.1 0.25 1) ;; TODO(parvesh) - rename easing functions, (design team input)
:easing2 (bezier 0 0.3 0.6 0.9)
:easing3 (bezier 0.3 0.3 0.3 0.9)})
2022-06-28 22:57:14 +05:30
;; Helper functions
(defn get-shared-value
[anim]
2022-06-28 22:57:14 +05:30
(.-value anim))
(defn set-shared-value
[anim val]
2022-06-28 22:57:14 +05:30
(set! (.-value anim) val))
(defn kebab-case->camelCase
[k]
2022-06-28 22:57:14 +05:30
(let [words (string/split (name k) #"-")]
(->> (map string/capitalize (rest words))
(apply str (first words))
keyword)))
(defn map-keys
[f m]
2022-06-28 22:57:14 +05:30
(->> (map (fn [[k v]] [(f k) v]) m)
(into {})))
;; Worklets
(def worklet-factory (js/require "../src/js/worklet_factory.js"))
(defn interpolate
[shared-value input-range output-range]
(.interpolateValue ^js worklet-factory
shared-value
(clj->js input-range)
(clj->js output-range)))
2022-06-28 22:57:14 +05:30
;;;; 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
[animations style]
2022-06-28 22:57:14 +05:30
(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
(js-obj "duration" duration
"easing" (get easings easing)))))
(defn animate-shared-value-with-delay
[anim val duration easing delay]
(set-shared-value anim
(with-delay delay
(with-timing val
(js-obj "duration" duration
"easing" (get easings easing))))))
(defn animate-shared-value-with-repeat
[anim val duration easing number-of-repetitions reverse?]
(set-shared-value anim
(with-repeat (with-timing val
(js-obj "duration" duration
"easing" (get easings easing)))
number-of-repetitions
reverse?)))
(defn animate-shared-value-with-delay-repeat
([anim val duration easing delay number-of-repetitions]
(animate-shared-value-with-delay-repeat anim val duration easing delay number-of-repetitions false))
([anim val duration easing delay number-of-repetitions reverse?]
(set-shared-value anim
(with-delay delay
(with-repeat
(with-timing val
#js
{:duration duration
:easing (get easings easing)})
number-of-repetitions
reverse?)))))
(defn animate-shared-value-with-spring
[anim val {:keys [mass stiffness damping]}]
(set-shared-value anim
(with-spring val
(js-obj "mass" mass
"damping" damping
"stiffness" stiffness))))