Skip to content

Commit 8d14fbf

Browse files
committed
add 'width' param for edge cases where spinner has no width; fixes #85
1 parent 42f1e4f commit 8d14fbf

File tree

7 files changed

+33
-10
lines changed

7 files changed

+33
-10
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: shinycssloaders
22
Title: Add Loading Animations to a 'shiny' Output While It's Recalculating
3-
Version: 1.1.0.9000
3+
Version: 1.1.0.9001
44
Authors@R: c(
55
person("Dean","Attali",email="[email protected]",role=c("aut","cre"),
66
comment = c("Maintainer/developer of shinycssloaders since 2019", ORCID="0000-0002-5645-3493")),

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Unreleased version
2+
3+
- Add `width` argument that can be used in rare cases where the spinner has no inherent width (#85)
4+
15
# shinycssloaders 1.1.0 (2024-07-30)
26

37
- Add `showSpinner()` and `hideSpinner()` that allow you to manually trigger the spinner (#56)

R/buildSpinner.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ buildSpinner <- function(
1212
image.width,
1313
image.height,
1414
hide.ui,
15-
caption
15+
caption,
16+
width = NULL
1617
) {
1718
spinner_type <- match.arg(spinner_type)
1819
output_spinner <- (spinner_type == "output")
@@ -35,7 +36,8 @@ buildSpinner <- function(
3536
id <- paste0("spinner-", digest::digest(ui_element))
3637
}
3738

38-
css_rules_tag <- get_spinner_css_tag(type, color, size, color.background, custom.css, id, image, caption, output_spinner)
39+
css_rules_tag <- get_spinner_css_tag(type, color, size, color.background, custom.css, id,
40+
image, caption, width, output_spinner)
3941

4042
if (!is.null(caption)) {
4143
caption <- shiny::div(

R/utils.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ get_proxy_element <- function(ui_element, proxy.height, hide.ui) {
6565
}
6666
}
6767

68-
get_spinner_css_tag <- function(type, color, size, color.background, custom.css, id, image, caption, output_spinner) {
68+
get_spinner_css_tag <- function(type, color, size, color.background, custom.css, id, image, caption, width, output_spinner) {
6969
base_css <- ""
7070
add_default_style <- (is.null(image) && !custom.css && type != 0)
7171
if (add_default_style) {
@@ -92,12 +92,17 @@ get_spinner_css_tag <- function(type, color, size, color.background, custom.css,
9292
base_css <- paste(base_css, glue::glue("#{id}__caption {{ color: {color}; font-size: {size + 0.5}em; }}"))
9393
}
9494

95+
if (!is.null(width)) {
96+
base_css <- paste(base_css, glue::glue('[data-spinner-id="{id}"] {{ min-width: {width}px; }}'))
97+
}
98+
9599
css_rules_tag <- NULL
96100
if (nzchar(base_css)) {
97101
css_rules_tag <- insertHead(shiny::tags$style(
98102
class = if (!output_spinner) "global-spinner-css",
99103
shiny::HTML(base_css)
100104
))
101105
}
106+
102107
css_rules_tag
103108
}

R/withSpinner.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#' Ignored if `image` is used.
1919
#' @param custom.css Set to `TRUE` if you have your own custom CSS that you defined and you don't want the automatic CSS applied to the spinner.
2020
#' Ignored if `image` is used.
21-
#' @param proxy.height If the output UI doesn't specify the output height, you can set a proxy height. It defaults to "400px"
21+
#' @param proxy.height If the output UI doesn't specify the output height, you can set a proxy height. For example, `textOutput()` usually needs this parameter. It defaults to "400px"
2222
#' for outputs with undefined height. Ignored if `hide.ui` is set to `FALSE`.
2323
#' @param id The HTML ID to use for the spinner. If you don't provide one, it will be generated automatically.
2424
#' @param image The path or URL of the image to use if you want to use a custom image instead of a built-in spinner.
@@ -31,6 +31,9 @@
3131
#' Setting `hide.ui = FALSE` will result in the spinner showing up on top of the previous output UI.
3232
#' @param caption Caption to display below the spinner or image (text or HTML). The caption's font color is determined
3333
#' by the `color` parameter. Ignored if `type` is 1.
34+
#' @param width The width of the spinner, in pixels. This is only needed in rare cases when the spinner
35+
#' is not appearing on the screen due to it having no inherent width (for example, when the output is inside
36+
#' a CSS flexbox without a specified width). Do not use this parameter if the spinner already works.
3437
#' @seealso [showSpinner()], [hideSpinner()], [showPageSpinner()]
3538
#' @examples
3639
#' if (interactive()) {
@@ -64,7 +67,8 @@ withSpinner <- function(
6467
image.width = getOption("spinner.image.width"),
6568
image.height = getOption("spinner.image.height"),
6669
hide.ui = getOption("spinner.hide.ui", default = TRUE),
67-
caption = getOption("spinner.caption")
70+
caption = getOption("spinner.caption"),
71+
width = getOption("spinner.width")
6872
) {
6973

7074
if (!inherits(ui_element, "shiny.tag") && !inherits(ui_element, "shiny.tag.list")) {
@@ -85,7 +89,8 @@ withSpinner <- function(
8589
image.width = image.width,
8690
image.height = image.height,
8791
hide.ui = hide.ui,
88-
caption = caption
92+
caption = caption,
93+
width = width
8994
)
9095

9196
htmltools::attachDependencies(spinner, getDependencies())

inst/assets/spinner.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ function escapeSelector(s) {
88
function show_spinner(id) {
99
var selector = "#" + escapeSelector(id);
1010
var parent = $(selector).closest(".shiny-spinner-output-container");
11+
if (parent.length == 0) return;
12+
1113
$(selector).siblings(".load-container, .shiny-spinner-placeholder").removeClass('shiny-spinner-hidden');
1214

1315
if (parent.hasClass("shiny-spinner-hideui")) {
@@ -69,7 +71,7 @@ Shiny.addCustomMessageHandler('shinycssloaders.show_spinner', function(params) {
6971
});
7072

7173
/* When new value or error comes in, hide spinner container (if any) & show the output */
72-
$(document).on('shiny:value shiny:error', function(event) {
74+
$(document).on('shiny:value shiny: error', function(event) {
7375
var id = event.target.id;
7476
if (id === undefined) {
7577
return;

man/withSpinner.Rd

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)