From 3ce06f063b52a6cde3b31a06a4c6b07849836687 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Fri, 14 Jul 2023 17:00:25 -0500 Subject: [PATCH] Allow non `tag()` object to be used for a `nav_panel()`/`nav_menu()` icon (#645) --- NEWS.md | 1 + R/navs-legacy.R | 19 +++++-------------- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/NEWS.md b/NEWS.md index 4cde02346..6297735ee 100644 --- a/NEWS.md +++ b/NEWS.md @@ -13,6 +13,7 @@ * Closed #636: Outputs in sidebars now work as expected when an initially-closed sidebar is opened. (#624) * Closed #640: `accordion()` no longer errors when an `id` isn't supplied inside a Shiny `session` context. (#646) +* Closed #639: `nav_panel()`'s `icon` argument now supports generic `HTML()`, meaning that things like `bsicons::bs_icon()` and `fontawesome::fa()` can be used as values. (#645) # bslib 0.5.0 diff --git a/R/navs-legacy.R b/R/navs-legacy.R index e9418669e..0b92432fd 100644 --- a/R/navs-legacy.R +++ b/R/navs-legacy.R @@ -295,7 +295,7 @@ navbarMenu_ <- function(title, ..., menuName = title, icon = NULL, align) { tabs = list2(...), # Here for legacy reasons # https://github.com/cran/miniUI/blob/74c87d3/R/layout.R#L369 - iconClass = tagGetAttribute(icon, "class"), + iconClass = if (inherits(icon, "shiny.tag")) tagGetAttribute(icon, "class"), icon = icon, align = align ), @@ -315,7 +315,7 @@ tabPanel_ <- function(title, ..., value = title, icon = NULL) { `data-value` = value, # Here for legacy reasons # https://github.com/cran/miniUI/blob/74c87d/R/layout.R#L395 - `data-icon-class` = tagGetAttribute(icon, "class"), + `data-icon-class` = if (inherits(icon, "shiny.tag")) tagGetAttribute(icon, "class"), ... ) attr(pane, "_shiny_icon") <- icon @@ -440,21 +440,12 @@ findAndMarkSelectedTab <- function(tabs, selected, foundSelected) { } prepTabIcon <- function(x = NULL) { - if (is.null(x)) return(NULL) - if (!inherits(x, "shiny.tag")) { - stop( - "`icon` must be a `shiny.tag` object. ", - "Try passing `icon()` (or `tags$i()`) to the `icon` parameter.", - call. = FALSE - ) - } + if (!inherits(x, "shiny.tag")) return(x) is_fa <- grepl("fa-", tagGetAttribute(x, "class") %||% "", fixed = TRUE) - if (!is_fa) { - return(x) - } + if (!is_fa) return(x) - # for font-awesome we specify fixed-width + # specify fixed-width for font-awesome tagAppendAttributes(x, class = "fa-fw") }