changeset 1582:f52b89365002

browser: new `popup` module to create dynamic popups
author Goffi <goffi@goffi.org>
date Tue, 28 Nov 2023 17:53:56 +0100
parents fe1995d0df09
children 9865013da86c
files libervia/web/pages/_browser/popup.py
diffstat 1 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libervia/web/pages/_browser/popup.py	Tue Nov 28 17:53:56 2023 +0100
@@ -0,0 +1,44 @@
+from browser import document, timer
+from js_modules.tippy_js import tippy
+
+
+def create_popup(
+    target,
+    content_elt,
+    focus_class="has-popup-focus",
+    focus_elt=None,
+    **kwargs
+):
+    """Create a popup and show a popup below a target
+
+    The popup is created and destroyed on each call, this can be used for dynamic content.
+    @param target: element where the popup will appear, or a selector
+    @param content_elt: HTML element to show in the popup
+    @param focus_class: class added to target element when popup is shown
+    @param focus_elt: element where focus_class is added. If None, target will be used.
+    @param kwargs: arguments used to override tippy options
+    """
+    if focus_elt is None:
+        focus_elt = target
+    def on_hide(__):
+        focus_elt.classList.remove(focus_class)
+        timer.set_timeout(tippy_instance.destroy, 0)
+    tippy_opts = {
+        "trigger": "click",
+        "content": content_elt,
+        "appendTo": document.body,
+        "placement": "bottom",
+        "interactive": True,
+        "trigger": "manual",
+        "theme": "light",
+        "onShow": lambda __: focus_elt.classList.add(focus_class),
+        "onHide": on_hide
+    }
+    tippy_opts.update(kwargs)
+
+    tippy_instance = tippy(
+        target,
+        tippy_opts
+    )
+    tippy_instance.show()
+    return tippy_instance