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
One HTML tag, such as
htmltools::tags$tr()orshiny::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 avalue:nav_panel(title = tip_on(span("Data"), "..."), value = "data"). Withoutvalue, bslib writes the tag's HTML into the tab's value.- text
One string of markdown (strong, emphasis, code, links, short lists), or
NULL. It is shown as written: build it withpaste0()orsprintf().NAand""count as none: the tag comes back without a tip. Whenrecordis given, the{{ field }}placeholders in the text are filled from it (as in a template, with one filter at most,number:norpercent:n), also whentemplateis the name of a defined template, which is then not used; whentemplateis atip_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 noaria-label, because they depend on the record.- ...
Must be empty. It makes a misspelt argument an error.
- template
The name of a template defined with
tip_define(), atip_template()made here, orNULL. Atip_template()travels with the tag, so it needs notip_define(); its fields are checked againstrecord. Iftextis given too, the text is the tip and the template is not used (seetextfor its placeholders).- record
A named list, or a data frame of one row: the values the template's placeholders read, one value each.
NAandNULLshow a dash. Dates are written asYYYY-MM-DD, times in UTC asYYYY-MM-DDTHH:MM:SSZand factors as their labels. Oncetip_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 withdata[i, c("label", "n")].- style
as a preset name,
as a style object. How this tag's tip looks, or
NULL(the default) to take the page's. A preset name fromtip_presets()("none"clears a preset set above), atip_style(), or one part of a style: atip_theme()or abslib::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;(styletakes no CSS); seetip_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 touse_hovercraft(), or use the roles oftip_theme().- placement
Where the tip opens:
"auto","top","bottom","left"or"right", orNULL(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
A visible cue that the tag holds a tip.
NULL(the default) and"none"draw none; the others are"underline","icon"or"corner", atip_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")istip_marker("icon", icon = ...)). Seetip_marker()for what each draws. For the cells of a table, use themarkerargument oftip_table().- id
A Shiny id for this tip, or
NULL(the default) for none: one non-empty string with no whitespace and no:, sons("baseline_tip")works in a module. Give it a different id from every other tip source on the page.tip_show(),tip_hide()andtip_update()find the tip by it, andeventsnames the inputs after it.- events
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 anid, or a tag with anidof 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 apinstops with an error. Each is a list that holds the fields below."hint"sendsinput$<id>_hinteach time the tip opens or closes:state("shown"or"hidden"),text(the tip's plain words, cut at 200 characters),by("reader","host"fortip_show()andtip_hide(), or"data"when the tag left the page),via("pointer","focus","tap","key","command", orNULL) andreason(why it closed,NULLon"shown":"leave","blur","escape","outside","pinned"when the hint became a pin,"replaced","gone"or"host")."pin"sendsinput$<id>_pineach 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", orNULL) andreason(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 (byis"data");NULLon"pinned"). A"state"of"updated"is not a pin: an app that tests only for"closed"should check it."pin"also keepsinput$<id>_pinned,TRUEwhile the tip is pinned andFALSEafter, sent in the same batch as_pinand as a plain value, so an equal value does not fire an observer again; read it withisTRUE(input$run_pinned). The values ofstatemay grow in a later version, so an app should not assume it knows them all. Every one of these inputs isNULLuntil its first event; inshiny::testServer()set them withsession$setInputs(), and inshinytest2withset_inputs(..., allow_no_input_binding_ = TRUE). On a page without Shiny (R Markdown, Quarto, a static page)eventsdoes nothing."copy"sendsinput$<id>_copyeach 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(TRUEwhen the browser let the copy happen),by("reader") andvia("click"or"key").- when_disabled
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 (textortemplate) is shown as usual.textandtemplatemay be left out, for a tag that only has a reason; then amarkeris 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: abutton,input,select,textareaorfieldset. It works when the page is built with the control disabled and when it is switched off later, by theupdate*(disabled = )functions of Shiny or by script, on a Bootstrap 5 page (bslib) and on the Bootstrap 3 page of a plainfluidPage(); a control disabled byaria-disabledalone 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 thedivShiny 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'sidandeventsstay on the control, and the wrapper answers to them while the control is disabled, sotip_show()and the hint input name the control either way. The value returned is the wrapper; callingtip_on()on it again replaces the tip whole, the reason included, and keeps one wrapper when a newwhen_disabledis 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 classesw-25,w-50,w-75,w-100,flex-fill,flex-grow-*,col-*,order-*,align-self-*andms-auto, move to the wrapper, and the control fills the wrapper. Ad-blockcontrol and the cells of ad-gridkeep their width. Not covered: a control that is a direct child of abtn-group,btn-group-verticalorinput-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
as words or a template,
as a
tip_more(). Extra content for the long form of the tip, orNULL(the default) for none: words (markdown, optionally with{{ field }}placeholders filled fromrecord), atip_template(), or atip_more()that also says when the long form shows.NAand""count as none. Words and a template are the same astip_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 noid. Seetip_more()for"hover","pin","click"(a click on the item opens it; not on a control),pin = TRUE(the button pins the tip too),asandwhere(a text link, and the side the control sits on) and the buttons' words; with"pin"orpin = TRUEthe tag must be able to pin and have anid(eitherid =or the tag's own HTMLid), otherwisetip_on()stops with an error.- pin
Whether a reader can keep this tip open, one at a time:
NULL(the default) orFALSEfor a tip that only shows on hover or focus (FALSEis "never", whatever a level above says),TRUEfor 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. UseTRUEthere: a plain button and adivaround an input take it. WithTRUE, 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 withtip_code(copy = TRUE)shows its Copy button there. A pin needs anid(theidargument or the tag's own), which names the tip inevents, and a tag that the engine can pin: not a link, a submit or reset button, aninput,select,textarea,summary,labelor 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 asaria-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 ortip_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 withuse_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.Escapecloses a pinned tip. Pointer, keyboard and tap are tested in Chrome only.- labels
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 (seeuse_hovercraft()). A named character vector, such asc(close = "Dismiss"); a word left out keeps its built-in one. The keys, with their built-in words, arecopy(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:pinnedtakes{label}(the name of the item),code_linetakes{first}(the first line of the code) andcode_linesandcode_lines_emptytake{n}(the number of lines;code_linestakes{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'slabelsbeat the page's, and a tag inside another tag withlabelsuses the nearer one.tip_more(label = )still sets the words of the "More" and "Less" buttons of one tip, and beatslabelsthere. The words go with the tag, solabelsneeds a tip on the call (text,templateorwhen_disabled); a secondtip_on()on the tag replaces them with its own. Withwhen_disabled, they are also on the wrapper, so "Not available:" changes while the control is off. For atip_table(), give the words touse_hovercraft(): a table is marked by its own script, and only a tag that has a tip of its own can carrylabelsaround 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()orbslib::popover(), or withdata-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.
See also
Other tooltips:
tip_blocks,
tip_define(),
tip_marker(),
tip_more(),
tip_output(),
tip_presets(),
tip_server,
tip_spec(),
tip_style(),
tip_table(),
tip_template(),
tip_theme(),
use_hovercraft()
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="{"label":"ADAE","differences":3}">
#> <td>ADAE</td>
#> </tr>
#>
#> [[2]]
#> <tr tabindex="0" data-hovercraft-tip-template="diff" data-hovercraft-tip-record="{"label":"ADSL","differences":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="{"close":"Dismiss"}" 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>