Skip to contents

[Stable] Gives one HTML tag a tip that shows after the pointer rests on it, or keyboard focus arrives, and hides when either leaves. The tip is plain text, or drawn from a named template (tip_define()) filled in from a record: the values of one item, such as a row of a table. The engine's script and CSS are attached to the tag, so it works in a bslib page, in a plain Shiny page and in output made later with renderUI(). A tag inside bslib::tooltip() or bslib::popover(), or with data-bs-toggle, keeps the tip that bslib draws and this one is left off.

Usage

tip_on(
  tag,
  text = NULL,
  ...,
  template = NULL,
  record = NULL,
  style = NULL,
  placement = NULL,
  marker = NULL,
  id = NULL,
  events = NULL,
  when_disabled = NULL,
  more = NULL,
  pin = NULL,
  labels = NULL
)

Arguments

tag

[Stable] One HTML tag, such as htmltools::tags$tr() or shiny::actionButton(). A list of tags, a tag list or text stops with an error. To tip a tab, put the tip on the tag that is its title, and give the tab a value: nav_panel(title = tip_on(span("Data"), "..."), value = "data"). Without value, bslib writes the tag's HTML into the tab's value.

text

[Stable] One string of markdown (strong, emphasis, code, links, short lists), or NULL. It is shown as written: build it with paste0() or sprintf(). NA and "" count as none: the tag comes back without a tip. When record is given, the {{ field }} placeholders in the text are filled from it (as in a template, with one filter at most, number:n or percent:n), also when template is the name of a defined template, which is then not used; when template is a tip_template(), the text is plain words and its braces stay as written, since the text beats the template. Without a record, braces stay as written. Words with placeholders give the tag no aria-label, because they depend on the record.

...

Must be empty. It makes a misspelt argument an error.

template

[Stable] The name of a template defined with tip_define(), a tip_template() made here, or NULL. A tip_template() travels with the tag, so it needs no tip_define(); its fields are checked against record. If text is given too, the text is the tip and the template is not used (see text for its placeholders).

record

[Stable] A named list, or a data frame of one row: the values the template's placeholders read, one value each. NA and NULL show a dash. Dates are written as YYYY-MM-DD, times in UTC as YYYY-MM-DDTHH:MM:SSZ and factors as their labels. Once tip_define() has seen the template, only the columns it names are kept, so nothing else in a row is written into the page; for a template not seen here, every column goes, so choose them with data[i, c("label", "n")].

style

[Stable] as a preset name, [Experimental] as a style object. How this tag's tip looks, or NULL (the default) to take the page's. A preset name from tip_presets() ("none" clears a preset set above), a tip_style(), or one part of a style: a tip_theme() or a bslib::bs_theme(). The tag's style beats the page's, and a value set nearer wins, value by value. A list is not taken, and neither is a string that holds : or ; (style takes no CSS); see tip_style(). A list of CSS variables (tip_style(theme = list("--x" = ...))) is for the whole page, so on one tag it stops with an error: give it to use_hovercraft(), or use the roles of tip_theme().

placement

[Stable] Where the tip opens: "auto", "top", "bottom", "left" or "right", or NULL (the default) for the page's choice, which is "auto" unless a tagged ancestor sets a side. A chosen side flips to the other when there is no room, and "left" and "right" mirror in right-to-left text. On a phone the tip always chooses its own side.

marker

[Experimental] A visible cue that the tag holds a tip. NULL (the default) and "none" draw none; the others are "underline", "icon" or "corner", a tip_marker() to choose the line of an underline or give an icon, or a tag, which is used as the icon (marker = fontawesome::fa("circle-info") is tip_marker("icon", icon = ...)). See tip_marker() for what each draws. For the cells of a table, use the marker argument of tip_table().

id

[Stable] A Shiny id for this tip, or NULL (the default) for none: one non-empty string with no whitespace and no :, so ns("baseline_tip") works in a module. Give it a different id from every other tip source on the page. tip_show(), tip_hide() and tip_update() find the tip by it, and events names the inputs after it.

events

