Responsive Navbar UI Built with Vue, Vue-Motion, PrimeVue, TailwindCSS, and TypeScript

Posted by


In this tutorial, we will be creating a responsive Navbar UI using Vue.js, Vue-motion, PrimeVue, TailwindCSS, and TypeScript. This tutorial will guide you through the process of setting up a Navbar component that adjusts its layout based on the screen size.

Before we start, make sure you have Node.js and npm installed on your machine. If not, you can download and install them from the official Node.js website.

Step 1: Setting up the project

First, create a new Vue project using Vue CLI by running the following commands in your terminal:

vue create navbar-responsive-ui
cd navbar-responsive-ui

Next, install the required dependencies:

npm install primevue vue-motion tailwindcss
npm install --save-dev @types/vue @types/tailwindcss @types/primevue

Step 2: Creating the Navbar component

Create a new file named Navbar.vue in the src/components directory of your project and add the following code to it:

<template>
  <div class="navbar">
    <div class="flex items-center justify-between p-4">
      <div>
        <img src="@/assets/logo.png" alt="Logo" class="h-8 w-auto">
      </div>
      <div class="flex items-center space-x-4 lg:space-x-8">
        <router-link to="/" class="nav-link">Home</router-link>
        <router-link to="/about" class="nav-link">About</router-link>
        <router-link to="/services" class="nav-link">Services</router-link>
        <router-link to="/contact" class="nav-link">Contact</router-link>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'

export default defineComponent({
  name: 'Navbar'
})
</script>

<style lang="postcss" scoped>
.navbar {
  background-color: #333;
  color: white;
}

.nav-link {
  color: white;
  text-decoration: none;
}

@media (min-width: 768px) {
  .navbar {
    padding: 1rem;
  }
}
</style>

Step 3: Importing PrimeVue styles

In the main entry file of your Vue project (src/main.ts), import the PrimeVue CSS file by adding the following line of code:

import 'primevue/resources/primevue.min.css'
import 'primeicons/primeicons.css'

Step 4: Adding the Navbar component to the layout

Open the src/App.vue file and replace the existing content with the following code:

<template>
  <div>
    <Navbar />
    <router-view />
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import Navbar from './components/Navbar.vue'

export default defineComponent({
  name: 'App',
  components: {
    Navbar
  }
})
</script>

Step 5: Testing the Navbar component

Run the Vue development server using the following command:

npm run serve

Navigate to http://localhost:8080 in your browser to see the Navbar component in action. Resize your browser window to test the responsive behavior of the Navbar.

Congratulations! You have successfully created a responsive Navbar UI using Vue.js, Vue-motion, PrimeVue, TailwindCSS, and TypeScript. Feel free to customize the styles and layout of the Navbar component to suit your needs.