rstudio / shiny

Easy interactive web applications with R
https://shiny.posit.co/
Other
5.37k stars 1.87k forks source link

session$setCurrentTheme should throw an error when trying to dynamically update to a different bootstrap version. #3202

Closed nstrayer closed 3 years ago

nstrayer commented 3 years ago

Issue

If you're using session$setCurrentTheme() and bslib::bs_theme() together to do dynamic theming, if there is a version mismatch between the original theme and the newly updated dynamic one (such as forgetting to set version in the update code) the theme change will silently change.

Proposed fix

Check to make sure that a dynamic update does not try and change the bootstrap version and throw an error if it does.

Demo app

The following demonstrates the problem. Switching the theme won't work when the version is set to 4 because the initial chosen version was 3. Once you switch the version to 3, then the bootswatch changing starts working.

Steps to reproduce:

app.R

library(shiny)
library(bslib)

# Setting version to 3 here
my_theme <- bs_theme(bootswatch = "darkly", version = "3")
ui <- fluidPage(
  theme = my_theme,
  radioButtons("current_theme", "Choose a theme for bslib",
               choices = c("darkly", "flatly")),
  radioButtons("bs_version", "Bootstrap version", choices = c("4", "3")),
)

server <- function(input, output, session) {
  observe({
    session$setCurrentTheme(
      bs_theme(bootswatch = input$current_theme, version = input$bs_version)
    )
  })
}

shinyApp(ui, server)
cpsievert commented 3 years ago

Closed via #3210