A trend that has increased in recent years is the use of CSS libraries and frameworks for the development of websites and systems in a faster and optimized way. It is very likely that you have heard of or even worked with frameworks like Bootstrap, Materialize, or Bulma. They are evidence of this trend and also show how such frameworks can greatly assist us in terms of productivity.
There are also many libraries that have the same goal, but are aimed at JavaScript frameworks. One example of this is Vuetify. It was created in 2016 with the aim of providing a series of styled components for Vue JS applications, based on Material Design.
In this article, we will learn how to install this library in a Vue project, as well as create our first code with Vuetify, which will be a login screen in an administrative system.
Starting a New Project
Vuetify needs to be added to a Vue project. Therefore, we will start a new project from scratch using vue-cli.
To begin, let's install the Vue CLI tool using the command below:
npm install -g @vue/cli
With this command-line tool installed, we can start a new project in Vue. We will create a project called hcode-admin by executing the following command in the Terminal:
vue create hcode-admin
When we execute this command, the creation process will begin, and we will need to answer a few questions. The first question is whether we want to use a more stable connection for a faster installation. We will type “Y” and press Enter:
Next, we will select which version of Vue we will use, as well as the tools we will use for compiling the project. We will choose version 2 because, as of the date of this article, Vuetify does not work with Vue 3. So, just press Enter:
With that, the creation of our project will begin:
After a few seconds, we will see the success message below:
It states that the project creation was successful, and now we are ready to start its development.
Adding Vuetify to the Project
Now we will add Vuetify to our project. First, we will access the project folder with the command cd hcode-admin.
Next, we will execute the command below:
vue add vuetify
A new installation process will begin, and we will need to answer some new questions.
The first question will be whether we want to perform the installation without first adding our files to Git. We will choose “y” and press Enter:
Next, we need to choose what the preset for our Vuetify will be. We will choose the default preset. So, just press Enter:
The download and addition of Vuetify to the project will begin. After a few seconds, we will see the message below:
This message indicates that the installation of Vuetify was successful!
We can now start the server for our project. To do this, simply execute the following command:
npm run serve
When we access the address http://localhost:8080/ in our browser, we will see the following result:
We can see that Vuetify is already being recognized in our project. Quite simple, don't you think?
Now let's start developing the structure of our login form using this library.
Getting to Know Vuetify's First Components
The first thing we will do is delete the component file called HelloWorld.vue, located in src/components.
Next, we will create a file called AdminForm.vue in this same directory. This name follows the recommendation of Vue itself to provide compound names for our components.
Moreover, it is important to remember that a Vue application uses the structure of a Single File Component. This means that the HTML, CSS, and JavaScript of the component are all in the same file.
The HTML structure of this component will initially be as follows:
<template>
<v-container class="grey lighten-3" id="login-page">
<v-row class="d-flex justify-center">
<v-col
cols="6"
>
<v-img src="../assets/hcode.svg" id="hcode-image"></v-img>
</v-col>
</v-row>
</v-container>
</template>
Explaining: is the tag used by Vue to start the HTML of the component. The
The
We also use the
Before we conduct our test, let's just add some styles to our elements using the tag, which is used in a Single File Component to define the page's CSS:
<style scoped>
#login-page {
margin-top: 10px;
}
#hcode-image {
margin: 20px 0;
}
</style>
We added the scoped attribute to this tag so that only the current page is affected by the styles and not the entire application.
Now, when we access our browser, we see the following:
Excellent, our image and container are already appearing!
Adding Tabs
It is very convenient when we see both the option to log in and to create a new user on a login page. We will do this in our example by creating tabs for these actions.
The component used for this is
<v-tabs
fixed-tabs
background-color="blue-grey lighten-5"
color="black"
v-model="tabsForm"
>
</v-tabs>
The fixed-tabs property defines that our component will completely occupy the space on the screen where it is inserted. The background-color property defines the background color of the element, which was chosen from the Vuetify color palette. The color property defines the text color.
The v-model property is standard in Vue. In this context, it is used so that the switching of elements happens according to the tab we click on.
Therefore, we need to create a property in our Vue instance called tabsForm. We can define a null value for it. Our tag will look like this:
<script>
export default {
name: 'AdminForm',
data: () => ({
tabsForm: null
})
}
</script>
We can now add the items for our tabs to the HTML. To do this, we will use the
<v-tab
key="register"
>
Register
</v-tab>
<v-tab
key="login"
>
Log In
</v-tab>
An important property we need to pay attention to is the key property, as it will be used later to relate our tab to its content.
So far, our code looks as follows:
Now, when we access our project, we see that it looks as follows:
Now our tabs are correctly defined!
Creating Registration and Login Forms
The next step is to create the structure of our forms and then relate them to our tabs.
To do this, we will create another
Our code will look like this:
<v-row class="d-flex justify-center">
<v-col cols="4">
<v-tabs-items
v-model="tabsForm"
cols="3"
>
</v-tabs-items>
</v-col>
</v-row>
Notice that we again use the v-model="tabsForm" property to connect the tabs to their respective contents.
Now let's add the first item of the tabs, which will be a registration form. The content for this structure will be as follows:
<v-tab-item
key="register"
>
<v-form class="text-center grey lighten-3">
<v-text-field
v-model="name"
label="Name"
:rules="nameRules"
required
></v-text-field>
<v-text-field
v-model="email"
label="Email"
:rules="emailRules"
required
></v-text-field>
<v-text-field
v-model="password"
label="Password"
type="password"
:rules="passwordRules"
required
></v-text-field>
<v-btn
color="success"
class="justify-space-between"
>
Sign Up
</v-btn>
</v-form>
</v-tab-item>
Explaining: the key property used in the
The
When we access our project again, we see the following result:
Our form was successfully created. However, note that the email validation is not working because we are providing an incorrect format and no error is returned. To resolve this issue, we will use a property called rules, which has been defined in our component. It represents some rules for form validation.
Now let's go back to our code and define the following in the tag:
nameRules: [
value => !!value || 'The name is required',
value => (value &amp;amp;amp;amp; value.length >= 3) || 'The name must be at least 3 characters long',
value => (value &amp;amp;amp;amp; value.length <= 10) || 'The name must be less than 10 characters long'
],
emailRules: [
value => !!value || 'The email is required',
value => /.+@.+\..+/.test(value) || 'The email is not valid',
],
passwordRules: [
value => !!value || 'The password is required',
value => (value &amp;amp;amp;amp; value.length >= 5) || 'The password must be at least 5 characters long'
]
Each array is identified with the context in which its validation will be applied. In the nameRules validation, we ensure that the name is provided and has between 3 and 10 characters. The emailRules validation ensures that the email is provided and has the correct format. Lastly, the passwordRules validation ensures that the password is provided and that it will have at least 5 characters.
With that, our JavaScript code will look as follows:
Thus, when we access our site and provide invalid data, we will see an error, as we can see in the image below:
Finally, we just need to add the second form to our tabs. We will do this with the following code:
<v-tab-item
key="login"
>
<v-form class="text-center grey lighten-3">
<v-text-field
v-model="email"
label="Email"
:rules="emailRules"
required
></v-text-field>
<v-text-field
v-model="password"
label="Password"
type="password"
:rules="passwordRules"
required
></v-text-field>
<v-btn
color="primary"
class="justify-space-between"
>
Access
</v-btn>
</v-form>
</v-tab-item>
Notice that this form has a different key property, as it refers to another action on our page. However, it is using the same validation functions through the rules property.
When we access our project again, we see the following result:
In this way, if you want to switch from one tab to another, it will work normally?!
Conclusion
In this article, we created our first project using Vuetify. You can check out the developed code on our GitHub.
We hope this article helped you understand what this library is and how it can assist us in our Vue projects. If you want to learn more about this amazing framework, be sure to check out our Complete Vue JS Course ?.
Until the next article!