Giter Club home page Giter Club logo

vue-flip-toolkit's Introduction

vue-flip-toolkit

Vue Flip Toolkit Version Badge

A Vue.js port of the wonderful react-flip-toolkit, developed by @aholachek (to whom all credit is due here)

Quick Start

yarn add vue-flip-toolkit

Wrap the components you wish to animate with a single Flipper component that has a flipKey prop. This prop must change every time you want an animation to happen.

Wrap elements that should be animated with Flipped components that have a flipId prop matching them across renders.

A basic example can be found here: https://codesandbox.io/s/m354w1mmp9

Why even port this to Vue.js?

Fair question. In developing my own library, vue-overdrive, I've felt the pain of not being able to find a declarative library for animating a given DOM element between two states. Upon discovering react-flip-toolkit, which has a first-class "core" API that can be used outside of React, I wanted to take a crack at using it to re-implement vue-overdrive. The fruit of my attempt is the following library, vue-flip-toolkit.

What's in this library?

This library strives to imitate its parent, react-flip-toolkit, as closely as possible. It thus exports the following two components that you can use in your Vue applications. For the sake of brevity, I've lifted descriptions/verbiage from the README of react-flip-toolkit, indicated via blockquotes.

Flipper.vue

The parent wrapper component that contains all the elements to be animated. You'll most typically need only one of these per page. Read more –>

Props

prop default type details
className string A class that will apply to the div rendered by Flipper
flipKey (required) string, number, boolean Changing this tells vue-flip-toolkit to transition child elements wrapped in Flipped components.
spring "noWobble" string, object Provide a string or referencing one of the spring presets — noWobble, veryGentle, gentle, wobbly, or stiff. Otherwise, pass a custom spring object
staggerConfig {} object Provide configuration for staggered Flipped children.

Flipped.vue

Wraps an element that should be animated.

Props

prop default type details
flipId (required) string Use this to tell vue-flip-toolkit how elements should be matched across renders so they can be animated.
inverseFlipId string Refer to the id of the parent Flipped container whose transform you want to cancel out. If this prop is provided, the Flipped component will become a limited version of itself that is only responsible for cancelling out its parent transform. It will read from any provided transform props and will ignore all other props (besides inverseFlipId.)
stagger string Provide a natural, spring-based staggering effect in which the spring easing of each item is pinned to the previous one's movement. If you want to get more granular, you can provide a string key and the element will be staggered with other elements with the same key.
delayUntil string Delay an animation by providing a reference to another Flipped component that it should wait for before animating (the other Flipped component should have a stagger delay as that is the only use case in which this prop is necessary.)
shouldInvert function A function provided with the current and previous decisionData props passed down by the Flipper component. Returns a boolean indicating whether to apply inverted transforms to all Flipped children that request it via an inverseFlipId.
shouldFlip function A function provided with the current and previous decisionData props passed down by the Flipper component. Returns a boolean to indicate whether a Flipped component should animate at that particular moment or not.
opacity false boolean
scale false boolean Tween scaleX and scaleY
translate false boolean Tween translateX and translateY

Events

eventName args details
@on-start {el: DOMElement, id: String} Emitted when the flipped animation begins.
@on-complete {el: DOMElement, id: String} Emitted when the flipped animation begins.

Cool, so how do I use it?

Install the library

yarn add vue-flip-toolkit

Import the respective components.

import { Flipper, Flipped } from "vue-flip-toolkit";

Register the components.

// Example.vue
<script>
export default {
  components: {
    Flipped,
    Flipper
  }
};
</script>

OK, time for some examples.

You got it.

1) Simple, Expanding Div Animation

Source

2) Two Divs

Source

3) List Shuffle Animation

Source

4) List Shuffle Animation (Staggered)

Source

5) Accordion (Staggered)

Source

6) Scale Animation + Anime.js

Source

7) Material Design inspired animation

Source

8) Vue Router Example

This example is very much a WIP. Nonetheless, it illustrates at a high-level how to use vue-flip-toolkit with vue-router, as well as hook into the @on-complete and @on-start events.

Source

What's next?

A lot.

Primarily, I want to make sure that this basic set of functionality works for Vue developers, and that the API makes sense.

Once we check that box, I'd like to add support for additional props for both Flipped and Flipper. Right now, you can't do too too much with them.

vue-flip-toolkit's People

Contributors

benjmichel avatar dependabot[bot] avatar mattrothenberg avatar tyankatsu0105 avatar v-stein avatar

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  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-flip-toolkit's Issues

Nuxt?

Does this work with nuxt.js transitions?

Vue 3 support

Hey, I've been using this toolkit for a while, and it's been great!

