Building a flash message component with Vue.js and Tailwind CSS
In this tutorial, I am going to walk through how to build a custom flash message component using Vue.js and TailwindCSS. I’ll be making it inside a brand-new Laravel 5.8 project, but you can adapt it for use in any project running Vue.js and TailwindCSS.
The component we build will have a “danger” theme and a “success” theme. You can choose to extend it with a “warning” theme or any other themes you see fit.
Prerequisites
This is an intermediate tutorial, so I am not going to cover the basics of Vue.js and TailwindCSS or how to set them up in your project. I will assume you have already done that following their documentation. I have also removed all the boilerplate JavaScript in the resources/js/app.js
file except the following:
1window.Vue = require('vue');2 3const app = new Vue({4 el: '#app',5});
In my routes/web.php
file, I am starting with:
1<?php2 3Route::view('/', 'welcome');
In my welcome view (resources/views/welcome.blade.php
), I am starting with:
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>13 <div id="app">14 <h1 class="font-bold">Example Project</h1>15 </div>16</body>17</html>
Let’s get started
To get started, let’s create our flash-message component and register it in our resources/js/app.js
file.
1window.Vue = require('vue');2 3Vue.component('flash-message', require('./components/FlashMessage.vue').default);4 5const app = new Vue({6 el: '#app',7});
Next, we need to include the component in our welcome view so that it will show up on the page. I usually insert it near the bottom of the #app div. We will want this component mounted on any page that might use it.
1<div id="app">2 <h1 class="font-bold">Example Project</h1>3 4 <flash-message></flash-message>5</div>
Styling the component
Let’s get some basic styling done using TailwindCSS. While styling the component, I will use a static message and our “danger” theme, but later there will be variable options. The following markup will place the component in the top right of the screen, add a close icon in the top right of the component, and provide some decent styling.
1<template> 2 <div class="fixed top-0 right-0 m-6"> 3 <div 4 class="bg-red-200 text-red-900 rounded-lg shadow-md p-6 pr-10" 5 style="min-width: 240px" 6 > 7 <button 8 class="opacity-75 cursor-pointer absolute top-0 right-0 py-2 px-3 hover:opacity-100" 9 >10 ×11 </button>12 <div class="flex items-center">13 Oops! Something terrible happened...14 </div>15 </div>16 </div>17</template>
Making the classes and text dynamic
If you replace the bg-red-200 text-red-900
classes with bg-green-200 text-green-900
, you’ll see our basic “success” styling. Let’s make the classes and message text change based on a message
property on our component. We’ll need to add the following to the bottom of the component:
1<template> 2 <div class="fixed top-0 right-0 m-6"> 3 <div 4 :class="{ 5 'bg-red-200 text-red-900': message.type === 'error', 6 'bg-green-200 text-green-900': message.type === 'success', 7 }" 8 class="rounded-lg shadow-md p-6 pr-10" 9 style="min-width: 240px"10 >11 <button12 class="opacity-75 cursor-pointer absolute top-0 right-0 py-2 px-3 hover:opacity-100"13 >14 ×15 </button>16 <div class="flex items-center">17 {{ message.text }}18 </div>19 </div>20 </div>21</template>22 23<script>24export default {25 data() {26 return {27 message: {28 text: 'Hey! Something awesome happened.',29 type: 'success',30 },31 };32 },33};34</script>
Communicating with the component
Now, I’d like to find a way to set the message from outside the component. I think a simple Vue event bus will work great for this purpose. To set that up, we need to update our resources/js/app.js
file to the following:
1window.Vue = require('vue');2window.Bus = new Vue();3 4Vue.component('flash-message', require('./components/FlashMessage.vue').default);5 6const app = new Vue({7 el: '#app',8});
You may have used custom events in your Vue components before. We will be using a similar syntax to emit and listen to events on a global level: Bus.$emit('flash-message')
and Bus.$on('flash-message')
. Now that we have the event bus set up let’s make the component conditionally render based on the message property. We can do that by adding a v-if
to the flash-message and setting the default message property to null.
1<template> 2 <div class="fixed top-0 right-0 m-6"> 3 <div 4 v-if="message" 5 :class="{ 6 'bg-red-200 text-red-900': message.type === 'error', 7 'bg-green-200 text-green-900': message.type === 'success', 8 }" 9 class="rounded-lg shadow-md p-6 pr-10"10 style="min-width: 240px"11 >12 <button13 class="opacity-75 cursor-pointer absolute top-0 right-0 py-2 px-3 hover:opacity-100"14 >15 ×16 </button>17 <div class="flex items-center">18 {{ message.text }}19 </div>20 </div>21 </div>22</template>23 24<script>25export default {26 data() {27 return {28 message: null,29 };30 },31};32</script>
When you load the page, you shouldn’t see anything. Just for an example, let’s add a trigger-form component that we can use to demonstrate how to send events with different options to the flash-message component. Start by creating the component at resources/js/TriggerForm.vue
and registering it in the resources/js/app.js
file and adding the component to the welcome view.
1// ...2Vue.component('flash-message', require('./components/FlashMessage.vue').default);3Vue.component('trigger-form', require('./components/TriggerForm.vue').default);4//...
1<div id="app">2 <h1 class="font-bold">Example Project</h1>3 4 <trigger-form></trigger-form>5 <flash-message></flash-message>6</div>
Inside the form component, we will need to add inputs, a button, and data properties bound to inputs.
1<template> 2 <form class="max-w-md" @submit.prevent="sendMessage"> 3 <label 4 for="message-text" 5 class="block mb-1 text-gray-700 text-sm" 6 > 7 Message Text 8 </label> 9 <input10 id="message-text"11 v-model="message.text"12 type="text"13 class="input mb-3"14 />15 <label16 for="message-type"17 class="block mb-1 text-gray-700 text-sm"18 >19 Message Type20 </label>21 <select id="message-type" v-model="message.type" class="input mb-3">22 <option value="success">23 Success24 </option>25 <option value="error">26 Error27 </option>28 </select>29 <button class="btn btn-blue">30 Send Message31 </button>32 </form>33</template>34 35<script>36export default {37 data() {38 return {39 message: {40 text: 'Hey! Something awesome happened.',41 type: 'success'42 }43 };44 },45 methods: {46 sendMessage() {47 // ...48 }49 }50};51</script>
Inside the sendMessage
method, we will need to use the event bus to emit an event to the flash-message component listener. When emitting an event from a Vue component, the first argument is the name of the event, and the second argument is any data the event listener will need. Here, we will pass ‘flash-message’ as the event name and this.message
as the second argument. We will also reset the message after emitting the event.
1sendMessage() {2 Bus.$emit('flash-message', this.message);3 4 this.message = {5 text: null,6 type: 'success',7 }8}
Inside our flash-message component, we need to set up a listener for this event and a callback to handle it. Let’s start by adding a mounted method. Initially, all we need to do is set the message inside the component equal to the message that was passed with the event.
1mounted() {2 Bus.$on('flash-message', (message) => {3 this.message = message;4 });5}
Now when we submit the form, the message component should appear with the text and theme we selected in the form.
Making the component disappear
To make our close button work, we need to add an event handler to the button.
1<button2 class="opacity-75 cursor-pointer absolute top-0 right-0 py-2 px-3 hover:opacity-100"3 @click.prevent="message = null"4>
Next, we’ll make the component automatically disappear after a few seconds. We can accomplish this pretty easily using the setTimeout
function.
After we handle setting the message in our mounted function, we can use setTimeout
to clear the message after 5 seconds. If you want yours to disappear faster or slower, you can change that value.
1mounted() {2 Bus.$on('flash-message', (message) => {3 this.message = message;4 5 setTimeout(() => {6 this.message = null;7 }, 5000);8 });9}
Initially, this solution may seem like it works fine, but if you submit the form twice within 5 seconds, the message will still disappear 5 seconds from when the first event was triggered. To solve that, we need to save the timer that’s returned from the call to setTimeout
and make sure to reset it when the next event comes in. We can easily do that by updating our code to the following.
1mounted() { 2 let timer; 3 Bus.$on('flash-message', (message) => { 4 clearTimeout(timer); 5 6 this.message = message; 7 8 timer = setTimeout(() => { 9 this.message = null;10 }, 5000);11 });12}
Transitioning the component in and out
Next, we will use Vue’s <Transition>
component to slide the component in and out. First, we need to add a <style>
tag to the bottom of the component. We’ll add the CSS classes necessary for the transitions there.
1<style scoped> 2.slide-fade-enter-active, 3.slide-fade-leave-active { 4 transition: all 0.4s; 5} 6.slide-fade-enter, 7.slide-fade-leave-to { 8 transform: translateX(400px); 9 opacity: 0;10}11</style>
Inside our template, we need to wrap the flash-message in a Transition
element and pass it a name.
1<template> 2 <div class="fixed top-0 right-0 m-6"> 3 <Transition name="slide-fade"> 4 <div 5 v-if="message" 6 :class="{ 7 'bg-red-200 text-red-900': message.type === 'error', 8 'bg-green-200 text-green-900': message.type === 'success' 9 }"10 class="rounded-lg shadow-md p-6 pr-10"11 style="min-width: 240px"12 >13 <button class="opacity-75 cursor-pointer absolute top-0 right-0 py-2 px-3 hover:opacity-100">14 ×15 </button>16 <div class="flex items-center">17 {{ message.text }}18 </div>19 </div>20 </Transition>21 </div>22</template>
In conclusion
If you’d like to add additional options like a message.delay
property that specifies when the message will be cleared, feel free to do so. I’d love to see the different ways you take this example and make it better.
To view the full source code including the CSS for the form components, go here, and if you have any questions feel free to contact me at Jason Beggs