mirror of
https://github.com/rstudio/shiny.git
synced 2026-01-10 07:28:01 -05:00
Cleaner logic for conditional CSS styles (#2671)
* Cleaner logic for conditional CSS styles It's really only plotOutput() that behaves differently; previously it was not possible to specify a NULL width or height and not get broken styles * require dev version of htmltools Co-authored-by: Joe Cheng <joe@rstudio.com>
This commit is contained in:
@@ -701,37 +701,3 @@ flexfill <- function(..., direction, flex, width = width, height = height) {
|
||||
)
|
||||
do.call(tags$div, c(attrs, divArgs))
|
||||
}
|
||||
|
||||
css <- function(..., collapse_ = "") {
|
||||
props <- list(...)
|
||||
if (length(props) == 0) {
|
||||
return("")
|
||||
}
|
||||
|
||||
if (is.null(names(props)) || any(names(props) == "")) {
|
||||
stop("cssList expects all arguments to be named")
|
||||
}
|
||||
|
||||
# Necessary to make factors show up as level names, not numbers
|
||||
props[] <- lapply(props, paste, collapse = " ")
|
||||
|
||||
# Drop null args
|
||||
props <- props[!sapply(props, empty)]
|
||||
if (length(props) == 0) {
|
||||
return("")
|
||||
}
|
||||
|
||||
# Replace all '.' and '_' in property names to '-'
|
||||
names(props) <- gsub("[._]", "-", tolower(gsub("([A-Z])", "-\\1", names(props))))
|
||||
|
||||
# Create "!important" suffix for each property whose name ends with !, then
|
||||
# remove the ! from the property name
|
||||
important <- ifelse(grepl("!$", names(props), perl = TRUE), " !important", "")
|
||||
names(props) <- sub("!$", "", names(props), perl = TRUE)
|
||||
|
||||
paste0(names(props), ":", props, important, ";", collapse = collapse_)
|
||||
}
|
||||
|
||||
empty <- function(x) {
|
||||
length(x) == 0 || (is.character(x) && !any(nzchar(x)))
|
||||
}
|
||||
|
||||
@@ -1031,9 +1031,9 @@ textOutput <- function(outputId, container = if (inline) span else div, inline =
|
||||
#' @rdname textOutput
|
||||
verbatimTextOutput <- function(outputId, placeholder = FALSE) {
|
||||
pre(id = outputId,
|
||||
class = paste(c("shiny-text-output", if (!placeholder) "noplaceholder"),
|
||||
collapse = " ")
|
||||
)
|
||||
class = "shiny-text-output",
|
||||
class = if (!placeholder) "noplaceholder"
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1045,7 +1045,9 @@ imageOutput <- function(outputId, width = "100%", height="400px",
|
||||
inline = FALSE) {
|
||||
|
||||
style <- if (!inline) {
|
||||
paste("width:", validateCssUnit(width), ";", "height:", validateCssUnit(height))
|
||||
# Using `css()` here instead of paste/sprintf so that NULL values will
|
||||
# result in the property being dropped altogether
|
||||
css(width = validateCssUnit(width), height = validateCssUnit(height))
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ actionButton <- function(inputId, label, icon = NULL, width = NULL, ...) {
|
||||
value <- restoreInput(id = inputId, default = NULL)
|
||||
|
||||
tags$button(id=inputId,
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
type="button",
|
||||
class="btn btn-default action-button",
|
||||
`data-val` = value,
|
||||
|
||||
@@ -36,7 +36,7 @@ checkboxInput <- function(inputId, label, value = FALSE, width = NULL) {
|
||||
inputTag$attribs$checked <- "checked"
|
||||
|
||||
div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
div(class = "checkbox",
|
||||
tags$label(inputTag, tags$span(label))
|
||||
)
|
||||
|
||||
@@ -95,7 +95,7 @@ checkboxGroupInput <- function(inputId, label, choices = NULL, selected = NULL,
|
||||
|
||||
# return label and select tag
|
||||
tags$div(id = inputId,
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
class = divClass,
|
||||
shinyInputLabel(inputId, label),
|
||||
options
|
||||
|
||||
@@ -105,7 +105,7 @@ dateInput <- function(inputId, label, value = NULL, min = NULL, max = NULL,
|
||||
|
||||
tags$div(id = inputId,
|
||||
class = "shiny-date-input form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
|
||||
shinyInputLabel(inputId, label),
|
||||
tags$input(type = "text",
|
||||
|
||||
@@ -92,7 +92,7 @@ dateRangeInput <- function(inputId, label, start = NULL, end = NULL,
|
||||
attachDependencies(
|
||||
div(id = inputId,
|
||||
class = "shiny-date-range-input form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
|
||||
shinyInputLabel(inputId, label),
|
||||
# input-daterange class is needed for dropdown behavior
|
||||
|
||||
@@ -103,7 +103,7 @@ fileInput <- function(inputId, label, multiple = FALSE, accept = NULL,
|
||||
|
||||
|
||||
div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
|
||||
div(class = "input-group",
|
||||
|
||||
@@ -45,7 +45,7 @@ numericInput <- function(inputId, label, value, min = NA, max = NA, step = NA,
|
||||
inputTag$attribs$step = step
|
||||
|
||||
div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
inputTag
|
||||
)
|
||||
|
||||
@@ -33,7 +33,7 @@
|
||||
passwordInput <- function(inputId, label, value = "", width = NULL,
|
||||
placeholder = NULL) {
|
||||
div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
tags$input(id = inputId, type="password", class="form-control", value=value,
|
||||
placeholder = placeholder)
|
||||
|
||||
@@ -105,7 +105,7 @@ radioButtons <- function(inputId, label, choices = NULL, selected = NULL,
|
||||
if (inline) divClass <- paste(divClass, "shiny-input-container-inline")
|
||||
|
||||
tags$div(id = inputId,
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
class = divClass,
|
||||
shinyInputLabel(inputId, label),
|
||||
options
|
||||
|
||||
@@ -116,7 +116,7 @@ selectInput <- function(inputId, label, choices, selected = NULL,
|
||||
# return label and select tag
|
||||
res <- div(
|
||||
class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
div(selectTag)
|
||||
)
|
||||
|
||||
@@ -175,7 +175,7 @@ sliderInput <- function(inputId, label, min, max, value, step = NULL,
|
||||
})
|
||||
|
||||
sliderTag <- div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
do.call(tags$input, sliderProps)
|
||||
)
|
||||
|
||||
@@ -58,7 +58,7 @@ submitButton <- function(text = "Apply Changes", icon = NULL, width = NULL) {
|
||||
tags$button(
|
||||
type="submit",
|
||||
class="btn btn-primary",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
list(icon, text)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -40,7 +40,7 @@ textInput <- function(inputId, label, value = "", width = NULL,
|
||||
value <- restoreInput(id = inputId, default = value)
|
||||
|
||||
div(class = "form-group shiny-input-container",
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
style = css(width = validateCssUnit(width)),
|
||||
shinyInputLabel(inputId, label),
|
||||
tags$input(id = inputId, type="text", class="form-control", value=value,
|
||||
placeholder = placeholder)
|
||||
|
||||
@@ -50,17 +50,13 @@ textAreaInput <- function(inputId, label, value = "", width = NULL, height = NUL
|
||||
resize <- match.arg(resize, c("both", "none", "vertical", "horizontal"))
|
||||
}
|
||||
|
||||
style <- paste(
|
||||
style <- css(
|
||||
# The width is specified on the parent div.
|
||||
if (!is.null(width)) paste0("width: ", "100%", ";"),
|
||||
if (!is.null(height)) paste0("height: ", validateCssUnit(height), ";"),
|
||||
if (!is.null(resize)) paste0("resize: ", resize, ";")
|
||||
width = if (!is.null(width)) "width: 100%;",
|
||||
height = validateCssUnit(height),
|
||||
resize = resize
|
||||
)
|
||||
|
||||
# Workaround for tag attribute=character(0) bug:
|
||||
# https://github.com/rstudio/htmltools/issues/65
|
||||
if (length(style) == 0) style <- NULL
|
||||
|
||||
div(class = "form-group shiny-input-container",
|
||||
shinyInputLabel(inputId, label),
|
||||
style = if (!is.null(width)) paste0("width: ", validateCssUnit(width), ";"),
|
||||
|
||||
Reference in New Issue
Block a user