-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supply smarter theming defaults if a {bslib} theme is active #96
base: main
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #96 +/- ##
==========================================
- Coverage 99.17% 99.02% -0.16%
==========================================
Files 15 9 -6
Lines 1823 1125 -698
Branches 335 335
==========================================
- Hits 1808 1114 -694
+ Misses 15 11 -4
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay - this is awesome! I've wanted to create a Bootstrap theme for reactable before, but this is much easier and more flexible than what I was thinking of.
My only hesitation is with applying the bslib theme by default, since reactable isn't a Bootstrap themed table. How about making the bslib theme opt-in, like having the theme
argument optionally take a bslib theme:
library(bslib)
theme <- bs_theme(
bg = "#202123", fg = "#B8BCC2", primary = "#EA80FC",
base_font = font_google("Grandstander")
)
# Theme an individual table
reactable(mtcars, theme = theme)
# Or all tables
option(reactable.theme = bslib::bs_current_theme())
You wouldn't be able to combine this with a reactableTheme()
, but I think that would be ok for now. bs_theme()
can theme most of the table already, and a future addition could make it possible to combine or merge themes.
width = width, | ||
height = height, | ||
package = "reactable", | ||
dependencies = dependencies, | ||
elementId = elementId | ||
elementId = elementId, | ||
preRenderHook = function(instance) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason to set the theme in preRenderHook
instead of reactable()
? I guess I'm just generally wondering what preRenderHook
is used for, since there's not much documentation about it out there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a specific reason to set the theme in preRenderHook instead of reactable()?
Yes, preRenderHook
gets called at print-time, just before the widget is serialized into JSON. When calling bs_current_theme()
at print-time, we can have some guarantee of knowing when a {bslib}
theme has been provided to a page layout function like fluidPage()
:
library(shiny)
ui <- fluidPage(
theme = bslib::bs_theme(),
verbatimTextOutput("txt")
)
server <- function(input, output, session) {
output$txt <- renderPrint(bslib::bs_current_theme())
}
shinyApp(ui, server)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh gotcha, that makes sense.
I understand the hesitation, but keep in mind that this implementation (i.e., retrieving How about, as some sort of compromise, we have reactable <- function(..., theme = getOption("reactable.theme", "auto")) {} |
I like the idea of an opt-out, but am still on the fence about applying the For now, I'd rather keep it opt-in and wait for user feedback. If the vast majority of users opt in and it becomes a pain, then we could change it to apply by default. Setting the |
Keep in mind that existing code, as well as non- |
|
Hi @glin, would you have the time and interest to resurrect this if we made this an opt-in feature (similar to what you originally proposed)? |
Hey @cpsievert - yep, still interested, and I still think it'd be safer as an opt-in. For example, I'm now using the BS5 theme in the pkgdown docs and wouldn't want to opt out in every example. A middle ground might be to provide a simple I don't think I'll have time to work on this any time soon though, because of general lack of time and a few other priorities to work on right now. |
This PR allows
reactable()
to supply smarterreactableTheme()
defaults when a{bslib}
theme is active at render time. For example:Since the widget calls
bslib::bs_current_theme()
inside it'spreRenderHook
, it'll automatically re-render with new theming defaults whenever the{bslib}
theme changes viabslib::bs_themer()
(or more generally, whenever the newsession$setCurrentTheme()
is called). This generally just works as long thereactable()
passed throughrenderReactable()
:This approach will also generally 'just work' in
{rmarkdown}
(when{bslib}
is being used to provide Bootstrap CSS) once rstudio/rmarkdown#1706 landsOutside of Shiny and R Markdown, this can also work in static HTML sites via global
{bslib}
themes: