<?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: Satish kumar pradhan</title>
    <description>The latest articles on DEV Community by Satish kumar pradhan (@satish313).</description>
    <link>https://dev.to/satish313</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%2F932787%2F7e8c9e94-4bc6-4707-8c85-12121f3e3c81.jpeg</url>
      <title>DEV Community: Satish kumar pradhan</title>
      <link>https://dev.to/satish313</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/satish313"/>
    <language>en</language>
    <item>
      <title>How to create fully animated React modal using tailwind and custom animation</title>
      <dc:creator>Satish kumar pradhan</dc:creator>
      <pubDate>Wed, 23 Nov 2022 10:56:22 +0000</pubDate>
      <link>https://dev.to/satish313/how-to-create-fully-animated-react-modal-using-tailwind-and-custom-animation-2bn7</link>
      <guid>https://dev.to/satish313/how-to-create-fully-animated-react-modal-using-tailwind-and-custom-animation-2bn7</guid>
      <description>&lt;p&gt;As web developer, learning custom modal with animation is good thing. Here I will explain how i created react modal with tailwindcss and nextjs. I usually used react-bootstrap for UI development, there react-modal is awesome. Here I don't want to install another UI building like React-bootstrap, I have already using tailwindcss.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create modal using tailwind in nextjs or react
&lt;/h2&gt;

&lt;p&gt;Before anything, what I am using for here, NextJs with TypeScript and Tailwindcss no other dependency. If want to check in github &lt;a href="https://github.com/satish-313/react-modal-nextj-tailwind/blob/main/src/pages/index.tsx"&gt;click here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup or installation
&lt;/h2&gt;

&lt;p&gt;for nextjs we can create npx create-next-app name or any other cli tool like CRA or npm vite&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npx create-next-app react-modal --ts&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;for tailwind &lt;a href="https://tailwindcss.com/docs/guides/nextjs"&gt;check here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;I am using all single file to write all style and component instead of creating different file and directory. First clear all the TSX or JSX in App file. I have Head and title in main file, it doesn't matter for modal. &lt;/p&gt;

