In this post, I’ll show you the basics of working with Nuxt.js, as well as how to create awesome transitions when building apps with Nuxt.

Interrupt a user’s experience with your app and watch them leave. One reason for this can be narrowed down to awkward navigation in your app. When moving from one section to another leaves users figuring out if the app is broken or a page is taking too long to load, improvements need to be made.

Besides being nice to watch and having the ability to boost branding when done well, transitions do a great job to enhance the user experience by preserving the user’s context, keeping their attention on the app as well as providing visual continuity.

Setting up a Nuxt.js Project

So you might ask: “But why Nuxt?” Simply put, Nuxt is a framework used for creating universal Vue.js applications. Inspired by Next.js, a framework for server-rendered React applications, its main scope is UI rendering while abstracting away the client/server distribution. Besides seamlessly adding transitions to your page, Nuxt comes along with amazing features such as:

Server-side rendering

Extensive support of pre-processors like Sass, Less, and Stylus

Easy routing

The Nuxt team has created a starter template to help us get up and running. To set up a Nuxt project, install the starter template via vue-cli :

vue init nuxt-community/starter-template <project-name> cd <project-name> #install dependencies npm install #launch the project npm run dev

N.B: If vue-cli is not installed, you can install it with npm install -g vue-cli . You just need to do this once.

For the demo, I’m going to name my project nuxt-lite . Feel free to be creative here.

The application will run on http://localhost:3000/. You should see the following welcome page in your browser:

The directory structure of your newly created Nuxt project should look like this:

Back to the browser, let’s find out what’s behind the initial view of our application. In the project folder, navigate to the pages directory and open the index.vue file, inside the file, is all the markup it took to create our application’s landing page. What makes this cool is that our .vue the file doesn’t require any special setup. Once it is placed inside the pages directory Nuxt.JS automatically makes it a server-side rendered page.

Let’s create something basic, such as a clothing store, and set up routing between the store’s pages. In the pages folder navigate to index.vue and replace the markup there with the following code block:

// index.vue <template> <div class="clothrack"> <h1>Welcome to Hommes, What do you want to sew?</h1> <div class="clothes"> <p><nuxt-link to="/shirts">Shirts</nuxt-link></p> </div> </div> </template> <style> .clothrack { font-family: 'Indie Flower', cursive; padding: 50px; width: 100vw; height: 100vh; display: flex; justify-content: space-evenly; flex-wrap: wrap; } .clothes { display: flex; justify-content: space-evenly; flex-wrap: wrap; width: 500px; } </style>

In the pages folder, create another page and call it shirts.vue . In it, insert the following code block:

// shirts.vue <template> <div class="clothrack"> <h1>Get a Shirt Today!</h1> <div class="clothes"> <p><nuxt-link to="/">Go back to the Home page</nuxt-link></p> </div> </div> </template>

In the pages folder, create a second page and call it trousers.vue . In it, insert the following code block:

// trousers.vue <template> <div class="clothrack"> <h1>Get Some Trousers Today!</h1> <div class="clothes"> <p><nuxt-link to="/">Go back to the Home page</nuxt-link></p> </div> </div> </template>

We want our app to be as extensive as possible. To this end, we’ll create yet another page. In the pages folder, create another page and call it coats.vue . In it, insert the following code block:

// coats.vue <template> <div class="clothrack"> <h1>Get a Coat Today!</h1> <div class="clothes"> <p><nuxt-link to="/">Go back to the Home page</nuxt-link></p> </div> </div> </template>

To set up internal routing links between our pages, we’ll use the <nuxt-link/> tag. This tag works just like an a tag, the only difference is that instead of an href it uses to=``"``/page-title-here``" . Let’s include the internal routing links for our Trousers page and our Coats page:

// index.vue <template> <div class="clothrack"> <h1>Welcome to Hommes, What do you want to sew?</h1> <div class="clothes"> <p><nuxt-link to="/shirts">Shirts</nuxt-link></p> <p><nuxt-link to="/trousers">Trousers</nuxt-link></p> <p><nuxt-link to="/coats">Coats</nuxt-link></p> </div> </div> </template>

Creating a Page Transition

You might have noticed a progress bar running across the top of the screen as we keep routing from page to page. For this demo, we won’t be needing it. In the project’s root folder, navigate to the nuxt.config.js file and change these lines of code:

// nuxt.config.js /* ** Customize the progress-bar color */ loading: { color: '#3B8070' }, to… loading: false,

Aside from the progress bar in the nuxt.config.js file, other things to notice include the meta and head tags as well as the content that will be rendered inside of them. Unlike our normal CLI build with Vue, we won’t have an index.html file. Nuxt breaks down and builds our index.vue file on the server, together with these tags, and then delivers the content for us. Links to fonts and CSS files can be added using the syntax below:

link: [ { rel: '', type: 'text/css', href: '<link to the font or CSS file>' } ]

