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?

  1. import:

    import { getKeyframes, useKeyframes } from 'styled-benefits'
  2. Add getKeyframes(name: string) to interactive component:

    const AnimatedBall = interactive(styled(Ball)`
    ${getKeyframes('bounce')}
    `)
  3. 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

NameAction
.playStarts animation
.pausePauses animation
.loopStarts animation and repeats infinite times
.reversePlays animation with reversed direction
.loopReverseLike .loop but with revesed direction
.replayIf 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*)

NameTypeCorresponding css property
repeatnumberanimation-iteration-count
directionstring ('normal' | 'reverse' | 'alternate' | 'alternate-reverse')animation-direction
durationnumber - seconds; default - 5 animation-duration
delaynumber - secondsanimation-delay
easingstring ('ease' | 'linear' | 'cubic-bezier()' | ...)animation-timing-function

Example

useKeyframes(raise, {
repeat: 2,
direction: 'alternate',
duration: 1.5,
delay: 0.2,
easing: 'linear',
})