--- title: "Themes and looks" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Themes and looks} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set(collapse = TRUE, comment = "#>") has_bslib <- requireNamespace("bslib", quietly = TRUE) && utils::packageVersion("bslib") >= "0.6.0" ``` ```{r} library(hovercraft) ``` ## Nothing to do on a bslib page The tips read Bootstrap's variables where the page has them, so a page built with `bslib::page_fluid()` and a `bslib::bs_theme()` already has tips in the theme's colours, corner and font, and they turn dark with `bslib::input_dark_mode()` on their own. The tip is a shade of the page: a little darker than a light page and a little lighter than a dark one, in Bootstrap's tertiary background with the page's own text colour. A small arrow points from the tip to its tag. ```{r, echo = FALSE, out.width = "48%", fig.alt = "A button with a tip below it and a small arrow between them, a pale grey tip on a light page and a dark grey tip on a dark page.", fig.show = "hold"} knitr::include_graphics(c("figures/default-light.png", "figures/default-dark.png")) ``` Use a theme when the tips must look different from the page, when the brand is not in a `bs_theme()`, or when the page has no bslib. ## Your own roles `tip_theme()` takes named roles, in a list for light and one for dark. A role you leave out keeps what the page gives. | Role | What it sets | Takes | |---|---|---| | `bg` | the tip's fill | a colour | | `text` | its words | a colour | | `border` | its border, seen in forced-colours mode and in a tip that stays open | a colour | | `code_bg` | behind code | a colour | | `radius` | the corner | pixels, or a length such as `"0.5rem"` | | `shadow` | the shadow | CSS `box-shadow` text, or `"none"` | | `font` | the typeface | CSS `font-family` text | ```{r} brand <- tip_theme( light = list(bg = "#1b3a6b", text = "#ffffff", radius = 10, shadow = "0 6px 18px rgba(27, 58, 107, 0.35)"), dark = list(bg = "#9ec5fe", text = "#0b1f3a") ) brand ``` A role set in `light` and not in `dark` is used in dark mode too, so a brand colour set once holds in both modes. Colours may be names, hex, `rgb()` text or three numbers from 0 to 255. Give the theme to `use_hovercraft()` as `style` once, in the page's UI: ```{r, eval = FALSE} ui <- bslib::page_fluid( use_hovercraft(style = brand), tip_on(actionButton("go", "Run"), "Runs the **check**") ) ``` ```{r, echo = FALSE, out.width = "48%", fig.alt = "The same button with a navy tip with a small arrow, rounded and shadowed, on a light page, and a pale blue tip on a dark page.", fig.show = "hold"} knitr::include_graphics(c("figures/theme-light.png", "figures/theme-dark.png")) ``` ### Light and dark `mode = "auto"` (the default) follows the page: the `data-bs-theme` attribute that `bslib::input_dark_mode()` sets on the page, and, when the page names none, the visitor's own setting. `"light"` and `"dark"` use that mode whatever the page says. A tip is drawn at the page level, so a `data-bs-theme` on a smaller part of the page does not reach it. ## Presets and styles A preset is a named look the engine ships: a corner, a font, colours for light and dark and a shadow. `tip_presets()` lists them, and a name is all `style` needs: ```{r} tip_presets()[c("name", "label")] use_hovercraft(style = "sober")[[2]]$name ``` `style` takes a preset name, `"none"` (clears a preset set further out), a `tip_style()`, or one part of a style, such as a `tip_theme()`. `tip_style()` bundles a preset, a theme, a class for the tip box, and its motion and size: `tip_style("sober", theme = brand)` is sober with the roles of `brand` changed. A string that holds `:` or `;` is not a style: `style` takes no CSS, and a list goes to `tip_theme()` or `tip_style(theme = list(...))`. A tag can have its own look. `tip_on(btn, "Run", style = "dense")` beats the page's style for that tag, and a value set nearer wins, value by value: a tag whose theme sets only `bg` keeps the page's `text`. A preset on a tag replaces the page's preset as a whole, and a theme beats a preset set at the same place or further out. The tips of a table take a look the same way: `tip_table(style = "sober")` styles all of them, and a column's `tip_spec("{{ note }}", style = "dense")` gives that column its own, keeping what it does not set from the table. A row's own style also reaches the cells of that row that have none of their own. `class` puts class names on the tip box, for your own CSS. The nearest class wins as a whole (a tag's `class = "wide"` replaces the page's `class = "brand"`), and `class = ""` clears one: ```{r} tip_style("sober", class = c("wide", "audit-tip")) ``` ## Motion and size `motion` is how a tip enters (`"fade"`, `"slide"` or `"none"`), `gap` the space between a tag and its tip, `max_width` the widest a hint grows and `pin_width` the widest a pinned tip grows, in pixels. They are set on the page or on one tag, and the nearest value wins, value by value: a tag that sets only `gap` keeps the page's `motion`. A reader's reduced-motion setting always wins over a fade or a slide. ```{r} tip_style("sober", motion = "slide", max_width = 280) tip_on(htmltools::tags$button("Run"), "Runs the **check**", style = tip_style(gap = 12)) ``` ## Markers A tip is out of sight until the pointer rests on its tag, so a reader cannot tell which words hold one. A marker is a visible cue on the tag, off by default and chosen for each tag with `marker =`: ```{r} tip_on(htmltools::tags$span("Baseline", tabindex = "0"), "The first **screening** value", marker = "underline") ``` `"underline"` draws a line under the words and takes no room, `"icon"` adds a small icon after them, and `"corner"` draws a small triangle in the top corner, as a spreadsheet does for a note. It suits a table cell or a value box; on a word it can look like a stray mark. Markers are drawn in the tag's own text colour, so they keep its contrast in light and dark, and the pointer over a marked tag is the help cursor. The line of an underline follows the preset: dotted for `"none"`, `"sober"` and `"dense"`, dashed for `"modern"` and `"editorial"`, and a long gentle wave for `"lively"`. `tip_marker()` chooses one yourself, or gives your own icon, as a tag: ```{r} tip_marker("underline", line = "wavy") tip_marker("icon", icon = htmltools::tags$b("?")) ``` An icon from `fontawesome::fa()` or `bsicons::bs_icon()` works the same way, and `marker = ` is short for `tip_marker("icon", icon = )`. An icon is added to the tag as a child that a screen reader skips; a name such as `"info"` is not looked up, because the package does not pick an icon library for you. For an underline on a block tag (a table cell, a paragraph), the words are put in a `span` so the line goes under them and not across the cell. A value box marks its title. A block of other tags has no words to underline, so it stops with an error that says where the marker goes. In a table, `tip_table(marker = "corner")` marks every cell and header cell that has a tip, in `DT` and `reactable`; a `tip_spec(marker = )` marks or unmarks one column. Only the corner is drawn there for now, and a row takes no marker. ```{r, eval = FALSE} DT::datatable(scores) |> tip_table(cells = c(wins = "{{ wins }} wins"), headers = c(wins = "Games won"), marker = "corner") ``` ## A bslib theme A `bslib::bs_theme()` is read through its variables (bslib 0.6.0 or later): the tip's fill is the theme's tertiary background, its words the theme's text colour, with the border colour, corner and font. This is for a page that is not built with the theme; a page that is needs no theme at all. ```{r, eval = has_bslib} warm <- bslib::bs_theme( version = 5, bg = "#fdf6e3", fg = "#073642", "border-radius" = "0.6rem", "body-bg-dark" = "#002b36", "body-color-dark" = "#eee8d5" ) use_hovercraft(style = warm)[[2]]$name ``` ```{r, echo = FALSE, out.width = "48%", fig.alt = "A button with a tip below it in pale cream with dark teal words, rounded corners and a small arrow, then the same in dark mode with a muted teal tip.", fig.show = "hold"} knitr::include_graphics(c("figures/bs-theme-light.png", "figures/bs-theme-dark.png")) ``` ## CSS variables A named list whose names all start with `--` sets CSS variables as they are, for both modes: the engine's own variables, or any variable the page's CSS defines. Give it to `tip_style(theme = )`: a bare list is not taken as `style`, because a style has more parts than a theme. ```{r} use_hovercraft(style = tip_style(theme = list("--hovercraft-tip-radius" = "4px")))[[2]]$name ``` These variables reach the tips from the page only, not from one tag. A role can also read a variable: `tip_theme(light = list(bg = "--team-blue"))` reads `--team-blue` where the page defines it. R cannot check the value of a variable; the browser works it out. ## Contrast The words of a tip must be readable on its fill. For each mode `tip_theme()` measures the `text` on the `bg` (and on the `code_bg`, if you give one). Below 4.5:1, the usual level for text, it warns and keeps the colours. A role you leave out is taken to be what a bslib page gives by default: Bootstrap's tertiary background with the page's text colour, in that mode. ```{r} tip_theme(light = list(bg = "#ffffff", text = "#dddddd")) |> suppressWarnings() |> invisible() ``` The warning has the class `hovercraft_contrast`. Turn the warnings off with `options(hovercraft.contrast_warnings = FALSE)`, or silence only them with `withCallingHandlers(..., hovercraft_contrast = function(w) invokeRestart("muffleWarning"))`.