ultimate_turbo_modal 1.7.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.tool-versions +2 -2
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +159 -156
- data/LICENSE.txt +1 -1
- data/README.md +24 -98
- data/VERSION +1 -0
- data/javascript/index.js +37 -0
- data/javascript/modal_controller.js +108 -0
- data/javascript/package-lock.json +1114 -0
- data/javascript/package.json +47 -0
- data/javascript/rollup.config.js +24 -0
- data/javascript/scripts/release-npm.sh +35 -0
- data/javascript/scripts/update-version.js +21 -0
- data/javascript/styles/vanilla.css +179 -0
- data/javascript/yarn.lock +611 -0
- data/lib/generators/ultimate_turbo_modal/install_generator.rb +224 -0
- data/lib/generators/ultimate_turbo_modal/templates/flavors/custom.rb +22 -0
- data/lib/{ultimate_turbo_modal → generators/ultimate_turbo_modal/templates}/flavors/tailwind.rb +5 -4
- data/lib/generators/ultimate_turbo_modal/templates/flavors/tailwind3.rb +21 -0
- data/lib/{ultimate_turbo_modal → generators/ultimate_turbo_modal/templates}/flavors/vanilla.rb +2 -1
- data/lib/generators/ultimate_turbo_modal/templates/ultimate_turbo_modal.rb +12 -0
- data/lib/phlex/deferred_render_with_main_content.rb +1 -1
- data/lib/ultimate_turbo_modal/base.rb +8 -0
- data/lib/ultimate_turbo_modal/railtie.rb +1 -1
- data/lib/ultimate_turbo_modal/version.rb +1 -1
- data/lib/ultimate_turbo_modal.rb +3 -3
- data/script/build_and_release.sh +39 -0
- data/yarn.lock +4 -0
- metadata +25 -18
@@ -0,0 +1,108 @@
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
2
|
+
import { enter, leave } from 'el-transition';
|
3
|
+
|
4
|
+
export default class extends Controller {
|
5
|
+
static targets = ["container", "content"]
|
6
|
+
static values = {
|
7
|
+
advanceUrl: String,
|
8
|
+
allowedClickOutsideSelector: String
|
9
|
+
}
|
10
|
+
|
11
|
+
connect() {
|
12
|
+
let _this = this;
|
13
|
+
this.showModal();
|
14
|
+
|
15
|
+
this.turboFrame = this.element.closest('turbo-frame');
|
16
|
+
|
17
|
+
// hide modal when back button is pressed
|
18
|
+
window.addEventListener('popstate', function (event) {
|
19
|
+
if (_this.#hasHistoryAdvanced()) _this.#resetModalElement();
|
20
|
+
});
|
21
|
+
|
22
|
+
window.modal = this;
|
23
|
+
}
|
24
|
+
|
25
|
+
disconnect() {
|
26
|
+
window.modal = undefined;
|
27
|
+
}
|
28
|
+
|
29
|
+
showModal() {
|
30
|
+
enter(this.containerTarget);
|
31
|
+
|
32
|
+
if (this.advanceUrlValue && !this.#hasHistoryAdvanced()) {
|
33
|
+
this.#setHistoryAdvanced();
|
34
|
+
history.pushState({}, "", this.advanceUrlValue);
|
35
|
+
}
|
36
|
+
}
|
37
|
+
|
38
|
+
// if we advanced history, go back, which will trigger
|
39
|
+
// hiding the model. Otherwise, hide the modal directly.
|
40
|
+
hideModal() {
|
41
|
+
// Prevent multiple calls to hideModal.
|
42
|
+
// Sometimes some events are double-triggered.
|
43
|
+
if (this.hidingModal) return
|
44
|
+
this.hidingModal = true;
|
45
|
+
|
46
|
+
let event = new Event('modal:closing', { cancelable: true });
|
47
|
+
this.turboFrame.dispatchEvent(event);
|
48
|
+
if (event.defaultPrevented) return
|
49
|
+
|
50
|
+
this.#resetModalElement();
|
51
|
+
|
52
|
+
event = new Event('modal:closed', { cancelable: false });
|
53
|
+
this.turboFrame.dispatchEvent(event);
|
54
|
+
|
55
|
+
if (this.#hasHistoryAdvanced())
|
56
|
+
history.back();
|
57
|
+
}
|
58
|
+
|
59
|
+
hide() {
|
60
|
+
this.hideModal();
|
61
|
+
}
|
62
|
+
|
63
|
+
refreshPage() {
|
64
|
+
window.Turbo.visit(window.location.href, { action: "replace" });
|
65
|
+
}
|
66
|
+
|
67
|
+
// hide modal on successful form submission
|
68
|
+
// action: "turbo:submit-end->modal#submitEnd"
|
69
|
+
submitEnd(e) {
|
70
|
+
if (e.detail.success) this.hideModal();
|
71
|
+
}
|
72
|
+
|
73
|
+
// hide modal when clicking ESC
|
74
|
+
// action: "keyup@window->modal#closeWithKeyboard"
|
75
|
+
closeWithKeyboard(e) {
|
76
|
+
if (e.code == "Escape") this.hideModal();
|
77
|
+
}
|
78
|
+
|
79
|
+
// hide modal when clicking outside of modal
|
80
|
+
// action: "click@window->modal#outsideModalClicked"
|
81
|
+
outsideModalClicked(e) {
|
82
|
+
let clickedInsideModal = this.contentTarget.contains(e.target) || this.contentTarget == e.target;
|
83
|
+
let clickedAllowedSelector = this.allowedClickOutsideSelectorValue && this.allowedClickOutsideSelectorValue !== '' && e.target.closest(this.allowedClickOutsideSelectorValue) != null;
|
84
|
+
|
85
|
+
if (!clickedInsideModal && !clickedAllowedSelector)
|
86
|
+
this.hideModal();
|
87
|
+
}
|
88
|
+
|
89
|
+
#resetModalElement() {
|
90
|
+
leave(this.containerTarget).then(() => {
|
91
|
+
this.turboFrame.removeAttribute("src");
|
92
|
+
this.containerTarget.remove();
|
93
|
+
this.#resetHistoryAdvanced();
|
94
|
+
});
|
95
|
+
}
|
96
|
+
|
97
|
+
#hasHistoryAdvanced() {
|
98
|
+
return document.body.getAttribute("data-turbo-modal-history-advanced") == "true"
|
99
|
+
}
|
100
|
+
|
101
|
+
#setHistoryAdvanced() {
|
102
|
+
return document.body.setAttribute("data-turbo-modal-history-advanced", "true")
|
103
|
+
}
|
104
|
+
|
105
|
+
#resetHistoryAdvanced() {
|
106
|
+
document.body.removeAttribute("data-turbo-modal-history-advanced");
|
107
|
+
}
|
108
|
+
}
|