Create a Reusable Modal in React: Responsive, TypeScript-Based & Tailwind Styled

Would you be interested in setting up your modals in a React + Next.js project?
In this post, Iβll walk you through a fully-featured and reusable Modal component that:
1- Supports multiple entry/exit animation alignments,
2- Handles back navigation when closed,
3- Works across small and large devices selectively,
4- Uses React Portals to render outside the component tree, and
5- Is styled using Tailwind CSS for rapid development.
No more rewriting modals from scratch every time! Letβs build one you can plug into any project. π
β
Why Use This Modal Component?
This component is modular, responsive, animated, and portable β making it ideal for use in real-world web apps and dashboards.
Use cases:
1- Displaying forms, filters, or dynamic content without page reloads.
2- Creating full-screen mobile modals.
3- Conditionally showing modals on different screen sizes.
4- Enhancing UX with entry/exit animations.
import React, { useState, useEffect, Dispatch, SetStateAction } from "react";
import { createPortal } from "react-dom";
import { useRouter } from "next/navigation";
const Modal = ({
show,
setShow,
children,
alignment,
className,
isIntercepting = false,
showCancelBtnINSmallDevice = false,
isOnlySmallDevice = false,
isOnlyLargeDevice = false,
}: {
show: boolean;
setShow: Dispatch<SetStateAction<boolean>>;
children: React.ReactNode;
alignment: "left" | "center" | "right" | "top" | "bottom";
className?: string;
isIntercepting?: boolean;
showCancelBtnINSmallDevice?: boolean;
isOnlySmallDevice?: boolean;
isOnlyLargeDevice?: boolean;
}) => {
const [animate, setAnimate] = useState(false);
const redirect = useRouter();
let appearAnimation;
let disappearAnimation;
if (alignment === "left") {
appearAnimation = "translate-x-0";
disappearAnimation = "-translate-x-1/2";
} else if (alignment === "center") {
appearAnimation = "scale-1";
disappearAnimation = "scale-0";
} else if (alignment === "right") {
appearAnimation = "translate-x-0";
disappearAnimation = "translate-x-1/2";
} else if (alignment === "top") {
appearAnimation = "translate-y-[-227px]";
disappearAnimation = "-translate-y-[100%]";
} else if (alignment === "bottom") {
appearAnimation = "translate-y-[227px]";
disappearAnimation = "translate-y-[100%]";
}
useEffect(() => {
if (show) {
setAnimate(true);
} else {
setAnimate(false);
}
}, [show]);
const handleClose = (e: React.MouseEvent) => {
e.stopPropagation();
setAnimate(false);
if (isIntercepting) {
redirect.back();
}
setTimeout(() => setShow(false), 300);
};
return createPortal(
<div
className={`fixed inset-0 z-50 backdrop-blur-sm bg-black-transparent transition-opacity duration-300 ease-in-out flex items-center
${animate ? "opacity-100" : "opacity-0"}
${alignment === "right" && "justify-end"}
${alignment === "center" && "justify-center"}
${isOnlySmallDevice && "md:hidden"}
${isOnlyLargeDevice && "hidden md:flex"}`}
onClick={handleClose}
>
<div
className={` relative shadow-black-50 drop-shadow-2xl bg-white lg:p-5 duration-300 ease-in-out
${alignment !== "center" && "h-full md:h-[calc(100%-16px)] md:m-2"}
${animate ? appearAnimation : disappearAnimation} ${className}`}
onClick={(e) => e.stopPropagation()}
>
{/* close handler */}
<button
className={`hover:rotate-90 transition-all duration-200 absolute top-5 right-5 lg:top-6 lg:right-6 text-black hover:text-[#ff4b4b] font-bold z-50 ${
showCancelBtnINSmallDevice ? "block" : "hidden"
}`}
onClick={handleClose}
>
✕
</button>
{/* children */}
{children}
</div>
</div>,
document.body
);
};
export default Modal;
π― Key Features:
β
Fully reusable with props for control
β
Device-based rendering (show only on mobile or desktop)
β
Multiple alignment-based animations (left, center, right, top, bottom)
β
Portal rendering with createPortal for proper stacking
β
Interceptable close behavior (router.back() support)
β
Tailwind CSS-based transitions & styling
β
Clean separation of logic and UI control
π‘ How It Works (in short):
1- show: Boolean toggle for modal visibility.
2- alignment: Determines the animation direction (like slide in from left, right, or scale in center).
3- isIntercepting: Useful for routing scenarios β goes back in history on close.
4- isOnlySmallDevice, isOnlyLargeDevice: Control modal visibility by device size.
5- createPortal: Ensures the modal is placed outside the component tree (document.body).
β
Pros:
π¨ Highly customizable
π± Responsive: Tailored for mobile and desktop experiences
π Lightweight and performant
π§© Easy to integrate in any layout
π Reusable across routes and pages
π§ Intuitive API for developers
π§ͺ Example Usage:
const [showModal, setShowModal] = useState(false);
<Modal
show={showModal}
setShow={setShowModal}
alignment="right"
isIntercepting={true}
showCancelBtnINSmallDevice={true}
>
<div>Your modal content here...</div>
</Modal>
π Conclusion
This reusable modal in React demonstrates the power of combining Tailwind CSS, TypeScript, and responsiveness. Whether youβre looking for a React modal example, building a responsive modal in React, or creating clean, production-ready React modal components, this approach gives you complete control and reusability. For more advanced use cases, you can extend it with context, animations or integrate it with global state. Keep this React modal documentation handy β your UI just got a whole lot smoother.
Subscribe to my newsletter
Read articles from PO RA N directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

PO RA N
PO RA N
Frontend Developer | Programming Enthusiast | Lifelong Learner Passionate frontend developer with 2 years of experience in React.js and Next.js. Always eager to learn and tackle new coding challenges.