shiny-bslib-theming
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTheming Shiny Apps with bslib
使用bslib为Shiny应用定制主题
Customize Shiny app appearance using bslib's Bootstrap 5 theming system. From quick Bootswatch themes to advanced Sass customization and dynamic color mode switching.
借助bslib的Bootstrap 5主题系统自定义Shiny应用的外观,从快速应用Bootswatch主题到高级Sass自定义、动态色彩模式切换都可实现。
Quick Start
快速开始
"shiny" preset (recommended starting point):
r
page_sidebar(
theme = bs_theme(), # "shiny" preset by default — polished, not plain Bootstrap
...
)Bootswatch theme (for a different visual style):
r
page_sidebar(
theme = bs_theme(preset = "zephyr"), # or "cosmo", "minty", "darkly", etc.
...
)Custom colors and fonts:
r
page_sidebar(
theme = bs_theme(
version = 5,
bg = "#FFFFFF",
fg = "#333333",
primary = "#2c3e50",
base_font = font_google("Lato"),
heading_font = font_google("Montserrat")
),
...
)Auto-brand from :
If a file exists in your app or project directory, automatically discovers and applies it. No code changes needed. Requires the R package.
_brand.yml_brand.ymlbs_theme()brand.ymlr
bs_theme(brand = FALSE) # Disable auto-discovery
bs_theme(brand = TRUE) # Require _brand.yml (error if not found)
bs_theme(brand = "path/to/brand.yml") # Explicit path"shiny"预设(推荐的入门选择):
r
page_sidebar(
theme = bs_theme(), # 默认使用"shiny"预设,效果精致,不是原生Bootstrap
...
)Bootswatch主题(用于实现不同的视觉风格):
r
page_sidebar(
theme = bs_theme(preset = "zephyr"), # 也可选"cosmo"、"minty"、"darkly"等
...
)自定义颜色和字体:
r
page_sidebar(
theme = bs_theme(
version = 5,
bg = "#FFFFFF",
fg = "#333333",
primary = "#2c3e50",
base_font = font_google("Lato"),
heading_font = font_google("Montserrat")
),
...
)从自动加载品牌配置:
如果你的应用或项目目录下存在文件,会自动识别并应用配置,无需修改代码。需要安装 R包。
_brand.yml_brand.ymlbs_theme()brand.ymlr
bs_theme(brand = FALSE) # 关闭自动识别
bs_theme(brand = TRUE) # 强制要求存在_brand.yml,找不到则报错
bs_theme(brand = "path/to/brand.yml") # 显式指定文件路径Theming Workflow
主题配置工作流
- Start with the preset (default) or a Bootswatch theme close to your desired look
"shiny" - Customize main colors (,
bg,fg)primary - Adjust fonts with or other font helpers
font_google() - Fine-tune with Bootstrap Sass variables via or
...bs_add_variables() - Add custom Sass rules with if needed
bs_add_rules() - Enable so plots match the theme
thematic::thematic_shiny() - Use during development for interactive preview
bs_themer()
Example:
r
theme <- bs_theme(preset = "minty") |>
bs_theme_update(
primary = "#1a9a7f",
base_font = font_google("Lato")
) |>
bs_add_rules("
.card { box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
")- 从"shiny"预设(默认)或接近你预期效果的Bootswatch主题开始
- 自定义主色调(、
bg、fg)primary - 通过或其他字体工具调整字体配置
font_google() - 通过参数或
...微调Bootstrap Sass变量bs_add_variables() - 如有需要用添加自定义Sass规则
bs_add_rules() - 启用让绘图样式匹配主题
thematic::thematic_shiny() - 开发过程中使用进行交互式预览
bs_themer()
示例:
r
theme <- bs_theme(preset = "minty") |>
bs_theme_update(
primary = "#1a9a7f",
base_font = font_google("Lato")
) |>
bs_add_rules("
.card { box-shadow: 0 2px 8px rgba(0,0,0,0.1); }
")bs_theme()
bs_theme()
Central function for creating Bootstrap themes. Returns a object.
sass::sass_bundle()r
bs_theme(
version = version_default(),
preset = NULL, # "shiny" (default for BS5+), "bootstrap", or Bootswatch name
..., # Bootstrap Sass variable overrides
brand = NULL, # brand.yml: NULL (auto), TRUE (require), FALSE (disable), or path
bg = NULL, fg = NULL,
primary = NULL, secondary = NULL,
success = NULL, info = NULL, warning = NULL, danger = NULL,
base_font = NULL, code_font = NULL, heading_font = NULL,
font_scale = NULL, # Scalar multiplier for base font size (e.g., 1.5 = 150%)
bootswatch = NULL # Alias for preset
)Use to modify an existing theme. Use to test if an object is a theme.
bs_theme_update(theme, ...)is_bs_theme(x)创建Bootstrap主题的核心函数,返回一个对象。
sass::sass_bundle()r
bs_theme(
version = version_default(),
preset = NULL, # "shiny"(BS5+版本默认)、"bootstrap"或Bootswatch主题名
..., # Bootstrap Sass变量覆盖配置
brand = NULL, # brand.yml配置:NULL(自动识别)、TRUE(强制要求)、FALSE(关闭)或文件路径
bg = NULL, fg = NULL,
primary = NULL, secondary = NULL,
success = NULL, info = NULL, warning = NULL, danger = NULL,
base_font = NULL, code_font = NULL, heading_font = NULL,
font_scale = NULL, # 基础字体大小的缩放系数(例如1.5 = 放大到150%)
bootswatch = NULL # preset的别名
)使用修改已有主题,使用判断对象是否为合法主题。
bs_theme_update(theme, ...)is_bs_theme(x)Presets and Bootswatch
预设与Bootswatch
The "shiny" preset (recommended): defaults to for Bootstrap 5+. This is a polished, purpose-built theme designed specifically for Shiny apps — it is not plain Bootstrap. It provides professional styling with well-chosen defaults for cards, sidebars, value boxes, and other bslib components. Start here and customize with colors and fonts before reaching for a Bootswatch theme.
bs_theme()preset = "shiny"Vanilla Bootstrap: Use to remove the "shiny" preset and get unmodified Bootstrap 5 styling.
preset = "bootstrap"Built-in presets: lists bslib's own presets.
builtin_themes()Bootswatch themes: lists all available Bootswatch themes. Choose one that fits the app's purpose and audience — don't apply one by default.
bootswatch_themes()Popular options: (light, modern), (clean), (fresh green), (flat design), (crisp), (dark), (dark), (minimalist), (hand-drawn).
"zephyr""cosmo""minty""flatly""litera""darkly""cyborg""simplex""sketchy""shiny"预设(推荐): Bootstrap 5及以上版本的默认使用,这是专门为Shiny应用设计的定制主题,并非原生Bootstrap。它为卡片、侧边栏、数值框等bslib组件提供了专业的默认样式,建议先从这个预设开始,自定义颜色和字体后再考虑使用Bootswatch主题。
bs_theme()preset = "shiny"原生Bootstrap: 使用可以移除"shiny"预设,获得未经修改的Bootstrap 5样式。
preset = "bootstrap"内置预设: 可以列出bslib自带的所有预设。
builtin_themes()Bootswatch主题: 列出所有可用的Bootswatch主题,选择符合应用用途和受众的主题即可,无需默认应用。
bootswatch_themes()热门选项:(浅色现代风)、(简洁风)、(清新绿风格)、(扁平化设计)、(清晰风)、(深色主题)、(深色主题)、(极简风)、(手绘风)。
"zephyr""cosmo""minty""flatly""litera""darkly""cyborg""simplex""sketchy"Main Colors
主色调配置
The most influential colors — changing these affects hundreds of CSS rules via variable cascading:
| Parameter | Description |
|---|---|
| Background color |
| Foreground (text) color |
| Primary brand color (links, nav active states, input focus) |
| Default for action buttons |
| Positive/success states (typically green) |
| Informational content (typically blue-green) |
| Warnings (typically yellow) |
| Errors/destructive actions (typically red) |
r
bs_theme(
bg = "#202123", fg = "#B8BCC2",
primary = "#EA80FC", secondary = "#48DAC6"
)Color tips:
- /
bg: similar hue, large luminance difference (ensure contrast for readability)fg - : contrasts with both
primaryandbg; used for hyperlinks, navigation, input focusfg - Colors can be any format understands
htmltools::parseCssColors()
这是影响力最大的颜色配置——修改这些参数会通过变量级联影响数百条CSS规则:
| 参数 | 描述 |
|---|---|
| 背景色 |
| 前景(文本)色 |
| 主品牌色(链接、导航激活态、输入框聚焦态) |
| 操作按钮默认色 |
| 正向/成功状态色(通常为绿色) |
| 信息类内容色(通常为蓝绿色) |
| 警告状态色(通常为黄色) |
| 错误/破坏性操作色(通常为红色) |
r
bs_theme(
bg = "#202123", fg = "#B8BCC2",
primary = "#EA80FC", secondary = "#48DAC6"
)颜色配置提示:
- /
bg:色相相近,亮度差异大(确保可读性对比度)fg - :需同时和
primary、bg形成对比,用于超链接、导航、输入框聚焦态fg - 颜色支持识别的所有格式
htmltools::parseCssColors()
Typography
排版配置
Three font arguments: , , . Use to uniformly scale all font sizes (e.g., for 150%).
base_fontheading_fontcode_fontfont_scale1.5Each argument accepts a single font, a , or a character vector of font names.
font_collection()提供三个字体参数:、、。使用可以统一缩放所有字体大小(例如对应放大到150%)。
base_fontheading_fontcode_fontfont_scale1.5每个参数可接受单个字体、或字体名称的字符向量。
font_collection()font_google()
font_google()
Downloads and caches Google Fonts locally ( by default). Internet needed only on first download.
local = TRUEr
bs_theme(
base_font = font_google("Roboto"),
heading_font = font_google("Montserrat"),
code_font = font_google("Fira Code")
)With variable weights:
font_google("Crimson Pro", wght = "200..900")With specific weights:
font_google("Raleway", wght = c(300, 400, 700))Recommend fallbacks to avoid Flash of Invisible Text (FOIT) on slow connections:
r
bs_theme(
base_font = font_collection(
font_google("Lato", local = FALSE),
"Helvetica Neue", "Arial", "sans-serif"
)
)Font pairing resource: fontpair.co
下载并本地缓存Google Fonts(默认),仅首次下载需要联网。
local = TRUEr
bs_theme(
base_font = font_google("Roboto"),
heading_font = font_google("Montserrat"),
code_font = font_google("Fira Code")
)可变字重示例:
font_google("Crimson Pro", wght = "200..900")指定字重示例:
font_google("Raleway", wght = c(300, 400, 700))建议添加 fallback 字体,避免网络慢时出现FOIT(无样式文本闪烁):
r
bs_theme(
base_font = font_collection(
font_google("Lato", local = FALSE),
"Helvetica Neue", "Arial", "sans-serif"
)
)字体搭配资源:fontpair.co
font_link()
font_link()
CSS web font interface for custom font URLs:
r
font_link("Crimson Pro",
href = "https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@200..900")用于自定义字体URL的CSS网络字体接口:
r
font_link("Crimson Pro",
href = "https://fonts.googleapis.com/css2?family=Crimson+Pro:wght@200..900")font_face()
font_face()
For locally hosted font files with full control:
@font-facer
font_face(
family = "Crimson Pro",
style = "normal",
weight = "200 900",
src = "url(fonts/crimson-pro.woff2) format('woff2')"
)用于本地托管的字体文件,可完全控制配置:
@font-facer
font_face(
family = "Crimson Pro",
style = "normal",
weight = "200 900",
src = "url(fonts/crimson-pro.woff2) format('woff2')"
)font_collection()
font_collection()
Combine multiple fonts with fallback order:
r
font_collection(font_google("Lato"), "Helvetica Neue", "Arial", "sans-serif")按 fallback 顺序组合多个字体:
r
font_collection(font_google("Lato"), "Helvetica Neue", "Arial", "sans-serif")Low-Level Theming Functions
底层主题定制函数
For customizations beyond 's named parameters. These work directly with Bootstrap's Sass layers.
bs_theme()用于命名参数之外的自定义需求,可直接操作Bootstrap的Sass层。
bs_theme()bs_add_variables()
bs_add_variables()
Add or override Bootstrap Sass variable defaults:
r
theme <- bs_add_variables(
bs_theme(preset = "sketchy", primary = "orange"),
"body-bg" = "#EEEEEE",
"font-family-base" = "monospace",
"font-size-base" = "1.4rem",
"btn-padding-y" = ".16rem"
)The parameter controls placement in the Sass compilation order:
.where | When to use |
|---|---|
| Set variable defaults with |
| Reference other Bootstrap variables (e.g., |
| Placed after all rules. Rarely needed. |
Referencing Bootstrap variables:
r
undefined添加或覆盖Bootstrap Sass变量默认值:
r
theme <- bs_add_variables(
bs_theme(preset = "sketchy", primary = "orange"),
"body-bg" = "#EEEEEE",
"font-family-base" = "monospace",
"font-size-base" = "1.4rem",
"btn-padding-y" = ".16rem"
).where | 使用场景 |
|---|---|
| 用 |
| 引用其他Bootstrap变量(例如 |
| 放在所有规则之后,很少使用 |
引用Bootstrap变量示例:
r
undefinedThis fails in bs_theme() because $secondary isn't defined yet:
直接在bs_theme()中使用会失败,因为$secondary还未定义:
bs_theme("progress-bar-bg" = "$secondary")
bs_theme("progress-bar-bg" = "$secondary")
Use bs_add_variables with .where = "declarations" instead:
改为使用.where = "declarations"的bs_add_variables:
bs_theme() |>
bs_add_variables("progress-bar-bg" = "$secondary", .where = "declarations")
undefinedbs_theme() |>
bs_add_variables("progress-bar-bg" = "$secondary", .where = "declarations")
undefinedbs_add_rules()
bs_add_rules()
Add custom Sass/CSS rules that can reference Bootstrap variables and mixins:
r
theme <- bs_theme(primary = "#007bff") |>
bs_add_rules("
.custom-card {
background: mix($bg, $primary, 95%);
border: 1px solid $primary;
padding: $spacer;
@include media-breakpoint-up(md) {
padding: $spacer * 2;
}
}
")From external file:
bs_add_rules(sass::sass_file("www/custom.scss"))Available Sass functions: , , , , .
Available Bootstrap mixins: , , .
lighten()darken()mix()rgba()color-contrast()@include media-breakpoint-up()@include box-shadow()@include border-radius()添加可引用Bootstrap变量和混合宏的自定义Sass/CSS规则:
r
theme <- bs_theme(primary = "#007bff") |>
bs_add_rules("
.custom-card {
background: mix($bg, $primary, 95%);
border: 1px solid $primary;
padding: $spacer;
@include media-breakpoint-up(md) {
padding: $spacer * 2;
}
}
")从外部文件加载:
bs_add_rules(sass::sass_file("www/custom.scss"))可用的Sass函数:、、、、。
可用的Bootstrap混合宏:、、。
lighten()darken()mix()rgba()color-contrast()@include media-breakpoint-up()@include box-shadow()@include border-radius()bs_add_functions() and bs_add_mixins()
bs_add_functions() 和 bs_add_mixins()
Add custom Sass functions or mixins to the theme bundle:
r
theme |>
bs_add_functions("@function my-tint($color) { @return mix(white, $color, 20%); }") |>
bs_add_rules(".highlight { background: my-tint($primary); }")向主题包中添加自定义Sass函数或混合宏:
r
theme |>
bs_add_functions("@function my-tint($color) { @return mix(white, $color, 20%); }") |>
bs_add_rules(".highlight { background: my-tint($primary); }")bs_bundle()
bs_bundle()
Append objects to a theme (for packaging reusable theme extensions):
sass::sass_bundle()r
my_extension <- sass::sass_layer(
defaults = list("my-var" = "red !default"),
rules = ".my-class { color: $my-var; }"
)
theme <- bs_theme() |> bs_bundle(my_extension)将对象追加到主题中(用于封装可复用的主题扩展):
sass::sass_bundle()r
my_extension <- sass::sass_layer(
defaults = list("my-var" = "red !default"),
rules = ".my-class { color: $my-var; }"
)
theme <- bs_theme() |> bs_bundle(my_extension)Bootstrap Sass Variables
Bootstrap Sass变量
Pass any Bootstrap 5 Sass variable through or .
bs_theme(...)bs_add_variables()Finding variable names: https://rstudio.github.io/bslib/articles/bs5-variables/
Common variables:
r
bs_theme(
"border-radius" = "0.5rem",
"card-border-radius" = "1rem",
"card-bg" = "lighten($bg, 5%)",
"navbar-bg" = "$primary",
"link-color" = "$primary",
"font-size-base" = "1rem",
"spacer" = "1rem",
"btn-padding-y" = ".5rem",
"btn-padding-x" = "1rem",
"input-border-color" = "#dee2e6"
)Values can be Sass expressions referencing variables, functions, and math.
可通过或传入任意Bootstrap 5 Sass变量。
bs_theme(...)bs_add_variables()常用变量示例:
r
bs_theme(
"border-radius" = "0.5rem",
"card-border-radius" = "1rem",
"card-bg" = "lighten($bg, 5%)",
"navbar-bg" = "$primary",
"link-color" = "$primary",
"font-size-base" = "1rem",
"spacer" = "1rem",
"btn-padding-y" = ".5rem",
"btn-padding-x" = "1rem",
"input-border-color" = "#dee2e6"
)变量值可以是引用变量、函数、数学运算的Sass表达式。
Bootstrap CSS Custom Properties
Bootstrap CSS自定义属性
See sass-and-css-variables.md for details on:
- How Sass variables compile into CSS custom properties
--bs-* - Runtime vs compile-time variable layers
- How Bootstrap 5.3 color modes use CSS variable overrides
- Per-element theming with
data-bs-theme - CSS utility classes for one-off styling
参考sass-and-css-variables.md了解以下内容的详情:
- Sass变量如何编译为开头的CSS自定义属性
--bs-* - 运行时 vs 编译时变量层
- Bootstrap 5.3色彩模式如何使用CSS变量覆盖
- 用实现单元素主题定制
data-bs-theme - 用于一次性样式调整的CSS工具类
Dark Mode and Color Modes
深色模式与色彩模式
See dark-mode.md for details on:
- Bootstrap 5.3's client-side color mode system (attribute)
data-bs-theme - and
input_dark_mode()for user-controlled switchingtoggle_dark_mode() - Server-side theme switching with
session$setCurrentTheme() - Writing custom Sass that works across light/dark modes
- Component compatibility (what responds to theming, what doesn't)
参考dark-mode.md了解以下内容的详情:
- Bootstrap 5.3的客户端色彩模式系统(属性)
data-bs-theme - 供用户切换的和
input_dark_mode()toggle_dark_mode() - 通过实现服务端主题切换
session$setCurrentTheme() - 编写可同时适配亮/深色模式的自定义Sass
- 组件兼容性(哪些组件支持主题适配,哪些不支持)
Theming R Plots
R绘图主题适配
bs_theme()thematicr
library(thematic)
thematic_shiny(font = "auto") # Call before shinyApp()
shinyApp(ui, server)- Works with base R, ggplot2, and lattice
- Translates CSS colors into R plotting defaults
- also matches fonts from
font = "auto"bs_theme() - Complements for real-time preview
bs_themer()
Set global ggplot2 theme for further consistency:
r
library(ggplot2)
theme_set(theme_minimal())bs_theme()thematicr
library(thematic)
thematic_shiny(font = "auto") # 在shinyApp()调用前执行
shinyApp(ui, server)- 支持base R、ggplot2和lattice绘图库
- 将CSS颜色转换为R绘图默认配置
- 还可自动匹配
font = "auto"中的字体设置bs_theme() - 可配合实现实时预览
bs_themer()
设置全局ggplot2主题进一步提升一致性:
r
library(ggplot2)
theme_set(theme_minimal())Dashboard Background Styling
仪表盘背景样式
The CSS class adds a light gray background behind the main content area, giving dashboard-style apps a polished look where cards stand out against the background. This is a theming detail — it doesn't change layout behavior, only the visual treatment.
bslib-page-dashboardFor dashboards:
page_sidebar()r
page_sidebar(
class = "bslib-page-dashboard",
title = "My Dashboard",
sidebar = sidebar(...),
...
)For with dashboard-focused pages:
Apply the class to individual containers (not itself) so only dashboard-oriented pages get the gray background:
page_navbar()nav_panel()page_navbar()r
page_navbar(
title = "Analytics",
nav_panel("Dashboard", class = "bslib-page-dashboard",
layout_column_wrap(...)
),
nav_panel("Report",
# No dashboard class — standard white background for prose/reports
...
)
)bslib-page-dashboard针对仪表盘:
page_sidebar()r
page_sidebar(
class = "bslib-page-dashboard",
title = "我的仪表盘",
sidebar = sidebar(...),
...
)针对含仪表盘页面的:
将类应用到单个容器(不要直接加在上),这样只有仪表盘页面会显示灰色背景:
page_navbar()nav_panel()page_navbar()r
page_navbar(
title = "数据分析平台",
nav_panel("仪表盘", class = "bslib-page-dashboard",
layout_column_wrap(...)
),
nav_panel("报告",
# 不添加dashboard类,使用标准白色背景适合展示文案/报告
...
)
)Interactive Theming Tools
交互式主题工具
bs_theme_preview()
bs_theme_preview()
Standalone demo app for previewing a theme with many example UI components:
r
bslib::bs_theme_preview() # Default theme
bslib::bs_theme_preview(bs_theme(preset = "darkly")) # Custom themeIncludes the theming UI by default ().
with_themer = TRUE独立的演示应用,可预览主题在多种示例UI组件上的效果:
r
bslib::bs_theme_preview() # 默认主题
bslib::bs_theme_preview(bs_theme(preset = "darkly")) # 自定义主题默认包含主题配置UI()。
with_themer = TRUErun_with_themer()
run_with_themer()
Run an existing Shiny app with the theme editor overlay (instead of ):
shiny::runApp()r
run_with_themer(shinyApp(ui, server))
run_with_themer("path/to/app")给现有Shiny应用添加主题编辑器浮层(替代启动应用):
shiny::runApp()r
run_with_themer(shinyApp(ui, server))
run_with_themer("path/to/app")bs_themer()
bs_themer()
Add the theme editor to your own app's server function:
r
server <- function(input, output, session) {
bs_themer() # Add during development, remove for production
# ...
}All three tools print the resulting code to the R console for easy copy-paste. Limitations: Bootstrap 5+ only, Shiny apps and R Markdown only, doesn't affect 3rd-party widgets that don't use .
bs_theme()runtime: shinybs_dependency_defer()在你自己的应用服务端函数中添加主题编辑器:
r
server <- function(input, output, session) {
bs_themer() # 开发阶段添加,生产环境可移除
# ...
}这三个工具都会将生成的代码打印到R控制台,方便复制粘贴。限制: 仅支持Bootstrap 5及以上版本,仅适用于Shiny应用和的R Markdown,不影响未使用的第三方组件。
bs_theme()runtime: shinybs_dependency_defer()Theme Inspection
主题检测
Retrieve computed Sass variable values:
r
vars <- c("body-bg", "body-color", "primary", "border-radius")
bs_get_variables(bs_theme(), varnames = vars)
bs_get_variables(bs_theme(preset = "darkly"), varnames = vars)Check contrast (for accessibility):
r
bs_get_contrast(bs_theme(), c("primary", "dark", "light"))Aim for WCAG AA compliance: 4.5:1 for normal text, 3:1 for large text.
获取计算后的Sass变量值:
r
vars <- c("body-bg", "body-color", "primary", "border-radius")
bs_get_variables(bs_theme(), varnames = vars)
bs_get_variables(bs_theme(preset = "darkly"), varnames = vars)检查对比度(用于无障碍适配):
r
bs_get_contrast(bs_theme(), c("primary", "dark", "light"))建议达到WCAG AA标准:普通文本对比度4.5:1,大文本3:1。
Best Practices
最佳实践
- Prefer over custom CSS -- variables cascade to all related components automatically
bs_theme() - Pin Bootstrap version: prevents breakage if defaults change
bs_theme(version = 5) - Use fallback fonts with to avoid FOIT on slow connections
font_collection() - Test across components: inputs, buttons, cards, navs, plots, tables, modals, toasts, mobile
- Check accessibility with and browser dev tools
bs_get_contrast() - Use CSS utility classes for one-off styling instead of custom CSS (see sass-and-css-variables.md)
- Organize complex themes in a separate :
theme.R
r
undefined- 优先使用而非自定义CSS——变量会自动级联到所有相关组件
bs_theme() - 固定Bootstrap版本:可避免默认值变更导致样式崩溃
bs_theme(version = 5) - 使用添加 fallback 字体,避免网络慢时出现FOIT
font_collection() - 在所有组件上测试:输入框、按钮、卡片、导航、绘图、表格、模态框、Toast提示、移动端
- 通过和浏览器开发者工具检查无障碍适配
bs_get_contrast() - 使用CSS工具类实现一次性样式调整,无需编写自定义CSS(参考sass-and-css-variables.md)
- 复杂主题单独放在文件中统一管理:
theme.R
r
undefinedtheme.R
theme.R
app_theme <- function() {
bs_theme(
version = 5,
primary = "#2c3e50",
base_font = font_google("Lato"),
heading_font = font_google("Montserrat", wght = c(400, 700))
) |>
bs_add_rules(sass::sass_file("www/custom.scss"))
}
undefinedapp_theme <- function() {
bs_theme(
version = 5,
primary = "#2c3e50",
base_font = font_google("Lato"),
heading_font = font_google("Montserrat", wght = c(400, 700))
) |>
bs_add_rules(sass::sass_file("www/custom.scss"))
}
undefinedReference Files
参考文件
- sass-and-css-variables.md -- Bootstrap's two-layer variable system, CSS custom properties, utility classes
- dark-mode.md -- Color modes, dark mode, dynamic theming, component compatibility
- sass-and-css-variables.md——Bootstrap的双层变量系统、CSS自定义属性、工具类
- dark-mode.md——色彩模式、深色模式、动态主题、组件兼容性