Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vue-view-transitions

Clarkkkk205MIT1.2.1TypeScript support: included

A simple way to use View Transitions API in Vue

vue, transition, view transition, view transitions, view transitions api, page transition, page transitions

readme

vue-view-transitions

NPM version NPM Downloads License Minified Size Build Status

A simple tool to use View Transition API in Vue

Introduction

The View Transitions API is an experimental API recently released in Chrome 111+. It provides a convenient way to create animated transitions between different DOM states. vue-view-transitions makes it easier to use View Transitions API in Vue. For more information about View Transition API, Please check Reference.

Install

npm i vue-view-transitions

Or

pnpm add vue-view-transitions
yarn add vue-view-transitions

Usage

Use ViewTransitionsPlugin to add view transition name easily

ViewTransitionsPlugin provides a directive v-view-transition-name (or v-trans for short) to apply view-transition-name to elments. You should use a unique view-transition-name for each element (or element pair).

In main.ts

import { createApp } from 'vue'
import { ViewTransitionsPlugin } from 'vue-view-transitions'
import App from './App.vue'

createApp(App).use(ViewTransitionsPlugin())

In App.vue

<h1 v-view-transition-name="'title'">Title</h1>

It will be rendered to this when mounted:

<h1 style="view-transition-name: title">Title</h1>

You can use object syntax to toggle it dynamically:

<h1 v-trans="{ title: someBoolean }">Title</h1>

Use startViewTransition to start a new view transition

async function animate() {
    const viewTransition = startViewTransition()
    await viewTransition.captured
    style.value.transform = style.value.transform === '' ? 'translateX(50px)' : ''
}

Note that you should wait viewTransition.captured to be fulfilled before making dom updates. Otherwise the dom change may finish before the browser can capture the snapshot of current state and results in no transitions at all.

Use startViewTransition in vue-router to apply page transitions

The code execution order here is a bit puzzling due to the asynchronous nature of both view transitions and routing. The order should be "beforeResolve start" -> "capture screenshot of current state" -> "beforeResolve end" -> "dom change callback" -> "view transition". As you can see, there are two promises: beforeResolve waits for the viewTransition.captured promise to fulfill, startViewTransition waits for the dom change callback to fulfill.

router.beforeResolve(async () => {
    const viewTransition = startViewTransition(async () => {
        // dom changes
    })
    await viewTransition.captured
    // ...
})

ViewTransition object

startViewTransition(callback?): return a ViewTransition object. The optional callback is supposed to return a promise. When the promise returned by the callback fulfills, the view transition starts. A few properties are used to describe the whole process. They fulfills in a sequential order: captured -> updateCallbackDone -> ready -> finished.

  • ViewTransition.captured: A promise that fulfills once user agent finishes capturing a screenshot of the current state

  • ViewTransition.updateCallbackDone: A promise that fulfills once the promise returned by the callback of startViewTransition fulfills

  • ViewTransition.ready: A Promise that fulfills once the pseudo-element tree is created and the transition animation is about to start.

  • ViewTransition.finished: A Promise that fulfills once the transition animation is finished, and the new page view is visible and interactive to the user.

  • ViewTransition.skipTransition(): A method used to skips the animation part of the view transition.

Use in Nuxt

Nuxt ships with an experimental support for the native View Transitions API since 3.4.0. vue-view-transitions provides a nuxt module for nuxt users. Here is how to enable it:

export default defineNuxtConfig({
    modules: [
        'vue-view-transitions/nuxt'
    ],
    experimental: {
        viewTransition: true
    }
})

Done. Now it's able to use page transitions with the v-trans directive during page routing without any extra work thanks to the nuxt intergration. As for the manual triggered transitions, it works the same as described above.

Examples

More examples are available here.

Compatibility

Vue 2

vue-view-transitions supports vue 2.6 or above. You should use ViewTransitionsLegacyPlugin instead of ViewTransitionsPlugin in Vue 2 project.

import { ViewTransitionsLegacyPlugin } from 'vue-view-transitions'

