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

Adding button with "reload" functionality. #1

Merged
merged 6 commits into from
Aug 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

export(hover_action_button)
export(hover_download_button)
export(hover_reload_button)
export(use_hover)
14 changes: 1 addition & 13 deletions R/hover_action_button.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#' @param icon An optional icon() to appear on the button.
#' @param button_animation The name of the button animation.
#' @param icon_animation The name of the icon animation.
#' @param width The width of the input, e.g. '400px', or '100%'; see validateCssUnit().
#' @param width The width of the input, e.g. '400px', or '100 percent'; see validateCssUnit().
#' @param ... Named attributes to be applied to the button or link.
#' @md
#' @examples
Expand Down Expand Up @@ -55,15 +55,3 @@ hover_action_button <- function(inputId, label, icon = NULL, button_animation =
...
)
}

validateIcon <- function (icon) {
if (is.null(icon) || identical(icon, character(0))) {
return(icon)
}
else if (inherits(icon, "shiny.tag") && icon$name == "i") {
return(icon)
}
else {
stop("Invalid icon. Use Shiny's 'icon()' function to generate a valid icon")
}
}
60 changes: 60 additions & 0 deletions R/hover_reload_button.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#' Reload button with button and icon animations
#'
#' Animate a reload button and it's icon using
#' [Hover.css](https://github.com/IanLunn/Hover)
Copy link
Member

Choose a reason for hiding this comment

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

Since reloadButton isn't an actual button type from shiny, let's reword:

Animate a reload button and it's icon using Hover.css. Note that a reload button is just a shiny::actionButton with onClick behavior to reload or refresh a web browser.

#' Note that a reload button is just a shiny::actionButton
#' with onClick behavior to reload or refresh a web browser.
#'
#' @source <https://github.com/IanLunn/Hover>
#'
#' @param inputId The input slot that will be used to access the value.
#' @param label The contents of the button or link–usually a text label, but you could also use any other HTML, like an image.
#' @param icon An optional icon() to appear on the button.
#' @param button_animation The name of the button animation.
#' @param icon_animation The name of the icon animation.
#' @param width The width of the input, e.g. '400px', or '100 percent'; see validateCssUnit().
#' @param ... Named attributes to be applied to the button or link.
#' @md
#' @examples
#' if (interactive()) {
#' library(shiny)
#' library(hover)
#'
#' ui <- fluidPage(
#' use_hover(),
#' hover_reload_button(
#' inputId = "btn",
#' label = "hello hover!",
#' icon = icon("refresh"),
#' button_animation = "rotate",
#' icon_animation = "spin"
#' )
#' )
#'
#' server <- function(input, output, session) {
#'
#' }
#'
#' shinyApp(ui, server)
#' }
#' @export
hover_reload_button <- function(inputId, label, icon = NULL, button_animation = NULL, icon_animation = NULL, width = NULL, ...) {
value <- shiny::restoreInput(id = inputId, default = NULL)

if (!is.null(icon)) {
icon <- validateIcon(htmltools::tagAppendAttributes(icon, class = "hvr-icon"))
}

shiny::tags$button(
id = inputId,
style = if (!is.null(width)) paste0("width: ", shiny::validateCssUnit(width), ";"),
type = "button",
class = "btn btn-default reload-button",
class = if (!is.null(button_animation)) paste0("hvr-", button_animation),
class = if (!is.null(icon_animation)) paste0("hvr-icon-", icon_animation),
`data-val` = value,
list(icon, label),
onClick = "location.reload();",
...
)
}
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
validateIcon <- function (icon) {
if (is.null(icon) || identical(icon, character(0))) {
return(icon)
}
else if (inherits(icon, "shiny.tag") && icon$name == "i") {
return(icon)
}
else {
stop("Invalid icon. Use Shiny's 'icon()' function to generate a valid icon")
}
}
4 changes: 1 addition & 3 deletions man/animations.Rd

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

13 changes: 3 additions & 10 deletions man/hover_action_button.Rd

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

10 changes: 2 additions & 8 deletions man/hover_download_button.Rd

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

56 changes: 56 additions & 0 deletions man/hover_reload_button.Rd

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

24 changes: 24 additions & 0 deletions tests/testthat/test-hover_reload_button.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
test_that("hover_reload_button has expected structure", {

# Create testing input
test_me <- hover_reload_button("id", "label")

# Test 1: attribs type is a button
expect_equal(
## actual
test_me$attribs$type,
## expect
"button"
)

# Test 2: names on attribs list contains onClick
expect_true(any("onClick" %in% names(test_me$attribs)))

# Test 3: Look for javascript command
expect_equal(
## actual
test_me$attribs$onClick,
## expect
"location.reload();"
)
})