Skip to content

Commit

Permalink
Improve & test iconset code
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed May 9, 2024
1 parent 31b5d70 commit 89db291
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
8 changes: 7 additions & 1 deletion R/navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,14 @@ bs4_navbar_links_tags <- function(links, depth = 0L, side = "left") {

bs4_navbar_link_text <- function(x, ...) {
if (!is.null(x$icon)) {
# Find the icon set
classes <- strsplit(x$icon, " ")[[1]]
icon_classes <- classes[grepl("-", classes)]
iconset <- purrr::map_chr(strsplit(icon_classes, "-"), 1)
class <- paste0(unique(c(iconset, classes)), collapse = " ")

text <- paste0(if (!is.null(x$text)) " ", x$text)
htmltools::tagList(htmltools::tags$span(class = x$icon), text, ...)
htmltools::tagList(htmltools::tags$span(class = class), text, ...)
} else {
htmltools::tagList(x$text, ...)
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/navbar.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
data_navbar(pkg)[c("left", "right")]
Output
$left
[1] "<li class=\"nav-item\">\n <a class=\"nav-link\" href=\"https://github.com/r-lib/pkgdown/\" aria-label=\"github\">\n <span class=\"fab fa fab fa-github fa-lg\"></span>\n \n </a>\n</li>\n<li><form class=\"form-inline\" role=\"search\">\n<input type=\"search\" class=\"form-control\" name=\"search-input\" id=\"search-input\" autocomplete=\"off\" aria-label=\"Search site\" placeholder=\"Search for\" data-search-index=\"search.json\">\n</form></li>"
[1] "<li class=\"nav-item\">\n <a class=\"nav-link\" href=\"https://github.com/r-lib/pkgdown/\" aria-label=\"github\">\n <span class=\"fa fab fa-github fa-lg\"></span>\n </a>\n</li>\n<li><form class=\"form-inline\" role=\"search\">\n<input type=\"search\" class=\"form-control\" name=\"search-input\" id=\"search-input\" autocomplete=\"off\" aria-label=\"Search site\" placeholder=\"Search for\" data-search-index=\"search.json\">\n</form></li>"
$right
[1] "<li class=\"nav-item\">\n <a class=\"nav-link\" href=\"news/index.html\">Changelog</a>\n</li>"
Expand Down Expand Up @@ -245,7 +245,7 @@
<a class="nav-link" href="reference/index.html">Reference</a>
</li>
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</a>
<button href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" type="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</button>
<div class="dropdown-menu" aria-labelledby="dropdown-articles">
<a class="dropdown-item" href="articles/linking.html">Auto-linking</a>
<a class="dropdown-item" href="articles/search.html">Search</a>
Expand All @@ -265,7 +265,7 @@
cat(render_navbar_links(x, pkg = list(bs_version = 4)))
Output
<li class="nav-item dropdown">
<a href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" role="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</a>
<button href="#" class="nav-link dropdown-toggle" data-bs-toggle="dropdown" type="button" aria-expanded="false" aria-haspopup="true" id="dropdown-articles">Articles</button>
<div class="dropdown-menu" aria-labelledby="dropdown-articles">
<h6 class="dropdown-header" data-toc-skip>First section</h6>
<a class="dropdown-item" href="articles/search.html">Search</a>
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-navbar.R
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,15 @@ test_that("can render search helper", {
expect_snapshot({
bs4_navbar_links_tags(list(menu = list(search = TRUE)))
})
})

test_that("icons extract icon set", {
expect_equal(
as.character(bs4_navbar_link_text(menu_icon("github", ""))),
'<span class="fa fas fa-github fa-lg"></span>'
)
expect_equal(
as.character(bs4_navbar_link_text(menu_icon("github", "", style = "fab"))),
'<span class="fa fab fa-github fa-lg"></span>'
)
})

0 comments on commit 89db291

Please sign in to comment.