DEV Community

Mbianou Bradon
Mbianou Bradon

Posted on • Originally published at

How to create Followers Component with Follow functionality Using TailwidCSS

Design Inspired from [icodethis]( platform

Social media are interactive technologies that facilitate the creation and sharing of information, ideas, interests, and other forms of expression through virtual communities and networks.

One of the amazing features social media offers is the ability to connect with people, and make new friends and sometimes even future colleagues. But Generally, before becoming friends or getting to know each other, you usually have to do a Friend Request or As in most platform, You have to Follow the person.

This feature allows you to build a solid and strong network of people in your field and beyond. It also permit you to surround yourself with like-minded indivuduals that help you stay motivated and focus. All social media offer this feature, you can see it on Facebook, Twitter, LinkedIn etc.

Together, we are going to build one of this Functional Followers components, that you can include in your next project, or add to you skills sections of your portfolio.

Let's begin!

Demo of Followers Components

Understanding the Task

This is component component is easy that it looks like. Basically, we are different Social Media Profiles, which you can press the follow the button and add the person to you list of people. So what we will be doing in this task, is create small profile list (stored in an array of Objects), and also add the follow/unfollow functionality.

Easy peasy right?

Understanding Component

Structure of Code

Our code will be divided into two parts. we will have the HTML part, with all the styling done with TailwindCSS of course, then we will also have a JavaScript file that contains our data to be displayed, and also has the code for the Follow/Unfollow funtionality.

HTML + TailwindCSS

As discussed earlier on, our HTML code will have our interface and a container to hold our social profiles.

As always, we have the code, then have detailed explanations immediately after it.

<body class="bg-blue-700 flex items-center justify-center min-h-screen">

<!-- First Layer -->
 <div class="w-[20rem] sm:max-w-[22rem] [&_*]:transition-all [&_*]:ease-linear [&_*]:duration-200">

<!-- Second Layer -->
                <!-- Main Container -->        
    <div class="bg-blue-900 py-6 px-3">
                <!-- Header -->
     <div class="flex w-full relative items-center justify-center text-slate-100 mb-3">
        <div class="text-3xl absolute left-0 hover:text-4xl cursor-pointer ">
    <iconify-icon icon="material-symbols:chevron-left-rounded"></iconify-icon>
      <h2 class="text-center">Find Friends</h2>
                <!-- Search Bar -->
  <div class="flex items-center bg-blue-200/70 rounded-full text-slate-200 px-2 py-1 gap-1">
     <div class="text-2xl pt-1">
      <iconify-icon icon="material-symbols:search-rounded"></iconify-icon>
   <input type="text" name="search" id="search" placeholder="Enter a friend's name" class="focus:rings-0 border-0 focus:border-0 focus:outline-none text-sm bg-transparent placeholder:text-slate-100 pl-1">
                <!-- Profile Container -->
<div id="root" class="py-4 px-2 bg-blue-50"></div>

<script src=""></script>
<script src="main.js"></script>
Enter fullscreen mode Exit fullscreen mode

No major styling properties applied. We principally use flex box flex, padding-inline px padding-block py and border-radius rounded-full properties to build most of the visible component

Our focus here is on the Profile container with id="root". This is the identifier we will be using as a reference to add different profiles from the JavaScript file, so it is important to remember it.

Now, let focus on a single profile. The following code is the template code we will be using for all the different profiles.

It consist of

  • a Profile image which accepts profileImage as paramenter

  • Profile details, which accepts userName as paramenter

  • Follow button: This is the button when clicked, you now follow the user whose profile was followed. Of course you can unfollow if you change your mind

<div class="flex items-center  gap-2">
  <div class="w-14 h-14 rounded-full p-1 hover:scale-105 bg-blue-200/80 overflow-hidden">

    <img src= {profileImage} alt="" class="rounded-full w-full">

   <div class="font-semibold text-blue-600 mr-auto">

<button class="px-4 py-2 bg-blue-700 text-slate-100 rounded-full text-sm font-semibold hover:bg-white border border-blue-700 hover:text-blue-700 shadow-md shadow-blue-700">follow</button>

Enter fullscreen mode Exit fullscreen mode

This should be all for the HTML and TailwindCSS part of the task.

Component without profiles


As we earlier, Our Javascript file contains our profiles data, stored in an array of objects, as each user is indeed an object on programming language. And also, it contains the code to make our follow/unfollow button functional.

For our project, this is how our javascript will looks like

const root = document.getElementById("root")

//Local user Data set. Array of Objects
const userData = [
        userName: "Mbianou Bradon",
        profileImage : ",f_auto,fl_progressive,h_320,q_auto,w_320/"
        userName: "Kimboh Lovette",
        profileImage : ""
       userName: "Florin Pop",
        profileImage : ""
        userName: "Adrian Lica",
        profileImage : ""


//Rendering the user Profiles to the DOM using template

root.innerHTML = => {
    return (
        `<div class="flex items-center gap-2 mb-5">
            <div class="w-14 h-14 rounded-full p-1 hover:scale-105 bg-blue-200/80 overflow-hidden">
                <img src=${friend.profileImage} alt="" class="rounded-full w-full">
            <div class="font-semibold text-blue-600 mr-auto"><h2>${friend.userName}</h2></div>
            <button class="btn follow_friend px-4 py-2 bg-blue-700 text-slate-100 rounded-full text-sm font-semibold hover:bg-white border border-blue-700 hover:text-blue-700 shadow-md shadow-blue-700">follow</button>


// Making Follow/unfollow button funtional

window.addEventListener("click", (e) => {
    if ("btn")) {"follow_friend")
            ? ( = "unfollow")
            : ( = "follow");

Enter fullscreen mode Exit fullscreen mode

As you can see, we divide our javascript code into 3 parts. The array of objects users, the section we are rendering this information through our template and the last past is how we make the button functional.

Take note we rendered our user profiles using an array method map(), and we accessed the DOM using the id="root", earlier defined in the HTML file.

That's pretty much it.

End Result


We just built a very beautiful, responsive and functional Followers Component.

We had the opportunity to also revise our knowledge of Vanilla Javascript and apply the power of one of the array method, map() to loop through our dataset and render them to the browser.

I hope like me you enjoyed building this wonderful component.

You can have a Live preview on Codepen or find the code on GitHub

Donโ€™t hesitate to share with me if you were able to complete the tutorial on your end, Iโ€™d be happy to see any additions styling you added to your Pop-up.

If you have any worries or suggestions, donโ€™t hesitate to leave them in the comment section! ๐Ÿ˜Š

See ya! ๐Ÿ‘‹

Top comments (0)