<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <script src="https://kit.fontawesome.com/7744e10e83.js" crossorigin="anonymous"></script>
    <title>Document</title>
</head>

<body>
    <button id="show-offer-button">Show Offer</button>
    <div class="gray-overlay hidden"></div>
    <section class="modal hidden">
        <i class="fa-solid fa-square-xmark" id="x-button"></i>
        <p>Click the button below to accept our amazing offer!</p>
        <button id="accept-offer-button">Accept Offer</button>
    </section>
    <script src="app.js"></script>
</body>

</html>
html {
  position: relative;
}

body {
  height: 90vh;
  display: flex;
  justify-content: center;
  align-items: center;
  font-family: sans-serif;
}

#show-offer-button {
  padding: 1rem;
}

.gray-overlay {
  width: 100vw;
  height: 100vh;
  position: fixed;
  background-color: rgba(0, 0, 0, 0.8);
  left: 0;
  top: 0;
}

.modal {
  background-color: white;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  gap: 2rem;
  padding: 1.5rem 2rem;
  align-items: center;
  box-shadow: 12px 12px 2px 1px rgba(0, 0, 0, 0.2);
  z-index: 2;
}

#x-button {
  align-self: flex-start;
  cursor: pointer;
}

#x-button:hover {
  color: gray;
}

.modal p {
  width: 80%;
  text-align: center;
}

#accept-offer-button {
  padding: 0.5rem;
}

.hidden {
  display: none;
}
/*
* https://frontendeval.com/questions/modal-overlay
*
* Build a modal control and overlay
*/

const buttons = {
  showButton: document.querySelector("#show-offer-button"),
  xButton: document.querySelector("#x-button"),
  acceptButton: document.querySelector("#accept-offer-button"),
};
const grayOverlay = document.querySelector(".gray-overlay");
const modal = document.querySelector(".modal");

function toggleModal() {
  grayOverlay.classList.toggle("hidden");
  modal.classList.toggle("hidden");
}

grayOverlay.addEventListener("click", function () {
  toggleModal();
});

Object.values(buttons).forEach((button) => {
  button.addEventListener("click", () => {
    toggleModal();
  });
});

buttons.acceptButton.addEventListener("click", function () {
  buttons.showButton.classList.toggle("hidden");
  const message = document.createElement("div");
  message.innerText = "Offer Accepted";
  buttons.showButton.after(message);
});
View Compiled
Run Pen

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.