Skip to content

Commit 8799cc0

Browse files
authored
Merge pull request #69 from lemonsaurus/lemon/bulma_modal
Add a bulma-modal extension to handle modal events.
2 parents 231bf7e + cfa55b0 commit 8799cc0

File tree

5 files changed

+83
-6
lines changed

5 files changed

+83
-6
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ django-simple-bulma
22
===================
33
`django-simple-bulma` is a Django application that makes [Bulma](https://bulma.io) and [Bulma-Extensions](https://wikiki.github.io/) available to use in your Django project with as little setup as possible. The goal of this project is to make it as easy as possible to use Bulma with Django.
44

5-
This project currently uses **Bulma v0.9.1**, and is automatically updated with every new release. If a new version has come out with features you'd like to make use of, please [create an issue](https://github.com/python-discord/django-simple-bulma/issues), and we will be happy to make a release to update it.
5+
This project currently uses **Bulma v0.9.2**, and is automatically updated with every new release. If a new version has come out with features you'd like to make use of, please [create an issue](https://github.com/python-discord/django-simple-bulma/issues), and we will be happy to make a release to update it.
66

77
Installation
88
------------
@@ -122,6 +122,7 @@ For your convenience, we also give you the option to add other quality of life i
122122
* `bulma-navbar-burger` will hook up your `navbar-burger`s and `navbar-menu`s automatically, to provide a toggle for mobile users. We use a slightly updated version of [the example from Bulma's documentation](https://bulma.io/documentation/components/navbar/#navbarJsExample) - simply add a `data-target` attribute to your `navbar-burger` that refers to the `id` of the `navbar-menu` that should be expanded and collapsed by the button.
123123
* `bulma-notifications` will allow you to close [notifications](https://bulma.io/documentation/elements/notification/) by clicking on the X button.
124124
* `bulma-dropdown` will open/close dropdowns using the `is-active` class. It mimics how the dropdowns function on the [documentation page](https://bulma.io/documentation/components/dropdown/#hoverable-or-toggable).
125+
* `bulma-modal` will handle opening and closing modals. Just assign the `modal-button` class to a `<button>`, and make sure it has a `data-target` attribute that matches the `id` of the modal that you want to open. See [the example code from Bulma's documentation](https://bulma.io/documentation/components/modal/) for modal element code.
125126

126127
Compiling additional SCSS
127128
------------------------
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
});

django_simple_bulma/finders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def __init__(self):
4545
self.storage = FileSystemStorage(simple_bulma_path)
4646

4747
def _get_extension_imports(self) -> str:
48-
"""Return a string that, in SASS, imports all enabled extensions"""
48+
"""Return a string that, in SASS, imports all enabled extensions."""
4949
scss_imports = ""
5050

5151
for ext in (simple_bulma_path / "extensions").iterdir():
@@ -57,7 +57,7 @@ def _get_extension_imports(self) -> str:
5757
return scss_imports
5858

5959
def _unpack_variables(self, variables: dict) -> str:
60-
"""Unpacks SASS variables from a dictionary to a compilable string"""
60+
"""Unpacks SASS variables from a dictionary to a compilable string."""
6161
scss_string = ""
6262
for var, value in variables.items():
6363
scss_string += f"${var}: {value};\n"

django_simple_bulma/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444

4545
def is_enabled(extension: Union[Path, str]) -> bool:
46-
"""Return whether an extension is enabled or not"""
46+
"""Return whether an extension is enabled or not."""
4747
if isinstance(extension, Path):
4848
return extensions == "all" or extension.name in extensions
4949
return extensions == "all" or extension in extensions
@@ -69,7 +69,7 @@ def get_js_files() -> Generator[str, None, None]:
6969

7070

7171
def get_sass_files(ext: Path) -> List[Path]:
72-
"""Given the path to an extension, find and yield all files that should be imported"""
72+
"""Given the path to an extension, find and yield all files that should be imported."""
7373
for rel_path, glob in sass_files_searches:
7474
src_files = list((ext / rel_path).rglob(glob))
7575

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
setup(
1111
name="django-simple-bulma",
12-
version="2.1.0",
12+
version="2.2.0",
1313
description="Django application to add the Bulma CSS framework and its extensions",
1414
long_description=open("README.md").read(),
1515
long_description_content_type="text/markdown",

0 commit comments

Comments
 (0)