Skip to content

Commit

Permalink
Brand: Support typography font family shortcuts (#1174)
Browse files Browse the repository at this point in the history
Fixes #1172
  • Loading branch information
gadenbuie authored Jan 22, 2025
1 parent e0e29ad commit 07dceab
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
25 changes: 25 additions & 0 deletions R/bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ as_brand_yml <- function(brand = list()) {
# Normalize brand internals !! MINIMAL VALIDATION !!
brand <- brand_meta_normalize(brand)
brand <- brand_color_normalize(brand)
brand <- brand_typography_normalize(brand)

class(brand) <- "brand_yml"
brand
Expand Down Expand Up @@ -674,6 +675,30 @@ brand_color_normalize <- function(brand) {
brand
}

brand_typography_normalize <- function(brand) {
if (!brand_has(brand, "typography")) {
return(brand)
}

expand_family <- c(
"base",
"headings",
"monospace",
"monospace-inline",
"monospace-block"
)

for (field in expand_family) {
if (brand_has_string(brand, "typography", field)) {
brand[["typography"]][[field]] <- list(
family = brand[["typography"]][[field]]
)
}
}

brand
}

brand_color_pluck <- function(brand, key) {
if (!brand_has(brand, "color")) {
return(key)
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-bs-theme-preset-brand.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ describe("as_brand_yml()", {
expect_equal(brand$color$palette$red, brand$color$primary)
expect_equal(brand$color$secondary, "berry")
})

it("normalizes font family choices", {
brand <- list(
typography = list(
base = "Times New Roman",
headings = "Helvetica",
monospace = "Courier New",
"monospace-inline" = "Fira Code"
)
)

brand <- as_brand_yml(brand)
expect_s3_class(brand, "brand_yml")
expect_equal(brand$typography$base$family, "Times New Roman")
expect_equal(brand$typography$headings$family, "Helvetica")
expect_equal(brand$typography[["monospace"]]$family, "Courier New")
expect_equal(brand$typography[["monospace-inline"]]$family, "Fira Code")
})
})

describe("brand_resolve()", {
Expand Down

0 comments on commit 07dceab

Please sign in to comment.