Vue.use(ViewTransitionsLegacyPlugin())

Browser Compatibility

vue-view-transitions uses View Transitions API under the hood. Currently, View Transitions API is only available in Chrome 111+. If running in unsupported browsers, it simply executes the callback of startViewTransition (if provided). If you need a more compatible solution, use vue-starport, or nuxt transitions if you are using Nuxt. (Browser compatibility)

Migrate from 0.x

startViewTransition no longer return a promise.

Previous:

await startViewTransition()

Now:

const viewTransition = startViewTransition()
await viewTransition.captured

Changelog

See CHANGELOG

Reference

Smooth and simple transitions with the View Transitions API

MDN ViewTransition

Acknowledgment

If you found it useful somehow, I would be grateful if you could leave a star in the project's GitHub repository.

Thank you.

changelog

Changelog

All notable changes to this project will be documented in this file. See commit-and-tag-version for commit guidelines.

1.2.1 (2024-02-17)

Bug Fixes

  • fix errors in tsconfg.json (85e04282)
  • make nuxt as optional dependency (#1) (6c60b93a)

Chores

  • update conventional-changelog-aaron-preset (5b6b7379)

1.2.0 (2023-10-11)

Features

  • support nuxt as a nuxt module vue-view-transitions/nuxt (f298e0e0)

Documentation

1.1.1 (2023-07-30)

Bug Fixes

  • fix an error when pass a value of wrong type and improve error message's readability (839340e1)

Test

Chores

  • use aaron-preset to regenerate the whole CHANGELOG.md (323cf1c3)
  • update README and keywords (1a6ce423)

1.1.0 (2023-07-29)

Features

  • provides a shorthand v-trans for v-view-transition-name (4bb3f69e)

Bug Fixes

  • should execute the callback of startViewTransition as well when running in unsupported browsers (1b190965)

Chores

1.0.0 (2023-07-27)

Bug Fixes

  • return ViewTransition object with default values when browser does not support view transitions api (a47d5ade)

1.0.0-beta.2 (2023-07-25)

Bug Fixes

  • should not access capturedPromise before initialization (abf2cb97)

1.0.0-beta.1 (2023-07-25)

Features

  • add captured property to ViewTransition object (2648719b)

    Description

    • captured property is promise resolved when user agent finishes capturing images of the current state
### **BREAKING CHANGE**

- `startViewTransition` no longer return a promise.
    Previous:
    ```js
    await startViewTransition()
    ```
    Now:
    ```js
    const viewTransition = startViewTransition()
    await viewTransition.captured
    ```

0.4.0-beta.1 (2023-07-25)

Features

  • startViewTransition accepts an optional callback (cf27ddcb)

    Description

    • startViewTransition accepts an optional callback, which returns a promise. the view transition starts once the promise is resolved.
    • viewTransition.updateCallbackDone is provided in the returned ViewTransition object, which is the same as the native ViewTransition object

0.3.1 (2023-07-23)

Bug Fixes

  • fix an error when using object syntax (0edbb633)

0.3.0 (2023-07-02)

Features

  • support passing an object for dynamic toggling (f80358ce)

0.2.4 (2023-07-01)

Bug Fixes

  • directive callback should be called in udpate hook (b3e50e95)

Chores

  • trigger github action on tag push (0195dca9)

0.2.3 (2023-06-20)

Bug Fixes

0.2.2 (2023-06-20)

Bug Fixes

  • correct information in LICENSE (b63f8f1e)

Documentation

0.2.1 (2023-06-20)

Bug Fixes

0.2.0 (2023-06-20)

Features

Documentation

Chores

0.1.2 (2023-06-20)

Bug Fixes

  • fix incorrect exports field (afabf87d)

0.1.1 (2023-06-20)

Bug Fixes

  • add types to package.json to fix missing type declarations error (81fced57)

Chores

  • update repository in package.json (5ab615d0)

0.1.0 (2023-06-20)

Features

  • ViewTransitionsPlugin and startViewTransition (df1ec5f9)

Documentation

Chores