Skip to content

Commit

Permalink
refactor: live example cards
Browse files Browse the repository at this point in the history
  • Loading branch information
Jakub Sobolewski committed Jun 29, 2023
1 parent dde35f5 commit bc70053
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
4 changes: 2 additions & 2 deletions inst/examples/dashboard/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ router_page_elements <- append(
route("/", homePage),
route("about", aboutPage)
),
map(examples_routes, "router")
map(examples_routes, "route")
)

router_page <- do.call(router_ui, router_page_elements)
Expand Down Expand Up @@ -65,7 +65,7 @@ sass(
server <- function(input, output, session) {
router_server()
examples_routes %>%
map("server") %>%
map("servers") %>%
flatten() %>%
iwalk(function(server, id) server(id))
}
Expand Down
17 changes: 12 additions & 5 deletions inst/examples/dashboard/examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ readExample <- function(path) {
list(code = code, ui = module$ui, server = module$server)
}

#' Splits `text` into paragraphs.
makeText <- function(text) {
strsplit(text, "\\n\\n")[[1]] %>%
map(Text) %>%
Expand All @@ -113,8 +114,14 @@ makeExamplePage <- function(name, example) {

makeLiveExamplePage <- function(example, id) {
tagList(
makeCard("Live example", div(style = "padding: 20px", example$ui(id))),
makeCard("Live example code", pre(example$code))
makeCard(
title = Text("Live example", variant = "large"),
content = tagList(
example$ui(id),
Separator(),
pre(example$code)
)
)
)
}

Expand All @@ -124,16 +131,16 @@ makeExampleRoute <- function(name) {
full.names = TRUE
)
# Match on component names with optional digits at the end
pattern <- paste0("^", name, "([0-9]+)?.R")
pattern <- paste0("^", name, "[0-9]*.R")
path <- examples_files[grepl(pattern, basename(examples_files))]
examples_names <- tools::file_path_sans_ext(basename(path))
example <- path %>%
map(readExample) %>%
set_names(examples_names)

list(
server = map(example, "server"),
router = route(
servers = map(example, "server"),
route = route(
path = name,
ui = makeExamplePage(
name = name,
Expand Down
5 changes: 4 additions & 1 deletion inst/examples/dashboard/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ makeCard <- function(title, content) {
div(
class = "card ms-depth-8",
Text(variant = "large", title, block = TRUE),
content
div(
style = "margin-top: 10px;",
content
)
)
}

0 comments on commit bc70053

Please sign in to comment.