3

A bsModal window in shiny app comes with a default Close button. Is there a way that can be disabled? I tried to look up on SO for similar questions, but didn't find one matching my requirements. I think, if a user can close the window using the top right corner X button, there is not really a need for another Close button. Please advise. Following reproducible code will generate a sample bsModal window to understand my question.

library(shiny)
library(shinyBS)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      actionButton("open", "Open"), #action button to trigger the modal window.
      bsModal("id1", "Box 1", "open", size = "small",
              HTML(paste("A simple modal window."))
              )
      ),

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

    }
  )
}
Sagar
  • 2,778
  • 1
  • 8
  • 16

2 Answers2

8

This should do it

library(shiny)
library(shinyBS)

if(interactive()){
  shinyApp(
    ui <- fluidPage(
      actionButton("open", "Open"), #action button to trigger the modal window.
      bsModal("id1", "Box 1", "open", size = "small",
              HTML(paste("A simple modal window.")),
              tags$head(tags$style("#id1 .modal-footer{ display:none}"))
      )
    ),

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

    }
  )
}
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • Awesome. Exactly what I was looking for. Didn't know we could use `tags` to achieve this. Thanks for this great solution. – Sagar Sep 15 '17 at 14:14
  • Please accept the answer if this is what you needed – Pork Chop Sep 15 '17 at 14:18
  • I am trying to do that but it is showing some error (asking to me to try again later). Same thing when I try to upvote. I am assuming I have rights to accept/upvote. – Sagar Sep 15 '17 at 14:19
1

Alternatively to @PorkChop's solution, you can write the modal without shinyBS:

library(shiny)

shinyApp(
  ui <- fluidPage(
    tags$button(class="btn btn-default", 
                "data-toggle"="modal", "data-target"="#simplemodal",
                "Open modal"),
    tags$div(
      id = "simplemodal",
      class="modal fade", role="dialog",
      tags$div(
        class="modal-dialog",
        tags$div(
          class="modal-content",
          #### Header ####
          tags$div(
            class="modal-header",
            tags$button(
              type="button", class="close", "data-dismiss"="modal",
              HTML("&times;")
            )
          ),
          #### Body ####
          tags$div(
            class="modal-body",
            HTML("A simple modal window")
          ),
          #### Footer (remove it if you want) ####
          tags$div(
            class="modal-footer",
            tags$button(
              type="button", class="btn btn-default", "data-dismiss"="modal",
              "Close"
            )
          )
        )
      )
    )
  ),

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

  }
)
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225
  • Thanks for sharing alternative. My app currently uses `shinyBS`, so will stick with the first solution. – Sagar Sep 18 '17 at 20:44
  • Note that `bsModal` preserve the state of inputs inside it. If you need inputs preserved after closing/reopening, go with `bsModal`. – tyluRp Sep 17 '20 at 22:07