Skip to content

Commit 33892d6

Browse files
committed
add delay argument; fixes #95
1 parent 8d14fbf commit 33892d6

File tree

9 files changed

+44
-7
lines changed

9 files changed

+44
-7
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.9001
3+
Version: 1.1.0.9002
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Unreleased version
22

3+
- Add `delay` argument to show the spinner after a short delay (#95)
34
- Add `width` argument that can be used in rare cases where the spinner has no inherent width (#85)
45

56
# shinycssloaders 1.1.0 (2024-07-30)

R/buildSpinner.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ buildSpinner <- function(
1313
image.height,
1414
hide.ui,
1515
caption,
16-
width = NULL
16+
delay,
17+
width
1718
) {
1819
spinner_type <- match.arg(spinner_type)
1920
output_spinner <- (spinner_type == "output")
@@ -77,6 +78,7 @@ buildSpinner <- function(
7778
shiny::div(
7879
css_rules_tag,
7980
`data-spinner-id` = id,
81+
`data-spinner-delay` = if (delay == 0) NULL else delay,
8082
class = parent_cls,
8183
shiny::div(
8284
class = child_cls,

R/pageSpinner.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ showPageSpinner <- function(
9696
caption = caption,
9797
ui_element = NULL,
9898
proxy.height = NULL,
99-
hide.ui = FALSE
99+
hide.ui = FALSE,
100+
delay = 0,
101+
width = NULL
100102
)
101103

102104
hidePageSpinner()

R/withSpinner.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
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 delay Specify a delay (in milliseconds) before the spinner is displayed. This can be useful to avoid showing the spinner for very short loading times, for a better user experience.
3435
#' @param width The width of the spinner, in pixels. This is only needed in rare cases when the spinner
3536
#' is not appearing on the screen due to it having no inherent width (for example, when the output is inside
3637
#' a CSS flexbox without a specified width). Do not use this parameter if the spinner already works.
@@ -68,6 +69,7 @@ withSpinner <- function(
6869
image.height = getOption("spinner.image.height"),
6970
hide.ui = getOption("spinner.hide.ui", default = TRUE),
7071
caption = getOption("spinner.caption"),
72+
delay = getOption("spinner.delay", default = 0),
7173
width = getOption("spinner.width")
7274
) {
7375

@@ -90,6 +92,7 @@ withSpinner <- function(
9092
image.height = image.height,
9193
hide.ui = hide.ui,
9294
caption = caption,
95+
delay = delay,
9396
width = width
9497
)
9598

inst/assets/spinner.js

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(function() {
22
var output_states = {};
3+
let timeout_ids = {};
34

45
function escapeSelector(s) {
56
return s.replace(/([!"#$%&'()*+,-./:;<=>?@\[\\\]^`{|}~])/g, "\\$1");
@@ -10,18 +11,37 @@ function show_spinner(id) {
1011
var parent = $(selector).closest(".shiny-spinner-output-container");
1112
if (parent.length == 0) return;
1213

14+
if (parent.data() && parent.data().spinnerDelay && parent.data().spinnerDelay > 0) {
15+
let tid = setTimeout(() => {
16+
show_spinner_logic(selector);
17+
}, parent.data().spinnerDelay);
18+
timeout_ids[selector] = tid;
19+
} else {
20+
show_spinner_logic(selector);
21+
}
22+
}
23+
24+
let show_spinner_logic = function(selector) {
25+
delete timeout_ids[selector];
26+
let parent = $(selector).closest(".shiny-spinner-output-container");
27+
1328
$(selector).siblings(".load-container, .shiny-spinner-placeholder").removeClass('shiny-spinner-hidden');
1429

1530
if (parent.hasClass("shiny-spinner-hideui")) {
16-
$(selector).siblings(".load-container").siblings('.shiny-bound-output, .shiny-output-error').css('visibility', 'hidden');
17-
// if there is a proxy div, hide the previous output
18-
$(selector).siblings(".shiny-spinner-placeholder").siblings('.shiny-bound-output, .shiny-output-error').addClass('shiny-spinner-hidden');
31+
$(selector).siblings(".load-container").siblings('.shiny-bound-output, .shiny-output-error').css('visibility', 'hidden');
32+
// if there is a proxy div, hide the previous output
33+
$(selector).siblings(".shiny-spinner-placeholder").siblings('.shiny-bound-output, .shiny-output-error').addClass('shiny-spinner-hidden');
1934
}
20-
}
35+
};
2136

2237
function hide_spinner(id) {
2338
var selector = "#" + escapeSelector(id);
39+
if (selector in timeout_ids) {
40+
clearTimeout(timeout_ids[selector]);
41+
delete timeout_ids[selector];
42+
}
2443
var parent = $(selector).closest(".shiny-spinner-output-container");
44+
2545
$(selector).siblings(".load-container, .shiny-spinner-placeholder").addClass('shiny-spinner-hidden');
2646
if (parent.hasClass("shiny-spinner-hideui")) {
2747
$(selector).siblings(".load-container").siblings('.shiny-bound-output').css('visibility', 'visible');

inst/examples/demo/server.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ server <- function(input, output, session) {
2222
if (nzchar(input$caption)) {
2323
params$caption <- input$caption
2424
}
25+
params$delay <- input$delay * 1000
2526

2627
params
2728
})
@@ -46,6 +47,7 @@ server <- function(input, output, session) {
4647
params <- spinner_params()
4748
params$ui_element <- NULL
4849
params$background <- input$bg
50+
params$delay <- NULL
4951
suppressWarnings(do.call(shinycssloaders::showPageSpinner, params))
5052
Sys.sleep(input$time)
5153
shinycssloaders::hidePageSpinner()

inst/examples/demo/ui.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ fluidPage(
8888
),
8989
textInput("caption", "Caption", ""),
9090
sliderInput("time", "Seconds to show spinner", 0, 3, 1, 0.5),
91+
conditionalPanel(
92+
"input.spinner_type == 'output'",
93+
sliderInput("delay", "Delay (seconds)", 0, 3, 0, 0.25),
94+
),
9195
conditionalPanel(
9296
"input.spinner_type == 'output'",
9397
actionButton("update", "Update Plot", class = "btn-primary btn-lg")

man/withSpinner.Rd

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

0 commit comments

Comments
 (0)