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

save_html() now works correctly with components and pages with non-default theme values #823

Merged
merged 8 commits into from
Oct 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Imports:
base64enc,
cachem,
grDevices,
htmltools (>= 0.5.4.9000),
htmltools (>= 0.5.6.9001),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

main should already be on 0.5.6.9000 🙈

jquerylib (>= 0.1.3),
jsonlite,
lifecycle,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ S3method(print,bslib_fragment)
S3method(print,bslib_page)
S3method(print,bslib_showcase_layout)
S3method(print,bslib_value_box_theme)
S3method(save_html,bslib_fragment)
S3method(save_html,bslib_page)
export(accordion)
export(accordion_panel)
export(accordion_panel_close)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

* Improved the style and appearance of the button to enter full screen in `card()`s and `value_box()`es to better adapt to Bootstrap's dark mode. (#780)

* `htmltools::save_html()` now works as expected when applied directly to components (e.g., `card()`, etc) and pages with a non-default theme. (#823, #815)

* `layout_sidebar()` received a new design. The button to collapse and expand the sidebar now appears at the top edge of the sidebar, and we now use the [arrow-bar-left](https://icons.getbootstrap.com/icons/arrow-bar-left/) icon instead of [chevron-left](https://icons.getbootstrap.com/icons/chevron-left/). On mobile devices, the sidebar now fills the `layout_sidebar()` area as an overlay, rather than expanding from above the main content area. **Note** the `max_mobile_height` argument of `sidebar()` determines the maximum height of the sidebar area on mobile, but it now only applies when `open = "always"`. (#798)

* `layout_sidebar()` now uses an `<aside>` element for the sidebar's container and a `<header>` element for the sidebar title. The classes of each element remain the same, but the semantic meaning of the elements is now better reflected in the HTML markup. (#580)
Expand Down
28 changes: 25 additions & 3 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ as_page <- function(x, theme = bs_theme()) {
x
}

#' Make HTML browsable by default
#' Print a bslib page/fragment
#'
#' @param x a [tag()] object.
#' @param ... passed along to an underlying print method
#' @param x a bslib page/fragment.
#' @param ... passed along to an underlying print method.
#' @export
#' @keywords internal
#' @rdname html-browse
Expand All @@ -23,6 +23,18 @@ print.bslib_fragment <- function(x, ...) {
invisible(print(x, ...))
}

#' Save a bslib page/fragment as HTML
#'
#' @param x a bslib page/fragment.
#' @param ... passed along to an underlying [save_html()] method.
#' @export
#' @keywords internal
#' @rdname save-html
save_html.bslib_fragment <- function(html, file, ...) {
html <- attr(html, "bslib_page")(html)
save_html(html, file, ...)
}

#' @rdname html-browse
#' @export
print.bslib_page <- function(x, ...) {
Expand All @@ -36,3 +48,13 @@ print.bslib_page <- function(x, ...) {

invisible(NextMethod())
}

#' @rdname save-html
#' @export
save_html.bslib_page <- function(html, file, ...) {
old_theme <- bs_global_get()
bs_global_set(attr(html, "bs_theme", exact = TRUE))
on.exit(bs_global_set(old_theme), add = TRUE)

NextMethod()
}
8 changes: 4 additions & 4 deletions man/html-browse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions man/save-html.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions tests/testthat/_snaps/page/card.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<style>body{background-color:white;}</style>
<script src="lib/jquery/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />
<script src="lib/bootstrap/bootstrap.bundle.min.js"></script>
<script src="lib/bslib-component-js/components.min.js"></script>
<script src="lib/bslib-component-js/web-components.min.js" type="module"></script>
<link href="lib/bslib-component-css/components.css" rel="stylesheet" />
<script src="lib/bs3compat/transition.js"></script>
<script src="lib/bs3compat/tabs.js"></script>
<script src="lib/bs3compat/bs3compat.js"></script>
<link href="lib/htmltools-fill/fill.css" rel="stylesheet" />
<script src="lib/bslib-tag-require/tag-require.js"></script>

</head>
<body>
<div class="container-fluid">
<div class="card bslib-card bslib-mb-spacing html-fill-item html-fill-container" data-bslib-card-init data-require-bs-caller="card()" data-require-bs-version="5">
<div class="card-body bslib-gap-spacing html-fill-item html-fill-container" style="margin-top:auto;margin-bottom:auto;flex:1 1 auto;">A simple card</div>
<script data-bslib-card-init>bslib.Card.initializeAllCards();</script>
</div>
</div>
</body>
</html>
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/page/modern-page.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<style>body{background-color:white;}</style>
<script src="lib/jquery/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<link href="lib/bootstrap/bootstrap.min.css" rel="stylesheet" />
<script src="lib/bootstrap/bootstrap.bundle.min.js"></script>
<script src="lib/bslib-component-js/components.min.js"></script>
<script src="lib/bslib-component-js/web-components.min.js" type="module"></script>
<link href="lib/bslib-component-css/bslib-component-css.min.css" rel="stylesheet" />

</head>
<body>A simple page without bs3compat dependencies</body>
</html>
23 changes: 23 additions & 0 deletions tests/testthat/test-page.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,26 @@ test_that("page_sidebar()", {
)

})

test_that("save_html() works on components and pages with a custom theme", {
withr::local_options(list(htmltools.dir.version = FALSE))

withr::with_tempdir({
save_html(
card("A simple card"),
"card.html"
)
expect_snapshot_file("card.html")
})

withr::with_tempdir({
save_html(
page(
theme = bs_remove(bs_theme(), "bs3compat"),
"A simple page without bs3compat dependencies"
),
"modern-page.html"
)
expect_snapshot_file("modern-page.html")
})
})
Loading