Mercurial > libervia-web
comparison libervia/web/pages/_browser/popup.py @ 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 | |
children |
comparison
equal
deleted
inserted
replaced
1581:fe1995d0df09 | 1582:f52b89365002 |
---|---|
1 from browser import document, timer | |
2 from js_modules.tippy_js import tippy | |
3 | |
4 | |
5 def create_popup( | |
6 target, | |
7 content_elt, | |
8 focus_class="has-popup-focus", | |
9 focus_elt=None, | |
10 **kwargs | |
11 ): | |
12 """Create a popup and show a popup below a target | |
13 | |
14 The popup is created and destroyed on each call, this can be used for dynamic content. | |
15 @param target: element where the popup will appear, or a selector | |
16 @param content_elt: HTML element to show in the popup | |
17 @param focus_class: class added to target element when popup is shown | |
18 @param focus_elt: element where focus_class is added. If None, target will be used. | |
19 @param kwargs: arguments used to override tippy options | |
20 """ | |
21 if focus_elt is None: | |
22 focus_elt = target | |
23 def on_hide(__): | |
24 focus_elt.classList.remove(focus_class) | |
25 timer.set_timeout(tippy_instance.destroy, 0) | |
26 tippy_opts = { | |
27 "trigger": "click", | |
28 "content": content_elt, | |
29 "appendTo": document.body, | |
30 "placement": "bottom", | |
31 "interactive": True, | |
32 "trigger": "manual", | |
33 "theme": "light", | |
34 "onShow": lambda __: focus_elt.classList.add(focus_class), | |
35 "onHide": on_hide | |
36 } | |
37 tippy_opts.update(kwargs) | |
38 | |
39 tippy_instance = tippy( | |
40 target, | |
41 tippy_opts | |
42 ) | |
43 tippy_instance.show() | |
44 return tippy_instance |