Usage
The router plugin enables smooth page transitions by automatically managing element animations between routes. It handles cleanup of animated elements and provides a declarative way to define when and how animations should occur during navigation.
Example
Changing the page, you will see this box moving to the new position with a flip animation.
Demo code
vue
<script setup lang="ts">
import { FlipElement } from '@vue-gsap-flip/core'
defineProps<{ type?: 'big' }>()
</script>
<template>
<FlipElement id="box" v-slot="{ setEl }">
<div
:ref="setEl"
class="bg-blue"
:class="{
'w-10 h-10': type !== 'big',
'w-20 h-20': type === 'big'
}"
/>
</FlipElement>
</template>
Change page to see the animation
Configuration
The plugin adds a routes
property to the FlipElementConfig
:
typescript
interface RouteConfig {
name: string | RegExp
params: RouteLocationNormalizedGeneric['params']
}
interface RouterFlipElementConfig {
routes?: (RouteConfig & { from?: RouteConfig[] })[]
}
Route Configuration Options
Property | Type | Description |
---|---|---|
routes | (RouteConfig & { from?: RouteConfig[] })[] | Array of route configurations |
name | string | RegExp | Route name (exact string or RegExp pattern) |
params | RouteLocationNormalizedGeneric['params'] | Route parameters for matching |
from | RouteConfig[] | Array of routes that can animate to this route |
Route Configuration
In this example we can see an element that will flip during a page transition and on certain pages defined in the routes parameter.
vue
<template>
<FlipElement
id="my-element"
:config="{
clone: true,
routes: [
// only on the about page
{ name: 'about' },
// only on the credits page if locale is 'it'
{ name: 'credits', params: { locale: 'it' } },
// only on product detail with id 123 and coming from product-list page
{
name: 'product-detail',
params: { id: '123' },
from: [
{ name: 'product-list' }
]
},
// all pages named with list-.. and coming from product list
{
name: /^list-.*$/,
from: [
{ name: 'product-list' }
]
}
]
}"
>
<div>Content that animates on specific routes</div>
</FlipElement>
</template>
Best Practices
- Use Named Routes: Always use named routes for better control
- Be Specific: Define specific route transitions rather than allowing all routes
- Parameter Matching: Use route parameters for precise control
- RegExp Patterns: Use RegExp patterns for dynamic route matching
- Test Navigation: Test all navigation paths to ensure animations work as expected
- Performance: Only animate when it enhances the user experience
Troubleshooting
Animations not working?
- Make sure the plugin is registered before any FlipElement components are used
- Check that you're using named routes in your Vue Router configuration
- Verify that route names in your configuration match your router route names
- Ensure route parameters match exactly (case-sensitive)
- Check that the route configuration array format is correct
TypeScript errors?
- Make sure you've imported the plugin types:
import '@vue-gsap-flip/vue-router'
- Check that your
tsconfig.json
includes the plugin in the compilation - Restart your TypeScript language server if using VS Code