<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Felix Leung</title>
    <description>The latest articles on DEV Community by Felix Leung (@felixleung0307).</description>
    <link>https://dev.to/felixleung0307</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1279279%2Fcd637cb8-9106-4209-9012-c365214bafaf.jpg</url>
      <title>DEV Community: Felix Leung</title>
      <link>https://dev.to/felixleung0307</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/felixleung0307"/>
    <language>en</language>
    <item>
      <title>How to create a Popover using Tailwind CSS.</title>
      <dc:creator>Felix Leung</dc:creator>
      <pubDate>Mon, 12 Feb 2024 18:48:15 +0000</pubDate>
      <link>https://dev.to/felixleung0307/how-to-create-a-popover-using-tailwind-css-3g4b</link>
      <guid>https://dev.to/felixleung0307/how-to-create-a-popover-using-tailwind-css-3g4b</guid>
      <description>&lt;p&gt;&lt;code&gt;Popover&lt;/code&gt; is a common UI element in web applications, providing a way to display additional information or options when interacting with a particular element. With &lt;code&gt;React&lt;/code&gt; and &lt;code&gt;Tailwind CSS&lt;/code&gt;, most of the time developers use an npm library for the Popover or Popover. You know, when we use an npm library, it increases the project build sizes.&lt;br&gt;
In this article, I will create a reusable &lt;code&gt;Popover&lt;/code&gt; component using &lt;code&gt;Tailwind CSS&lt;/code&gt;. We will use click and hover triggers for the Popover.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;Popover&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// @flow strict
"use client"
import { useEffect, useRef, useState } from "react";

function ReactPopover({
  children,
  content,
  trigger = "click"
}) {
  const [show, setShow] = useState(false);
  const wrapperRef = useRef(null);

  const handleMouseOver = () =&amp;gt; {
    if (trigger === "hover") {
      setShow(true);
    };
  };

  const handleMouseLeft = () =&amp;gt; {
    if (trigger === "hover") {
      setShow(false);
    };
  };

  useEffect(() =&amp;gt; {
    function handleClickOutside(event) {
      if (wrapperRef.current &amp;amp;&amp;amp; !wrapperRef.current.contains(event.target)) {
        setShow(false);
      }
    }

    if (show) {
      // Bind the event listener
      document.addEventListener("mousedown", handleClickOutside);
      return () =&amp;gt; {
        // Unbind the event listener on clean up
        document.removeEventListener("mousedown", handleClickOutside);
      };
    }
  }, [show, wrapperRef]);

  return (
    &amp;lt;div
      ref={wrapperRef}
      onMouseEnter={handleMouseOver}
      onMouseLeave={handleMouseLeft}
      className="w-fit h-fit relative flex justify-center"&amp;gt;
      &amp;lt;div
        onClick={() =&amp;gt; setShow(!show)}
      &amp;gt;
        {children}
      &amp;lt;/div&amp;gt;
      &amp;lt;div
        hidden={!show}
        className="min-w-fit w-[200px] h-fit absolute bottom-[100%] z-50 transition-all"&amp;gt;
        &amp;lt;div className="rounded bg-white p-3 shadow-[10px_30px_150px_rgba(46,38,92,0.25)] mb-[10px]"&amp;gt;
          {content}
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default ReactPopover;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this component the trigger default value is click and you can pass hover as an attribute. When you click outside of the Popover, the Popover will be closed.&lt;/p&gt;

&lt;p&gt;Use the Popover component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactPopover from "@/components/common/react-popover";

const Page = () =&amp;gt; {
  return (
    &amp;lt;div className="w-screen h-screen flex justify-center items-center gap-4"&amp;gt;
      &amp;lt;ReactPopover
        content={
          &amp;lt;p&amp;gt;This Content Will be render in Popover.&amp;lt;/p&amp;gt;
        }
      &amp;gt;
        &amp;lt;button className="bg-indigo-500 px-4 py-1.5 border rounded text-white"&amp;gt;
          Click me
        &amp;lt;/button&amp;gt;
      &amp;lt;/ReactPopover&amp;gt;
      &amp;lt;ReactPopover
        trigger="hover"
        content={
          &amp;lt;p&amp;gt;This Content Will be render in Popover.&amp;lt;/p&amp;gt;
        }
      &amp;gt;
        &amp;lt;button className="bg-indigo-500 px-4 py-1.5 border rounded text-white"&amp;gt;
          Hover me
        &amp;lt;/button&amp;gt;
      &amp;lt;/ReactPopover&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Page;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
