1.5.1 • Published 6 years ago

react-native-declan v1.5.1

Weekly downloads
3
License
MIT
Repository
github
Last release
6 years ago

Declan is a library that makes it easy to incorporate declarative animations and interactions into your React Native app. It's inspired heavily by the UX language created by Fuse.

Demos

Run the demo app on Expo

https://expo.io/@akalyan/examples

Build and run the demo app on your computer

Clone the repo and run:

cd examples
yarn
yarn start

...then scan the QR code in the Expo app.

Installation

npm install --save react-native-declan

or

yarn add react-native-declan

Usage

We suggest reading the Fuse documentation for Animations to get a better sense for what react-native-declan strives to be and how to implement compelling user interactions with a truly declarative API. In lieu of that, here is a summary.

There are several entities that react-native-declan comprises:

  • Animators - descriptions of how components change
  • Triggers - stimuli that invoke change, wrap a set of Animators
  • Higher-Order Animators - like the offspring of Animator and Trigger, wrap and invoke a set of Animators, but also behave like an Animator
  • Components - what will change
  • Behaviors - give Components capabilities or detects gestures that are used by Triggers

Animators

Animators describe how components will change. There are a handful of basic animators to use: Move, Rotate, Scale, Fade.

New, more complex, animators can be built by combining those basic animators. For an example of this, see the Shake. These new animators should have the same behavior as the basic animators.

Animators essentially wrap the Animated.timing API twice -- once running forward, and another running backward.

Example:

<Move
  getTargetRef={() => elementToMove}
  x={80}
  duration={1000}
  easing={Easing.bounce}
  durationBack={500}
  easing={Easing.linear}
/>

The above snippet describes a movement of the component referenced by elementToMove, which will move right 80 pixels over 1 second with a bounce easing curve...when triggered.

Supported Animators

NamePropsDescriptionNotes
Movex: number, y: numberTranslate by x or y or both-
Scalex: number, y: number, factor: numberScale by x or y or factor (both) from center of AnimatableView-
Rotatedegrees: number, degreesX: number, degreesY: number, degreesZ: numberRotate reference AnimatableView by props from centerdegrees is an alias of degreesZ
Fadevalue: numberChance opacity of referenced AnimatableView to value-
Changefield: string, value: numberChance a style property field of referenced AnimatableView to value(1) Only one of these can apply to an AnimatableView, (2) This Animator does not use the native driver, so use sparingly!
DebugActionmessage: stringJust prints the message to the console (does nothing in reverse)-
Callbackaction: () => anyCalls the action (does nothing in reverse)-

All of the visual Animators above (Move, Scale, Rotate, Fade, Change) also take the following props:

PropTypeDescription
getTargetRef() => AnimatableViewFunction that returns the AnimatableView the Animator should apply to
initialValuenumber or { x: number, y: number }Depending on the type of Trigger used, you may need to specify where the Animator should return to. Some just take a value (e.g. Fade), while others take x and y (e.g. Move)
onFinish() => voidCallback called when Animator finishes playing forward
onFinishBack() => voidCallback called when Animator finishes playing backward
durationnumberDuration of forward animation
durationBacknumberDuration of backward animation
delaynumberDelay before starting forward animation
delayBacknumberDelay before starting backward animation
easingEasingEasing function to apply to forward animation
easingBackEasingEasing function to apply to backward animation
extrapolate'extend' or 'clamp'Extrapolation setting for driver-based Animator
extrapolateLeft'extend' or 'clamp'Extrapolation setting for driver-based Animator
easingBack'extend' or 'clamp'Extrapolation setting for driver-based Animator

Triggers

Triggers actually cause change to happen. This is where we take the user's actions into account. There are three types of triggers:

  • Pulse triggers - play a set of animators forward
  • While triggers - play forward while a condition is met, and play backward otherwise
  • Gesture-responsive triggers - animators that play in response to, and in proportion to, some user gesture

Supported triggers

NamePropsDescriptionNotes
ManualTrigger-A wrapper for a set of Animators that you can trigger by calling .start()-
Mounted-Triggers its Animators when this trigger is mountedPut this at the bottom of the component tree to ensure it mounts last
WhileTruevalue: booleanTriggers its Animators when value evaluates to true, stops the Animators when false-
ScrollPositionAnimationfrom: number, to: number, driver: ScrollDriverGesture-based trigger that plays its Animators forward when ScrollView is scrolled from from to toSee Scroll Position Demo for an example of how to hook it up to the ScrollView
WhileScrollingdirection: up, down, or either, driver: ScrollDriverPlays forward when associated ScrollView is scrolling in direction, otherwise plays backwardSee Scroll Direction Demo for an example of how to hook it up to the ScrollView
StateGroupdefaultState: stringThis is a sort of wrapper Trigger for StateTrigger a specific state by calling goToState on this component's ref
Statename: stringWill trigger its Animators when parent StateGroup changes to this state, and play backward when changed away from this state-
SwipingAnimationgetSourceRef: () => SwipeGesturePlays forward when associated SwipeGesture is swiped forward and returns to resting state when going backwardSee Swipe Demo for examples of how to hook it up to a swipe-able view
SwipedgetSourceRef: () => SwipeGesture, how: toActive, toInactive, or toEitherTriggered when the associated SwipeGesture is swipedSee Swipe Demo for examples of how to hook it up to a swipe-able view

Higher-Order animators

NamePropsDescription
Parallel-When started, plays all its children forward, when stopped plays them backward
Cycle-Plays its children Animators playing forward, and restarts them all when the last one finishes playing
Sequence-Plays each child Animator forward one-by-one (when the first finishes, second one beings, etc.)
StaggereachDelay: numberWhen started, plays each child Animator with delay eachDelay between then, same behavior on stop

All of the higher-order components also pass onto its children the following properties:

  • getTargetRef
  • x
  • y
  • initialValue
  • value
  • duration
  • easing
  • delay
  • durationBack
  • easingBack
  • delayBack

See the Stagger Demo for usage example.

Components

Finally, react-native-declan has a set of components that can be manipulated through animators and triggers. Right now, the only component is a static view, but that will soon change.

Behaviors

Note: this may change in the future as we determine whether the Behavior model or the Driver model is more appropriate for react-native.

NamePropDescription
SwipeGesture*type: activeOnly type supported right now
direction: left, right, up, or downDirection to detect the swipe. Will also detect a swipe in the opposite direction to go back to inActive state
length: numberHow long does the user need to swipe for it be considered "successful" (note, also fast swipes will count as success)
edge: left, right, top, bottomTo only enable swipes from the edge of an element
hitSize: numberFor edge, how far from the edge is in play to start the gesture
* See Swipe Demo for examples of how to hook it up to a swipe-able view by using the PanEventEmitter

Contributing

If you are interested in contributing to react-native-declan or have feedback, please contact us. Pull requests are also welcome.

License

MIT