Created
February 15, 2024 20:29
-
-
Save rileyrichter/e2b2d6c26fcf0f0a6ef2349d26f08bb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this | |
software and associated documentation files (the "Software"), to deal in the Software | |
without restriction, including without limitation the rights to use, copy, modify, merge, | |
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons | |
to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or | |
substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING | |
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
*/ | |
// Global variables | |
const dialog = document.querySelector("dialog"); | |
const showButton = document.querySelector("dialog + button"); | |
const closeButton = document.querySelector("dialog button"); | |
// "Show the dialog" button opens the dialog modally | |
showButton.addEventListener("click", () => { | |
dialog.showModal(); | |
}); | |
// "Close" button closes the dialog | |
closeButton.addEventListener("click", () => { | |
dialog.close(); | |
}); | |
// Close the dialog when clicking outside of it | |
dialog.addEventListener("click", (e) => { | |
const dialogDimensions = dialog.getBoundingClientRect(); | |
if ( | |
e.clientX < dialogDimensions.left || | |
e.clientX > dialogDimensions.right || | |
e.clientY < dialogDimensions.top || | |
e.clientY > dialogDimensions.bottom | |
) { | |
dialog.close(); | |
} | |
}); | |
// When the DOM is ready | |
document.addEventListener("DOMContentLoaded", () => { | |
// Check if the modal has been shown before | |
let modalShown = localStorage.getItem("modalShown") === "true"; | |
// Show the modal after 15 seconds if | |
// the modal hasn't been shown before | |
const showModal = () => { | |
if (!modalShown) { | |
localStorage.setItem("modalShown", true); | |
modalShown = true; | |
dialog.showModal(); | |
console.log("Modal shown"); | |
} | |
}; | |
setTimeout(showModal, 15000); | |
// Show the modal if the user leaves | |
// the page and hasn't seen the modal before | |
document.addEventListener("mouseleave", (event) => { | |
if (event.clientY <= 0) { | |
showModal(); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment