Building a modal with Vue.js and Tailwind CSS

Published on

Modal windows are a popular UI component and are useful for many different scenarios. You might use one for alerting a user, showing a form, or even popping up a login form. The uses are limitless.

In this tutorial, we’ll walk through how to build a reusable card modal using Vue.js and Tailwind CSS. The component will use Vue.js slots, so you can change the contents of the modal wherever it is used while retaining the open/close functionality and the wrapper design.

We’ll be starting with a brand-new Laravel 5.8 project. The only additional setup we need to perform is setting up Tailwind, but I won’t be going into detail on how to setup Vue and Tailwind in this tutorial.

Getting started with the modal

To begin, let’s create a CardModal Vue component and register it in the resources/js/app.js file.

1// resources/assets/js/components/CardModal.vue
2<template>
3 <div>
4 The modal will go here.
5 </div>
6</template>
7 
8<script>
9export default {
10 //
11}
12</script>
1// resources/js/app.js
2Vue.component('card-modal', require('./components/CardModal.vue').default);
3 
4const app = new Vue({
5 el: '#app',
6});

To start using the component, we need to update the resources/views/welcome.blade.php view to the following. Note the .relative class on the body tag.

1 
2<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
3<head>
4 <meta charset="utf-8">
5 <meta name="viewport" content="width=device-width, initial-scale=1">
6 
7 <title>{{ config('app.name', 'Laravel') }}</title>
8 
9 <script src="{{ asset('js/app.js') }}" defer></script>
10 <link href="{{ asset('css/app.css') }}" rel="stylesheet">
11</head>
12<body class="relative font-sans p-8">
13 <div id="app">
14 <h1 class="font-bold text-2xl text-gray-900">Example Project</h1>
15 <p class="mb-6">This is just a example text for my tutorial.</p>
16 
17 <card-modal></card-modal>
18 </div>
19</body>
20</html>

Making the modal appear

Right now, the text inside the modal will always show. Let’s start by making the component accept a prop to show or hide the contents.

Update the component to accept a showing prop and add a v-if directive to the div in the template to show/hide the contents when the showing prop changes.

1<template>
2 <div v-if="showing">
3 The modal will go here.
4 </div>
5</template>
6 
7<script>
8export default {
9 props: {
10 showing: {
11 required: true,
12 type: Boolean
13 }
14 }
15}
16</script>

We’ll also need to add a data property to our Vue instance so we can show or hide the modal from outside the CardModal component. We’ll default the property to false so the modal will be hidden when the page loads.

1const app = new Vue({
2 el: '#app',
3 data: {
4 exampleModalShowing: false,
5 },
6});

Then, we need to pass the exampleModalShowing prop to the CardModal in our welcome view. We’ll also need a button to show the modal.

1<div id="app">
2 <h1 class="font-bold text-2xl text-gray-900 ">Example Project</h1>
3 <p class="mb-6">This is just a example text for my tutorial.</p>
4 
5 <button
6 class="bg-blue-600 text-white px-4 py-2 text-sm uppercase tracking-wide font-bold rounded-lg"
7 @click="exampleModalShowing = true"
8 >
9 Show Modal
10 </button>
11 <card-modal :showing="exampleModalShowing"></card-modal>
12</div>

Styling the modal

Next, let’s add some styling to the modal. We’ll need a card surrounding the contents and a semi-transparent background around the card. The background will also need to be position fixed so it can take up the full screen without moving any of the other contents on the page. Let’s start by adding the background and centering the contents. For the transparent background, we will need to add a semi-75 color to our Tailwind configuration.

1<template>
2 <div
3 v-if="showing"
4 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
5 >
6 The modal will go here.
7 </div>
8</template>

To add the semi-75 color so the bg-semi-75 class works, we will extend the colors configuration in our tailwind.config.js file.

1module.exports = {
2 theme: {
3 extend: {
4 colors: {
5 'bg-semi-75': 'rgba(0, 0, 0, 0.75)'
6 }
7 }
8 }
9};

Now, we need to set a max width, background color, shadow, rounded edges, and padding for the card. We’ll add a div to wrap the content inside the modal and add these classes to it.

1<div
2 v-if="showing"
3 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
4>
5 <div class="w-full max-w-2xl bg-white shadow-lg rounded-lg p-8">
6 The modal will go here.
7 </div>
8</div>

Using slots for the content

Now that we have the basic styling finished, let’s update the component to use a slot so the content of the modal can be configured where the component is used instead of inside the component. This will make the component much more reusable.

First, we need to replace the content inside the component with a <slot>. If you’re not familiar with Vue.js slots, essentially, they allow you to pass html into a component and it will be rendered wherever you specify the <slot> tags.

1<div
2 v-if="showing"
3 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
4>
5 <div class="w-full max-w-2xl bg-white shadow-lg rounded-lg p-8">
6 <slot />
7 </div>
8</div>

Second, in the welcome view, we just place the html we want to show inside the modal between the <card-modal> and </card-modal> tags.

1<card-modal :showing="exampleModalShowing">
2 <h2>Example modal</h2>
3 <p>This is example text passed through to the modal via a slot.</p>
4</card-modal>

Closing the modal

