| 1 |
#' Show or remove a modal dialog |
| 2 |
#' |
| 3 |
#' This causes a modal dialog to be displayed in the client browser, and is |
| 4 |
#' typically used with [modalDialog()]. |
| 5 |
#' |
| 6 |
#' @param ui UI content to show in the modal. |
| 7 |
#' @param session The `session` object passed to function given to |
| 8 |
#' `shinyServer`. |
| 9 |
#' |
| 10 |
#' @seealso [modalDialog()] for examples. |
| 11 |
#' @export |
| 12 |
showModal <- function(ui, session = getDefaultReactiveDomain()) { |
| 13 |
res <- processDeps(ui, session) |
| 14 |
|
| 15 |
session$sendModal("show", |
| 16 |
list( |
| 17 |
html = res$html, |
| 18 |
deps = res$deps |
| 19 |
) |
| 20 |
) |
| 21 |
} |
| 22 |
|
| 23 |
#' @rdname showModal |
| 24 |
#' @export |
| 25 |
removeModal <- function(session = getDefaultReactiveDomain()) { |
| 26 |
session$sendModal("remove", NULL) |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
#' Create a modal dialog UI |
| 31 |
#' |
| 32 |
#' @description |
| 33 |
#' `modalDialog()` creates the UI for a modal dialog, using Bootstrap's modal |
| 34 |
#' class. Modals are typically used for showing important messages, or for |
| 35 |
#' presenting UI that requires input from the user, such as a user name and |
| 36 |
#' password input. |
| 37 |
#' |
| 38 |
#' `modalButton()` creates a button that will dismiss the dialog when clicked, |
| 39 |
#' typically used when customising the `footer`. |
| 40 |
#' |
| 41 |
#' @inheritParams actionButton |
| 42 |
#' @param ... UI elements for the body of the modal dialog box. |
| 43 |
#' @param title An optional title for the dialog. |
| 44 |
#' @param footer UI for footer. Use `NULL` for no footer. |
| 45 |
#' @param size One of `"s"` for small, `"m"` (the default) for medium, |
| 46 |
#' `"l"` for large, or `"xl"` for extra large. Note that `"xl"` only |
| 47 |
#' works with Bootstrap 4 and above (to opt-in to Bootstrap 4+, |
| 48 |
#' pass [bslib::bs_theme()] to the `theme` argument of a page container |
| 49 |
#' like [fluidPage()]). |
| 50 |
#' @param easyClose If `TRUE`, the modal dialog can be dismissed by |
| 51 |
#' clicking outside the dialog box, or be pressing the Escape key. If |
| 52 |
#' `FALSE` (the default), the modal dialog can't be dismissed in those |
| 53 |
#' ways; instead it must be dismissed by clicking on a `modalButton()`, or |
| 54 |
#' from a call to [removeModal()] on the server. |
| 55 |
#' @param fade If `FALSE`, the modal dialog will have no fade-in animation |
| 56 |
#' (it will simply appear rather than fade in to view). |
| 57 |
#' |
| 58 |
#' @examples |
| 59 |
#' if (interactive()) { |
| 60 |
#' # Display an important message that can be dismissed only by clicking the |
| 61 |
#' # dismiss button. |
| 62 |
#' shinyApp( |
| 63 |
#' ui = basicPage( |
| 64 |
#' actionButton("show", "Show modal dialog") |
| 65 |
#' ), |
| 66 |
#' server = function(input, output) { |
| 67 |
#' observeEvent(input$show, { |
| 68 |
#' showModal(modalDialog( |
| 69 |
#' title = "Important message", |
| 70 |
#' "This is an important message!" |
| 71 |
#' )) |
| 72 |
#' }) |
| 73 |
#' } |
| 74 |
#' ) |
| 75 |
#' |
| 76 |
#' |
| 77 |
#' # Display a message that can be dismissed by clicking outside the modal dialog, |
| 78 |
#' # or by pressing Esc. |
| 79 |
#' shinyApp( |
| 80 |
#' ui = basicPage( |
| 81 |
#' actionButton("show", "Show modal dialog") |
| 82 |
#' ), |
| 83 |
#' server = function(input, output) { |
| 84 |
#' observeEvent(input$show, { |
| 85 |
#' showModal(modalDialog( |
| 86 |
#' title = "Somewhat important message", |
| 87 |
#' "This is a somewhat important message.", |
| 88 |
#' easyClose = TRUE, |
| 89 |
#' footer = NULL |
| 90 |
#' )) |
| 91 |
#' }) |
| 92 |
#' } |
| 93 |
#' ) |
| 94 |
#' |
| 95 |
#' |
| 96 |
#' # Display a modal that requires valid input before continuing. |
| 97 |
#' shinyApp( |
| 98 |
#' ui = basicPage( |
| 99 |
#' actionButton("show", "Show modal dialog"), |
| 100 |
#' verbatimTextOutput("dataInfo") |
| 101 |
#' ), |
| 102 |
#' |
| 103 |
#' server = function(input, output) { |
| 104 |
#' # reactiveValues object for storing current data set. |
| 105 |
#' vals <- reactiveValues(data = NULL) |
| 106 |
#' |
| 107 |
#' # Return the UI for a modal dialog with data selection input. If 'failed' is |
| 108 |
#' # TRUE, then display a message that the previous value was invalid. |
| 109 |
#' dataModal <- function(failed = FALSE) { |
| 110 |
#' modalDialog( |
| 111 |
#' textInput("dataset", "Choose data set", |
| 112 |
#' placeholder = 'Try "mtcars" or "abc"' |
| 113 |
#' ), |
| 114 |
#' span('(Try the name of a valid data object like "mtcars", ', |
| 115 |
#' 'then a name of a non-existent object like "abc")'), |
| 116 |
#' if (failed) |
| 117 |
#' div(tags$b("Invalid name of data object", style = "color: red;")), |
| 118 |
#' |
| 119 |
#' footer = tagList( |
| 120 |
#' modalButton("Cancel"), |
| 121 |
#' actionButton("ok", "OK") |
| 122 |
#' ) |
| 123 |
#' ) |
| 124 |
#' } |
| 125 |
#' |
| 126 |
#' # Show modal when button is clicked. |
| 127 |
#' observeEvent(input$show, { |
| 128 |
#' showModal(dataModal()) |
| 129 |
#' }) |
| 130 |
#' |
| 131 |
#' # When OK button is pressed, attempt to load the data set. If successful, |
| 132 |
#' # remove the modal. If not show another modal, but this time with a failure |
| 133 |
#' # message. |
| 134 |
#' observeEvent(input$ok, { |
| 135 |
#' # Check that data object exists and is data frame. |
| 136 |
#' if (!is.null(input$dataset) && nzchar(input$dataset) && |
| 137 |
#' exists(input$dataset) && is.data.frame(get(input$dataset))) { |
| 138 |
#' vals$data <- get(input$dataset) |
| 139 |
#' removeModal() |
| 140 |
#' } else { |
| 141 |
#' showModal(dataModal(failed = TRUE)) |
| 142 |
#' } |
| 143 |
#' }) |
| 144 |
#' |
| 145 |
#' # Display information about selected data |
| 146 |
#' output$dataInfo <- renderPrint({ |
| 147 |
#' if (is.null(vals$data)) |
| 148 |
#' "No data selected" |
| 149 |
#' else |
| 150 |
#' summary(vals$data) |
| 151 |
#' }) |
| 152 |
#' } |
| 153 |
#' ) |
| 154 |
#' } |
| 155 |
#' @export |
| 156 |
modalDialog <- function(..., title = NULL, footer = modalButton("Dismiss"), |
| 157 |
size = c("m", "s", "l", "xl"), easyClose = FALSE, fade = TRUE) { |
| 158 |
|
| 159 |
size <- match.arg(size) |
| 160 |
|
| 161 |
backdrop <- if (!easyClose) "static" |
| 162 |
keyboard <- if (!easyClose) "false" |
| 163 |
div( |
| 164 |
id = "shiny-modal", |
| 165 |
class = "modal", |
| 166 |
class = if (fade) "fade", |
| 167 |
tabindex = "-1", |
| 168 |
`data-backdrop` = backdrop, |
| 169 |
`data-bs-backdrop` = backdrop, |
| 170 |
`data-keyboard` = keyboard, |
| 171 |
`data-bs-keyboard` = keyboard, |
| 172 |
|
| 173 |
div( |
| 174 |
class = "modal-dialog", |
| 175 |
class = switch(size, s = "modal-sm", m = NULL, l = "modal-lg", xl = "modal-xl"), |
| 176 |
div(class = "modal-content", |
| 177 |
if (!is.null(title)) div(class = "modal-header", |
| 178 |
tags$h4(class = "modal-title", title) |
| 179 |
), |
| 180 |
div(class = "modal-body", ...), |
| 181 |
if (!is.null(footer)) div(class = "modal-footer", footer) |
| 182 |
) |
| 183 |
), |
| 184 |
# jQuery plugin doesn't work in Bootstrap 5, but vanilla JS doesn't work in Bootstrap 4 :sob: |
| 185 |
tags$script(HTML( |
| 186 |
"if (window.bootstrap && !window.bootstrap.Modal.VERSION.match(/^4\\./)) { |
| 187 |
var modal = new bootstrap.Modal(document.getElementById('shiny-modal')); |
| 188 |
modal.show(); |
| 189 |
} else { |
| 190 |
$('#shiny-modal').modal().focus(); |
| 191 |
}" |
| 192 |
)) |
| 193 |
) |
| 194 |
} |
| 195 |
|
| 196 |
#' @export |
| 197 |
#' @rdname modalDialog |
| 198 |
modalButton <- function(label, icon = NULL) { |
| 199 |
tags$button( |
| 200 |
type = "button", |
| 201 |
class = "btn btn-default", |
| 202 |
`data-dismiss` = "modal", |
| 203 |
`data-bs-dismiss` = "modal", |
| 204 |
validateIcon(icon), label |
| 205 |
) |
| 206 |
} |