Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny Modal: Having width adjust automatically to content inside

I am trying to have my modals adjust it's width based on the content inside. Depending on the modal, I will have various length buttons and I would rather not format each modal for this.

Below is my code:

library(shiny)
library (shinydashboard)

header <- dashboardHeader(title = "MRO Dash")
sidebar <- dashboardSidebar(actionButton("downloadBT", "Downloads", icon = icon("download")))

####FORMATTING MODAL HERE###
body <- dashboardBody(
  tags$head(tags$style("#test .modal-content {width: auto;}"))
  )
############################

ui <- dashboardPage(header, sidebar, body)

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

  myModal <- function() {
    div(id = "test",
      modalDialog(downloadButton("download1","Download Shipments tonight let's go"),
                  br(),
                  br(),
                  downloadButton("download2","Download Shipments"),
                  easyClose = TRUE, footer = NULL)
    )
  }

  # open modal on button click
  observeEvent(input$downloadBT,
               ignoreNULL = TRUE,   # Show modal on start up
               showModal(myModal())
  )

  output$download1 <- downloadHandler(
    filename = function(){paste("MTD of SBU Shipments ",Sys.time(), ".csv", sep = "")},
    content = function(file){write.csv(, file, row.names = FALSE)}
  )

  output$download2 <- downloadHandler(
    filename = function(){paste("MTD of SBU Shipments ",Sys.time(), ".csv", sep = "")},
    content = function(file){write.csv(, file, row.names = FALSE)}
  )

}

shinyApp(ui, server)

For modals, I understand there are 3 main classes which I've tried them all:

  • .modal-dialog: expands the box to fit the entire width of screen (as expected)

  • .modal-content: does nothing

  • .modal-body: does nothing

Not sure why {width: auto;} doesn't work for the 2 classes.

like image 627
Kevin Avatar asked Oct 10 '18 15:10

Kevin


1 Answers

I know this is old, but on the off-chance that you didn't figure it out or others are looking for an answer:

CSS:

.modal-dialog {
    width: fit-content !important;
}

Hardcoding it into your R shiny app:

####FORMATTING MODAL HERE###
body <- dashboardBody(
  tags$head(tags$style("#test .modal-dialog {width: fit-content !important;}"))
  )
############################
like image 89
waskawy_wombat Avatar answered Nov 15 '22 05:11

waskawy_wombat