The component is getting close to finished, but we have one little problem. We haven’t made a way to close the modal yet. I’d like to add a few different ways to close the modal. First, we’ll add a simple close x at the top right of the card. We need to add a button to the template that calls a close method inside the component. Be sure to add the .relative class to the card div.

1<template>
2 <div
3 v-if="showing"
4 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
5 >
6 <div class="relative w-full max-w-2xl bg-white shadow-lg rounded-lg p-8">
7 <button
8 aria-label="close"
9 class="absolute top-0 right-0 text-xl text-gray-500 my-2 mx-4"
10 @click.prevent="close"
11 >
12 ×
13 </button>
14 <slot />
15 </div>
16 </div>
17</template>
18 
19<script>
20export default {
21 props: {
22 showing: {
23 required: true,
24 type: Boolean
25 }
26 },
27 methods: {
28 close() {
29 this.$emit('close');
30 }
31 }
32};
33</script>

You’ll see that the close method emits a close event. We’ll need to listen for the event outside the component and update the exampleModalShowing property to false. In the welcome view, we can listen for the event by adding a @close listener on the <card-modal> tag.

1<card-modal :showing="exampleModalShowing" @close="exampleModalShowing = false">
2 <h2 class="text-xl font-bold text-gray-900">Example modal</h2>
3 <p>This is example text passed through to the modal via a slot.</p>
4</card-modal>

To close the modal from outside the component, we can add a button that sets exampleModalShowing to false as well.

1<card-modal :showing="exampleModalShowing" @close="exampleModalShowing = false">
2 <h2 class="text-xl font-bold text-gray-900">Example modal</h2>
3 <p class="mb-6">This is example text passed through to the modal via a slot.</p>
4 <button
5 class="bg-blue-600 text-white px-4 py-2 text-sm uppercase tracking-wide font-bold rounded-lg"
6 @click="exampleModalShowing = false"
7 >
8 Close
9 </button>
10</card-modal>

Now when we click the “Show Modal” button, the modal should appear. When we click the close button or the x inside the modal, the modal should disappear.

I’d also like the modal to close when the background behind the card is clicked. Using Vue.js, it’s pretty easy to add that functionality. We can just add @click.self="close" to the background div and Vue will handle the rest. The .self modifier will make it so the listener is only triggered when the background itself is clicked. Without that modifier, the modal would close whenever anything inside the card is clicked as well, which is not what we want.

1<template>
2 <div
3 v-if="showing"
4 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
5 @click.self="close"
6 >
7 <div class="relative w-full max-w-2xl bg-white shadow-lg rounded-lg p-8">
8 <button
9 aria-label="close"
10 class="absolute top-0 right-0 text-xl text-gray-500 my-2 mx-4"
11 @click.prevent="close"
12 >
13 ×
14 </button>
15 <slot />
16 </div>
17 </div>
18</template>

Adding a transition

To make the component feel smoother, let’s wrap the component in a transition so the modal fades in. Once again, Vue makes this pretty easy with <Transition> components. We just need to wrap the background div in a <Transition> tag and add a few CSS classes to the bottom of the component.

1<template>
2 <Transition name="fade">
3 <div
4 v-if="showing"
5 class="fixed inset-0 w-full h-screen flex items-center justify-center bg-semi-75"
6 @click.self="close"
7 >
8 <div class="relative w-full max-w-2xl bg-white shadow-lg rounded-lg p-8">
9 <button
10 aria-label="close"
11 class="absolute top-0 right-0 text-xl text-gray-500 my-2 mx-4"
12 @click.prevent="close"
13 >
14 ×
15 </button>
16 <slot />
17 </div>
18 </div>
19 </Transition>
20</template>
21 
22// script...
23 
24<style scoped>
25.fade-enter-active,
26.fade-leave-active {
27 transition: all 0.4s;
28}
29.fade-enter,
30.fade-leave-to {
31 opacity: 0;
32}
33</style>

Fixing scroll issues

Overall, the component is working pretty well. We can open/close the modal, it fades in nicely, and is really reusable. If you add the component to a page with a lot of content though, you might notice one issue. While the modal is open, if you try to scroll the page, the background is allowed to scroll. This is usually not desirable, so I’ll show you how to fix that issue. We can add a Vue watcher to the showing prop. When the showing prop is set to true, we need to add overflow: hidden to the body element of our page. When it is set to false, we need to remove that style. We can use the .overflow-hidden class provided by Tailwind.

1<script>
2export default {
3 props: {
4 showing: {
5 required: true,
6 type: Boolean
7 }
8 },
9 watch: {
10 showing(value) {
11 if (value) {
12 return document.querySelector('body').classList.add('overflow-hidden');
13 }
14 
15 document.querySelector('body').classList.remove('overflow-hidden');
16 }
17 },
18 methods: {
19 close() {
20 this.$emit('close');
21 }
22 }
23};
24</script>

Conclusion

Now that our component is complete, you’re free to use it as you wish, in multiple places with different content in each place. It’s a really useful component for showing small forms, getting user confirmations, and other use cases. I’d love to hear how you end up using the component!

This component is based on some principles taught in Adam Wathan’s “Advanced Vue Component Design” course and simplified/modified for my needs. If you’re interested in learning more about this subject and other advanced Vue.js practices, I would highly recommend checking out his course!