How to Animate Page Transitions in Turbo Drive Apps Using Turn

Apr 14, 2024 | Programming

Creating smooth transitions between pages can significantly enhance user experience in web applications. This article will guide you step-by-step on how to implement page animations using Turn in Turbo Drive applications.

Installation

  1. First, ensure you have installed [@hotwired/turbo](https://www.npmjs.com/package/@hotwired/turbo).
  2. Next, install Turn by running the command:
  3. npm install @domchristieturn

Usage

  1. Include Turbo, dist/turn.js and dist/turn.css in your project, depending on how you build your JavaScript and CSS.
  2. Add the attributes data-turn-enter and data-turn-exit to the elements you wish to animate (this is optional if you are only using View Transitions).
  3. Import Turn and initialize it in your application’s JavaScript:
  4. import Turn from '@domchristieturn'; 
    Turn.start();
  5. Now, navigate between pages to see the animations in effect!

Customizing Animations

The magic happens when Turn dynamically adds various classes at different stages of the navigation lifecycle. You can customize these animations by defining your own styles scoped within the classes.

css
html.turn-advance.turn-exit [data-turn-exit] {
    animation-name: MY_ANIMATE_OUT;
    animation-duration: .3s;
    animation-fill-mode: forwards;
}
html.turn-advance.turn-enter [data-turn-enter] {
    animation-name: MY_ANIMATE_IN;
    animation-duration: .6s;
    animation-fill-mode: forwards;
}

Understanding Class Names

Throughout the navigation lifecycle, several classes are added and removed from the HTML element. Understanding these classes is crucial for harnessing the power of animations:

  • turn-view-transitions: This class is added if the device supports View Transitions.
  • turn-advance: Applied at the start of a visit if the action is an advance.
  • turn-exit: Used to scope exit animations, removed when exit animations are complete.

Event Listeners

Turn also dispatches several events that you can listen for to add even more customization:

  • turn:before-exit: Fired before exit animations start.
  • turn:enter: Fired after all transitions and animations have completed.

Integrating with Tailwind CSS

If you are using Tailwind CSS, defining animations is a breeze by modifying your tailwind.config.js.

const plugin = require('tailwindcss/plugin');

module.exports = {
    theme: {
        extend: {
            animation: {
                exit: 'fade-out-up 0.3s cubic-bezier(0.65, 0.05, 0.35, 1) forwards',
                enter: 'fade-in-up 0.6s cubic-bezier(0.65, 0.05, 0.35, 1) forwards',
            },
            keyframes: {
                'fade-out-up': { /* ... */ },
                'fade-in-up': { /* ... */ },
            },
        },
    },
    plugins: [
        plugin(function({ addVariant }) {
            addVariant('turn-exit', '.turn-exit &');
            addVariant('turn-enter', '.turn-enter &');
        }),
    ],
};

Disabling Animations

To opt out of animations on a specific page, simply add data-turn="false" to the body tag. This gives you finer control over your page transitions.

Troubleshooting Tips

  • If your animations aren’t appearing, check if your device’s settings have requested reduced motion. Turn respects the prefers-reduced-motion media query and won’t animate if it does.
  • Make sure to target only the elements that change on navigation, rather than the entire body, as this can lead to performance issues.
  • If animations are jumpy, consider using the will-change CSS property to optimize them. Turn adds turn-before-exit for this reason.
  • In the case of slow requests, consider using a loading spinner that appears after the exit animation finishes.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By following these steps, you can easily enhance the user experience of your Turbo Drive applications with captivating animations, making your web interactions feel seamless and engaging.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox