To customize the CSS class properties (specifically .shiny-notification
), you can utilize tags$style
. This allows you to modify other attributes such as width and height as well.
Here is the CSS snippet:
.shiny-notification {
position:fixed;
top: calc(50%);
left: calc(50%);
}
This code positions the notification at 50% of the screen's width and 50% of its height.
Incorporating the css code within shiny involves adding the following structure in the ui
function.
tags$head(
tags$style(
HTML(CSS-CODE....)
)
)
A comprehensive demonstration app can be found below:
library(shiny)
shinyApp(
ui = fluidPage(
tags$head(
tags$style(
HTML(".shiny-notification {
position:fixed;
top: calc(50%);
left: calc(50%);
}
"
)
)
),
textInput("txt", "Content", "Text of message"),
radioButtons("duration", "Seconds before fading out",
choices = c("2", "5", "10", "Never"),
inline = TRUE
),
radioButtons("type", "Type",
choices = c("default", "message", "warning", "error"),
inline = TRUE
),
checkboxInput("close", "Close button?", TRUE),
actionButton("show", "Show"),
actionButton("remove", "Remove most recent")
),
server = function(input, output) {
id <- NULL
observeEvent(input$show, {
if (input$duration == "Never")
duration <- NA
else
duration <- as.numeric(input$duration)
type <- input$type
if (is.null(type)) type <- NULL
id <<- showNotification(
input$txt,
duration = duration,
closeButton = input$close,
type = type
)
})
observeEvent(input$remove, {
removeNotification(id)
})
}
)
The app template used here was sourced from the code provided in your initial link.