Hone logo
Hone
Problems

Vue Router Transitions: Bringing Pages to Life

This challenge focuses on implementing engaging visual transitions between different routes in a Vue.js application using Vue Router. Smooth and dynamic page transitions significantly enhance user experience, making your application feel more polished and interactive.

Problem Description

Your task is to create a multi-page Vue application that utilizes Vue Router for navigation and implements custom transition effects when navigating between different views. You will be responsible for defining and applying these transitions.

Key Requirements:

  • Vue Application Structure: Set up a basic Vue 3 application with Vue Router.
  • Multiple Routes/Views: Create at least three distinct views (e.g., Home, About, Contact).
  • Navigation: Implement navigation links between these views using <router-link>.
  • Transition Component: Utilize Vue's built-in <transition> component.
  • Custom CSS Transitions: Define CSS classes to create visually appealing transition effects (e.g., fade, slide, scale).
  • Applying Transitions: Apply these transitions to the <router-view> component.

Expected Behavior:

When a user navigates from one route to another, a smooth, animated transition should occur. For instance, when moving from the "Home" page to the "About" page, the "Home" component should animate out, and the "About" component should animate in.

Edge Cases to Consider:

  • Directional Transitions: Consider how you might implement transitions that vary based on the direction of navigation (e.g., sliding right when going forward, sliding left when going backward). While not strictly required for the initial implementation, it's a good point for advanced thinking.
  • Complex Components: Ensure your transitions work correctly even with components that might have their own internal state or animations.

Examples

Example 1: Basic Fade Transition

Input: A Vue application with three routes: /, /about, /contact. Navigation links are present. The <router-view> is wrapped in a <transition> component with a name like fade. CSS classes .fade-enter-active, .fade-leave-active, .fade-enter-from, .fade-enter-to, .fade-leave-from, .fade-leave-to are defined for a simple fade effect.

Output: When navigating between routes, components smoothly fade in and out.

/* Example CSS for fade transition */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.5s ease;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.fade-enter-to,
.fade-leave-from {
  opacity: 1;
}

Example 2: Slide Transition

Input: Same as Example 1, but CSS classes are defined for a sliding effect.

Output: When navigating between routes, components slide in and out from a specific direction (e.g., right to left).

/* Example CSS for slide transition (from right) */
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.5s ease;
}
.slide-enter-from,
.slide-leave-to {
  transform: translateX(100%);
}
.slide-enter-to,
.slide-leave-from {
  transform: translateX(0);
}

Constraints

  • Vue Version: Vue 3.x.
  • Vue Router Version: Vue Router 4.x.
  • Language: TypeScript.
  • CSS: Standard CSS or SCSS.
  • Component Structure: Your solution should be a functional Vue application, not just isolated components.

Notes

  • Remember to correctly configure your Vue Router instance.
  • The <transition> component in Vue works by applying specific CSS classes based on the lifecycle of the element entering or leaving the DOM. You'll need to define these classes.
  • Consider using mode="out-in" or mode="in-out" on the <transition> component for different animation behaviors. out-in is often preferred for route transitions as it waits for the current element to finish exiting before the new element starts entering.
  • Think about how to organize your transition CSS for maintainability.
Loading editor...
typescript