Skip to content

Commit

Permalink
added show_toast
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed May 13, 2020
1 parent e72ba6b commit da8cac8
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 5 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export(setShadow)
export(setSliderColor)
export(shinyWidgetsGallery)
export(showDropMenu)
export(show_toast)
export(sliderTextInput)
export(spectrumInput)
export(switchInput)
Expand Down
36 changes: 31 additions & 5 deletions R/sweetalert.R
Original file line number Diff line number Diff line change
Expand Up @@ -722,10 +722,37 @@ progressSweetAlert <- function(session, id, value, total = NULL,



toastSweetAlert <- function(title, text, type = NULL,
timer = 2000, position = "bottom-end",
animation = TRUE, width = NULL,
session = shiny::getDefaultReactiveDomain()) {
#' Show a toast notification
#'
#' @param title Title for the toast.
#' @param text Text for the toast.
#' @param type Type of the toast: \code{"default"},
#' \code{"success"}, \code{"error"}, \code{"info"},
#' \code{"warning"} or \code{"question"}.
#' @param timer Auto close timer of the modal. Set in ms (milliseconds).
#' @param position Modal window position, can be \code{"top"}, \code{"top-start"},
#' \code{"top-end"}, \code{"center"}, \code{"center-start"}, \code{"center-end"},
#' \code{"bottom"}, \code{"bottom-start"}, or \code{"bottom-end"}.
#' @param width Modal window width, including paddings.
#' @param session The \code{session} object passed to function given to shinyServer.
#'
#' @return No value.
#' @export
#'
#' @example examples/show_toast.R
show_toast <- function(title, text,
type = c("default", "success", "error",
"info", "warning", "question"),
timer = 3000,
position = c("bottom-end", "top", "top-start",
"top-end", "center", "center-start",
"center-end", "bottom", "bottom-start"),
width = NULL,
session = shiny::getDefaultReactiveDomain()) {
type <- match.arg(type)
if (identical(type, "default"))
type <- NULL
position <- match.arg(position)
insertUI(
selector = "body",
where = "afterBegin",
Expand All @@ -745,7 +772,6 @@ toastSweetAlert <- function(title, text, type = NULL,
type = type,
toast = TRUE,
timer = timer,
animation = animation,
width = width,
showConfirmButton = FALSE,
showCloseButton = TRUE
Expand Down
80 changes: 80 additions & 0 deletions examples/show_toast.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
tags$h2("Sweet Alert Toast"),
actionButton(
inputId = "toast",
label = "Show default toast"
),
actionButton(
inputId = "success",
label = "Show success toast",
icon = icon("check")
),
actionButton(
inputId = "error",
label = "Show error toast",
icon = icon("remove")
),
actionButton(
inputId = "warning",
label = "Show warning toast",
icon = icon("exclamation-triangle")
),
actionButton(
inputId = "info",
label = "Show info toast",
icon = icon("info")
)
)

server <- function(input, output, session) {

observeEvent(input$toast, {
show_toast(
title = "Notification",
text = "An imortant message"
)
})

observeEvent(input$success, {
show_toast(
title = "Bravo",
text = "Well done!",
type = "success"
)
})

observeEvent(input$error, {
show_toast(
title = "Ooops",
text = "It's broken",
type = "error",
width = "800px",
position = "bottom"
)
})

observeEvent(input$warning, {
show_toast(
title = "Careful!",
text = "Almost broken",
type = "warning",
position = "top-end"
)
})

observeEvent(input$info, {
show_toast(
title = "Heads up",
text = "Just a message",
type = "info",
position = "top-end"
)
})
}

if (interactive())
shinyApp(ui, server)
124 changes: 124 additions & 0 deletions man/show_toast.Rd

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

0 comments on commit da8cac8

Please sign in to comment.