-
Notifications
You must be signed in to change notification settings - Fork 65
Improve examples in navs_tab() and family
#521
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
067c7e1
Expand `navs` examples into full examples with embedded screenshots
gadenbuie 9a37c54
Re-render screenshots in CI
gadenbuie 3c9cef2
ci: Add optipng shrinking of man/figures to routine
gadenbuie 9e29ae7
`devtools::document()` (GitHub Actions)
gadenbuie 9ac299b
Resave data (GitHub Action)
gadenbuie 5ee3b67
ci: actually commit the shrunk pngs
gadenbuie 9e4953a
Wait for chromote session to initialize
gadenbuie c5647e8
Make sure we await the page load event while navigating
gadenbuie 542d6cd
`devtools::document()` (GitHub Actions)
gadenbuie f9b33b3
Shrink images in man/figures (Github Action)
gadenbuie 08c6e63
Prove example screenshots work as expected
gadenbuie be1b825
`devtools::document()` (GitHub Actions)
gadenbuie 4849215
Shrink images in man/figures (Github Action)
gadenbuie 3b22f76
don't shrink images (will happen with routine)
gadenbuie 814b266
fix typo
gadenbuie 3a11a75
Add links and other small `navs_*_card()` details
gadenbuie 28970d8
Merge 'origin/main' into 'navs-tab-examples'
gadenbuie 4068610
`devtools::document()` (GitHub Actions)
gadenbuie 4d6c140
A small, meaningless change to test CI
gadenbuie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| *.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,228 @@ | ||
| ```{r setup, include = FALSE} | ||
gadenbuie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| library(bslib) | ||
| ## Screenshots are created during `devtools::document()` in CI | ||
| ## or set to `TRUE` below to test or update locally | ||
| SCREENSHOT <- nzchar(Sys.getenv("CI", "")) || FALSE | ||
| ``` | ||
|
|
||
| ```{r include = FALSE} | ||
| if (isTRUE(SCREENSHOT)) { | ||
| library(chromote) | ||
| b <- ChromoteSession$new(width = 800, height = 300, wait_ = TRUE) | ||
| } | ||
|
|
||
| render_screenshot <- function(x, options) { | ||
| if (inherits(x, "bslib_fragment")) { | ||
| x <- attr(x, "bslib_page")(x) | ||
| } | ||
| x <- tagList( | ||
| x, | ||
| tags$head(tags$style(" | ||
| body { min-height: 300px; padding: 1rem } | ||
| .bslib-card .card-body { min-height: 200px; } | ||
| ")) | ||
| ) | ||
| tmpdir <- tempfile() | ||
| dir.create(tmpdir) | ||
| on.exit(unlink(tmpdir, recursive = TRUE)) | ||
|
|
||
| tmpfile <- file.path(tmpdir, basename(tempfile(fileext = ".html"))) | ||
| save_html(x, tmpfile) | ||
|
|
||
| file_out <- paste0(options$label, ".png") | ||
|
|
||
| { | ||
| p <- b$Page$loadEventFired(wait_ = FALSE) | ||
| b$Page$navigate(paste0("file://", tmpfile), wait_ = FALSE) | ||
| b$wait_for(p) | ||
| } | ||
|
|
||
| b$Runtime$evaluate( | ||
| "if ($('.dropdown-toggle').length > 0) $('.dropdown-toggle').dropdown('toggle')" | ||
| ) | ||
| b$screenshot(filename = file.path( | ||
| rprojroot::find_package_root_file(), | ||
| "man/figures/", file_out | ||
| )) | ||
|
|
||
| invisible(file_out) | ||
| } | ||
|
|
||
| include_screenshot <- function(x, options) { | ||
| file_name <- paste0(options$label, ".png") | ||
| alt <- options$fig.alt | ||
| if (is.null(alt)) { | ||
| alt <- sprintf("Screenshot of a %s() example.", options$label) | ||
| } | ||
| img <- sprintf('', file_name, alt) | ||
| knitr::asis_output(img) | ||
| } | ||
|
|
||
| knitr::opts_chunk$set( | ||
| render = function(x, options) { | ||
| if (isTRUE(SCREENSHOT)) { | ||
| render_screenshot(x, options) | ||
| } | ||
| include_screenshot(x, options) | ||
| } | ||
| ) | ||
| ``` | ||
|
|
||
| ## A basic example | ||
|
|
||
| This first example creates a simple tabbed navigation container with two tabs. | ||
| The tab name and the content of each tab are specified in the `nav()` calls | ||
| and `navs_tab()` creates the tabbed navigation around these two tabs. | ||
|
|
||
| ```{r navs_tab-basic, fig.alt = "Screenshot of a basic navs_tab() example."} | ||
| library(htmltools) | ||
|
|
||
| navs_tab( | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")) | ||
| ) | ||
| ``` | ||
|
|
||
| In the rest of the examples, we'll include links among the tabs (or pills) in the navigation controls. | ||
|
|
||
| ```{r echo = TRUE, eval = TRUE, results="hide"} | ||
| link_shiny <- tags$a(shiny::icon("github"), "Shiny", href = "https://github.com/rstudio/shiny", target = "_blank") | ||
| link_posit <- tags$a(shiny::icon("r-project"), "Posit", href = "https://posit.co", target = "_blank") | ||
| ``` | ||
|
|
||
| ## `navs_tab()` | ||
|
|
||
| You can fully customize the controls in the navigation component. | ||
| In this example, we've added a direct link to the Shiny repository using `nav_item()`. | ||
| We've also included a dropdown menu using `nav_menu()` | ||
| containing an option to select a third tab panel | ||
| and another direct link to Posit's website. | ||
| Finally, we've separated the primary tabs on the left | ||
| from the direct link and dropdown menu on the right | ||
| using `nav_spacer()`. | ||
|
|
||
| ```{r navs_tab} | ||
| navs_tab( | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third tab content")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## `navs_pill()` | ||
|
|
||
| `navs_pill()` creates a navigation container that behaves exactly like `navs_tab()`, | ||
| but the tab toggles are _pills_ or button-shaped. | ||
|
|
||
| ```{r navs_pill} | ||
| navs_pill( | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third tab content")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## `navs_tab_card()` | ||
|
|
||
| The tabbed navigation container can also be used in a `card()` component | ||
| thanks to `navs_tab_card()`. | ||
| Learn more about this approach in the | ||
| [article about Cards](https://pkgs.rstudio.com/bslib/articles/cards.html), | ||
| including how to add [a shared sidebar](https://pkgs.rstudio.com/bslib/articles/sidebars.html#multi-page-layout) | ||
| to all tabs in the card | ||
| using the `sidebar` argument of `navs_tab_card()`. | ||
|
|
||
| ```{r navs_tab_card} | ||
| navs_tab_card( | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third tab content")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## `navs_pill_card()` | ||
|
|
||
| Similar to `navs_pill()`, | ||
gadenbuie marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| `navs_pill_card()` provides a pill-shaped variant to `navs_tab_card()`. | ||
| You can use the `placement` argument to position the navbar | ||
| `"above"` or `"below"` the card body. | ||
|
|
||
| ```{r navs_pill_card} | ||
| navs_pill_card( | ||
| placement = "above", | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third tab content")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## `navs_pill_list()` | ||
|
|
||
| Furthermore, `navs_pill_list()` creates a vertical list of navigation controls | ||
| adjacent to, rather than on top of, the tab content panels. | ||
|
|
||
| ```{r navs_pill_list} | ||
| navs_pill_list( | ||
| nav(title = "One", p("First tab content.")), | ||
| nav(title = "Two", p("Second tab content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third tab content")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| ## `page_navbar()` | ||
|
|
||
| Finally, `page_navbar()` provides full-page navigation container | ||
| similar to `navs_tab()` but where each `nav()` is treated as a full page of content | ||
| and the navigation controls appear in a top-level navigation bar. | ||
|
|
||
| ```{r page_navbar} | ||
| page_navbar( | ||
| title = "My App", | ||
| bg = "#0062cc", | ||
| nav(title = "One", p("First page content.")), | ||
| nav(title = "Two", p("Second page content.")), | ||
| nav_spacer(), | ||
| nav_item(link_shiny), | ||
| nav_menu( | ||
| title = "Other links", | ||
| align = "right", | ||
| nav("Three", p("Third page content.")), | ||
| nav_item(link_posit) | ||
| ) | ||
| ) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.