Last active
April 5, 2021 14:21
-
-
Save IlanVivanco/694a19541ba58590e149df9cdccef4aa to your computer and use it in GitHub Desktop.
Basic Javascript example for Bulma modals.
This file contains hidden or 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
class BulmaModal { | |
constructor(modalButton) { | |
const target = modalButton.dataset.target; | |
if (target) { | |
this.button = modalButton; | |
this.modal = document.querySelector(target); | |
this.html = document.querySelector("html"); | |
this.openEvent(); | |
this.closeEvents(); | |
} | |
} | |
show() { | |
this.modal.classList.toggle("is-active"); | |
this.html.classList.add("is-clipped"); | |
this.onShow(); | |
} | |
close() { | |
this.modal.classList.toggle("is-active"); | |
this.html.classList.toggle("is-clipped"); | |
this.onClose(); | |
} | |
openEvent() { | |
this.button.addEventListener("click", (e) => { | |
e.preventDefault(); | |
this.show(); | |
}); | |
} | |
closeEvents() { | |
const closeElements = this.modal.querySelectorAll(".modal-background, .modal-close"); | |
closeElements.forEach((element) => { | |
element.addEventListener("click", (e) => { | |
e.preventDefault(); | |
this.close(); | |
}); | |
}); | |
} | |
onShow() { | |
const event = new Event("modal:show"); | |
this.modal.dispatchEvent(event); | |
} | |
onClose() { | |
const event = new Event("modal:close"); | |
this.modal.dispatchEvent(event); | |
} | |
} | |
export default BulmaModal; |
This file contains hidden or 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
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css"> | |
<button class="modal-button" data-toggle="modal" data-target="#video">Open modal</button> | |
<div class="modal" id="video"> | |
<div class="modal-background"></div> | |
<div class="modal-content"> | |
Lorem ipsum dolor sit amet | |
</div> | |
<button class="modal-close is-large" aria-label="close"></button> | |
</div> | |
<script src="./main.js" type="module"></script> |
This file contains hidden or 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
import BulmaModal from './BulmaModal.js' | |
const modals = document.querySelectorAll("[data-toggle='modal']"); | |
modals.forEach((modal) => new BulmaModal(modal)); | |
/** show events */ | |
document.querySelector(".modal").addEventListener("modal:show", (event) =>{ console.log(event) }); | |
/** show events */ | |
document.querySelector(".modal").addEventListener("modal:close", (event) =>{ console.log(event) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment