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

Add removeToast #44

Merged
merged 8 commits into from
Jul 21, 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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ License: MIT + file LICENSE
LazyData: true
Depends:
R (>= 3.1.2)
RoxygenNote: 7.1.0
RoxygenNote: 7.1.1
Encoding: UTF-8
Suggests:
knitr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(feedbackDanger)
export(feedbackSuccess)
export(feedbackWarning)
export(hideFeedback)
export(hideToast)
export(loadingButton)
export(resetLoadingButton)
export(showFeedback)
Expand Down
28 changes: 28 additions & 0 deletions R/hideToast.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


#' Hide existing toast messages
#'
#' @param animate a logical indicating whether to remove the toast message(s) instantly or use its \code{hideMethod} with animations to remove (Default).
#' @param session the Shiny session. Defaults to \code{shiny::getDefaultReactiveDomain()}.
#'
#' @export
#'
#' @importFrom shiny getDefaultReactiveDomain
#'
#' @return `invisible()`
#'
hideToast <- function(
animate = TRUE,
session = shiny::getDefaultReactiveDomain()
) {

session$sendCustomMessage(
type = "hide_toastr",
message = list(
'animate' = animate
)
)

invisible()
}

7 changes: 7 additions & 0 deletions R/showToast.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#' @param type length 1 character vector. Valid values are "success", "error", "warning", and "info"
#' @param message the toast message
#' @param title the toast title. Defaults to \code{NULL}
#' @param keepVisible a logical. If \code{TRUE}, the toast notification will remain visible until removed with \code{\link{hideToast}}. If \code{FALSE}, the default, the toast will automatically hide once the "showDuration" option has elapsed.
#' @param .options other options to pass to the \code{toastr} JavaScript library. See
#' \url{https://codeseven.github.io/toastr/demo.html} for a full demo of options.
#' @param session the Shiny session. Defaults to \code{shiny::getDefaultReactiveDomain()}.
Expand All @@ -23,6 +24,7 @@ showToast <- function(
type,
message,
title = NULL,
keepVisible = FALSE,
.options = list(
positionClass = "toast-bottom-center",
progressBar = TRUE,
Expand All @@ -43,6 +45,11 @@ showToast <- function(
session = shiny::getDefaultReactiveDomain()
) {

if (isTRUE(keepVisible)) {
.options$timeOut <- 0
.options$extendedTimeOut <- 0
}

session$sendCustomMessage(
type = "toastr",
message = list(
Expand Down
13 changes: 13 additions & 0 deletions inst/assets/toastr/js/shinytoastr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@ Shiny.addCustomMessageHandler(
);
}
);

Shiny.addCustomMessageHandler(
"hide_toastr",
function(message) {

if (message.animate) {
toastr.clear();
} else {
toastr.remove();
}

}
);
7 changes: 7 additions & 0 deletions inst/examples/supported_inputs_app/server.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ function(input, output, session) {
})


observeEvent(input$removeToast, {
hideToast()
})
observeEvent(input$removeToastASAP, {
hideToast(animate = FALSE)
})


observeEvent(input$mySelectizeInput, {

Expand Down
12 changes: 12 additions & 0 deletions inst/examples/supported_inputs_app/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ fluidPage(
class = "btn btn-danger",
loadingSpinner = "cog",
loadingLabel = "Cancelling..."
),
br(),
h2("Remove Toast"),
actionButton(
"removeToast",
"Remove Toast",
icon = icon("trash-o")
),
actionButton(
"removeToastASAP",
"Remove Toast ASAP",
icon = icon("trash-o")
)

),
Expand Down
19 changes: 19 additions & 0 deletions man/hideToast.Rd

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

3 changes: 3 additions & 0 deletions man/showToast.Rd

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