To create our page transitions, we use Nuxt to plug into the functionality of Vue’s <transition/> component. This component gives us some defaults and hooks that we will work with. To get an in-depth knowledge of how Vue’s transition component works, you can check out this section in the docs here. Check out this image from the docs that best describes the positions at which the transitions start, occur and end:

In the image above, there’s a hook for what happens right before the animation starts v-``enter , during the transition from enter to enter-to and when it finishes. There’s also a hook for when the animation is leaving leave , during the transition from leave to leave-to and when it finishes. These hooks enable us to make simple transitions that insert between states. In Vue, the hooks began with v but Nuxt has a different approach, Nuxt hooks begin with page . Let’s create transitions that will plug into the hooks. In our index.vue file, we’ll insert some lines of CSS for the transitions, we’ll also include some extra styling so we can detect the transitions easily:

<!-- index.vue --> <style> /* Transitions using the page hook */ page-enter-active, .page-leave-active { transition: all .30s ease-out; } .page-enter, .page-leave-active { opacity: 0; transform-origin: 50% 50%; } .clothrack { font-family: 'Indie Flower', cursive; padding: 50px; width: 100vw; height: 100vh; display: flex; justify-content: space-evenly; flex-wrap: wrap; } .clothes { display: flex; justify-content: space-evenly; flex-wrap: wrap; width: 500px; } html, body { font-family: 'Indie Flower', cursive; background: #9fdfbf; color: #000000; width: 100vw; height: 100vh; } h1 { width: 1000px; justify-content: center; display: flex; } a, a:visited { color: #ffffff; text-decoration: none; } </style>

Adding multiple page transitions

Right now, all we’ve added is just a CSS transition that flips between our Home page and our other pages; there’s nothing to suggest where a page could be coming from or going to. Let’s create a different transition between our Home page and our Shirts page. In our shirts.vue file, we’ll use CSS animations to specify the direction in which our pages are coming from and going to, then plug into each of .page-enter-active , and .page-leave-active :

<!-- shirts.vue --> <style scoped> .page-enter-active { animation: acrossIn .40s ease-out both; } .page-leave-active { animation: acrossOut .60s ease-in both; } @keyframes acrossIn { 0% { transform: translate3d(-100%, 0, 0); } 100% { transform: translate3d(0, 0, 0); } } @keyframes acrossOut { 0% { transform: translate3d(0, 0, 0); } 100% { transform: translate3d(100%, 0, 0); } } .clothrack { font-family: 'Indie Flower', cursive; padding: 50px; width: 100vw; height: 100vh; } .clothes { display: flex; justify-content: space-evenly; flex-wrap: wrap; width: 500px; } html, body { font-family: 'Indie Flower', cursive; background: #9fdfbf; color: #000000; width: 100vw; height: 100vh; } h1 { width: 1000px; justify-content: center; display: flex; } a, a:visited { color: #ffffff; text-decoration: none; } </style>

The function of the scoped tag is to limit the styles to be applied to this page only. Let’s give shirts.vue a different text color so we can tell the difference:

//shirts.vue <style scoped> .clothrack { color: #595959; } </style>

Okay let’s say, for some of us, that was way too much transition, we want something basic, say a fade out. To create separate transitions, in the .vue file where we want a different transition, we rename our transition hook just to set it out:

<script> export default { transition: '<transition hook name>' } </script>

Let’s create a fade out transitions for our Trousers and Coats page. In the pages folder, navigate to the trousers.vue file and insert this block of code:

//trousers.vue <script> export default { transition: 'tweakOpacity' } </script>

We have given the transition for our Trousers page a new identity, let’s proceed to insert the animations for our Trousers page:

//trousers.vue <style> .tweakOpacity-enter-active, .tweakOpacity-leave-active { transition: opacity .40s ease-out; } .tweakOpacity-enter, .tweakOpacity-leave-active { opacity: 0; } .clothrack { font-family: 'Indie Flower', cursive; padding: 50px; width: 100vw; height: 100vh; display: flex; flex-wrap: wrap; justify-content: center; } .clothes { display: flex; justify-content: space-evenly; flex-wrap: wrap; width: 500px; } html, body { font-family: 'Indie Flower', cursive; background: #9fdfbf; color: #000000; width: 100vw; height: 100vh; } h1 { width: 1000px; justify-content: center; display: flex; } a, a:visited { color: #ffffff; text-decoration: none; } </style>

When we are through with our Trousers page, we perform the same actions on our Coats page. It’s possible to go even further and create more CSS animations for every new page. Let’s check out what our app looks like now:

Conclusion

It’s very clear that working with Nuxt offers more possibilities and ways of building projects in an easier, less daunting way. Animations and transitions should be considered when building user interfaces as they say a thousand words about your work. The Nuxt.js documentation is a good place to start. Throwing Vue into your shopping basket wouldn’t be a bad bargain. Happy coding!