&lt;p&gt;For modal show or not show I am using the useState from react and default is false, using another useState for list of skill or any strings of array for displaying when i add another skill.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const [showModal, setShowModal] = useState(false);
  const [skills, setSkills] = useState&amp;lt;string[(["JavaScript",
    "TypeScript","NodeJs","Python","HTML","CSS",]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For above state to change, I created the two helper function for changing showModal state and adding new skill in skills state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const modalToggle = () =&amp;gt; {
    setShowModal(!showModal);
  };
  const addSkill = (s: string) =&amp;gt; {
    setSkills([...skills, s]);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Home page
&lt;/h2&gt;

&lt;p&gt;I have created the button with different tailwind style for onClick event trigger modalToggle to change.&lt;/p&gt;

&lt;p&gt;Used the ternary or conditional operator to show the ReactModal component if true show or null, props drill the two helper function to ReactModal component to access. And li for showing all skill of skills state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   &amp;lt;div&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;next modal tailwind&amp;lt;/title&amp;gt;
        &amp;lt;meta name="description" content="React modal with tailwindcss" /&amp;gt;
        &amp;lt;link rel="icon" href="/favicon.ico" /&amp;gt;
      &amp;lt;/Head&amp;gt;

      &amp;lt;div className="flex justify-center p-2"&amp;gt;
        &amp;lt;button
          onClick={modalToggle}
          className="mt-2 border px-5 py-1.5 bg-blue-400 rounded-lg font-semibold text-white hover:bg-blue-700"
        &amp;gt;
          Add List
        &amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
      {showModal ? (
        &amp;lt;ReactModal toggle={modalToggle} addSkill={addSkill} /&amp;gt;
      ) : null}
      &amp;lt;h3 className="text-2xl font-semibold underline text-gray-700"&amp;gt;
        My skills :{" "}
      &amp;lt;/h3&amp;gt;

      &amp;lt;div className="py-1 5" /&amp;gt;
      &amp;lt;ul className="list-disc ml-10"&amp;gt;
        {skills.map((skill, idx) =&amp;gt; (
          &amp;lt;li key={idx}&amp;gt;{skill}&amp;lt;/li&amp;gt;
        ))}
      &amp;lt;/ul&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  modal feature ?
&lt;/h2&gt;

&lt;p&gt;The modal working is similar to popup. When clicking the button it show the modal. In modal it can form or Oauth, by submitting it close the modal or click close button it can be X or close. Another feature by clicking another then modal it will close the modal. &lt;/p&gt;

&lt;h2&gt;
  
  
  How modal work ?
&lt;/h2&gt;

&lt;p&gt;Most of modal or popup work on the z-index feature of css that allow us to 3-dimension of web page. For z-index to work it have to position property of css like absolute or relative. If z-index on below html can't access like over don't work.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to build modal with React or tailwindcss
&lt;/h2&gt;

&lt;p&gt;For me, I used the ReactModal component with some position absolute and top-0,button-0,left-0,right-0 in tailwind it is inset-0.&lt;/p&gt;

&lt;p&gt;First create a component, I used React.FC type to functional component.It show the error if proper tsx don't return. In typescript we have to pass prop generic to access the property in props that passed in ReactModal component above. destructure the props toggle and addSkill feature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ReactModal: FC&amp;lt;{ toggle: () =&amp;gt; void; addSkill: (s: string) =&amp;gt; void }&amp;gt; = ({
  toggle,
  addSkill,
}) =&amp;gt; { 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;State for modal section *&lt;/em&gt;&lt;br&gt;
Here used three hook for state management. two useRef for background of modal and input field for skill add. another one for closing animation. If toogle false it will immediately close the modal for smooth transition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const modalRef = useRef(null);
  const inputRef = useRef&amp;lt;HTMLInputElement&amp;gt;(null);
  const [close, setClose] = useState(false); // closing animation
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Animation in modal&lt;/strong&gt;&lt;br&gt;
Tailwind animation are limited not great for all situation. To smooth animation i used the css instead of make custom tailwind.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;for starting animation used delay 0s in animation otherwise it will render immediately and show animation.&lt;/li&gt;
&lt;li&gt;for closing animation, animation time more then delay closing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me closing animation is tricky, therefore i used another state for showing closing animation, I used promise to delay 1s in that time i used delay animation to show. Before the toggle, change the close state to true to render closing animation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.modal-body-animation {
  animation: updown 800ms ease-in-out 0s 1;
}

.modal-body-animation-close {
  animation: downup 1.1s ease-in-out 0s 1;
}

@keyframes updown {
  0% {
    transform: translateY(-100%);
  }
  100% {
    transform: translateY(0%);
  }
}

@keyframes downup {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(-200%);
  }
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closing when click in background and form submit
&lt;/h2&gt;

&lt;p&gt;As below code display, when mouseEvent trigger it check whether it on background, we can access via useRef hook to find out event. similar to submit event and trigger close.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  const closeHelper = async () =&amp;gt; {
    setClose(true);
    await new Promise((r) =&amp;gt; setTimeout(r, 1000));
    toggle();
  };

  const closeModalBackground = (e: MouseEvent) =&amp;gt; {
    if (modalRef.current === e.target) {
      closeHelper();
    }
  };

  const Submit = (e: FormEvent) =&amp;gt; {
    e.preventDefault();
    const skill = inputRef.current?.value;
    if (skill !== undefined &amp;amp;&amp;amp; skill?.trim() !== "") {
      addSkill(skill!);
    }
    closeHelper();
  };

return (
    &amp;lt;div
      ref={modalRef}
      onClick={(e) =&amp;gt; closeModalBackground(e)}
      className="absolute inset-0 z-10 flex flex-col items-center"
    &amp;gt;
      &amp;lt;div className="h-1/5" /&amp;gt;
      &amp;lt;div
        className={`bg-indigo-700 sm:w-1/2 h-40 p-3 rounded-lg relative modal-body-animation ${
          close ? "modal-body-animation-close" : ""
        }`}
      &amp;gt;
        &amp;lt;button
          onClick={closeHelper}
          className="absolute top-0 right-0 text-xl font-semibold text-red-500 px-3"
        &amp;gt;
          X
        &amp;lt;/button&amp;gt;
        &amp;lt;h3 className="text-xl font-semibold text-white text-center"&amp;gt;
          Add your new skill
        &amp;lt;/h3&amp;gt;
        &amp;lt;div className="py-1 5" /&amp;gt;
        &amp;lt;form onSubmit={(e) =&amp;gt; Submit(e)}&amp;gt;
          &amp;lt;input
            className="block w-1/2 mx-auto px-2 py-1.5 rounded outline-none"
            type="text"
            placeholder="add skill"
            ref={inputRef}
          /&amp;gt;
          &amp;lt;button className="block mx-auto mt-2 py-1.5 px-3 rounded text-white font-semibold border hover:bg-indigo-400"&amp;gt;
            Add skill
          &amp;lt;/button&amp;gt;
        &amp;lt;/form&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope you enjoy the blog&lt;/p&gt;

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