Skip to content

Commit dd28523

Browse files
committed
Refactor modal component-partial to have more sensible API
1 parent 11870f9 commit dd28523

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

view/base/templates/script/component-partial/modal-component-partial.phtml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,43 @@ use Magento\Framework\View\Element\Template;
99
?>
1010
<script>
1111
const LokiModalComponentPartial = {
12+
modalOpen: false,
1213
initModalClosing() {
13-
const modal = this.$refs.modal;
14-
if (!modal) {
15-
return;
16-
}
1714

18-
modal.addEventListener('click', function(event) {
19-
if (event.target === modal) {
20-
modal.close();
15+
this.$watch('modalOpen', (modalOpen) => {
16+
if (false === modalOpen) {
17+
this.$refs.modal.close();
18+
} else {
19+
this.$refs.modal.showModal();
2120
}
2221
});
22+
23+
this.$nextTick(() => {
24+
this.modalOpen = this.$refs.modal.open;
25+
26+
const modal = this.$refs.modal;
27+
if (!modal) {
28+
return;
29+
}
30+
31+
modal.addEventListener('click', (event) => {
32+
if (event.target === modal) {
33+
this.modalOpen = false;
34+
}
35+
});
36+
});
2337
},
2438
toggleModal() {
25-
if (this.isModalOpen()) {
26-
this.$refs.modal.close();
27-
return;
28-
}
29-
30-
this.$refs.modal.showModal();
39+
this.modalOpen = !this.modalOpen;
40+
},
41+
closeModal() {
42+
this.modalOpen = false;
43+
},
44+
openModal() {
45+
this.modalOpen = true;
3146
},
3247
isModalOpen() {
33-
return this.$refs.modal.open;
48+
return this.modalOpen;
3449
},
3550
isModalClosed() {
3651
return !this.isModalOpen();

0 commit comments

Comments
 (0)