Skip to content

Commit

Permalink
Save a dependencies.json that captures all bs_theme() (+ component-sp…
Browse files Browse the repository at this point in the history
…ecific) HTML dependencies as well as the Sass layers used to generate the relevant CSS
  • Loading branch information
cpsievert committed Nov 21, 2023
1 parent ecada4b commit e63b12f
Show file tree
Hide file tree
Showing 16 changed files with 428 additions and 305 deletions.
89 changes: 35 additions & 54 deletions R/bs-theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ bs_theme <- function(
preset <- resolve_bs_preset(preset, bootswatch, version = version)

bundle <- bs_bundle(
bs_theme_init(version),
bootstrap_bundle(version),
if (version > 3) bs3compat_bundle(),
bs_preset_bundle(preset)
)

Expand Down Expand Up @@ -276,25 +276,12 @@ is_bs_theme <- function(x) {
inherits(x, "bs_theme")
}

# Start an empty bundle with special classes that
# theme_version() & theme_bootswatch() search for
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};",
"}"
)
)
new_bs_theme <- function(x, version) {
if (!is_sass_bundle(x)) {
stop("`theme` must be a `sass_bundle()` object")
}

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

assert_bs_theme <- function(theme) {
Expand All @@ -309,13 +296,6 @@ assert_bs_theme <- function(theme) {
# -----------------------------------------------------------------

bootstrap_bundle <- function(version) {
pandoc_tables <- list(
# Pandoc uses align attribute to align content but BS4 styles take precedence...
# we may want to consider adopting this more generally in "strict" BS4 mode as well
".table th[align=left] { text-align: left; }",
".table th[align=right] { text-align: right; }",
".table th[align=center] { text-align: center; }"
)

main_bundle <- switch_version(
version,
Expand All @@ -339,13 +319,6 @@ bootstrap_bundle <- function(version) {
"toasts", "modal", "tooltip", "popover", "carousel", "spinners",
"offcanvas", "placeholders", "helpers", "utilities/api"
))
),
# Additions to BS5 that are always included (i.e., not a part of compatibility)
sass_layer(rules = pandoc_tables),
bs3compat = bs3compat_bundle(),
# Enable CSS Grid powered Bootstrap grid
sass_layer(
defaults = list("enable-cssgrid" = "true !default")
)
),
four = sass_bundle(
Expand All @@ -365,10 +338,7 @@ bootstrap_bundle <- function(version) {
"progress", "media", "list-group", "close", "toasts", "modal",
"tooltip", "popover", "carousel", "spinners", "utilities", "print"
))
),
# Additions to BS4 that are always included (i.e., not a part of compatibility)
sass_layer(rules = pandoc_tables),
bs3compat = bs3compat_bundle()
)
),
three = sass_bundle(
sass_layer(
Expand Down Expand Up @@ -396,10 +366,12 @@ bootstrap_bundle <- function(version) {
)
)

sass_bundle(
full_bundle <- sass_bundle(
main_bundle,
bslib_bundle()
bslib = bslib_bundle(version)
)

new_bs_theme(full_bundle, version)
}

bootstrap_javascript_map <- function(version) {
Expand All @@ -422,10 +394,16 @@ bootstrap_javascript <- function(version) {
# bslib specific Sass that gets bundled with Bootstrap
# -----------------------------------------------------------------

bslib_bundle <- function() {
# N.B. If you find yourself changing this function, be careful about what
# the implications might be for Quarto!
bslib_bundle <- function(version) {
sass_layer(
functions = sass_file(path_inst("bslib-scss", "functions.scss")),
rules = sass_file(path_inst("bslib-scss", "bslib.scss"))
defaults = list(
"bootstrap-version" = version,
sass_file(path_inst("bslib-scss", "defaults.scss"))
),
rules = sass_file(path_inst("bslib-scss", "rules.scss"))
)
}

Expand All @@ -434,21 +412,24 @@ bslib_bundle <- function() {
# -----------------------------------------------------------------

bs3compat_bundle <- function() {
sass_layer(
defaults = sass_file(system_file("bs3compat", "_defaults.scss", package = "bslib")),
mixins = sass_file(system_file("bs3compat", "_declarations.scss", package = "bslib")),
rules = sass_file(system_file("bs3compat", "_rules.scss", package = "bslib")),
# Gyliphicon font files
file_attachments = c(
fonts = path_lib("bs3", "assets", "fonts")
),
html_deps = htmltools::htmlDependency(
"bs3compat", packageVersion("bslib"),
package = "bslib",
src = "bs3compat/js",
script = c("transition.js", "tabs.js", "bs3compat.js")
sass_bundle(
bs3compat = sass_layer(
defaults = sass_file(system_file("bs3compat", "_defaults.scss", package = "bslib")),
mixins = sass_file(system_file("bs3compat", "_declarations.scss", package = "bslib")),
rules = sass_file(system_file("bs3compat", "_rules.scss", package = "bslib")),
# Gyliphicon font files
file_attachments = c(
fonts = path_lib("bs3", "assets", "fonts")
),
html_deps = htmltools::htmlDependency(
"bs3compat", packageVersion("bslib"),
package = "bslib",
src = "bs3compat/js",
script = c("transition.js", "tabs.js", "bs3compat.js")
)
)
)

}

# -----------------------------------------------------------------
Expand Down
25 changes: 16 additions & 9 deletions R/utils-deps.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,8 @@ component_dependency_sass <- function(theme) {
}

component_dependency_sass_ <- function(theme) {
scss_dir <- path_inst("components", "scss")
scss_files <- c(
file.path(scss_dir, "mixins", "_mixins.scss"),
dir(scss_dir, pattern = "\\.scss$", full.names = TRUE)
)

# Although rare, it's possible for bs_dependency_defer() to pass
# along a NULL theme (e.g., renderTags(accordion())), so fallback
# Although rare, it's possible for bs_dependency_defer() to pass
# along a NULL theme (e.g., renderTags(accordion())), so fallback
# to the default theme if need be
theme <- theme %||% bs_theme()

Expand All @@ -65,7 +59,7 @@ component_dependency_sass_ <- function(theme) {
}

bs_dependency(
input = lapply(scss_files, sass_file),
input = component_sass_bundle(),
theme = theme,
name = "bslib-component-css",
version = get_package_version("bslib"),
Expand All @@ -75,6 +69,19 @@ component_dependency_sass_ <- function(theme) {
}


component_sass_bundle <- function() {
scss_dir <- path_inst("components", "scss")
sass_layer(
mixins = sass_file(
file.path(scss_dir, "mixins", "_mixins.scss")
),
rules = lapply(
dir(scss_dir, pattern = "\\.scss$", full.names = TRUE),
sass_file
)
)
}

web_component <- function(tagName, ...) {
deps <- component_dependencies()
args <- c(deps, rlang::list2(...))
Expand Down
3 changes: 0 additions & 3 deletions inst/bslib-scss/bslib.scss

This file was deleted.

74 changes: 0 additions & 74 deletions inst/bslib-scss/color-utilities.scss

This file was deleted.

9 changes: 9 additions & 0 deletions inst/bslib-scss/defaults.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Our own Sass variables that we use in our rules
$bootstrap-version: null !default; // Should always be brought in via R
$bslib-preset-name: null !default;
$bslib-preset-type: null !default;



// Bootstrap Sass defaults that we take advantage of
$enable-cssgrid: true !default;
Loading

0 comments on commit e63b12f

Please sign in to comment.