Certainly! Below is an example of a Modal component written in React 19 (React 18+ syntax is compatible with React 19, so this will be standard React 18+ code), designed to be used in a Laravel 12 app with Inertia 2 and Tailwind CSS 4.
Key points:
- React 19 is not officially released yet, but React 18+ syntax is forward-compatible.
- Inertia 2 is the latest major version of Inertia.js.
- Tailwind CSS 4 is hypothetical (latest stable is v3.x as of 2024), but the syntax remains similar.
- The modal supports open/close state, accessibility, and basic styling.
Modal.jsx
import React, { useEffect } from 'react';
import { createPortal } from 'react-dom';
export default function Modal({ isOpen, onClose, title, children }) {
// Prevent scrolling when modal is open
useEffect(() => {
if (isOpen) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = '';
}
return () => {
document.body.style.overflow = '';
};
}, [isOpen]);
// Close modal on ESC key press
useEffect(() => {
function onKeyDown(e) {
if (e.key === 'Escape' && isOpen) {
onClose();
}
}
window.addEventListener('keydown', onKeyDown);
return () => window.removeEventListener('keydown', onKeyDown);
}, [isOpen, onClose]);
if (!isOpen) return null;
return createPortal(
<div
className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"
aria-modal="true"
role="dialog"
aria-labelledby="modal-title"
onClick={onClose} // close when clicking outside modal content
>
<div
className="bg-white rounded-lg shadow-lg max-w-lg w-full p-6 relative"
onClick={(e) => e.stopPropagation()} // prevent closing when clicking inside modal
>
<header className="flex justify-between items-center mb-4">
<h2 id="modal-title" className="text-xl font-semibold">
{title}
</h2>
<button
onClick={onClose}
aria-label="Close modal"
className="text-gray-500 hover:text-gray-700 focus:outline-none focus:ring-2 focus:ring-indigo-500 rounded"
>
<svg
xmlns="http://www.w3.org/2000/svg"
className="h-6 w-6"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
strokeWidth={2}
>
<path strokeLinecap="round" strokeLinejoin="round" d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</header>
<div>{children}</div>
</div>
</div>,
document.body
);
}
Usage example in an Inertia page component
import React, { useState } from 'react';
import Modal from '@/Components/Modal'; // Adjust path as needed
export default function ExamplePage() {
const [modalOpen, setModalOpen] = useState(false);
return (
<div className="p-6">
<button
onClick={() => setModalOpen(true)}
className="px-4 py-2 bg-indigo-600 text-white rounded hover:bg-indigo-700"
>
Open Modal
</button>
<Modal
isOpen={modalOpen}
onClose={() => setModalOpen(false)}
title="Example Modal"
>
<p>This is the modal content. You can put any React nodes here.</p>
</Modal>
</div>
);
}
Explanation:
- The modal uses React portals (
createPortal) to render outside the main DOM hierarchy.
- Clicking outside the modal content or pressing ESC closes the modal.
- The modal disables background scrolling when open.
- Tailwind classes provide styling and responsive design.
- Accessible roles and labels are included.
If you want me to generate a Laravel + Inertia controller or route example to complement this, just ask!