Skip to content
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

refactor(theme-preset): Streamline S3 classes, store theme info in sass variables #644

Merged
merged 24 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c1464d1
refactor(bs_preset_bundle): Use S3 dispatch for bundle types
gadenbuie Jun 20, 2023
a62975b
feat(preset): Prioritze `preset = "bootstrap"` over `preset = "default"`
gadenbuie Jun 20, 2023
4f30b76
feat(preset): Use sass variables to store preset name and information
gadenbuie Jun 20, 2023
5c902cc
Apply suggestions from code review
gadenbuie Jun 21, 2023
fbf656b
refactor(preset): Only add "bs_theme_with_preset" class to `bs_theme`…
gadenbuie Jun 21, 2023
62f320c
feat(bs_theme_init): Include default null bslib vars
gadenbuie Jun 21, 2023
28ddfb3
feat(bs_theme_init): Pass bslib sass vars to CSS custom properties
gadenbuie Jun 21, 2023
c891fb8
Resave distributed files (GitHub Action)
gadenbuie Jun 21, 2023
0c17392
ci: check again after rebuilding compiled theme
gadenbuie Jun 21, 2023
7805b89
tests(theme_version): Ensure up to date with bundled BS versions
gadenbuie Jun 21, 2023
b4eff4f
refactor(preset): Don't use `bs_preset_{type}` subclass anywhere
gadenbuie Jun 21, 2023
586dc6f
Resave data (GitHub Action)
gadenbuie Jun 21, 2023
c6cade0
chore: Apply suggestions from code review
gadenbuie Jun 21, 2023
132386a
chore(theme_version): Restore class-grepping version
gadenbuie Jun 21, 2023
160360b
fix(theme_preset_info): Typo "is_na()"
gadenbuie Jun 21, 2023
1a602d2
feat(theme_preset_class): Store theme class in a function and in the …
gadenbuie Jun 21, 2023
41f8aa8
fix: Apply suggestions from code review
gadenbuie Jun 22, 2023
2f95827
feat(theme_version): When ambiguous, consult sass bundle
gadenbuie Jun 22, 2023
17112ee
tests(theme_version): Test that class or sass bundle are used
gadenbuie Jun 22, 2023
d17a40d
tests(theme_version): Be slightly less tricky
gadenbuie Jun 22, 2023
7e26ba3
Revert "feat(theme_preset_class): Store theme class in a function and…
gadenbuie Jun 22, 2023
3c44cc3
refactor(theme_preset): Use constant for theme preset class
gadenbuie Jun 22, 2023
2150ba8
fix(typo): bs_theme_with_preset
gadenbuie Jun 22, 2023
65fe8a4
Merge 'origin/main' into refresh/preset-theme-classes
gadenbuie Jun 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
# Generated by roxygen2: do not edit by hand