[Experimental] Which Shiny inputs this tag's tip sends, or NULL (the default) for none; one or more of "hint", "pin" and "copy". All are off unless asked for, because they send a message to the server every time a reader meets the tag. They need an id, or a tag with an id of its own; in a module the input is read without the namespace. "pin" and "copy" belong to a pinned tip, so asking for either without a pin stops with an error. Each is a list that holds the fields below.

"hint" sends input$<id>_hint each time the tip opens or closes: state ("shown" or "hidden"), text (the tip's plain words, cut at 200 characters), by ("reader", "host" for tip_show() and tip_hide(), or "data" when the tag left the page), via ("pointer", "focus", "tap", "key", "command", or NULL) and reason (why it closed, NULL on "shown": "leave", "blur", "escape", "outside", "pinned" when the hint became a pin, "replaced", "gone" or "host"). "pin" sends input$<id>_pin each time the tip is pinned, closed or drawn again while pinned: state ("pinned", "closed" or "updated"), text, by ("reader", "host" or "data"), via ("click", "key", "command", or NULL) and reason (why it closed: "select", "escape", "button", "outside", "replaced", "gone", "below" when a layer around it closed, "host" or "redraw" when it was drawn again with other host slots (by is "data"); NULL on "pinned"). A "state" of "updated" is not a pin: an app that tests only for "closed" should check it. "pin" also keeps input$<id>_pinned, TRUE while the tip is pinned and FALSE after, sent in the same batch as _pin and as a plain value, so an equal value does not fire an observer again; read it with isTRUE(input$run_pinned). The values of state may grow in a later version, so an app should not assume it knows them all. Every one of these inputs is NULL until its first event; in shiny::testServer() set them with session$setInputs(), and in shinytest2 with set_inputs(..., allow_no_input_binding_ = TRUE). On a page without Shiny (R Markdown, Quarto, a static page) events does nothing. "copy" sends input$<id>_copy each time the Copy button of a code block in the pinned tip is pressed: field (the record field the code came from), block (the code block's place among the tip's code blocks, from 0), ok (TRUE when the browser let the copy happen), by ("reader") and via ("click" or "key").

when_disabled

[Experimental] The reason the control is switched off, as one string of plain text with a word in it, or NULL (the default) for none. While the control is disabled, its tip says "Not available:" and the reason, with a small lock before it; when the control is enabled again the reason goes and the tag's own tip (text or template) is shown as usual. text and template may be left out, for a tag that only has a reason; then a marker is left off with a warning, since it would show on an enabled control that has no tip. The tag must hold a control that can be disabled: a button, input, select, textarea or fieldset. It works when the page is built with the control disabled and when it is switched off later, by the update*(disabled = ) functions of Shiny or by script, on a Bootstrap 5 page (bslib) and on the Bootstrap 3 page of a plain fluidPage(); a control disabled by aria-disabled alone is not covered. tip_update() changes the control's own tip, also while it is disabled: the new words show once the control is enabled.

A disabled control takes no keyboard focus and, under Bootstrap 5, no pointer, so tip_on() puts it in a small wrapper that does: a <span>, or a <div> for a tag that is a block, such as the div Shiny puts around an input. While the control is disabled the wrapper is one tab stop, and the control gets no pointer, so the reason is the wrapper's. A button's wrapper has the role of a button; any other control is a group named by the control's label, so a screen reader still says what the field is. The wrapper is not a tab stop when the control is enabled. A control that has the focus when it is switched off hands it to the wrapper, and takes it back when it is switched on. The tip's id and events stay on the control, and the wrapper answers to them while the control is disabled, so tip_show() and the hint input name the control either way. The value returned is the wrapper; calling tip_on() on it again replaces the tip whole, the reason included, and keeps one wrapper when a new when_disabled is given; without one you get the bare control back, with the layout it had before it was wrapped.

The wrapper takes the control's place in a layout: a width, a share of a flex row and a side written in its style, or as the classes w-25, w-50, w-75, w-100, flex-fill, flex-grow-*, col-*, order-*, align-self-* and ms-auto, move to the wrapper, and the control fills the wrapper. A d-block control and the cells of a d-grid keep their width. Not covered: a control that is a direct child of a btn-group, btn-group-vertical or input-group, since the wrapper then comes between the control and the joined borders; put the tip on the group instead, and do not give a control in a group a reason. A proposed fix is a reason that needs no wrapper, which asks the tip engine for a keyboard stop of its own.

more

[Stable] as words or a template, [Experimental] as a tip_more(). Extra content for the long form of the tip, or NULL (the default) for none: words (markdown, optionally with {{ field }} placeholders filled from record), a tip_template(), or a tip_more() that also says when the long form shows. NA and "" count as none. Words and a template are the same as tip_more() with its default: the tip is short, and a "More" button in it (and the M key) opens the long form in the same tip, with no pin and no id. See tip_more() for "hover", "pin", "click" (a click on the item opens it; not on a control), pin = TRUE (the button pins the tip too), as and where (a text link, and the side the control sits on) and the buttons' words; with "pin" or pin = TRUE the tag must be able to pin and have an id (either id = or the tag's own HTML id), otherwise tip_on() stops with an error.

