Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update markup of BS5 tabs + conditional TOC JS inclusion #1890

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion R/build-home-index.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ data_home <- function(pkg = ".") {
sidebar = data_home_sidebar(pkg),
opengraph = list(description = pkg$meta$home[["description"]] %||%
cran_unquote(pkg$desc$get("Description")[[1]])),
has_trailingslash = pkg$meta$template$trailing_slash_redirect %||% FALSE
has_trailingslash = pkg$meta$template$trailing_slash_redirect %||% FALSE,
has_toc = "toc" %in% pkg$meta$home[["structure"]]
))
}

Expand Down
4 changes: 4 additions & 0 deletions R/render.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
render_page <- function(pkg = ".", name, data, path = "", depth = NULL, quiet = FALSE) {
pkg <- as_pkgdown(pkg)

if (is.null(data$has_toc)) {
data$has_toc <- TRUE
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing I only tested quickly is creating a vignette with tabsets and no h2: then the tabs work because there's a TOC even if it's empty.

}

if (is.null(depth)) {
depth <- length(strsplit(path, "/")[[1]]) - 1L
}
Expand Down
14 changes: 8 additions & 6 deletions R/tweak-tabset.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ tweak_tabset <- function(div) {

# Add empty ul for nav and div for content
nav_class <- if (has_class(div, "tabset-pills")) {
"nav nav-pills nav-row"
"nav nav-pills"
} else {
"nav nav-tabs nav-row"
"nav nav-tabs"
}
fade <- has_class(div, "tabset-fade")

Expand All @@ -34,7 +34,9 @@ tweak_tabset <- function(div) {

# if not tabs active, activate the first tab
if (!any(has_class(xml2::xml_children(content), "active"))) {
tweak_class_prepend(xml2::xml_find_first(nav, ".//li/a"), "active")
first_tab <- xml2::xml_find_first(nav, ".//li/button")
tweak_class_prepend(first_tab, "active")
xml2::xml_attr(first_tab, "aria-selected") <- TRUE

tab_class <- paste("active", if (has_class(div, "tabset-fade")) "show")
tweak_class_prepend(xml2::xml_child(content), tab_class)
Expand All @@ -49,12 +51,12 @@ tablist_item <- function(tab, nav, parent_id) {
# Activate (if there was "{.active}" in the source Rmd)
active <- has_class(tab, "active")
li_class <- paste0("nav-link", if (active) " active")

li <- xml2::xml_add_child(nav, "li", role = "presentation", class = "nav-item")
xml2::xml_add_child(li, "a", title,
xml2::xml_add_child(li, "button", title,
`data-bs-toggle` = "tab",
`data-bs-target` = paste0("#", id),
id = paste0(id, "-tab"),
href = paste0("#", id),
type = "button",
role = "tab",
`aria-controls` = id,
`aria-selected` = tolower(active),
Expand Down
15 changes: 15 additions & 0 deletions inst/BS5/assets/pkgdown-toc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* http://gregfranko.com/blog/jquery-best-practices/ */
(function($) {
$(function() {
Toc.init({
$nav: $("#toc"),
$scope: $(".contents h2, .contents h3, .contents h4, .contents h5,.contents h6")
});
$('body').scrollspy({
target: '#toc',
offset: 56 // headroom height
});
});
})(window.jQuery || window.$)


10 changes: 0 additions & 10 deletions inst/BS5/assets/pkgdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,6 @@

$('nav.navbar').headroom();

Toc.init({
$nav: $("#toc"),
$scope: $(".contents h2, .contents h3, .contents h4, .contents h5,.contents h6")
});

$('body').scrollspy({
target: '#toc',
offset: 56 // headroom height
});

// Activate popovers
$('[data-bs-toggle="popover"]').popover({
container: 'body',
Expand Down
3 changes: 3 additions & 0 deletions inst/BS5/templates/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous" />

{{#has_toc}}
<!-- bootstrap-toc -->
<script src="https://cdn.rawgit.com/afeld/bootstrap-toc/v1.0.1/dist/bootstrap-toc.min.js"></script>
<script src="{{#site}}{{root}}{{/site}}pkgdown-toc.js"></script>
{{/has_toc}}

<!-- headroom.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script>
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/_snaps/tweak-tabset.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</h1>


<ul class="nav nav-pills nav-row" id="tabset" role="tablist">
<li role="presentation" class="nav-item"><a data-bs-toggle="tab" id="tab-1-tab" href="#tab-1" role="tab" aria-controls="tab-1" aria-selected="false" class="active nav-link">Tab 1</a></li>
<li role="presentation" class="nav-item"><a data-bs-toggle="tab" id="tab-2-tab" href="#tab-2" role="tab" aria-controls="tab-2" aria-selected="false" class="nav-link">Tab 2</a></li>
<ul class="nav nav-pills" id="tabset" role="tablist">
<li role="presentation" class="nav-item"><button data-bs-toggle="tab" data-bs-target="#tab-1" id="tab-1-tab" type="button" role="tab" aria-controls="tab-1" aria-selected="TRUE" class="active nav-link">Tab 1</button></li>
<li role="presentation" class="nav-item"><button data-bs-toggle="tab" data-bs-target="#tab-2" id="tab-2-tab" type="button" role="tab" aria-controls="tab-2" aria-selected="false" class="nav-link">Tab 2</button></li>
</ul>
<div class="tab-content">
<div class="active tab-pane" id="tab-1" role="tabpanel" aria-labelledby="tab-1-tab">
Expand Down