diff --git a/R/bootstrap.R b/R/bootstrap.R index 7adff9802..3fb8f29c0 100644 --- a/R/bootstrap.R +++ b/R/bootstrap.R @@ -1490,7 +1490,8 @@ downloadLink <- function(outputId, label="Download", class=NULL, ...) { #' #' @param name Name of icon. Icons are drawn from the #' \href{https://fontawesome.com/}{Font Awesome Free} (currently icons from -#' the v5.3.1 set are supported with the v4 naming convention) and +#' version \Sexpr[echo=TRUE,stage=build]{shiny:::fa_version} are supported, +#' along with backward compatibility for v4 icons) and #' \href{http://getbootstrap.com/components/#glyphicons}{Glyphicons} #' libraries. Note that the "fa-" and "glyphicon-" prefixes should not be used #' in icon names (i.e. the "fa-calendar" icon should be referred to as @@ -1498,7 +1499,13 @@ downloadLink <- function(outputId, label="Download", class=NULL, ...) { #' @param class Additional classes to customize the style of the icon (see the #' \href{http://fontawesome.io/examples/}{usage examples} for details on #' supported styles). -#' @param lib Icon library to use ("font-awesome" or "glyphicon") +#' @param lib Icon library to use. Can be "font-awesome" (the default), "fa5", +#' "fa4", or "glyphicon". If "font-awesome", that means to use the icon with +#' the given name from the current version of Font-Awesome +#' (\Sexpr[echo=TRUE,stage=build]{shiny:::fa_version}), and if it is not found +#' there, then use the icon from Font-Awesome 4. If "fa5", that means to use +#' Font-Awesome version 5 only; if "fa4", that means to use Font-Awesome +#' version 4 only. #' #' @return An icon element #' @@ -1508,8 +1515,11 @@ downloadLink <- function(outputId, label="Download", class=NULL, ...) { #' #' #' @examples -#' icon("calendar") # standard icon +#' icon("calendar") # Standard icon, from Font-Awesome 5 #' icon("calendar", "fa-3x") # 3x normal size +#' icon("calendar", lib = "fa4") # From Font-Awesome 4 +#' icon("battery") # This will use the "battery" icon from FA v4, +#' # because "battery" is not present in v5. #' icon("cog", lib = "glyphicon") # From glyphicon library #' #' # add an icon to a submit button @@ -1521,16 +1531,30 @@ downloadLink <- function(outputId, label="Download", class=NULL, ...) { #' tabPanel("Table", icon = icon("table")) #' ) #' @export -icon <- function(name, class = NULL, lib = c("font-awesome", "glyphicon")) { +icon <- function( + name, + class = NULL, + lib = c("font-awesome", "fa4", "fa5", "glyphicon") +) { lib <- match.arg(lib) + if (lib == "fa4") { + lib <- "font-awesome" + fa_ver <- "v4" + } else if (lib == "fa5") { + lib <- "font-awesome" + fa_ver <- "v5" + } else if (lib == "font-awesome") { + fa_ver <- "default" + } + # build the icon class (allow name to be null so that other functions # e.g. buildTabset can pass an explicit class value) iconClass <- "" if (!is.null(name)) { if (lib == "font-awesome") { - prefix_class <- find_fa_prefix(name) + prefix_class <- find_fa_prefix(name, fa_ver) prefix <- "fa" } else{ prefix_class <- "glyphicon" @@ -1566,11 +1590,27 @@ iconClass <- function(icon) { } # Returns the default Font-Awesome CSS prefix class to use for a given icon. -find_fa_prefix <- function(name) { - prefix <- fa_icons$default[fa_icons$name == name] - if (length(prefix == 1)) { - return(prefix) +find_fa_prefix <- function(name, fa_ver = c("default", "v5", "v4")) { + fa_ver <- match.arg(fa_ver) + + idx <- (fa_icons$name == name) + if (sum(idx) != 1) { + stop("Unknown icon name in Font-Awesome: ", name) } - stop("Unknown icon name in Font-Awesome ", fa_version, ": ", name) + if (fa_ver == "default") { + prefix <- fa_icons$default[idx] + } else if (fa_ver == "v5") { + prefix <- fa_icons$default_v5[idx] + } else if (fa_ver == "v4") { + prefix <- fa_icons$default_v4[idx] + } + + if (is.na(prefix)) { + # Will only get here for v4 or v5; there are no NA's in the `default` + # column. + stop("Unknown icon name in Font-Awesome (", fa_ver, "): ", name) + } + + prefix } diff --git a/R/sysdata.rda b/R/sysdata.rda index 473a610e5..7879148bb 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/man/icon.Rd b/man/icon.Rd index 6c1830871..214a0377b 100644 --- a/man/icon.Rd +++ b/man/icon.Rd @@ -4,12 +4,14 @@ \alias{icon} \title{Create an icon} \usage{ -icon(name, class = NULL, lib = c("font-awesome", "glyphicon")) +icon(name, class = NULL, lib = c("font-awesome", "fa4", "fa5", + "glyphicon")) } \arguments{ \item{name}{Name of icon. Icons are drawn from the \href{https://fontawesome.com/}{Font Awesome Free} (currently icons from -the v5.3.1 set are supported with the v4 naming convention) and +version \Sexpr[echo=TRUE,stage=build]{shiny:::fa_version} are supported, +along with backward compatibility for v4 icons) and \href{http://getbootstrap.com/components/#glyphicons}{Glyphicons} libraries. Note that the "fa-" and "glyphicon-" prefixes should not be used in icon names (i.e. the "fa-calendar" icon should be referred to as @@ -19,7 +21,13 @@ in icon names (i.e. the "fa-calendar" icon should be referred to as \href{http://fontawesome.io/examples/}{usage examples} for details on supported styles).} -\item{lib}{Icon library to use ("font-awesome" or "glyphicon")} +\item{lib}{Icon library to use. Can be "font-awesome" (the default), "fa5", +"fa4", or "glyphicon". If "font-awesome", that means to use the icon with +the given name from the current version of Font-Awesome +(\Sexpr[echo=TRUE,stage=build]{shiny:::fa_version}), and if it is not found +there, then use the icon from Font-Awesome 4. If "fa5", that means to use +Font-Awesome version 5 only; if "fa4", that means to use Font-Awesome +version 4 only.} } \value{ An icon element @@ -30,8 +38,11 @@ of a button, or as an icon for a \code{\link{tabPanel}} within a \code{\link{navbarPage}}. } \examples{ -icon("calendar") # standard icon +icon("calendar") # Standard icon, from Font-Awesome 5 icon("calendar", "fa-3x") # 3x normal size +icon("calendar", lib = "fa4") # From Font-Awesome 4 +icon("battery") # This will use the "battery" icon from FA v4, + # because "battery" is not present in v5. icon("cog", lib = "glyphicon") # From glyphicon library # add an icon to a submit button diff --git a/tools/updateFontAwesome.R b/tools/updateFontAwesome.R index 3a5f60313..d73252727 100644 --- a/tools/updateFontAwesome.R +++ b/tools/updateFontAwesome.R @@ -73,11 +73,6 @@ copy_files(source_dir, dest_dir, filenames) # v4-shims.css file will make "fa fa-calendar" (this is V4 syntax) display one # icon (which is very similar to the V4 version), and "fas fa-calendar" (V5 # syntax) display a different icon. -# -# In this version of Shiny, we decided to minimize visible changes for the same -# code. That means that if someone uses icon("calendar"), they will get "fa -# fa-calendar". In the future we may change it so that it defaults to "fas -# fa-calendar", which will result in the V5 icon. # Find the icons that existed in version 4. These icons will have the CSS class # "fa". @@ -113,17 +108,32 @@ fa_icons$fab[is.na(fa_icons$fab)] <- FALSE fa_icons$fas[is.na(fa_icons$fas)] <- FALSE fa_icons$far[is.na(fa_icons$far)] <- FALSE +# The default behavior is to try to provide the v5 icon (for a given name) if +# available, and if not, then provide the v4 icon. fa_icons$default <- - ifelse(fa_icons$fa, "fa", - ifelse(fa_icons$fab, "fab", - ifelse(fa_icons$fas, "fas", - ifelse(fa_icons$far, "far", + ifelse(fa_icons$fab, "fab", + ifelse(fa_icons$fas, "fas", + ifelse(fa_icons$far, "far", + ifelse(fa_icons$fa, "fa", NA_character_ ) ) ) ) +# If the user specifies v4, then only use the v4 icon. +fa_icons$default_v4 <- ifelse(fa_icons$fa, "fa", NA_character_) + +# If the user specifies v5, then only use the v5 icon. +fa_icons$default_v5 <- + ifelse(fa_icons$fab, "fab", + ifelse(fa_icons$fas, "fas", + ifelse(fa_icons$far, "far", + NA_character_ + ) + ) + ) + fa_version <- version message("Writing fa_icons and fa_version to R/sysdata.rda")