Change backdrop for a bsModal in Shiny app

1.1k Views Asked by At

I am developing a Shiny app and I need to make sure the end users won't accidentally close a bsModal, because there are some action buttons on it. I've done some research and learned I need to overwrite backdrop and keyboard parameters, but even though I've seen some suggestions, I have no idea where exactly this needs to sit in my code. I am not proficient with JavaScript and very new to Shiny, so even though it feels like a simple task, I cannot get this right.

In case anyone needs it, here's a bit of dummy code that opens a modal window after a button press; I need to prevent people from closing it by accidentally clicking in the background or hitting esc.

    library(shiny)
    library(shinyBS)

    ui <- fluidPage(

      sidebarLayout(

        sidebarPanel(
            actionButton("go", "Go")
            ,bsModal("window", "Window", "go"
                    ,textOutput("print"))
          )
        ,mainPanel()
      )

    )

    server <- function(input, output, session) {

      output$print = renderText("This is a test")

    }

    shinyApp(ui, server)

I tried to combine the solutions provided in these two threads:

Is there a way to hide/disable the `Close` button on a `bsModal` window?

Prevent Bootstrap Modal from disappearing when clicking outside or pressing escape?

to do something like this (in a few different combinations), but that didn't really work:

            actionButton("go", "Go")
            ,bsModal("window", "Window", "go"
                    ,textOutput("print")
                    ,tags$head(tags$style("#window .modal{backdrop: 'static'}")))
          )

Any help would be really appreciated!

1

There are 1 best solutions below

2
On BEST ANSWER

This will do it:

bsModalNoClose <-function(...) {
  b = bsModal(...)
  b[[2]]$`data-backdrop` = "static"
  b[[2]]$`data-keyboard` = "false"
  return(b)
}

And then you can close the header and footer as well, to prevent closing there:

bsModalNoClose("window", "Window", "go"
               ,textOutput("print"),
               tags$head(tags$style("#window .modal-footer{display:none}
                                             .modal-header{display:none}")))