Skip to content
Prev Previous commit
Next Next commit
feat: implement modal with open/close functionality and overlay
  • Loading branch information
vmanidev committed Sep 25, 2025
commit 43ce057e7dd85f8fb83b37ce59c7d59f8d7db3da
1 change: 1 addition & 0 deletions useReducer/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</head>
<body>
<div id="root"></div>
<div id="modal"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>
3 changes: 3 additions & 0 deletions useReducer/src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useReducer, useState } from "react";
import "./index.css";
import Modal from "./ui-components/Modal";

const todoReducer = (state, action) => {
const { type, payload } = action;
Expand Down Expand Up @@ -35,6 +36,7 @@ export default function App() {
title: "",
isDone: false,
});
const [closeModal, setCloseModal] = useState(false);

const addTodo = (payload) => {
dispatch({ type: "add", payload });
Expand Down Expand Up @@ -87,6 +89,7 @@ export default function App() {
</li>
))}
</ul>
{closeModal && <Modal onCloseModal={setCloseModal(true)}></Modal>}
</div>
);
}
21 changes: 21 additions & 0 deletions useReducer/src/ui-components/Modal.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { createPortal } from "react-dom";

export default function Modal({ children, onCloseModal }) {
const modal = document.getElementById("modal");

return createPortal(
<div className="fixed inset-0 flex items-center justify-center bg-black/50">
<div className="bg-white p-6 rounded-lg shadow-lg relative max-w-md w-full">
{children}

<button
onClick={onCloseModal}
className="absolute top-2 right-2 text-gray-500 hover:text-gray-800"
>
</button>
</div>
</div>,
modal
);
}