rstudio / bslib

Tools for theming Shiny and R Markdown via Bootstrap 3, 4, or 5.
https://rstudio.github.io/bslib/
Other
443 stars 52 forks source link

cant update active/selected row background color for datatables #811

Open ncullen93 opened 9 months ago

ncullen93 commented 9 months ago

Updating table-active-bg has no effect for preset of "shiny" or "bootstrap", while table-color does work, for instance. See reprex:

library(shiny)
library(DT)

my_theme <- bslib::bs_theme(
  version = 5
)

my_theme <- bslib::bs_add_variables(
  my_theme,
  'table-active-bg' = 'red',
  'table-color' = 'purple'
)

ui <- bslib::page_navbar(
  theme=my_theme,
  bslib::nav_panel('test', DT::DTOutput('table'))
)

server <- function(input, output, session) {
  output$table <- DT::renderDT({
    mtcars
  })
}

shinyApp(ui, server)

Noticing also that there may be mixed use of selected and active classes when selecting datatable rows. Not sure which is the right one. I'm using the following code from tables/_rules.scss as reference to make me think I should update table-active-bg:

  tbody td.active,
  tbody tr.active td {
    background-color: var(--bs-table-active-bg);
  }
ncullen93 commented 9 months ago

Apparently datatables has moved to using box-shadow for selected rows. This works:

my_theme <- bslib::bs_add_rules(
  my_theme,
  rules = sass::as_sass('table.display.dataTable > tbody > tr.selected > *{
    box-shadow: inset 0 0 0 9999px lightgray !important;
    color: black;
}')
)
gadenbuie commented 9 months ago

What may be happening here is that the latest version of DT will now correctly recognize that bslib is using Bootstrap 5 and will automatically apply the BS5 styles to datatables (where previously it was using BS4 styles). I vaguely remember seeing that the newer BS5 styles used box-shadow.

Here are the rules I've used to change the default datatables styles for bs_theme(preset = "shiny"), you might find some interesting things here:

https://github.com/rstudio/bslib/blob/ad946cafdbf1c91cfd714cb39948bb203ae66e66/inst/builtin/bs5/shiny/tables/_rules.scss#L9-L35

ncullen93 commented 9 months ago

Thanks! I would expect something like this to work, but it has no effect:

  my_theme <- bslib::bs_theme(
    version = 5,
    preset = 'shiny'
  )

  my_theme <- bslib::bs_add_variables(
    my_theme,
    "table-striped-bg" = "red",
    "table-active-bg" = "red"
  )

Changing table-hover-bg does work, however. And note that the table-hover-bg doesnt work when the preset is "bootstrap".

gadenbuie commented 9 months ago

Yeah, only the bs_theme(preset = "shiny") uses the var(--bs-table-*-bg) CSS variables which take their values from their Sass counterparts.

The rules here in bslib are essentially a patch for behavior that should be implemented upstream in DT. I opened an issue there that covers some of this behavior, I'll add a note there: https://github.com/rstudio/DT/issues/1081