S3method(bs_preset_bundle,bs_preset)
S3method(bs_preset_bundle,default)
S3method(is_fill_item,default)
S3method(is_fill_item,htmlwidget)
S3method(is_fillable_container,default)
Expand Down
16 changes: 10 additions & 6 deletions R/bs-theme-preset-bootswatch.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ bootswatch_themes <- function(version = version_default(), full_path = FALSE) {
#' @export
theme_bootswatch <- function(theme) {
if (!is_bs_theme(theme)) return(NULL)

swatch <- grep("^bs_bootswatch_", class(theme), value = TRUE)
if (!length(swatch)) return(NULL)

sub("^bs_bootswatch_", "", swatch)
info <- theme_preset_info(theme)
if (identical("bootswatch", info$type)) info$name else NULL
}

#' Obtain a theme's Bootstrap version
Expand All @@ -31,7 +28,12 @@ theme_version <- function(theme) {
if (!is_bs_theme(theme)) return(NULL)

version <- grep("^bs_version_", class(theme), value = TRUE)
sub("^bs_version_", "", version)
if (length(version) == 1) {
return(sub("^bs_version_", "", version))
}

# If no version class, or more than one class, we consult the sass bundle
theme_preset_info(theme)$version
}


Expand Down Expand Up @@ -86,6 +88,8 @@ bootswatch_bundle <- function(bootswatch, version) {
bootswatch = sass_layer(
file_attachments = attachments,
defaults = list(
"bslib-preset-type" = "bootswatch",
"bslib-preset-name" = bootswatch,
# Use local fonts (this path is relative to the bootstrap HTML dependency dir)
'$web-font-path: "font.css" !default;',
bootswatch_sass_file(bootswatch, "variables", version),
Expand Down
2 changes: 2 additions & 0 deletions R/bs-theme-preset-builtin.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ builtin_bundle <- function(name = "shiny", version = version_default()) {
"builtin" = sass_layer(
file_attachments = attachments,
defaults = list(
"bslib-preset-type" = "builtin",
"bslib-preset-name" = name,
'$web-font-path: "font.css" !default;',
sass_file(path_variables)
),
Expand Down
67 changes: 35 additions & 32 deletions R/bs-theme-preset.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ resolve_bs_preset <- function(
preset_name <- preset %||% bootswatch

if (preset_name %in% c("default", "bootstrap")) {
# "default" means no preset bundle, just bare default Bootstrap
return(new_bs_preset("default", version))
# "bootstrap" means no preset bundle, just bare default Bootstrap
return(new_bs_preset("bootstrap", version))
}

builtin_themes <- builtin_themes(version)
Expand All @@ -33,47 +33,50 @@ resolve_bs_preset <- function(
}

new_bs_preset <- function(name, version, type = NULL) {
subclass <- if (!is.null(type)) paste0("bs_preset_", type)

preset_class <- if (!is.null(type)) {
name_safe <- gsub("[^[:alnum:]]", "_", name)
paste("bs", type, name_safe, sep = "_")
}

preset <- list(
version = version,
name = name,
type = type,
class = preset_class
version = version, # bootstrap version
name = name, # preset name
type = type, # preset type (e.g. "builtin", "bootswatch")
theme_class = if (!is.null(type)) theme_preset_class()
)

structure(dropNulls(preset), class = c(subclass, "bs_preset"))
}

# The `bs_preset_bundle()` function is the main entry point for creating a
# SASS bundle for a theme preset. This currently dispatches to create a
# bundle for a built-in theme or for a Bootswatch theme.
bs_preset_bundle <- function(preset, ...) {
UseMethod("bs_preset_bundle", preset)
structure(dropNulls(preset), class = "bs_preset")
}

#' @export
bs_preset_bundle.default <- function(preset, ...) {
# Sub-classes are used to create a bundle for a specific type of preset; this
# default case is used for "bare" Bootstrap, or an empty preset bundle.
NULL
}
# The `bs_preset_bundle()` function is the main entry point for creating a SASS
# bundle for a theme preset. This calls out to the appropriate functions to
# create a bundle for a built-in theme or for a Bootswatch theme.
bs_preset_bundle <- function(preset) {
if (!inherits(preset, "bs_preset")) return(NULL)
if (is.null(preset$type)) return(NULL)

#' @export
bs_preset_bundle.bs_preset <- function(preset, ...) {
switch(
preset$type %||% "",
builtin = builtin_bundle(preset$name, version = preset$version),
preset$type,
bootswatch = bootswatch_bundle(preset$name, version = preset$version),
NextMethod()
builtin = builtin_bundle(preset$name, version = preset$version),
stop("Unknown preset type: ", preset$type)
)
}

theme_preset_info <- function(theme) {
if (!is_bs_theme(theme)) return(NULL)

info <- bs_get_variables(theme, c("bslib-preset-type", "bslib-preset-name", "bootstrap-version"))

name <- info[["bslib-preset-name"]]
type <- info[["bslib-preset-type"]]

new_bs_preset(
name = if (!is.na(name)) name else "bootstrap",
version = info[["bootstrap-version"]],
type = if (!is.na(type)) type
)
}

theme_preset_class <- function() {
"bs_theme_with_preset"
}

assert_preset_scalar_string <- function(var, .frame = rlang::caller_env()) {
var_name <- deparse(substitute(var))

Expand Down
53 changes: 32 additions & 21 deletions R/bs-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@
#' over the `bootswatch` argument and only one `theme` or `bootswatch` can be
#' provided. When provided to `bs_theme_update()`, any previous preset theme
#' is first removed before the new theme preset is applied. You can use
#' `theme = "default"` to remove any preset theme and to revert to a base
#' `theme = "bootstrap"` to remove any preset theme and to revert to a base
#' Bootstrap theme.
#' @param bootswatch The name of a bootswatch theme (see [bootswatch_themes()]
#' for possible values). When provided to `bs_theme_update()`, any previous
#' Bootswatch theme is first removed before the new one is applied (use
#' `bootswatch = "default"` to effectively remove the Bootswatch theme).
#' `bootswatch = "bootstrap"` to effectively remove the Bootswatch theme).
#' @param ... arguments passed along to [bs_add_variables()].
#' @param bg A color string for the background.
#' @param fg A color string for the foreground.
Expand Down Expand Up @@ -128,11 +128,15 @@ bs_theme <- function(version = version_default(), preset = NULL, ...,
preset <- resolve_bs_preset(preset, bootswatch, version = version)

bundle <- bs_bundle(
bs_theme_init(version, subclass = preset$class),
bs_theme_init(version),
bootstrap_bundle(version),
bs_preset_bundle(preset)
)

if (!is.null(preset$theme_class)) {
bundle <- add_class(bundle, preset$theme_class)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still doesn't feel quite right that $theme_class exists. Could we get rid of it, and do something like this?

Suggested change
if (!is.null(preset$theme_class)) {
bundle <- add_class(bundle, preset$theme_class)
}
if (!is.null(preset$type)) {
bundle <- add_class(bundle, theme_preset_class())
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't mind, I made the change to a constant in 3c44cc3.

The reason I didn't originally was because it felt like preset$type being NULL was an indirect signal of whether this class should be added. I included preset$theme_class so that if we changed that assumption later we'd only need to reason about the preset object.

But the problem is that we have to remove preset the preset class as part of bs_theme_update() ... so I'm on board with constant as a the signal that we're doing something special.


bs_theme_update(
bundle, ...,
bg = bg, fg = fg,
Expand All @@ -159,23 +163,22 @@ bs_theme_update <- function(theme, ..., preset = NULL, bg = NULL, fg = NULL,
font_scale = NULL, bootswatch = NULL) {
assert_bs_theme(theme)

theme_has_preset <- any(grepl("^bs_(builtin|bootswatch)_", class(theme)))

preset <- resolve_bs_preset(preset, bootswatch, version = theme_version(theme))

if (!is.null(preset)) {
if (theme_has_preset) {
old_preset_class <- grep("^bs_(builtin|bootswatch)_", class(theme), value = TRUE)
old_preset_type <- sub("^bs_(builtin|bootswatch)_.+", "\\1", old_preset_class)
theme_has_preset <- inherits(theme, theme_preset_class())

if (theme_has_preset) {
# remove the old preset
theme <- bs_remove(theme, old_preset_type)
class(theme) <- setdiff(class(theme), old_preset_class)
theme <- bs_remove(theme, theme_preset_info(theme)$type)
class(theme) <- setdiff(class(theme), theme_preset_class())
}

# Add the new preset (both no-op when preset$name is "default")
theme <- add_class(theme, preset$class)
theme <- bs_bundle(theme, bs_preset_bundle(preset))
# Add in the new preset unless vanilla bootstrap was requested
if (!identical(preset$name, "bootstrap")) {
theme <- add_class(theme, preset$theme_class)
theme <- bs_bundle(theme, bs_preset_bundle(preset))
}
}

# See R/bs-theme-update.R for the implementation of these
Expand Down Expand Up @@ -229,15 +232,23 @@ is_bs_theme <- function(x) {

# Start an empty bundle with special classes that
# theme_version() & theme_bootswatch() search for
bs_theme_init <- function(version, subclass = NULL) {
add_class(
sass_layer(defaults = list("bootstrap-version" = version)),
c(
subclass,
paste0("bs_version_", version),
"bs_theme"
bs_theme_init <- function(version) {
init_layer <- sass_layer(
defaults = list(
"bootstrap-version" = version,
"bslib-preset-name" = "null !default",
"bslib-preset-type" = "null !default"
),
rules = c(
":root {",
"--bslib-bootstrap-version: #{$bootstrap-version};",
"--bslib-preset-name: #{$bslib-preset-name};",
"--bslib-preset-type: #{$bslib-preset-type};",
"}"
)
)
)

add_class(init_layer, c(paste0("bs_version_", version), "bs_theme"))
}

assert_bs_theme <- function(theme) {
Expand Down
Binary file modified R/sysdata.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion inst/css-precompiled/3/bootstrap.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/css-precompiled/4/bootstrap.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion inst/css-precompiled/5/bootstrap.min.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/bs_global_theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/bs_theme.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions tests/testthat/_snaps/bs-theme-preset.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# new_theme_preset(): throws an error if both `name` and `bootswatch` are provided
# resolve_bs_preset(): throws an error if both `name` and `bootswatch` are provided

Code
resolve_bs_preset(preset = "name", bootswatch = "bootswatch")
Expand All @@ -9,7 +9,7 @@
* `preset = "bootswatch"`
* `bootswatch = "bootswatch"`

# new_theme_preset(): throws an error if `name` or `bootswatch` are not scalar strings
# resolve_bs_preset(): throws an error if `name` or `bootswatch` are not scalar strings

Code
resolve_bs_preset(preset = c("a", "b"))
Expand All @@ -27,7 +27,7 @@
x Bad: `bootswatch = c("flatly", "darkly")`
v Good: `bootswatch = "flatly"`

# new_theme_preset(): throws an error if `name` or `bootswatch` don't match existing presets
# resolve_bs_preset(): throws an error if `name` or `bootswatch` don't match existing presets

Code
resolve_bs_preset(preset = "not_a_preset", version = 4)
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-bs-theme-preset-bootswatch.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("theme_version() works for current versions", {
bs_versions <- versions()

for (bs_v in bs_versions) {
expect_equal(theme_version(bs_theme(!!bs_v)), !!bs_v)
}
})

test_that("theme_version() uses class first, then sass bundle", {
theme <- theme_unclassed <- bs_theme(version = 5)
class(theme_unclassed) <- setdiff(class(theme), "bs_version_5")
theme_99 <- add_class(theme_unclassed, "bs_version_99")

# These should use the class information
expect_equal(theme_version(theme), "5")
expect_equal(theme_version(theme_99), "99")

# These should consult the sass bundle
expect_equal(theme_version(add_class(theme, "bs_version_99")), "5")
expect_equal(theme_version(theme_unclassed), "5")
})
Loading
Loading