1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
#' Add a tooltip to a UI element
#'
#' @description
#' Display additional information when focusing (or hovering over) a UI element.
#'
#' @param trigger A UI element (i.e., [htmltools tag][htmltools::tags]) to serve
#' as the tooltip trigger. If `trigger` renders as multiple HTML
#' elements (e.g., it's a `tagList()`), the last HTML element is used for the
#' trigger. If the `trigger` should contain all of those elements, wrap the
#' object in a [htmltools::div()] or [htmltools::span()].
#' @param ... UI elements for the tooltip. Character strings are [automatically
#' escaped][htmltools::htmlEscape()] unless marked as [htmltools::HTML()].
#' @param id A character string. Required to re-actively respond to the
#' visibility of the tooltip (via the `input[[id]]` value) and/or update the
#' visibility/contents of the tooltip.
#' @param placement The placement of the tooltip relative to its trigger.
#' @param options A list of additional [options](https://getbootstrap.com/docs/5.3/components/tooltips/#options).
#'
#' @section Theming/Styling:
#'
#' ```{r child="man/fragments/tooltip-popover_theming.Rmd", el="tooltip"}
#' ```
#'
#' ```
#' tooltip(
#' "Trigger", "Tooltip message",
#' options = list(customClass = "my-tip")
#' )
#' ```
#'
#' And then add relevant rules to [bs_theme()] via [bs_add_rules()]:
#'
#' ```
#' bs_theme() |> bs_add_rules(".my-tip { max-width: none; }")
#' ```
#'
#' @section Accessibility of Tooltip Triggers:
#'
#' ```{r child="man/fragments/tooltip-popover_a11y-trigger.Rmd", el = "tooltip"}
#' ```
#'
#' ```r
#' tooltip(
#' bsicons::bs_icon("info-circle", title = "About tooltips"),
#' "Text shown in the tooltip."
#' )
#' ```
#'
#' ```r
#' tooltip(
#' fontawesome::fa("info-circle", a11y = "sem", title = "About tooltips"),
#' "Text shown in the tooltip."
#' )
#' ```
#'
#' @describeIn tooltip Add a tooltip to a UI element
#'
#' @export
#' @family Components
#'
#' @references Tooltips are based on [Bootstrap's Tooltip
#' component](https://getbootstrap.com/docs/5.3/components/tooltips/). See the
#' bslib website for an [interactive introduction to tooltips and
#' popovers](https://rstudio.github.io/bslib/articles/tooltips-popovers/index.html).
#'
#' @seealso [popover()] provides a an alternative and more persistent container
#' for additional elements, typically revealed by clicking on a target
#' element.
#'
#' @examplesIf rlang::is_interactive()
#'
#' tooltip(
#' shiny::actionButton("btn", "A button"),
#' "A message"
#' )
#'
#' card(
#' card_header(
#' tooltip(
#' span("Card title ", bsicons::bs_icon("question-circle-fill")),
#' "Additional info",
#' placement = "right"
#' )
#' ),
#' "Card body content..."
#' )
tooltip <- function(
trigger,
...,
id = NULL,
placement = c("auto", "top", "right", "bottom", "left"),
options = list()
) {
args <- separate_arguments(...)
children <- args$children
attribs <- args$attribs
if (length(children) == 0) {
abort("At least one value must be provided to `...`.")
}
bad_opts <- intersect(c("title", "placement"), names(options))
if (length(bad_opts) > 0) {
rlang::abort(
sprintf("The `%s` option cannot be specified directly.", bad_opts[1])
)
}
res <- web_component(
"bslib-tooltip",
id = id,
placement = rlang::arg_match(placement),
bsOptions = to_json(options),
!!!attribs,
# Use <template> as a way to protect these children from potentially being
# pulled outside this element (the browser's parser does this to, for
# example, block elements inside a <p> tag)
tags$template(!!!children),
trigger
)
res <- tag_require(res, version = 5, caller = "tooltip()")
as_fragment(res)
}
#' @describeIn tooltip Programmatically show/hide a tooltip.
#'
#' @param id a character string that matches an existing tooltip id.
#' @param show Whether to show (`TRUE`) or hide (`FALSE`) the tooltip. The
#' default (`NULL`) will show if currently hidden and hide if currently shown.
#' Note that a tooltip will not be shown if the trigger is not visible (e.g.,
#' it's hidden behind a tab).
#' @param session A Shiny session object (the default should almost always be
#' used).
#'
#' @export
toggle_tooltip <- function(id, show = NULL, session = get_current_session()) {
show <- normalize_show_value(show)
msg <- list(method = "toggle", value = show)
force(id)
callback <- function() {
session$sendInputMessage(id, msg)
}
session$onFlush(callback, once = TRUE)
}
#' @describeIn tooltip Update the contents of a tooltip.
#' @export
update_tooltip <- function(id, ..., session = get_current_session()) {
title <- tagList(...)
msg <- dropNulls(
list(
method = "update",
title = if (length(title) > 0) processDeps(title, session)
)
)
force(id)
callback <- function() {
session$sendInputMessage(id, msg)
}
session$onFlush(callback, once = TRUE)
}
normalize_show_value <- function(show) {
if (is.null(show)) return("toggle")
if (length(show) != 1 || !is.logical(show)) {
abort("`show` must be `TRUE`, `FALSE`, or `NULL`.")
}
if (show) "show" else "hide"
}
to_json <- function(..., auto_unbox = TRUE, null = "null") {
jsonlite::toJSON(..., auto_unbox = auto_unbox, null = null)
}
|