+{% endcapture %}
+
+{% capture modal_js_trigger %}
+
+{% endcapture %}
+
+{% capture modal_js_trigger_bulma %}
+
+{% endcapture %}
+
+{% capture modal_js_code %}
+document.addEventListener('DOMContentLoaded', () => {
+ // Functions to open and close a modal
+ function openModal($el) {
+ $el.classList.add('is-active');
+ }
+
+ function closeModal($el) {
+ $el.classList.remove('is-active');
+ }
+
+ function closeAllModals() {
+ (document.querySelectorAll('.modal') || []).forEach(($modal) => {
+ closeModal($modal);
+ });
+ }
+
+ // Add a click event on buttons to open a specific modal
+ (document.querySelectorAll('.js-modal-trigger') || []).forEach(($trigger) => {
+ const modal = $trigger.dataset.target;
+ const $target = document.getElementById(modal);
+ console.log($target);
+
+ $trigger.addEventListener('click', () => {
+ openModal($target);
+ });
+ });
+
+ // Add a click event on various child elements to close the parent modal
+ (document.querySelectorAll('.modal-background, .modal-close, .modal-card-head .delete, .modal-card-foot .button') || []).forEach(($close) => {
+ const $target = $close.closest('.modal');
+
+ $close.addEventListener('click', () => {
+ closeModal($target);
+ });
+ });
+
+ // Add a keyboard event to close all modals
+ document.addEventListener('keydown', (event) => {
+ const e = event || window.event;
+
+ if (e.keyCode === 27) { // Escape key
+ closeAllModals();
+ }
+ });
+});
+{% endcapture %}
+
@@ -91,10 +166,10 @@
- No JavaScript
+ JavaScript implementation example
- Bulma does not include any JavaScript interaction. You will have to implement the class toggle yourself.
+ Bulma does not include any JavaScript. However, this documentation provides a JS implementation example that you are free to use.
+{% include elements/anchor.html name="JavaScript implementation example" %}
+
+
+
+ The Bulma package does not come with any JavaScript. Here is however an implementation example, which sets the click handlers for custom elements, in vanilla JavaScript.
+
+
+
+ There are 3 parts to this implementation:
+
+
+
+
+ add the HTML for the modal (this modal is hidden by default)
+
+
+ add the HTML for a button to trigger the modal (you can style this button however you want)
+
+
+ add the JS code to add the click event on the trigger to open the modal
+
+
+
+
+
+
1. Add the HTML for the modal
+
+
+ At the end of your page, before the closing </body> tag, at the following HTML snippet:
+
+ You can style it however you want, as long as it has the js-modal-trigger CSS class and the appropriate data-target value. For example, you can add the button is-primary Bulma classes:
+
+
+
+
+ {{ modal_js_trigger_bulma }}
+
+
+
+
3. Add the JS for the trigger
+
+
+ In a script element (or in a seperate .js file), add the following JS code:
+
- The Bulma package does not come with any JavaScript. Here is however an implementation example, which sets the click handler for Bulma delete all on the page, in vanilla JavaScript.
+ The Bulma package does not come with any JavaScript. Here is however an implementation example, which sets the click handler for Bulma delete elements, anywhere on the page, in vanilla JavaScript.
{% highlight html %}{{ notification_js_html }}{% endhighlight %}