GithubHelp home page GithubHelp logo

tanvesh01 / motion-hooks Goto Github PK

View Code? Open in Web Editor NEW
99.0 3.0 5.0 1.41 MB

A simple Hooks wrapper over Motion One, An animation library, built on the Web Animations API for the smallest filesize and the fastest performance.

Home Page: https://motion-hooks.netlify.app/

License: MIT License

JavaScript 7.28% TypeScript 92.72%
motion react animation hooks rollup esm

motion-hooks's Introduction

motion-hooks

A React Hooks wrapper over Motion One, An animation library, built on the Web Animations API for the smallest filesize and the fastest performance.

npm version npm Twitter Follow

Installation

npm install motion-hooks motion

Your project needs to have [email protected] [email protected] or greater

Hooks

As of now, motion-hooks has 2 hooks that wrap around animate and timeline of motion one respectively

Example usage

Things You could do with useMotionAnimate

Animating List - Link to codesandbox

useMotionAnimate List Example

Animating Counter - Link to codesandbox

useMotionAnimate Counter Example

Things You could do with useMotionTimeline

Animating elements independently - Link to codesandbox

useMotionTimeline Example Usage

useMotionAnimate

Returns all the properties returned by animate and some helper functions and state

Props returned my animate are null initially

You may view this example here on codesandbox.

function App() {
    const { play, isFinished, replay } = useMotionAnimate(
        '.listItem',
        { y: -20, opacity: 1 },
        {
            delay: stagger(0.3),
            duration: 0.5,
            easing: [0.22, 0.03, 0.26, 1],
        },
    );

    // Play the animation on mount of the component
    useEffect(() => {
        play();
    }, []);

    return (
        // Replay the animation anytime by calling a function, anywhere
        <div className="App">
            <button disabled={!isFinished} onClick={() => replay()}>
                Replay
            </button>

            <ul className="list">
                <li className="listItem">Item 1</li>
                <li className="listItem">Item 2</li>
                <li className="listItem">Item 3</li>
            </ul>
        </div>
    );
}

Instead of passing strings to select elements, you can also pass a ref ๐Ÿ‘‡

const boxRef = useRef(null);
const { play, isFinished, replay } = useMotionAnimate(
    boxRef,
    { y: -20, scale: 1.2 },
    { duration: 1 },
);

return <div ref={boxRef}>BOX</div>;

API

const { play, replay, reset, isFinished, animateInstance } = useMotionAnimate(
    selector,
    keyframes,
    options,
    events,
);

useMotionAnimate returns:

  • play: plays the animation
  • replay: Resets and plays the animation
  • reset: resets the element to its original styling
  • isFinished: is true when animation has finished playing
  • animateInstance: Animation Controls. Refer to motion one docs for more.

useMotionAnimate accepts:

  • selector - The target element, can be string or a ref

  • keyframes - Element will animate from its current style to those defined in the keyframe. Refer to motion's docs for more.

  • options - Optional parameter. Refer to motion doc's for the values you could pass to this.

  • events - Pass functions of whatever you want to happen when a event like onFinish happens.

    events usage example

    const { play, isFinished, replay } = useMotionAnimate(
        '.listItem',
        { y: -20, opacity: 1 },
        {
            delay: stagger(0.3),
            duration: 0.5,
        },
        {
            onFinish: () => {
                // Whatever you want to do when animation finishes
            },
        },
    );

useMotionTimeline

Create complex sequences of animations across multiple elements.

returns timelineInstance (Animation Controls) that are returned by timeline and some helper functions and state

Props returned my timeline are null initially

You may view this example here on codesandbox.

function App() {
    const gifRef = useRef(null);
    const { play, isFinished, replay } = useMotionTimeline(
        [
            // You can use Refs too!
            [gifRef, { scale: [0, 1.2], opacity: 1 }],
            ['.heading', { y: [50, 0], opacity: [0, 1] }],
            ['.container p', { opacity: 1 }],
        ],
        { duration: 2 },
    );

    useEffect(() => {
        play();
    }, []);

    return (
        <div className="App">
            <button disabled={!isFinished} onClick={() => replay()}>
                Replay
            </button>

            <div className="container">
                <img
                    ref={gifRef}
                    className="gif"
                    src={Image}
                    alt="mind explosion gif"
                />
                <div>
                    <h1 className="heading">Tanvesh</h1>
                    <p>@sarve__tanvesh</p>
                </div>
            </div>
        </div>
    );
}

API

const { play, replay, reset, isFinished, timelineInstance } = useMotionTimeline(
    sequence,
    options,
    events,
);

useMotionTimeline returns:

  • play: plays the animation
  • replay: Resets and plays the animation
  • reset: resets the element to its original styling
  • isFinished: is true when animation has finished playing
  • timelineInstance: Animation Controls. Refer to motion one docs for more.

useMotionTimeline accepts:

  • sequence - sequence is an array, defines animations with the same settings as the animate function. In the arrays, Element can be either a string or a ref. You can refer to sequence docs for more on this.
  • options - Optional parameter. Refer to motion doc's for the values you could pass to this.
  • events - Pass functions of whatever you want to happen when a event like onFinish happens. Exactly same as useMotionAnimate's onFinish.

Local Installation & Contributing

git clone https://github.com/:github-username/motion-hooks.git
cd motion-hooks
npm install # Installs dependencies for motion-hooks
cd examples # React app to test out changes
npm install # Installs dependencies for example app
npm run dev # To run example on localhost:3000

The contributing guidelines along with local setup guide is mentioned in CONTRIBUTING.md

Any Type of feedback is more than welcome! This project is in very early stage and would love to have contributors of any skill/exp level.

You can contact me on my Twitter handle @Sarve___tanvesh

motion-hooks's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

motion-hooks's Issues

Getting issue with project setup

I am interested in contributing to this project.
As mentioned in CONTRIBUTING.md I follow the step:

  • Fork motion-hooks
  • Take a clone
  • install dependency in the root directory
  • run npm audit fix to deal with vulnerability
  • move to the example directory
  • install dependency
  • run npm run dev

Getting dependency error to run the project

image

Can't build the example project

Can't generate the static files of example project.

Reproduction:
Go into the example/ directory and run:

npm run build

This outputs:

image

Work with refs?

Cool lib! Could this be accomplished?

const { play, isFinished, replay, ref } = useMotionAnimate(
    { y: -20, opacity: 1 },
    {
      delay: stagger(0.3),
      duration: 0.5,
      easing: [0.22, 0.03, 0.26, 1],
    },
  );

return (
  <div ref={ref} />
);

Or even: useMotionAnimate(ref.current, deltas, opts) where ref is a useRef.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.