|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* |
| 4 | +How to use this script |
| 5 | +
|
| 6 | +First, create a modal like this |
| 7 | +<div class="modal" id="my-unique-modal-identifier"> |
| 8 | + <div class="modal-background"></div> |
| 9 | + <div class="modal-content"> |
| 10 | + <!-- Any other Bulma elements you want --> |
| 11 | + </div> |
| 12 | + <button class="modal-close is-large" aria-label="close"></button> |
| 13 | +</div> |
| 14 | +
|
| 15 | +The div class has a unique ID to identify this particular modal. |
| 16 | +
|
| 17 | +Next, you create a button (or some other element that you can interact with) like this: |
| 18 | +<button class="button modal-button" data-target="my-unique-modal-identifier"> |
| 19 | + Open the modal! |
| 20 | +</button> |
| 21 | +
|
| 22 | +See how the `data-target` matches the id of the modal we want to open? This is important. |
| 23 | +
|
| 24 | +Once this is set up and this script is loaded, the button should open the modal! |
| 25 | +*/ |
| 26 | + |
| 27 | +document.addEventListener('DOMContentLoaded', function () { |
| 28 | + var rootEl = document.documentElement; |
| 29 | + var $modals = _getAll('.modal'); |
| 30 | + var $modalButtons = _getAll('.modal-button'); |
| 31 | + var $modalCloses = _getAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button'); |
| 32 | + |
| 33 | + // Add click event listeners to all the modal buttons, |
| 34 | + // which are buttons with the .modal-button class. |
| 35 | + if ($modalButtons.length > 0) { |
| 36 | + $modalButtons.forEach(function ($el) { |
| 37 | + $el.addEventListener('click', function () { |
| 38 | + var target = $el.dataset.target; |
| 39 | + var $target = document.getElementById(target); |
| 40 | + rootEl.classList.add('is-clipped'); |
| 41 | + $target.classList.add('is-active'); |
| 42 | + }); |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + // Add click event listeners to all close icons, like an X |
| 47 | + // icon in the top right corner of the modal window. |
| 48 | + if ($modalCloses.length > 0) { |
| 49 | + $modalCloses.forEach(function ($el) { |
| 50 | + $el.addEventListener('click', function () { |
| 51 | + closeModals(); |
| 52 | + }); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + // Close the modal if the user hits the escape key |
| 57 | + document.addEventListener('keydown', function (event) { |
| 58 | + var e = event || window.event; |
| 59 | + if (e.keyCode === 27) { |
| 60 | + closeModals(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + // Model closing logics |
| 65 | + function closeModals() { |
| 66 | + rootEl.classList.remove('is-clipped'); |
| 67 | + $modals.forEach(function ($el) { |
| 68 | + $el.classList.remove('is-active'); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + // Helper function to fetch all elements with certain classes. |
| 73 | + function _getAll(selector) { |
| 74 | + return Array.prototype.slice.call(document.querySelectorAll(selector), 0); |
| 75 | + } |
| 76 | +}); |
0 commit comments