Modal Component for React 19 with Laravel 12, Inertia 2, and Tailwind 4
Here's a comprehensive modal component that works with the latest versions of these technologies:
import { useEffect, useRef } from 'react';
import { createPortal } from 'react-dom';
interface ModalProps {
show: boolean;
onClose: () => void;
children: React.ReactNode;
maxWidth?: 'sm' | 'md' | 'lg' | 'xl' | '2xl' | 'full';
closeable?: boolean;
}
export default function Modal({
show,
onClose,
children,
maxWidth = '2xl',
closeable = true,
}: ModalProps) {
const modalRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (e.key === 'Escape' && show && closeable) {
onClose();
}
};
const handleOutsideClick = (e: MouseEvent) => {
if (modalRef.current && !modalRef.current.contains(e.target as Node) && show && closeable) {
onClose();
}
};
if (show) {
document.addEventListener('keydown', handleKeyDown);
document.addEventListener('mousedown', handleOutsideClick);
document.body.style.overflow = 'hidden';
}
return () => {
document.removeEventListener('keydown', handleKeyDown);
document.removeEventListener('mousedown', handleOutsideClick);
document.body.style.overflow = 'auto';
};
}, [show, closeable, onClose]);
if (!show) return null;
const maxWidthClasses = {
sm: 'max-w-sm',
md: 'max-w-md',
lg: 'max-w-lg',
xl: 'max-w-xl',
'2xl': 'max-w-2xl',
full: 'max-w-full',
};
return createPortal(
<div className="fixed inset-0 z-50 overflow-y-auto">
<div className="fixed inset-0 bg-black/50 backdrop-blur-sm transition-opacity" />
<div className="flex min-h-full items-center justify-center p-4 text-center">
<div
ref={modalRef}
className={`w-full ${maxWidthClasses[maxWidth]} relative transform overflow-hidden rounded-lg bg-white px-4 pb-4 pt-5 text-left shadow-xl transition-all dark:bg-gray-800`}
>
{closeable && (
<button
type="button"
onClick={onClose}
className="absolute right-4 top-4 rounded-md p-1 text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:text-gray-500 dark:hover:text-gray-400"
>
<span className="sr-only">Close</span>
<svg
className="h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path
fillRule="evenodd"
d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z"
clipRule="evenodd"
/>
</svg>
</button>
)}
{children}
</div>
</div>
</div>,
document.body
);
}
Usage Example
Here's how you would use this modal component in your application:
import { useState } from 'react';
import Modal from '@/Components/Modal';
export default function ExamplePage() {
const [isOpen, setIsOpen] = useState(false);
return (
<div>
<button
onClick={() => setIsOpen(true)}
className="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700"
>
Open Modal
</button>
<Modal show={isOpen} onClose={() => setIsOpen(false)}>
<div className="p-6">
<h2 className="text-xl font-bold text-gray-900 dark:text-white">
Modal Title
</h2>
<p className="mt-4 text-gray-600 dark:text-gray-300">
This is your modal content. You can put anything here.
</p>
<div className="mt-6 flex justify-end space-x-3">
<button
type="button"
onClick={() => setIsOpen(false)}
className="rounded-md bg-gray-200 px-4 py-2 text-gray-700 hover:bg-gray-300 dark:bg-gray-700 dark:text-gray-200 dark:hover:bg-gray-600"
>
Cancel
</button>
<button
type="button"
className="rounded-md bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700"
>
Save
</button>
</div>
</div>
</Modal>
</div>
);
}
Features
- Accessibility: Includes proper focus management and keyboard navigation
- Dark Mode Support: Works with Tailwind's dark mode
- Customizable: Adjustable max-width sizes
- Portal Rendering: Uses React's createPortal to render outside the main DOM hierarchy
- Escape Key Handling: Closes when pressing Escape
- Outside Click Handling: Closes when clicking outside the modal
- Scroll Locking: Prevents background scrolling when modal is open
- TypeScript Support: Fully typed component
Installation Notes
Make sure you have the following dependencies installed:
npm install @types/react @types/react-dom
This component assumes you're using Tailwind CSS with the dark mode configured. If you're not using dark mode, you can remove the dark mode classes.