Version: 0.1.x
Controlling Animations
Styled Benfits makes trigering animations easy with interactions, however; it might not be enough and more controll is needed. Styled Benfits wraps your keyframes and allows triggering animations in declarative way by exposing functions like play, pause, seek.
How to?
import:import { getKeyframes, useKeyframes } from 'styled-benefits'Add
getKeyframes(name: string)to interactive component:const AnimatedBall = interactive(styled(Ball)`${getKeyframes('bounce')}`)Utilize
useKeyframes(keyframes, options)hook:const bounceAnimation = keyframes`...`export default ({ isBouncing }) => {const bounce = useKeyframes(bounceAnimation)useEffect(() => {if (isBouncing) bounce.loop()else bounce.pause()}, [isBouncing, bounce])return <AnimatedBall bounce={bounce} />}
Play/Pause animation
Seek animation
caution
Be aware of using seek with range sliders or scroll events. Remember to wrap seek execution with throttle or raf handlers
Avaliable methods
| Name | Action |
|---|---|
.play | Starts animation |
.pause | Pauses animation |
.loop | Starts animation and repeats infinite times |
.reverse | Plays animation with reversed direction |
.loopReverse | Like .loop but with revesed direction |
.replay | If animation's paused starts from begining |
.seek(percentage) | Jumps to specific percentage of animation (percentage - number between 0 and 1 ) |
Options
Provide them as second argument to useKeyframes(keyframes, *options*)
| Name | Type | Corresponding css property |
|---|---|---|
repeat | number | animation-iteration-count |
direction | string ('normal' | 'reverse' | 'alternate' | 'alternate-reverse') | animation-direction |
duration | number - seconds; default - 5 | animation-duration |
delay | number - seconds | animation-delay |
easing | string ('ease' | 'linear' | 'cubic-bezier()' | ...) | animation-timing-function |
Example
useKeyframes(raise, {repeat: 2,direction: 'alternate',duration: 1.5,delay: 0.2,easing: 'linear',})