Skip to content

Commit

Permalink
fix: Drawer, Modal & BottomSheet no-close on selected text (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
phgrey authored Aug 29, 2023
1 parent 4952cda commit 15dc52b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
23 changes: 21 additions & 2 deletions assets/js/hooks/animation.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}

export default {

hiddenClasses() {
Expand All @@ -14,16 +24,25 @@ export default {
});
},


updated() {
this.showElementIfNeeded();
(this.el.dataset.is_closing === "true") && this.hideElement()
this.hideElementIfNeeded();
},

showElementIfNeeded() {
(this.el.dataset.is_open === undefined) || !this.el.classList.contains("hidden") || this.showElement();
},

hideElementIfNeeded() {
if (this.el.dataset.is_closing === "true"){
if (getSelectionText()) {
this.pushEventTo(this.el, "stop_closing", {});
} else {
this.hideElement()
}
}
},

hideElement() {
if(this.el.dataset.lg_persists !== undefined && document.body.clientWidth >= 1024) return;
this.panel.classList.remove(...this.panel.dataset.animate_enter_class.split(" "));
Expand Down
4 changes: 4 additions & 0 deletions lib/moon/design/bottom_sheet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ defmodule Moon.Design.BottomSheet do
{:noreply, assign(socket, is_closing: true)}
end

def handle_event("stop_closing", _, socket) do
{:noreply, assign(socket, is_closing: false)}
end

def handle_event("set_close", _, socket) do
{:noreply, assign(socket, is_open: false, is_closing: false)}
end
Expand Down
4 changes: 4 additions & 0 deletions lib/moon/design/drawer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ defmodule Moon.Design.Drawer do
{:noreply, assign(socket, is_closing: true)}
end

def handle_event("stop_closing", _, socket) do
{:noreply, assign(socket, is_closing: false)}
end

def handle_event("set_close", _, socket) do
{:noreply, assign(socket, is_open: false, is_closing: false)}
end
Expand Down
4 changes: 4 additions & 0 deletions lib/moon/design/modal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ defmodule Moon.Design.Modal do
{:noreply, assign(socket, is_closing: true)}
end

def handle_event("stop_closingt", _, socket) do
{:noreply, assign(socket, is_closing: false)}
end

def handle_event("set_close", _, socket) do
{:noreply, assign(socket, is_open: false, is_closing: false)}
end
Expand Down

0 comments on commit 15dc52b

Please sign in to comment.