-
Notifications
You must be signed in to change notification settings - Fork 82
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
Draft
cpsievert
wants to merge
3
commits into
glin:main
Choose a base branch
from
cpsievert:bslib-themability
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
supplyBsThemeDefaults <- function(instance) { | ||
if (system.file(package = "bslib") == "") { | ||
return(instance) | ||
} | ||
theme <- bslib::bs_current_theme() | ||
if (!bslib::is_bs_theme(theme)) { | ||
return(instance) | ||
} | ||
# If a bslib theme is relevant, supply new reactableTheme() defaults | ||
# based on the relevant Bootstrap Sass variables | ||
themeVars <- getThemeVars(theme) | ||
for (x in names(themeVars)) { | ||
instance$x$tag$attribs$theme[[x]] <- | ||
instance$x$tag$attribs$theme[[x]] %||% themeVars[[x]] | ||
} | ||
styleVals <- getStyleVals(theme) | ||
for (x in names(styleVals)) { | ||
vals <- styleVals[[x]] | ||
if (isTRUE(is.na(vals))) next # failed to parse Sass rules | ||
instance$x$tag$attribs$theme[[x]] <- utils::modifyList( | ||
vals, instance$x$tag$attribs$theme[[x]] %||% list() | ||
) | ||
} | ||
|
||
instance | ||
} | ||
|
||
|
||
getThemeVars <- function(theme) { | ||
map <- if (is_bs3(theme)) bsVariableMap3 else bsVariableMap | ||
vars <- bslib::bs_get_variables(theme, as.character(map)) | ||
vars <- setNames(vars, names(map)) | ||
vars[!is.na(vars)] | ||
} | ||
|
||
# Map the non-style reactableTheme() settings to main Bootstrap Sass variables | ||
bsVariableMap <- c( | ||
color = "table-color", | ||
borderColor = "table-border-color", | ||
borderWidth = "table-border-width", | ||
stripedColor = "table-accent-bg", | ||
highlightColor = "primary", | ||
cellPadding = "table-cell-padding" | ||
) | ||
|
||
bsVariableMap3 <- c( | ||
color = "text-color", | ||
borderColor = "table-border-color", | ||
stripedColor = "table-bg-accent", | ||
highlightColor = "brand-primary", | ||
cellPadding = "table-cell-padding" | ||
) | ||
|
||
|
||
getStyleVals <- function(theme) { | ||
lapply(bsStyleMap, computeStyles, theme = theme) | ||
} | ||
|
||
computeStyles <- function(x, theme) { | ||
# Handle BS3isms (without requiring a different bsStyleMap) | ||
if (is_bs3(theme)) { | ||
theme <- bslib::bs_add_variables( | ||
theme, "input-border-width" = "1px", | ||
"pagination-border-width" = "1px", | ||
"pagination-border-color" = "$pagination-border", | ||
"pagination-hover-border-color" = "$pagination-hover-border", | ||
"pagination-active-border-color" = "$pagination-active-border", | ||
.where = "declarations" | ||
) | ||
} | ||
# Try to compile the Sass rules. Note that an error could happen | ||
# if Bootstrap Sass variables change in future versions. | ||
# (In that case, we'll need to update accordingly to support BS5+) | ||
prop_string <- paste0(names(x), ":", x, collapse = ";") | ||
res <- try( | ||
sass::sass_partial( | ||
paste0(".fake-selector{", prop_string, "}"), | ||
theme, options = sass::sass_options(output_style = "compressed") | ||
), | ||
silent = TRUE | ||
) | ||
if (inherits(res, "try-error")) { | ||
warning( | ||
"Failed to compute the following Sass rule(s) '", prop_string, "'. ", | ||
"{reactable}'s theming defaults may not reflect the {bslib} theme.", | ||
call. = FALSE | ||
) | ||
return(NA) | ||
} | ||
matches <- regmatches(res, regexec(".fake-selector\\s*\\{(.+)\\}", res)) | ||
asReactStyle(matches[[1]][2]) | ||
} | ||
|
||
bsStyleMap <- list( | ||
style = list( | ||
fontFamily = "$font-family-base", | ||
backgroundColor = "if($table-bg==null or alpha($table-bg)==0, $body-bg, $table-bg)" | ||
), | ||
headerStyle = list( | ||
fontFamily = "$headings-font-family" | ||
), | ||
rowHighlightStyle = list( | ||
color = "color-contrast($primary)" | ||
), | ||
inputStyle = list( | ||
color = "$input-color", | ||
backgroundColor = "$input-bg", | ||
border = "$input-border-width solid $input-border-color" | ||
), | ||
pageButtonStyle = list( | ||
color = "$pagination-color", | ||
backgroundColor = "$pagination-bg", | ||
border = "$pagination-border-width solid $pagination-border-color" | ||
), | ||
pageButtonHoverStyle = list( | ||
color = "$pagination-hover-color", | ||
backgroundColor = "$pagination-hover-bg", | ||
border = "$pagination-border-width solid $pagination-hover-border-color" | ||
), | ||
pageButtonActiveStyle = list( | ||
color = "$pagination-active-color", | ||
backgroundColor = "$pagination-active-bg", | ||
border = "$pagination-border-width solid $pagination-active-border-color" | ||
), | ||
pageButtonCurrentStyle = list( | ||
color = "$pagination-active-color", | ||
backgroundColor = "$pagination-active-bg", | ||
border = "$pagination-border-width solid $pagination-active-border-color" | ||
) | ||
) | ||
|
||
is_bs3 <- function(theme) { | ||
"3" %in% bslib::theme_version(theme) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 ofreactable()
? I guess I'm just generally wondering whatpreRenderHook
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.
Yes,
preRenderHook
gets called at print-time, just before the widget is serialized into JSON. When callingbs_current_theme()
at print-time, we can have some guarantee of knowing when a{bslib}
theme has been provided to a page layout function likefluidPage()
: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.