pin

[Stable] Whether a reader can keep this tip open, one at a time: NULL (the default) or FALSE for a tip that only shows on hover or focus (FALSE is "never", whatever a level above says), TRUE for a tip that is pinned by the small pin button in the corner of the tip, or by the pin key, and "click" for a tip that a click on the tagged item also pins. "click" is for a tag that has nothing to click on its own: on a control, or on a tag that holds one, it stops with an error, because the click would pin the tip instead of using the control. Use TRUE there: a plain button and a div around an input take it. With TRUE, a click, a key or a tap on the item never pins, so an item that does something when it is clicked, such as a row that selects itself, keeps doing it; a tap shows the tip. A tip that is pinned stays open, with the engine's own close button, until it is closed (the button, Escape, or a click elsewhere), and a code block with tip_code(copy = TRUE) shows its Copy button there. A pin needs an id (the id argument or the tag's own), which names the tip in events, and a tag that the engine can pin: not a link, a submit or reset button, an input, select, textarea, summary, label or editable tag; put the tip on a <div> or <span> around it.

The pin button is drawn over the corner of the tip as a separate element, so a screen reader still gets a tip that it only reads; the first block of the tip keeps room for it, so a one-sentence tip has the button at the end of its line. The pin key is P, which the tagged item announces as aria-keyshortcuts: it pins the tagged item while the focus is on the item or inside it, or while the pointer rests on it, and does nothing while the focus is in a field or while neither holds. When the focus is on one item and the pointer rests on another, the key pins the item under the pointer, the tip the reader sees, not an item around it; an item that is pinned and holds the focus keeps the key. A tip that a tap or tip_show() opened does not take the key from the pointer. The key works from a hover, so a page switches it off for every reader with use_hovercraft(keys = FALSE); there is no switch for one reader. A screen reader that is in browse mode may take the letter for its own navigation, so check the key with the screen reader you support. Escape closes a pinned tip. Pointer, keyboard and tap are tested in Chrome only.

labels

[Experimental] The words the tip shows itself, such as "Copy", "Close", "More" and the pin button's name, for this tag's tip and every tip inside the tag, or NULL (the default) to take the page's words (see use_hovercraft()). A named character vector, such as c(close = "Dismiss"); a word left out keeps its built-in one. The keys, with their built-in words, are copy (Copy), copied (Copied), copy_failed (Copy failed), close (Close), not_available (Not available:), pinned ({label}: details pinned. Tab to reach them.), code_line (code: {first}), code_line_empty (code, 1 line), code_lines (code, {n} lines, starting {first}), code_lines_empty (code, {n} lines), more (More), more_pinned (More when pinned), less (Less), pin (Pin this tip). Four keys take placeholders, which the page fills in: pinned takes {label} (the name of the item), code_line takes {first} (the first line of the code) and code_lines and code_lines_empty take {n} (the number of lines; code_lines takes {first} too); other braces stay as written. An unknown key, an empty or blank word, or a value that is not words stops with an error; the unknown key's error lists the keys. The nearest words win, key by key: a tag's labels beat the page's, and a tag inside another tag with labels uses the nearer one. tip_more(label = ) still sets the words of the "More" and "Less" buttons of one tip, and beats labels there. The words go with the tag, so labels needs a tip on the call (text, template or when_disabled); a second tip_on() on the tag replaces them with its own. With when_disabled, they are also on the wrapper, so "Not available:" changes while the control is off. For a tip_table(), give the words to use_hovercraft(): a table is marked by its own script, and only a tag that has a tip of its own can carry labels around it.

Value

The tag, with the tip's attributes and the engine's dependency added; with a marker, its attributes, and the marker's own dependency; with when_disabled, inside a wrapper that holds the reason.

Details

A tip shows on keyboard focus as well as on a pointer, so a tag that cannot take focus (a table row, a div) gives a keyboard user no way to the tip. tip_on() looks at the tag and at everything inside it, so the div that Shiny puts around an input is fine: the input takes focus. It warns once about a tag with nothing focusable in it, and never adds a tabindex itself, because a table of 3,000 rows would become 3,000 Tab stops: give the tags worth a stop tabindex = "0", or put the tip on a button or a link.

A button with no visible words, such as one that holds only an icon, has no name for a screen reader. When it also has no aria-label or aria-labelledby of its own, and the tip is plain text, tip_on() gives it an aria-label made from the text, with the markdown marks taken out. A tip drawn from a template gives no label, because its words depend on the record: set aria-label yourself. The engine reads the tip once: when a tag's name, from aria-label, aria-labelledby or its own words, is the same as the tip's words, the tip is not also given to a screen reader as a description.

If both text and template are given, the text is shown for this tag. A template whose name has been defined in this session is checked against the record here: a field it names that the record does not hold is an error. A name that has not been defined is left to the page, which says so once in the browser's console.

A second tip_on() on a tag that already has a tip replaces the old tip whole: its words, look, id, events, pin and when_disabled all go, and the new call's arguments are the tip. When the old tip had an id, events or a pin and the new call gives none of them, it warns once, with the class hovercraft_warning_retip, so a wrapper that tips a tag again on purpose can silence it with suppressWarnings(classes = ).

A tag that already has a title attribute keeps it, and the browser then shows its own tooltip beside the tip. tip_on() warns once, with the class hovercraft_warning_title, and says to remove the title; it does not remove it itself, because on an icon it may be the only name.

Stable and experimental

tag, text, template, record, placement, id, pin (one pinned tip at a time, with its pin button and the P key), more as words or a template (a "More" button opens the long form in the same tip) and a style that is a preset name are r lifecycle::badge("stable"): their names, values and defaults do not change in 0.1.x. What the long form's button looks like and the keys beyond the pin's are not part of that promise: the look and place of the "More" button, the M key and the keys argument of use_hovercraft() are r lifecycle::badge("experimental"), like events and the fields of the inputs it turns on, when_disabled, marker, a style that is a tip_style(), a tip_theme() or a bslib theme, a tip_more(), and keeping more than one pin (max_pins of use_hovercraft()). They may change.

Known limits

  • A tag inside bslib::tooltip() or bslib::popover(), or with data-bs-toggle, keeps the tip that bslib draws.

  • A pin closes (reason "gone") when its item is removed, or hidden by a tab, a conditional panel or a sidebar, and it does not come back with the item. A pin whose item only scrolls out of view is hidden and returns when the item does.

  • tip_on() costs about a millisecond of R time for each tag, and the page's script some time on every change of the page: for a long table, page it.

  • Reconnecting an app (allowReconnect) is untested.

Examples

tip_on(htmltools::tags$button("Run"), "Runs the **check**")
#> <button data-hovercraft-tip="Runs the **check**">Run</button>

# The reason a control is switched off
tip_on(
  htmltools::tags$button("Run", disabled = NA),
  "Runs the **check**",
  when_disabled = "Pick a dataset first"
)
#> <span class="hovercraft-when-disabled" data-hovercraft-when-disabled="Pick a dataset first">
#>   <button disabled data-hovercraft-tip="Runs the **check**">Run</button>
#> </span>

