Declarative shared-element transitions between pages for Vue.js.
Uses illusory under the hood.
Vue 3 is now supported with Vue 2 backwords compatibility
Special thanks to @domgew for #26
Source code for the example can be found on the
example
branch.
Index
- Install
- Usage
- Options
- Usage with page transitions
- illusory
- Asking questions and reporting bugs
- How to contributing
- License
npm
$ npm i v-shared-element
or
CDN
<script src="https://unpkg.com/illusory"></script>
<script src="https://unpkg.com/v-shared-element"></script>
//main.js
import Vue from 'vue'
import {
SharedElementRouteGuard,
SharedElementDirective,
createSharedElementDirective
} from 'v-shared-element'
Vue.use(SharedElementDirective)
const router = new VueRouter({ ... })
router.beforeEach(SharedElementRouteGuard)
or
//main.ts
import { createApp } from 'vue'
import {
createSharedElementDirective,
SharedElementRouteGuard,
SharedElementDirective
} from 'v-shared-element'
const app = createApp(App)
app.use(SharedElementDirective)
// or app.use(createSharedElementDirective())
const router = new VueRouter({ ... })
router.beforeEach(SharedElementRouteGuard)
or
Simply add it as a module in your nuxt.config.js
// nuxt.config.js
export default {
modules: ['v-shared-element/nuxt'],
}
Add v-shared-element:<namespace>
to an element to transform it into a shared-element. On another page add the directive to an element and use the same namespace. That's it, you're done (there are more options below if you need them).
Note: A given namespace should only be used once per-page. See below for usage with
v-for
.
Also,keep-alive
routes need special treatment (see below).
<div v-shared-element:namespace></div>
Suppose you have a list of contacts and you want all the profile pictures to be shared-elements.
Use "dynamic directive arguments" to give a different namespace to each contact in the list (this is typically the same ID used for v-for
's :key
prop).
<img
:src="contact.profile"
v-shared-element:[contact.id]
/>
Detailed example
<template>
<div>
<h1>Contacts</h1>
<ul>
<li
v-for="contact in contacts"
:key="contact.id"
>
<img
:src="contact.profile"
v-shared-element:[contact.id]
/>
<span>{{ contact.name }}</span>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
contacts: [
{
id: '11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000',
profile: './user/11bf5b37-e0b8-42e0-8dcf-dc8c4aefc000/profile',
name: 'John Doe'
},
...
]
}
}
}
</script>
If you have routes that use <keep-alive>
, you must add some additional code. Otherwise, the transition will only run once, and not run again while the component remains alive.
To fix this, use sharedElementMixin
on routes that are "kept alive".
Import the mixin into any components on your keep-alive
routes that contain shared-elements. Then, in those components, pass $keepSharedElementAlive
—a method provided by the mixin—as an option to every v-shared-element
directive on that route. Everything should now work as you would expect.
Note: This is only necessary for routes that are kept alive. For example, if
/home
is kept alive but/about
is not, then only/home
needs to import the mixin.
keep-alive
example
<template>
<div>
<img
src="logo.png"
v-shared-element:logo="{ $keepSharedElementAlive }"
/>
</div>
</template>
<script>
import { sharedElementMixin } from 'v-shared-element'
export default {
mixins: [sharedElementMixin]
}
</script>
Options can be applied globally (when you register the plugin) and/or on each individual shared-element.
- Per-element options always override global options.
- Per-element options on the page being navigated away from take precedence.
- The only exception to this is
includeChildren
since it applies to each element individually.
If you're navigating from
/home
to/about
, per-element options specified in/home
will override those specified in/about
. - The only exception to this is
// main.js
Vue.use(SharedElementDirective, {
/* options */
});
or
// main.ts
app.use(SharedElementDirective, {
/* options */
});
// or
app.use(createSharedElementDirective({
/* options */
}))
or
// nuxt.config.js
export default {
modules: ['v-shared-element/nuxt'],
vSharedElement: {
/* options */
}
}
<img
src="logo.png"
v-shared-element:logo="{
/* options */
}"
/>
option | type | default |
---|---|---|
easing | string |
"ease" |
duration | string |
"300ms" |
endDuration | string |
"150ms" |
zIndex | number |
1 |
compositeOnly | boolean |
false |
includeChildren | boolean |
true |
ignoreTransparency | boolean | string[] |
["img"] |
restrictToViewport | boolean |
true |
restrictToRoutes | boolean |
false |
-
type:
string
-
default:
"ease"
A CSS easing-function defining the acceleration curve of the transition (e.g.
"ease-in"
,"cubic-bezier(.29, 1.01, 1, -0.68)"
).
-
type:
string
-
default:
"300ms"
A CSS time denoting the amount of time the transition should take (e.g.
"0.5s"
,"250ms"
).
-
type:
string | false
-
default:
"100ms"
A CSS time denoting the duration of the "fade out" stage of the animation to blend the duplicated element with the real one. Set to
false
or"0s"
to disable the fade-out effect.Note: This option only applies if
includeChildren
isfalse
.
-
type:
number
-
default:
1
The
z-index
to be used for the shared-element during the animation.
-
type:
boolean
-
default:
false
Setting this to
true
will limit the transition totransform
andopacity
properties only (improves performance).compositeOnly: true
(notice thatborder-radius
is not animated)
-
type:
boolean
-
default:
true
v-shared-element
works by cloning each element (and its computed styles) then positioning the clones over the original element. By default, only the root node—the one that has the directive on it—will be cloned. SettingincludeChildren
totrue
will also clone the root node's entire subtree (this is needed to clone text elements such ash1
).includeChildren: false
includeChildren: true
-
type:
boolean | string[]
-
default:
["img"]
Typically, if you're navigating from
/home
to/about
, then the clone of the shared-element on/about
will fade in and the clone from/home
will remain atopacity: 1
.
However, if the shared-element on/home
has abackground-color
with an alpha channel (e.g.rgba(0, 0, 0, 0.5)
), or nobackground-color
at all, then the clone of the/home
element will fade out while the clone of the element from/about
fades in. This is usually what you want unless the element has a background thatv-shared-element
can't detect. This could happen if the element is acting as a container for an<img>
element but has no background of its own, or it has abackground-image
without abackground-color
specified. In this case, settingignoreTransparency
totrue
will override this behavior. You can also specify an array of HTML tag-names (e.g.["img", "div", "button"]
) to automatically set this option totrue
for those elements (astring[]
like this is best used as a global option).Try setting this to
true
if you see a "flash" half-way through the transition.
-
type:
boolean
-
default:
true
With
restrictToViewport
set totrue
, only those elements which are in the viewport will be activated (those outside the viewport will behave as normal elements). By setting this tofalse
, every element on the current route will be considered as a candidate. If you have many shared-elements on a single route, disabling this will come with a large performance overhead.Recommended:
Setting this totrue
makes navigation feel smoother.
This setting is disabled by default to preserve backwards compatibility.restrictToViewport: true
-
type:
false
|string[]
|(to: Route, from: Route, id: string) => boolean
-
default:
false
Recommendation:
If you have many shared-elements on a single route (such as in a list) this option can significantly improve the performance of navigation.Prevents a shared-element from entering the cloning phase unless it meets one of the following criteria:
• If
restrictToRoutes
is an array and the path of the upcoming route matches one of the items in the array.Example:
<img :src="userProfile" v-shared-element:user-profile="{ restrictToRoutes: ['/user'] }" />
• If
restrictToRoutes
is a function and the function returnstrue
.Example:
<li v-for="product in products" :key="product.id" v-shared-element:[`product-title-${product.id}`]="{ restrictToRoutes(to, from, id) { return id.includes(to.params.id) } }" > {{ product.title }} </li>
Built-in comparison functions for common use-cases (such as the example above) are likely to arrive in v3.1.0
Thanks to @719media for figuring out how to make this work.
This section assumes you have an understanding of Vue's transition component.
In your CSS
.page-enter-active {
transition: opacity 150ms ease 150ms;
}
.page-leave-active {
position: absolute;
transition: opacity 150ms ease;
}
.page-leave-to,
.page-enter {
opacity: 0;
}
For Vue.js + vue-router
// App.vue (or equivalent)
<div id="app">
<transition
name="page"
@before-leave="beforeLeave"
@after-leave="afterLeave"
>
<router-view></router-view>
</transition>
</div>
<script>
export default {
methods: {
beforeLeave(el) {
const {top} = el.getBoundingClientRect();
el.style.position = "fixed";
el.style.top = `${top}px`;
el.style.left = 0;
el.style.right = 0;
el.style.zIndex = '-1';
},
afterLeave(el) {
el.style.position = '';
el.style.top = '';
el.style.left = '';
el.style.right = '';
el.style.zIndex = '';
}
}
}
</script>
or
For Nuxt.js
// nuxt.config.js
export default {
pageTransition: {
name: 'page',
mode: '',
beforeLeave(el) {
const {top} = el.getBoundingClientRect();
el.style.position = "fixed";
el.style.top = `${top}px`;
el.style.left = 0;
el.style.right = 0;
el.style.zIndex = '-1';
},
afterLeave(el) {
el.style.position = '';
el.style.top = '';
el.style.left = '';
el.style.right = '';
el.style.zIndex = '';
}
}
}
Important note about page transitions
If the total duration of the page transition is longer than the duration of a shared-element on that page, things will get weird. You have been warned.
v-shared-element
derives its element-morphing powers from its sister project illusory.
illusory comes bundled with v-shared-element
as Vue instance methods.
For more information on how to use it, see the illusory documentation or the illusory example page.
illusory is exposed on the Vue instance as $illusory
and $createIllusoryElement
.
For example:
<template>
<div>
<div ref="from"></div>
<div ref="to"></div>
<button @click="morph">Morph!</button>
</div>
</template>
<script>
export default {
methods: {
morph() {
this.$illusory(this.$refs.from, this.$refs.to)
}
}
}
</script>
If you're experiencing any problems, or have general questions about the plugin, feel free open a new issue (but search through the existing ones first, as your question may have been answered already).
Note that issues related to the
$illusory
and$createIllusoryElement
should be opened on the illusory repository instead.
-
Fork and clone the repo.
$ git clone https://github.com/<your username>/v-shared-element.git
-
Install the dependencies
$ npm install
-
Create a new branch for your pull request
$ git checkout -b pr/your-branch-name
npm run build
— Runs the build script.npm run dev
— Runs the build script in watch mode.npm run test
— Runs the tests.npm run format
— Fixes any code formatting issues.npm run lint
— Lints the code.
Run npm link
so you can use it in other local projects.
Method one
You can either create a new Vue.js or Nuxt.js project and use npm link v-shared-element
to test your changes.
or
Method two
Clone the repo again, this time into a new directory. Then and run the following:
$ git checkout example
$ npm install
$ npm link v-shared-element
$ npm run dev
You should now have the example page running on localhost
.
Hot reload will be triggered by changes made to v-shared-element
.
See CONTRIBUTING.md for info.
This project is distributed under the MIT License.
Copyright (c) 2020 Justin Taddei
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.