-
Notifications
You must be signed in to change notification settings - Fork 985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switcher and Bottom Tabs Animations and UI Performance Improvements #13470
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
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" | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { useDerivedValue } from 'react-native-reanimated'; | ||
|
||
// Generic 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); | ||
}; | ||
}; | ||
|
||
// Switcher Worklets | ||
|
||
export function switcherCloseButtonOpacity (switcherButtonOpacity) { | ||
return useDerivedValue( | ||
function () { | ||
'worklet' | ||
return 1 - switcherButtonOpacity.value; | ||
} | ||
); | ||
} | ||
|
||
export function switcherScreenRadius (switcherScreenSize) { | ||
return useDerivedValue( | ||
function () { | ||
'worklet' | ||
return switcherScreenSize.value/2; | ||
} | ||
); | ||
} | ||
|
||
export function switcherScreenBottomPosition (switcherScreenRadius, switcherPressedRadius, initalPosition) { | ||
return useDerivedValue( | ||
function () { | ||
'worklet' | ||
return initalPosition + switcherPressedRadius - switcherScreenRadius.value; | ||
} | ||
); | ||
} | ||
|
||
export function switcherContainerBottomPosition (switcherScreenBottom, heightOffset) { | ||
return useDerivedValue( | ||
function () { | ||
'worklet' | ||
return - (switcherScreenBottom.value + heightOffset); | ||
} | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
(ns quo2.reanimated | ||
(:require ["react-native" :as rn] | ||
[reagent.core :as reagent] | ||
[clojure.string :as string] | ||
["react-native-reanimated" :default reanimated | ||
:refer (useSharedValue useAnimatedStyle withTiming withDelay Easing Keyframe)])) | ||
|
||
;; 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 with-delay withDelay) | ||
(def key-frame Keyframe) | ||
|
||
;; 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)}) | ||
|
||
;; 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 "../src/js/worklet_factory.js")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ./js/worklet_factory.js ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason
|
||
|
||
;;;; 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] | ||
(let [animations (map-keys kebab-case->camelCase animations) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could you elaborate why map-keys and kebab-case->camelCase There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, I think kebab case only works with clojure due to reagent, but as worklets are inside js file which is directly processed by metro instead of shadow-cljs (due to js/require for directives) only camel case works there |
||
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)))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
./js/worklet_factory.js ?