annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1582
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
1 from browser import document, timer
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
2 from js_modules.tippy_js import tippy
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
4
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
5 def create_popup(
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
6 target,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
7 content_elt,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
8 focus_class="has-popup-focus",
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
9 focus_elt=None,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
10 **kwargs
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
11 ):
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
12 """Create a popup and show a popup below a target
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
13
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
14 The popup is created and destroyed on each call, this can be used for dynamic content.
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
15 @param target: element where the popup will appear, or a selector
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
16 @param content_elt: HTML element to show in the popup
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
17 @param focus_class: class added to target element when popup is shown
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
18 @param focus_elt: element where focus_class is added. If None, target will be used.
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
19 @param kwargs: arguments used to override tippy options
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
21 if focus_elt is None:
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
22 focus_elt = target
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
23 def on_hide(__):
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
24 focus_elt.classList.remove(focus_class)
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
25 timer.set_timeout(tippy_instance.destroy, 0)
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
26 tippy_opts = {
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
27 "trigger": "click",
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
28 "content": content_elt,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
29 "appendTo": document.body,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
30 "placement": "bottom",
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
31 "interactive": True,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
32 "trigger": "manual",
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
33 "theme": "light",
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
34 "onShow": lambda __: focus_elt.classList.add(focus_class),
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
35 "onHide": on_hide
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
36 }
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
37 tippy_opts.update(kwargs)
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
38
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
39 tippy_instance = tippy(
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
40 target,
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
41 tippy_opts
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
42 )
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
43 tippy_instance.show()
f52b89365002 browser: new `popup` module to create dynamic popups
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return tippy_instance