<?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>Forem: Mustafa Al Ajmi</title>
    <description>The latest articles on Forem by Mustafa Al Ajmi (@skrooob).</description>
    <link>https://forem.com/skrooob</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%2F1081782%2Ff1f46abb-0e80-4804-80c2-1a348e7cd359.jpg</url>
      <title>Forem: Mustafa Al Ajmi</title>
      <link>https://forem.com/skrooob</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/skrooob"/>
    <language>en</language>
    <item>
      <title>Easy React Modal with Tailwindcss 😎</title>
      <dc:creator>Mustafa Al Ajmi</dc:creator>
      <pubDate>Sat, 13 May 2023 08:26:57 +0000</pubDate>
      <link>https://forem.com/skrooob/easy-react-modal-with-tailwindcss-1g5g</link>
      <guid>https://forem.com/skrooob/easy-react-modal-with-tailwindcss-1g5g</guid>
      <description>&lt;p&gt;Modals are essential nowdays and i've seen alot of ways to create them, Today i wanted to create my first post here using only reactjs &amp;amp; Tailwindcss.&lt;/p&gt;

&lt;p&gt;Here is what we are going to build&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iyACUH1v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguxp507x12qdvb3b8u1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iyACUH1v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguxp507x12qdvb3b8u1.gif" alt="Modal preview" width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Let's go&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Create React Project&lt;/strong&gt;&lt;br&gt;
First, Create a simple react project using create-react-app cli&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx  Create-react-app  react-modal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import logo from './logo.svg';
import './App.css';

function App() {
  return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;header className="App-header"&amp;gt;
        &amp;lt;img src={logo} className="App-logo" alt="logo" /&amp;gt;
        &amp;lt;p&amp;gt;
          Edit &amp;lt;code&amp;gt;src/App.js&amp;lt;/code&amp;gt; and save to reload.
        &amp;lt;/p&amp;gt;
        &amp;lt;a
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        &amp;gt;
          Learn React
        &amp;lt;/a&amp;gt;
      &amp;lt;/header&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;Lets do some cleaning&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";

function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Hello world&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Lets add Tailwindcss&lt;/strong&gt;&lt;br&gt;
as it shows on &lt;a href="https://tailwindcss.com/docs/guides/create-react-app"&gt;Tailwindcss&lt;/a&gt; doc&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D tailwindcss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx tailwindcss init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Configure your template&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/** @type {import('tailwindcss').Config} */
module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
  ],
  theme: {
    extend: {},
  },
  plugins: [],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and Add Tailwind directives to your CSS&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;We are done lets start our app&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see everything works perfectly now 👍&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";

function App() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1 className="text-center text-2xl text-red-500"&amp;gt;Hello world&amp;lt;/h1&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;In this tutorial we will be needing two things button &amp;amp; the modal it self&lt;/p&gt;

&lt;p&gt;so lets create them this way&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ViYGN8yV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg8zqaa60f110yuv0lxa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ViYGN8yV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qg8zqaa60f110yuv0lxa.png" alt="Project dir" width="258" height="330"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import "./App.css";
import ReactModal from "./ReactModal";

function App() {
  return (
    &amp;lt;div className="flex flex-col justify-center items-center h-screen w-screen"&amp;gt;
      &amp;lt;ReactModal/&amp;gt;
      &amp;lt;button className="py-2 px-4 border rounded-md text-blue-100 bg-blue-400 hover:bg-blue-500 hover:text-white duration-75"&amp;gt;
        Click here
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;you can customize the button as you like 😋&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;now we add a useState to control the modal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [visible, setIsVisible] = useState(false);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;go to ReactModal component and create it&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

const ReactModal = ({ isOpen, onClose }) =&amp;gt; {
  if (!isOpen) return null;

  return (
    &amp;lt;div
      onClick={onClose}
      className="fixed z-50 bg-black bg-opacity-40 top-0 bottom-0 left-0 right-0 w-full h-full flex justify-center items-center"
    &amp;gt;
      &amp;lt;div
        onClick={(e) =&amp;gt; {
          e.stopPropagation();
        }}
        className="bg-white p-2 lg:p-5 rounded-xl shadow-md"
      &amp;gt;
        &amp;lt;div onClick={onClose} className="text-2xl cursor-pointer w-fit h-fit"&amp;gt;
          X
        &amp;lt;/div&amp;gt;
        &amp;lt;div className=" my-2"&amp;gt;This is my Awsome Modal 😎&amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;We pass isOpen &amp;amp; onClose to the parent so we use them on the useState to control the Modal visibility&lt;/p&gt;

&lt;p&gt;notice that i added &lt;em&gt;onClick&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;on the main div so you dismiss the modal when you click anywhere&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={onClose}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and &lt;em&gt;stopPropagation&lt;/em&gt; on the inner div so it wont dismiss when you click on the inner modal&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={(e) =&amp;gt; {e.stopPropagation();}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ofcourse you can change the ❌ to a fancy SVG icon i just wanted to make it look nifty using the emoji 😋&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Finally in the parent div *&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState } from "react";
import "./App.css";
import ReactModal from "./ReactModal";

function App() {
  const [visible, setIsVisible] = useState(false);

  return (
    &amp;lt;div className="flex flex-col justify-center items-center h-screen w-screen"&amp;gt;
      &amp;lt;ReactModal isOpen={visible} onClose={() =&amp;gt; setIsVisible(false)} /&amp;gt;
      &amp;lt;button
        onClick={() =&amp;gt; setIsVisible(true)}
        className="py-2 px-4 border rounded-md text-blue-100 bg-blue-400 hover:bg-blue-500 hover:text-white duration-75"
      &amp;gt;
        Click here
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;and now you have a customized fast easy and cool looking modal without using any packages... ofcourse you can do that without Tailwindcss too&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iyACUH1v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguxp507x12qdvb3b8u1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iyACUH1v--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dguxp507x12qdvb3b8u1.gif" alt="Modal preview" width="800" height="551"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you think other people should read this post and use this project, tweet, and share the post.&lt;/p&gt;

&lt;p&gt;Remember to follow me, so you can get notified about my future posts.&lt;/p&gt;

</description>
      <category>react</category>
      <category>tailwindcss</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
