Let’s code a dribble design with Vue.js & Tailwindcss (Working demo) — Part 2 of 2
Tags: code, javascript, vue, design
Published: 2020-04-25 10:35:27

---
title: Let’s code a dribble design with Vue.js & Tailwindcss (Working demo) — Part 2 of 2
published: true
date: 2020-04-25 09:50:58 UTC
tags: code,javascript,vue,design
canonical_url: https://medium.com/@fayazara/lets-code-a-dribble-design-with-vue-js-tailwindcss-working-demo-part-2-of-2-256799d40ee
cover_image: https://cdn-images-1.medium.com/max/1024/1*uXp7R4kZ4B9pssZy4xhdww.png
---

### Let’s code a dribble design with Vue.js & Tailwindcss (Working demo) — Part 2 of 2

> Article #6 of my “1 article a day till lockdown ends”

So in the [part 1](https://dev.to/fayazara/let-s-code-a-dribble-design-with-vue-js-tailwindcss-working-demo-part-1-of-2-3h9) of this article, we made a UI from the dribble design we picked and used tailwindcss to code it. Let’s add the behaviour & some code to actually make it work.

We have divided our input fields into seperate components and trying to get their value by clicking a button which is outside these components, due to which we need some logic to hear changes from these components to our parent component, i.e our index.vue file, it needs to listen to changes happening inside gender.vue.

Vue let’s you listen to child components using the [emit](https://vuejs.org/v2/guide/components.html#Listening-to-Child-Components-Events) property. So we need to “emit” a function in our child component and a listener in our parent component.

![](https://cdn-images-1.medium.com/max/1024/1*d3HobKxxLzxlnC1ukdBVZg.png)<figcaption>Child and parent component communication</figcaption>

In the above image, a child component has a button and we want to pass a value to our parent component, I will add a custom event listener in our parent component — <component @name-listenter="doSomethingWithName" /> the child component will be emitting a function like <button @click="$emit('name-listenter', 'Fayaz')"> , the name-listener is the key here and vue will listen to it whenever emitted.

Let’s do the same to our gender.vue file, where we will change the value on clicking the male/female card and emit the value to our index.vue file.

Here is how I did it.

{% gist https://gist.github.com/fayazara/032865c9deff1412f943bc500a7e9a97 %}

For the Height component I will use a watch proprty of vue, since the slider event is not explicitly triggering a manual event on value change, we will add a watch listerner and emit the value there.

{% gist https://gist.github.com/fayazara/d76676bbf1474352988038a471593960 %}

Similarly add emit events for our age and weight component. I have added a long press directive [plugin](https://github.com/ittus/vue-long-click) to the weight and age buttons, which let’s you update the value when you hold the buttons.

#### Calculating the BMI.

Now, that we have received all our value in our parent component. To calculate the BMI, the formula is weight(kg)/height\*height(m) , and we also find out that age and gender are not needed to calculate the BMI 😂.

I would suggest you add some sort of validation before showing the results, like handle the negative values and stuff.

#### Lets show the BMI in the result Page.

There are multiple ways we can pass the bmi to the next , we could use vuex, and store the value there, use localStorage or we could just pass the bmi value in the url, because the other two methods seem like a overkill. The below function is calculating the BMI and passing the value as a parameter in the url and redirecting to the result page.

{% gist https://gist.github.com/fayazara/ad021dd8b06dc3283324591d4e746215 %}

![](https://cdn-images-1.medium.com/max/318/1*s1l-qr0_fjjyh4WLoBNW1Q.png)

We can capture the bmi from the URL by using the route object of vue like $route.query.bmi . We now have the value, all we need to do is just show it in our result page, this was the design from the dribble page.

There’s also the BMI range classification, which I found in [Wikipedia](https://en.wikipedia.org/wiki/Classification_of_obesity). Let’s make use of this as well.

There’s a Re calculate button, lets just redirect them back to home and for the “Save Button” Let’s replace it with “Share” chrome’s [Web Share API](https://web.dev/web-share/).

Here is the boiler plate code you can use to build the UI

{% gist https://gist.github.com/fayazara/20e0b8b9feb52f87d48745fd26434ea3 %}

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

The Final result page will look this, I have also added a Webshare button which share’s your BMI with others, this works only on phones though.

So far, we have divided a design in to components, and made the UI, added functionality with vue and passed the value in the next page.

This concludes this small project hope you enjoyed.

You can find the live working demo [here](https://bmi-app.netlify.app/) and the complete project on [github here](https://github.com/fayazara/bmi-calculator-example).

Let me know if you need any help with this or if you are stuck somewhere while making it.

Make sure you follow me on [twitter](https://twitter.com/fayazara) and here as well, to get more articles and updates.