Since Vue 3.0 have reached RC stage, isn't a good time to start working on a compatible version of vue-flip-toolkit?

Cannot set property 'flipId' of undefined

Not getting it to work with Gridsome. Getting the following errors:

vue-router.esm.js?8c4f:2748 [Vue warn]: Error in mounted hook: "TypeError: Cannot set property 'flipId' of undefined"

found in

---> <Flipped>
       <RouterLink>
         <Grid> at src/components/Grid.vue
           <Index> at src/pages/Index.vue
             <Flipper>
TypeError: Cannot set property 'flipId' of undefined
    at Flipper.addFlipped (vue-flip-toolkit.umd.js?0129:1946)
    at VueComponent.addFlippedElement (vue-flip-toolkit.umd.js?0129:2024)
    at VueComponent.mounted (vue-flip-toolkit.umd.js?0129:31)
    at invokeWithErrorHandling (vue.runtime.esm.js?2b0e:1854)
    at callHook (vue.runtime.esm.js?2b0e:4213)
 

Failed to mount component: template or render function not defined.

Thanks a lot for making it!

I think I must be missing something. When I add this to my App.vue, wrapping a Flipper around the <router-view>, Vue hits me with a

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <Flipper>
       <Swap> at src/components/utils/transitions/Swap/Swap.vue
         <App> at src/App.vue
           <Root>

and my App fails to render. This is with a fresh vue-cli generated app.

My App.vue:

<template>
  <div id="app">
    <Nav :routes="$router.options.routes" />
    <div id="app-content">
      <Header :viewName="$router.currentRoute.meta.friendlyName" />
      <Swap>
        <Flipper :flipKey="$route.path">
          <router-view />
        </Flipper>
      </Swap>
    </div>
  </div>
</template>

<script>
import Header from '@/components/Header'
import Nav from '@/components/Nav'

import Flipper from '@/components/Flip'

import Swap from '@/components/utils/transitions/Swap'

export default {
  components: {
    Header,
    Nav,
    Swap,
    Flipper
  }
}
</script>

<style lang="sass" src="@/styles/Global.sass">

Any ideas?

nuxt route transition

Hi,
I want to implement flip between route components, but any solution I found for vue not working. I was trying to wrap default layout component in Flipper, but animation still do not trigger.
sample repo

Thank you.

Edit: Route transition works, if in layout I replace <nuxt> component to <router-view>, but not sure it's right solution for Nuxt.

add support for event emit instead of flipKey

Maybe add support to force a refresh when emiting certain event instead of the component watching for the flipKey prop. I think this is more suitable for complex scenarios like large list or various components. At first I got trouble understanding the flipKey and I said, why we don't just give it a boolean and flip the value when we want it to refresh it!? but then I realized it will be more readable to support some emit('refresh'). I will be happy to start working on a PR draft but I want to start a discussion as well.

Vue Router development assistance

Hi I am currently in the process of trying to implement this package into a Chat Box that I currently building. I have attempted to use this in both a router and non-router environment with no luck unfortunately. Not sure if my key's or id's are off, or I am just missing the boat completely.

If you have a second to tell me what I'm doing wrong, I would appreciate it.

Simple

This is a simple implementation that I have built that I cannot get to work:

<template>
    <Flipper :flipKey="key">
        <Flipped flipId="btn-chat-launch" scale v-if="!visible">
            <chat-launch-button @click="showChat"></chat-launch-button>
        </Flipped>
        <Flipped flipId="btn-chat-launch" scale v-else>
            <chat-wrapper></chat-wrapper>
        </Flipped>
    </Flipper>
</template>

<script>
    import { EventBus } from '../../EventBus';
    import { Flipper, Flipped } from 'vue-flip-toolkit';
    import ChatLaunchButton from "./ChatLaunchButton";
    import ChatWrapper from "./ChatWrapper";

    export default {
        name: "Chat",
        components: {
            ChatLaunchButton,
            ChatWrapper,
            Flipper,
            Flipped,
        },
        data() {
            return {
                visible: true
            }
        },
        mounted() {
            EventBus.$on('hideChat', () => {
                this.visible = false;
            })
        },
        beforeDestroy() {
            EventBus.$off('hideChat');
        },
        computed: {
            key() {
                return this.visible.toString();
            }
        },
        methods: {
            showChat() {
                this.visible = true;
            }
        }
    }
</script>

Router

This is the router implementation that I cannot get to work either:

wrapper

<template>
    <div class="chat-wrapper shadow">
        <Flipper :flipKey="key" stagger="gentle" className="d-flex flex-column flex-1">
            <router-view></router-view>
        </Flipper>
    </div>
</template>

