From a821c381cb9bff07ad25c2c444b23480be275701 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Wed, 3 Jan 2024 15:40:14 -0600 Subject: [PATCH] Include `tooltip()` and `popover()` in express (#949) --- shiny/express/ui/__init__.py | 6 ++- shiny/express/ui/_cm_components.py | 87 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/shiny/express/ui/__init__.py b/shiny/express/ui/__init__.py index b016b9c70..730380281 100644 --- a/shiny/express/ui/__init__.py +++ b/shiny/express/ui/__init__.py @@ -133,6 +133,8 @@ panel_conditional, panel_fixed, panel_absolute, + tooltip, + popover, ) from ._page import ( @@ -269,6 +271,8 @@ "panel_conditional", "panel_fixed", "panel_absolute", + "popover", + "tooltip", # Imports from ._page "page_opts", ) @@ -294,11 +298,9 @@ "panel_main", # Deprecated "panel_sidebar", # Deprecated "panel_title", - "popover", "showcase_bottom", "showcase_left_center", "showcase_top_right", - "tooltip", ), # Items from shiny.express.ui that don't have a counterpart in shiny.ui "shiny.express.ui": ("page_opts",), diff --git a/shiny/express/ui/_cm_components.py b/shiny/express/ui/_cm_components.py index de4e251d2..97cc60778 100644 --- a/shiny/express/ui/_cm_components.py +++ b/shiny/express/ui/_cm_components.py @@ -1400,3 +1400,90 @@ def panel_absolute( **kwargs, ), ) + + +# ====================================================================================== +# Tooltips and popovers +# ====================================================================================== + + +def tooltip( + *, + id: Optional[str] = None, + placement: Literal["auto", "top", "right", "bottom", "left"] = "auto", + options: Optional[dict[str, object]] = None, + **kwargs: TagAttrValue, +) -> RecallContextManager[Tag]: + """ + Context manager for a tooltip + + This function wraps :func:`~shiny.ui.tooltip`. + + Display additional information when focusing (or hovering over) a UI element. + + Parameters + ---------- + id + A character string. Required to reactively respond to the visibility of the + tooltip (via the `input[id]` value) and/or update the visibility/contents of the + tooltip. + placement + The placement of the tooltip relative to its trigger. + options + A list of additional [Bootstrap + options](https://getbootstrap.com/docs/5.3/components/tooltips/#options). + """ + + return RecallContextManager( + ui.tooltip, + kwargs=dict( + id=id, + placement=placement, + options=options, + **kwargs, + ), + ) + + +def popover( + *, + title: Optional[TagChild] = None, + id: Optional[str] = None, + placement: Literal["auto", "top", "right", "bottom", "left"] = "auto", + options: Optional[dict[str, object]] = None, + **kwargs: TagAttrValue, +) -> RecallContextManager[Tag]: + """ + Context manager for a popover + + This function wraps :func:`~shiny.ui.popover`. + + Display additional information when clicking on a UI element (typically a + button). + + Parameters + ---------- + title + A title to display in the popover. Can be a character string or UI elements + (i.e., tags). + id + A character string. Required to reactively respond to the visibility of the + popover (via the `input[id]` value) and/or update the visibility/contents of the + popover. + placement + The placement of the popover relative to its trigger. + options + A list of additional [Bootstrap + options](https://getbootstrap.com/docs/5.3/components/popovers/#options). + """ + + return RecallContextManager( + ui.popover, + kwargs=dict( + title=title, + id=id, + placement=placement, + options=options, + **kwargs, + ), + )