DEV Community

kris
kris

Posted on • Originally published at Medium on

make a simple Vue Modal component from scratch

Feature Vue Course

Building Applications with VueJs, Vuex, VueRouter, and Nuxt

Use Vuejs 2 to Create a Beautiful SEO-Ready Website

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:

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.

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.

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.

https://medium.com/media/8e3a6747a8afeb01e36f0750fc4fa9b7/href

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 👋

This story is published in The Startup, Medium’s largest entrepreneurship publication followed by +412,714 people.

Subscribe to receive our top stories here.


Top comments (0)