# A tip that a reader can pin, by the button in its corner or by the P key
tip_on(
  htmltools::tags$button("Run", id = "run"),
  "Runs the **check**",
  id = "run_tip",
  pin = TRUE,
  events = c("hint", "pin")
)
#> <button id="run" data-hovercraft-tip="Runs the **check**" data-hovercraft-tip-id="run_tip" data-hovercraft-shiny-events="hint pin" data-hovercraft-tip-pin="manual" aria-keyshortcuts="P">Run</button>

# A short tip with a "More" button that opens the long form in the same tip
tip_on(
  htmltools::tags$button("Run"),
  "Runs the **check**",
  more = "Checks every column and prints a summary."
)
#> <button data-hovercraft-tip="Runs the **check**" data-hovercraft-more-held="Checks every column and prints a summary." aria-keyshortcuts="M">Run</button>

# The long form shows with the short one
tip_on(
  htmltools::tags$button("Run"),
  "Runs the **check**",
  more = tip_more("Checks every column and prints a summary.", on = "hover")
)
#> <button data-hovercraft-tip="Runs the **check**" data-hovercraft-tip-more="Checks every column and prints a summary." data-hovercraft-tip-more-on="hover">Run</button>

# A look for this tag only
tip_on(htmltools::tags$button("Run"), "Runs the **check**", style = "sober")
#> <button data-hovercraft-tip="Runs the **check**" data-hovercraft-tip-preset="sober">Run</button>
tip_on(
  htmltools::tags$button("Run"),
  "Runs the **check**",
  style = tip_theme(light = list(bg = "#1b3a6b", text = "#ffffff"))
)
#> <button data-hovercraft-tip="Runs the **check**" data-hovercraft-theme="d855b212b5f0">Run</button>

# A visible cue that a word holds a tip
tip_on(
  htmltools::tags$span("Baseline", tabindex = "0"),
  "The first **screening** value",
  marker = "underline"
)
#> <span tabindex="0" data-hovercraft-tip="The first **screening** value" data-hovercraft-marker="underline">Baseline</span>

tip_define(diff = tip_template(
  tip_title("{{ label }}"),
  tip_text("{{ differences | number:0 }} differences")
))
#> <script type="application/json" data-hovercraft-ops="">[{"op":"define","name":"diff","blocks":[{"type":"title","text":"{{ label }}"},{"type":"text","text":"{{ differences | number:0 }} differences"}]}]</script>
rows <- data.frame(label = c("ADAE", "ADSL"), differences = c(3, 0))
lapply(seq_len(nrow(rows)), function(i) {
  tip_on(
    htmltools::tags$tr(htmltools::tags$td(rows$label[[i]]), tabindex = "0"),
    template = "diff",
    record = rows[i, ]
  )
})
#> [[1]]
#> <tr tabindex="0" data-hovercraft-tip-template="diff" data-hovercraft-tip-record="{&quot;label&quot;:&quot;ADAE&quot;,&quot;differences&quot;:3}">
#>   <td>ADAE</td>
#> </tr>
#> 
#> [[2]]
#> <tr tabindex="0" data-hovercraft-tip-template="diff" data-hovercraft-tip-record="{&quot;label&quot;:&quot;ADSL&quot;,&quot;differences&quot;:0}">
#>   <td>ADSL</td>
#> </tr>
#> 

# The built-in words of the tip, with other words, for this tip
tip_on(
  htmltools::tags$button("Run"),
  "Runs the **check**",
  pin = TRUE,
  id = "run",
  labels = c(close = "Dismiss")
)
#> <button data-hovercraft-tip="Runs the **check**" data-hovercraft-tip-id="run" data-hovercraft-tip-pin="manual" data-hovercraft-labels="{&quot;close&quot;:&quot;Dismiss&quot;}" aria-keyshortcuts="P">Run</button>

# An icon-only button is named from its tip
tip_on(htmltools::tags$button(htmltools::tags$i(class = "icon-gear")), "Open the **settings**")
#> <button data-hovercraft-tip="Open the **settings**" aria-label="Open the settings">
#>   <i class="icon-gear"></i>
#> </button>