daattali / shinyjs

💡 Easily improve the user experience of your Shiny apps in seconds
https://deanattali.com/shinyjs
Other
734 stars 119 forks source link

Feature request - add more documentation/examples for addCssClass / removeCssClass / toggleCssClass #256

Closed jzadra closed 1 year ago

jzadra commented 2 years ago

I'm struggling a bit with the add/remove/toggle class/Cssclass functions. It would be great to have a bit more documentation regarding the arguments and a few other things:

In the documentation for these, the following example is given:

if (interactive()) {
  library(shiny)

  shinyApp(
    ui = fluidPage(
      useShinyjs(),  # Set up shinyjs
      # Add a CSS class for red text colour
      inlineCSS(list(.red = "background: red")),
      actionButton("btn", "Click me"),
      p(id = "element", "Watch what happens to me")
    ),
    server = function(input, output) {
      observeEvent(input$btn, {
        # Change the following line for more examples
        toggleClass("element", "red")
      })
    }
  )
}

The inclineCSS definitions apply to the top level css for background (or text if color: white;) is added, but it isn't clear how to specify a specific class like a panel. Further documentation on how to change more specific css classes would be helpful.

For context, thishis is coming up because I have an absolutePanel for which I want to change the background color. I have it set to a white background along with some other properties in my styles.css. I have been unable to affect a change in background color using a variety of versions of calls to add/remove/toggle.

UI excerpts:

useShinyjs(),
  tags$head(includeCSS("styles.css")),
   inlineCSS(list(.panel_dark = ".panel-default {
                 background-color: grey !important;
}"
                 )),

#...
absolutePanel(class = "panel-default", fixed = TRUE,
                draggable = T, top = 20, left = "auto", right = 20, bottom = "auto",
                width = 400, height = "auto",

                h4(id = "medoid_element", "Cluster Medians"),

                plotlyOutput("fingerprint_plot"),

  )

styles.css:

.panel-default {
  background-color: white;
padding: 0 20px 20px 20px;
cursor: move;
  opacity: 0.85;
zoom: 0.9;
transition: opacity 500ms 1s;
}

server:

observe({
   toggleClass(class = "panel_dark", condition = input$themeToggle)
})

I have tried numerous variations of this including setting an ID for the panels. It just isn't clear where the connection is between the inlineCSS definition, the call to toggleCss as far as the class argument, and whether or not to include the default (ie pre-toggle state) of the panel in styles.css or elsewhere.

daattali commented 1 year ago

The css definitions are added to the entire page, not to a specific element. You can target specific elements with your CSS rule, but the CSS rule itself will work on anything in the page that matches the selector. I looked at the documentation again now and I do think it's quite comprehensive, you may benefit from some CSS tutorials if it's still unclear.

jzadra commented 1 year ago

Thanks for the detailed response! I do need to learn more CSS.