From 15dc52bdd2c0259efd1af34bd597703d5425b697 Mon Sep 17 00:00:00 2001 From: Sergey Semenov Date: Tue, 29 Aug 2023 09:47:14 +0200 Subject: [PATCH] fix: Drawer, Modal & BottomSheet no-close on selected text (#703) --- assets/js/hooks/animation.js | 23 +++++++++++++++++++++-- lib/moon/design/bottom_sheet.ex | 4 ++++ lib/moon/design/drawer.ex | 4 ++++ lib/moon/design/modal.ex | 4 ++++ 4 files changed, 33 insertions(+), 2 deletions(-) diff --git a/assets/js/hooks/animation.js b/assets/js/hooks/animation.js index aa651ba59..400bf8e84 100644 --- a/assets/js/hooks/animation.js +++ b/assets/js/hooks/animation.js @@ -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() { @@ -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(" ")); diff --git a/lib/moon/design/bottom_sheet.ex b/lib/moon/design/bottom_sheet.ex index 3b836e01a..3a7388d83 100644 --- a/lib/moon/design/bottom_sheet.ex +++ b/lib/moon/design/bottom_sheet.ex @@ -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 diff --git a/lib/moon/design/drawer.ex b/lib/moon/design/drawer.ex index 9b677551b..2fe548938 100644 --- a/lib/moon/design/drawer.ex +++ b/lib/moon/design/drawer.ex @@ -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 diff --git a/lib/moon/design/modal.ex b/lib/moon/design/modal.ex index 38cd4a93a..827908d41 100644 --- a/lib/moon/design/modal.ex +++ b/lib/moon/design/modal.ex @@ -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