make a simple Vue Modal component from scratch
Tags: javascripttips, vue, javascript, vuejs2
Published: 2019-01-22 08:10:12

---
title: make a simple Vue Modal component from scratch
published: true
tags: javascript-tips,vue,javascript,vuejs-2
canonical_url: https://medium.com/swlh/make-a-simple-vue-modal-component-from-scratch-29b17a1ea983
---

![](https://cdn-images-1.medium.com/max/1024/1*6nkvufwRBEtQedF6QCpiKw.png)

#### Feature Vue Course

### [Building Applications with VueJs, Vuex, VueRouter, and Nuxt](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1917274&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fbuilding-applications-with-vuejs%2F)

### [Use Vuejs 2 to Create a Beautiful SEO-Ready Website](https://click.linksynergy.com/link?id=qt/jYwyHv8A&offerid=507388.1591824&type=2&murl=https%3A%2F%2Fwww.udemy.com%2Fvuejs-seo-website-course%2F)

Modals are a scripted effect that allows you to display a small element over a website, be it an image, form inputs or text information depending on the use case — we’ll discuss these a little later. The point is modals are great! The biggest benefit of the modal is that they avoid the need to use conventional window pop-ups. In short, modal dialog windows are a means to quickly display information to users on the same page they are working on, creating a better user experience for anyone accessing your website and decreasing annoyingly unnecessary page reloads.

We usually have to place modals on our web pages when faced with one or more of the following scenarios:

- **Warnings**  — When we have to warn the user of something that could be potentially harmful to them.
- **Errors — ** When we want to alert the user of an error.
- **Information — ** When we want to highlight very important information to the users.
- **Decisions**  — When we want the user to confirm a decision i.e confirm or decline.
- **Data collection — ** When we want to collect data from the user with the help of a form usually.

The list is not exhaustive as modals can be used in so many other ways.

Today we’ll use Vue.js, an increasingly popular JavaScript framework to create a simple Modal to collect data with. We’ll then use a product called Bit to make the component easily reusable and help us share this component with others.

Before we begin, make sure you have the following installed on your device:

- [Node.js and npm](https://nodejs.org/en/)
- [Visual Studio Code](https://code.visualstudio.com/)

Once you’re done checking that list, we need to install Vue on our device. We do this with this command npm install -g @vue/cli . When the installation is done, we need to initialize a new Vue project with the following command vue create bitmodal — we’ll name our project **bitmodal.** We then have key incd bitmodal to the terminal to change directories. You’re VS Code should now have a few new files created, key in yarn serve and you should see the usual Vue boiler plate.

![](https://cdn-images-1.medium.com/max/1024/1*MjcbG98VFyGfZ4io0BpEpg.png)

We’ll be met with the standard Vue project layout. Next, we edit the **App.vue** file to make it have the modal component we’ll add later on. Your **App.vue** file should look something like this.

```
<script>
export default {
  name: "Modal",
  data: () => ({
    author: "",
    name: ""
  }),
  methods: {
    close() {
      this.$emit("close");
    }
  }
};
</script>

<template>
  <transition name="modal-fade">
    <div class="modal-backdrop">
      <div class="modal">
        <h1>Hello! I'm a Modal</h1>
        <footer class="modal-footer">
          <slot name="footer">
            <button type="button" class="btn-green" @click="close">
              Close me
            </button>
          </slot>
        </footer>
      </div>
    </div>
  </transition>
</template>

<style>
.modal-fade-enter,
.modal-fade-leave-active {
  opacity: 0;
}
.modal-fade-enter-active,
.modal-fade-leave-active {
  transition: opacity 0.5s ease;
}
.modal-backdrop {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: rgba(0, 0, 0, 0.3);
  display: flex;
  justify-content: center;
  align-items: center;
}
.modal {
  background: #ffffff;
  box-shadow: 2px 2px 20px 1px;
  overflow-x: auto;
  display: flex;
  flex-direction: column;
}
.modal-header,
.modal-footer {
  padding: 5px;
  text-align: center;
}
.modal-footer {
  border-top: 1px solid #eeeeee;
  justify-content: flex-end;
}
.btn-close {
  border: none;
  font-size: 20px;
  padding: 20px;
  cursor: pointer;
  font-weight: bold;
  color: #000;
  background: transparent;
  text-align: right;
}
.btn-green {
  color: white;
  background: #828282;
  border: 1px solid #828282;
  border-radius: 2px;
  margin: 5px;
}
.input {
  margin-top: 5px;
}
.added-text {
  margin-block-end: -10px;
  margin-block-start: 0.5rem;
}
</style>
```

What we did was add a Modal component to the main Vue application. The contents of this component can be edited in the **Modal.vue** file we create next. To trigger the modal, we use the **“@click”** vue feature which calls the function showModal() whenever that button is clicked and the modal opens.

![](https://cdn-images-1.medium.com/max/453/1*lCYYaLHoWOgyH2sPE-ATdA.gif)

Now we have to actually create the modal component, in **src/components   
, **you need to create a file called** Modal.vue **and paste the code below into it. This code just displays a message along with a button for one close the modal. You can find the code to the** Modal.vue** file as well as the whole project in the CodeSandbox of the project below.

<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodesandbox.io%2Fembed%2Foy8jnrl5y&amp;url=https%3A%2F%2Fcodesandbox.io%2Fs%2Foy8jnrl5y&amp;image=https%3A%2F%2Fcodesandbox.io%2Fapi%2Fv1%2Fsandboxes%2Foy8jnrl5y%2Fscreenshot.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codesandbox" width="1000" height="500" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e3a6747a8afeb01e36f0750fc4fa9b7/href">https://medium.com/media/8e3a6747a8afeb01e36f0750fc4fa9b7/href</a></iframe>
### Review

If you were paying attention, we’ve done the following:

- Created a Vue Application in CodeSandbox.
- Added and built a new Modal component to our Vue application.

Now you can build and share app components with ease ✨

Happy programming 👋

[![](https://cdn-images-1.medium.com/max/1024/1*YqDjlKFwScoQYQ62DWEdig.png)](https://medium.com/swlh)

#### This story is published in [The Startup](https://medium.com/swlh), Medium’s largest entrepreneurship publication followed by +412,714 people.

#### Subscribe to receive [our top stories here](http://growthsupply.com/the-startup-newsletter/).

[![](https://cdn-images-1.medium.com/max/1024/1*ouK9XR4xuNWtCes-TIUNAw.png)](https://medium.com/swlh)

* * *