Skip to contents

Turn the mascot into a button for one action of your Shiny app, such as opening a help panel or a chat. Pass it as click to mascot() or update_mascot(), and handle the click in your server with shiny::observeEvent(input$<id>_click, ...). For a choice of several actions, use mascot_popup_menu() instead.

Usage

mascot_action(label = NULL, ..., tooltip = TRUE, disabled = FALSE)

Arguments

label

The action's name: one string, not blank, that says what a click does, such as "Open help". Screen readers announce the mascot as a button with this name, and the tooltip shows it. NULL (the default) makes an empty helper: in update_mascot(), it removes the click, so the mascot is a picture again. With label = NULL, setting tooltip or disabled yourself is an error; use mascot_action() without options to remove the click.

...

Must be empty. Forces the arguments after it to be named.

tooltip

A flag. TRUE (the default) shows the label in a tooltip when the pointer rests on the mascot or the keyboard focuses it. FALSE hides the tooltip; screen readers still announce the label.

disabled

A flag. TRUE keeps the mascot a button that does nothing: clicks are not sent, and screen readers announce the button as unavailable. Defaults to FALSE. Use it while the action makes no sense, such as during a long task, and send mascot_action(..., disabled = FALSE) afterwards.

Value

A mascot_action object. Its fields are internal; pass it on rather than reading them.

Details

What a click sends. Every activation of the button sets input$<id>_click to the number of activations since the mascot appeared, so shiny::observeEvent() runs each time. An activation is a click or tap, Enter or Space while the mascot has keyboard focus, or a screen reader's activate command. Holding a key down counts once. Opening and closing a mascot_popup_menu() never sets this input. Dragging a floating mascot, clicking elsewhere, pressing Escape, the minimise and restore buttons and updates from the server never count. In a module, use ns("helper") in the UI and read input$helper_click in that module's server.

Without a click. The default, click = NULL in mascot(), keeps the mascot a picture: keyboard focus skips it and it sends no clicks. Use a click only when it does something, so keyboard users never land on a control that does nothing.

The tooltip shows the label next to the mascot, on the side with room. It stays while the pointer is over the mascot or the tooltip, and Escape hides it without moving the focus. It never shows on touch screens or while a menu or bubble is open. On static pages the mascot stays a picture.

Examples

mascot_action("Open help")
#> mascot_action("Open help")
mascot_action("Open help", tooltip = FALSE)
#> mascot_action("Open help", tooltip = FALSE)
mascot_action()
#> mascot_action()
ui <- mascot("helper", click = "Open help")
if (FALSE) { # interactive()
shiny::shinyApp(
  shiny::fluidPage(mascot("helper", click = "Open help")),
  function(input, output, session) {
    shiny::observeEvent(input$helper_click, {
      shiny::showModal(shiny::modalDialog("Help for this app"))
    })
  }
)
}