Skip to content

Commit

Permalink
Include useBusyIndicators() as a separate htmlDependency()
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jul 23, 2024
1 parent bf580de commit 033c1d1
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## New features

* Shiny's busy indicators (see `shiny::useBusyIndicators()` in shiny > v1.8.1.1) are now enabled automatically for all of bslib's `page_*()` functions or when bslib is used to provide Bootstrap 5 for a Shiny app. If this behavior isn't desirable (perhaps because a package like `{shinycssloaders}` is being used to indicate busy state), then disable by putting `shiny::useBusyIndicators(spinners=FALSE, pulse=FALSE)` in the page. (#1053)
* Shiny's busy indicators (see `shiny::useBusyIndicators()` in shiny > v1.8.1.1) are now enabled automatically when bslib for the UI. If this behavior isn't desirable (perhaps because a package like `{shinycssloaders}` is being used to indicate busy state), then disable by putting `shiny::useBusyIndicators(spinners=FALSE, pulse=FALSE)` inside the UI definition. (#1053)

## Improvements

Expand Down
26 changes: 20 additions & 6 deletions R/bs-dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,32 @@ bs_theme_dependencies <- function(
stylesheet = basename(out_file),
script = basename(js_files),
all_files = TRUE, # include font and map files
meta = list(viewport = "width=device-width, initial-scale=1, shrink-to-fit=no"),
# Enable busy indicators by default.
# This doesn't have much of anything to Bootstrap,
# but it's a convenient place to hang this feature's
# initialization code (so it works in Rmd etc).
head = if (isTRUE(version >= 5)) as.character(use_busy_indicators())
meta = list(viewport = "width=device-width, initial-scale=1, shrink-to-fit=no")
)
),
busy_indicator_deps(),
htmlDependencies(out_file)
))
}

# Busy indicators are orthogonal to Bootstrap, but since they have the potential
# to introduce breaking changes, they aren't enabled by default in R/Shiny.
# However, since bslib is the place for "modern UI" practices, we enable them by
# default in bslib. Note also that the way in which busy indicators are enabled
# is here is fairly consistent with how they're enabled in PyShiny...
# https://github.com/posit-dev/py-shiny/blob/dc0b294/shiny/ui/_html_deps_py_shiny.py#L73-L81
busy_indicator_deps <- function() {
if (is_installed("shiny", "1.8.1.9001")) {
list(htmlDependency(
name = "bslib-enable-busy-indicators",
version = get_package_version("bslib"),
src = "",
package = "bslib",
head = as.character(shiny::useBusyIndicators())
))
}
}


#' Themeable HTML components
#'
Expand Down
6 changes: 0 additions & 6 deletions R/page.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ page <- function(..., title = NULL, theme = bs_theme(), lang = NULL) {
)
}

use_busy_indicators <- function() {
# Use busy indicators were added in shiny 1.8.1.9001 (after 1.8.1.1)
ubi <- asNamespace("shiny")[["useBusyIndicators"]]
if (!is.null(ubi)) ubi()
}

#' @describeIn page A \pkg{bslib} wrapper for [shiny::fluidPage()], a fluid
#' Bootstrap-based page layout that extends to the full viewport width.
#' @inheritParams shiny::fluidPage
Expand Down
4 changes: 0 additions & 4 deletions tests/testthat/_snaps/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@
<script data-bslib-sidebar-init>bslib.Sidebar.initCollapsibleAll()</script>
</div>
</body>
<script>document.documentElement.dataset.shinyBusySpinners = 'true';
document.documentElement.dataset.shinyBusyPulse = 'true';</script>

---

Expand All @@ -78,6 +76,4 @@
<script data-bslib-sidebar-init>bslib.Sidebar.initCollapsibleAll()</script>
</div>
</body>
<script>document.documentElement.dataset.shinyBusySpinners = 'true';
document.documentElement.dataset.shinyBusyPulse = 'true';</script>

2 changes: 2 additions & 0 deletions tests/testthat/_snaps/page/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />
<script src="lib/bootstrap/bootstrap.bundle.min.js"></script>
<script>document.documentElement.dataset.shinyBusySpinners = 'true';
document.documentElement.dataset.shinyBusyPulse = 'true';</script>
<script src="lib/bs3compat/transition.js"></script>
<script src="lib/bs3compat/tabs.js"></script>
<script src="lib/bs3compat/bs3compat.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/_snaps/page/modern-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />
<script src="lib/bootstrap/bootstrap.bundle.min.js"></script>
<script>document.documentElement.dataset.shinyBusySpinners = 'true';
document.documentElement.dataset.shinyBusyPulse = 'true';</script>
<script src="lib/bslib-component-js/components.min.js"></script>
<script src="lib/bslib-component-js/web-components.min.js" type="module"></script>
<link href="lib/bslib-component-css/bslib-component-css.min.css" rel="stylesheet" />

</head>
<body>A simple page without bs3compat dependencies</body>
<script>document.documentElement.dataset.shinyBusySpinners = 'true';
document.documentElement.dataset.shinyBusyPulse = 'true';</script>
</html>

0 comments on commit 033c1d1

Please sign in to comment.