<script>
    import { Flipper } from 'vue-flip-toolkit';
    export default {
        name: "ChatWrapper",
        components: {
            Flipper,
        },
        beforeCreate() {
            this.$router.push('/')
        },
        computed: {
            key() {
                return this.$route.path;
            }
        }
    }
</script>

home

<template>
    <div class="chat-page">
        <chat-header>
            <button @click="hideChat" class="chat-header-button text-white bg-transparent border-0 "><i class="fa fa-bars" aria-hidden="true"></i></button>
            <i class="fa fa-comment mr-2 font-size-large" aria-hidden="true"></i>
            <span class="font-size-small">Chats</span>
        </chat-header>
        <section class="chat-list px-3">
            <div class="chat-list-heading">
                Recent Conversations
            </div>

            <Flipped v-for="conversation of conversations" :key="conversation.id" :flipId="conversations.slug">
                <chat-conversation-card :conversation="conversation" @click="viewConversation(conversation.slug)"></chat-conversation-card>
            </Flipped>

        </section>
    </div>
</template>

<script>
    import { EventBus } from '../../EventBus';
    import { Flipped } from 'vue-flip-toolkit';
    import ChatHeader from "./ChatHeader";
    import ChatConversationCard from "./ChatConversationCard";

    export default {
        name: "ChatIndex",
        components: {
            ChatConversationCard,
            ChatHeader,
            Flipped,
        },
        data() {
            return {
                conversations: [
                    {'id': 1, 'slug': 'abc'},
                    {'id': 2, 'slug': 'def'},
                ]
            }
        },
        methods: {
            hideChat() {
                EventBus.$emit('hideChat');
            },
            viewConversation(slug) {
                this.$router.push(`/conversations/${slug}`);
            }
        },
    }
</script>

detail

<template>
    <Flipped scale translate :flipId="flipId">
        <div class="chat-page">
            <chat-header>
                <router-link to="/" class="chat-header-button text-white bg-transparent border-0"><i class="fa fa-bars" aria-hidden="true"></i></router-link>
                <span class="font-size-small">Chats</span>
            </chat-header>
            <section class="chat-list px-3">

            </section>
        </div>
    </Flipped>
</template>

<script>
    import ChatHeader from "./ChatHeader";
    import { Flipped } from 'vue-flip-toolkit';

    export default {
        name: "ChatConversation",
        components: {
            ChatHeader,
            Flipped
        },
        computed: {
            flipId() {
                return this.$route.params.conversation;
            },
        }
    }
</script>

License needed

What is the license of the project ?

Am I allowed to use this plugin for a closed-source commercial project ?

inverseFlipId transition not work properly in v-for loop

Hi.
I am trying to implement flip to list items using v-for loop, but unfortunately inverseFlipId does not prevent from content scaling.
Transition works fine using simple v-if directive for every item in array, but it is not what i am after.
example code

<template>
  <Flipper :flip-key="active" style="display:flex">
    <div v-for="(item,idx) in items" :key="idx">
      <button @click="active=idx">{{idx}}</button>
        <Flipped flip-id="square" scale translate v-if="active===idx">
          <div class="item">
           <Flipped inverse-flip-id="square" scale >
             <div>
              <component :is="current"/>
             </div>
           </Flipped>
         </div>
       </Flipped>
    </div>
  </Flipper>
</template>
<script>
import { Flipper, Flipped } from "vue-flip-toolkit";
import one from "@/components/one";
import two from "@/components/two";

export default {
  components: { Flipper, Flipped, one, two},
  data() {
    return {
      items: [{ component: one }, { component: two }],
      active: 0
    };
  },
  computed: {
    current() {
      return this.items[this.active].component;
    }
  },
};
</script>

Thanks.

Can staggerConfig props be reactive ?

Hi,

I'm getting a problem and I was wondering if you think it can be solved without too much work.

I have this computed property

  computed: {
    staggerConfig () {
      return {
        listItem: {
          reverse: this.active !== 0,
          speed: 1
        }
      }
    }
  }

used this way

  <Flipper
          :flipKey="active"
          :staggerConfig="staggerConfig"
    >
   // ....
</Flipper>

Unfortunately, the props staggerConfig only use the first value of staggerConfig and is never reversed. If we want to change the behaviour we have to mutate the object.

    onClick (item) {
      this.active = this.active === item ? 0 : item
      this.staggerConfig.listItem.reverse = this.active !== 0
    },

My fix would be to watch staggerConfig here https://github.com/mattrothenberg/vue-flip-toolkit/blob/master/src/Flipper.vue#L82 and update Flipper instance accordingly. Unfortunately i'm not familiar enough with Flipper to handle a PR :(

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.