Building a Modal Using Vue JS

Posted by


Modals are a common UI component used in web development to display content in a pop-up window. They are great for showing images, videos, forms, or any other content that needs to be focused on by the user.

In this tutorial, we will be creating a modal using Vue JS, a popular JavaScript framework for building interactive web applications. We will use Vue components to create our modal and make it easy to reuse throughout our application.

Step 1: Set up a new Vue project

The first step is to set up a new Vue project. You can do this by using the Vue CLI, a command-line tool for creating Vue projects.

First, make sure you have Node.js installed on your machine. You can check if you have Node.js installed by running the following command in your terminal:

node -v

If you don’t have Node.js installed, you can download and install it from the official website.

Next, install the Vue CLI globally by running the following command in your terminal:

npm install -g @vue/cli

Now, create a new Vue project by running the following command:

vue create my-modal-project

Follow the prompts to set up your Vue project. Once the project is set up, navigate to the project directory by running:

cd my-modal-project

Step 2: Create the modal component

In the Vue project directory, create a new Vue component for the modal. You can create a new file in the src/components directory called Modal.vue.

In Modal.vue, add the following code to create a simple modal component:

<template>
  <div class="modal">
    <div class="modal-content">
      <span class="close" @click="closeModal">&times;</span>
      <slot></slot>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    closeModal() {
      this.$emit('close');
    }
  }
}
</script>

<style>
.modal {
  display: none;
  position: fixed;
  z-index: 1;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0,0,0,0.4);
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #888;
  width: 80%;
}

.close {
  color: #aaaaaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
}

.close:hover,
.close:focus {
  color: #000;
  text-decoration: none;
  cursor: pointer;
}
</style>

This code creates a basic modal component with a close button and a slot for content. The modal is hidden by default and shown when a close event is emitted.

Step 3: Use the modal component in your app

Now that we have created the modal component, we can use it in our Vue app. Open the src/App.vue file and add the following code to use the modal component:

<template>
  <div id="app">
    <button @click="showModal">Open Modal</button>
    <modal v-if="isModalVisible" @close="hideModal">
      <h1>Hello, Modal!</h1>
    </modal>
  </div>
</template>

<script>
import Modal from './components/Modal.vue';

export default {
  components: {
    Modal
  },
  data() {
    return {
      isModalVisible: false
    }
  },
  methods: {
    showModal() {
      this.isModalVisible = true;
    },
    hideModal() {
      this.isModalVisible = false;
    }
  }
}
</script>

In this code, we import the Modal component and add it to the components object. We also define a data object with a property isModalVisible to control the visibility of the modal. The showModal and hideModal methods change the value of isModalVisible to show and hide the modal, respectively.

Step 4: Style the modal

Finally, add some CSS to style the modal. You can create a new file in the src/assets directory called styles.css and add the following code:

body {
  font-family: Arial, sans-serif;
  margin: 0;
  padding: 0;
}

button {
  padding: 10px 20px;
  background-color: #007bff;
  color: white;
  border: none;
  cursor: pointer;
}

button:hover {
  background-color: #0056b3;
}

Step 5: Run your Vue app

You can now run your Vue app by running the following command in your terminal:

npm run serve

Open your browser and navigate to http://localhost:8080 to see your modal in action. Click the "Open Modal" button to open the modal and click the close button to close it.

Congratulations! You have successfully created a modal with Vue JS. You can now customize the modal further by adding more features and styling to fit your needs. Enjoy coding!

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@numanakhtersiddiqui6573
3 months ago

👍👍💯💯💯

@timu5038
3 months ago

gorgeous, thanks! I am a student trying to comprehend VUE, can you be contacted via Telegram to ask a few questions? I just need a modal, but I can't use it in my case