Skip to content

Commit 03adf5c

Browse files
committed
refactor: Use brand.color.to_dict() method
And rewrite the sass color var resolution for clarity
1 parent 2af0941 commit 03adf5c

File tree

2 files changed

+25
-28
lines changed

2 files changed

+25
-28
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ dependencies = [
5454
[project.optional-dependencies]
5555
theme = [
5656
"libsass>=0.23.0",
57-
"brand_yml>=0.1.0rc5"
57+
"brand_yml>=0.1.0rc6"
5858
]
5959
test = [
6060
"pytest>=6.2.4",

shiny/ui/_theme_brand.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@
1212
from ._theme import Theme
1313
from ._theme_presets import ShinyThemePreset, shiny_theme_presets
1414

15-
color_extras_map = {
16-
"foreground": ["body-color", "pre-color", "black"],
17-
"background": ["body-bg", "white"],
18-
"secondary": ["body-secondary-color", "body-secondary"],
15+
color_map = {
16+
"foreground": ["brand--foreground", "body-color", "pre-color"],
17+
"background": ["brand--background", "body-bg"],
18+
"primary": ["primary"],
19+
"secondary": ["secondary", "body-secondary-color", "body-secondary"],
1920
"tertiary": ["body-tertiary-color", "body-tertiary"],
21+
"success": ["success"],
22+
"info": ["info"],
23+
"warning": ["warning"],
24+
"danger": ["danger"],
25+
"light": ["light"],
26+
"dark": ["dark"],
2027
}
2128
"""Maps brand.color fields to Bootstrap Sass variables"""
2229

@@ -164,30 +171,20 @@ def __init__(
164171
# brand.color -----------------------------------------------------------------
165172
sass_vars_colors: dict[str, str] = {}
166173
if brand.color:
167-
# Map values in colors directly to their Sass variable counterparts
168-
sass_vars_colors: dict[str, str] = {
169-
k: v
170-
for k, v in brand.color.model_dump(exclude_none=True).items()
171-
if k not in ("palette", "foreground", "background")
172-
}
174+
# Map values in colors to their Sass variable counterparts
175+
for field, theme_color in brand.color.to_dict(include="theme").items():
176+
if field not in color_map:
177+
print(f"skipping color.{field} not mapped")
178+
continue
179+
180+
for sass_var in color_map[field]:
181+
sass_vars_colors[sass_var] = theme_color
173182

174-
# Map values in colors to any additional Sass variables
175-
for extra, sass_var_list in color_extras_map.items():
176-
if extra in sass_vars_colors:
177-
sass_vars_colors_extras = {
178-
var: sass_vars_colors[extra] for var in sass_var_list
179-
}
180-
sass_vars_colors = {**sass_vars_colors, **sass_vars_colors_extras}
181-
182-
if brand.color.palette:
183-
# Map the brand color palette to Bootstrap's named colors, e.g. $red, $blue.
184-
# Note that we use ._color_defs() to ensure the palette is fully resolved.
185-
brand_color_palette = brand.color._color_defs(resolved=True)
186-
for bs_color_var in bootstrap_colors[brand_bootstrap.version]:
187-
if bs_color_var in brand_color_palette:
188-
sass_vars_colors[bs_color_var] = brand_color_palette[
189-
bs_color_var
190-
]
183+
# Map the brand color palette to Bootstrap's named colors, e.g. $red, $blue.
184+
bs_color_vars = bootstrap_colors[brand_bootstrap.version]
185+
for field, palette_color in brand.color.to_dict(include="palette").items():
186+
if field in bs_color_vars:
187+
sass_vars_colors[field] = palette_color
191188

192189
# brand.typography ------------------------------------------------------------
193190
sass_vars_typography: dict[str, str] = {}

0 commit comments

Comments
 (0)