Skip to content

Commit 0fe7fa1

Browse files
committed
chore(brand): rename methods and add sass comments for dividers
1 parent c5f6302 commit 0fe7fa1

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

shiny/ui/_theme_brand.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __str__(self):
2929
# Bootstrap uses $gray-900 and $white for the body bg-color by default, and then
3030
# swaps them for $gray-100 and $gray-900 in dark mode. brand.yml may end up with
3131
# light/dark variants for foreground/background, see posit-dev/brand-yml#38.
32-
"foreground": ["brand--foreground", "body-color", "pre-color", "body-bg-dark"],
32+
"foreground": ["brand--foreground", "body-color", "body-bg-dark"],
3333
"background": ["brand--background", "body-bg", "body-color-dark"],
3434
"primary": ["primary"],
3535
"secondary": ["secondary", "body-secondary-color", "body-secondary"],
@@ -204,21 +204,31 @@ def __init__(
204204
# 3. Gray scale variables from Brand fg/bg or black/white
205205
# 4. Fallback vars needed by additional Brand rules
206206

207+
self.add_defaults("", "// *---- brand: end of defaults ----* //", "")
207208
self._add_sass_ensure_variables()
208209
self._add_sass_brand_grays()
209210
self.add_defaults(**brand_bootstrap.defaults)
210-
self.add_defaults(**sass_vars_colors, **sass_vars_typography)
211-
# Brand Rules ----
211+
self.add_defaults(
212+
"// *---- brand.defaults.bootstrap + brand.defaults.shiny.theme ----* //"
213+
)
214+
self.add_defaults(**sass_vars_typography)
215+
self.add_defaults("\n// *---- brand.typography ----* //")
216+
self.add_defaults(**sass_vars_colors)
217+
self.add_defaults("\n// *---- brand.color ----* //")
218+
219+
# Brand rules (now in forwards order)
220+
self.add_rules("\n// *---- brand.color.palette ----*/ /")
212221
self.add_rules(":root {", *css_vars_colors, "}")
213222
self._add_sass_brand_rules()
214223

215224
def _prepare_color_vars(self) -> tuple[dict[str, str], list[str]]:
225+
"""Colors: create a dictionary of Sass variables and a list of brand CSS variables"""
216226
if not self.brand.color:
217227
return {}, []
218228

219-
sass_vars_colors: dict[str, str] = {}
220-
sass_vars_brand_colors: dict[str, str] = {}
221-
css_vars_brand_colors: list[str] = []
229+
mapped: dict[str, str] = {}
230+
brand_sass_vars: dict[str, str] = {}
231+
brand_css_vars: list[str] = []
222232

223233
# Map values in colors to their Sass variable counterparts
224234
for thm_name, thm_color in self.brand.color.to_dict(include="theme").items():
@@ -227,57 +237,54 @@ def _prepare_color_vars(self) -> tuple[dict[str, str], list[str]]:
227237
continue
228238

229239
for sass_var in color_map[thm_name]:
230-
sass_vars_colors[sass_var] = thm_color
240+
mapped[sass_var] = thm_color
231241

232242
brand_color_palette = self.brand.color.to_dict(include="palette")
233243

234244
# Map the brand color palette to Bootstrap's named colors, e.g. $red, $blue.
235245
for pal_name, pal_color in brand_color_palette.items():
236246
if pal_name in bootstrap_colors:
237-
sass_vars_colors[pal_name] = pal_color
247+
mapped[pal_name] = pal_color
238248

239249
# Create Sass and CSS variables for the brand color palette
240250
color_var = sanitize_sass_var_name(pal_name)
241251

242252
# => Sass var: `$brand-{name}: {value}`
243-
sass_vars_brand_colors.update({f"brand-{color_var}": pal_color})
253+
brand_sass_vars.update({f"brand-{color_var}": pal_color})
244254
# => CSS var: `--brand-{name}: {value}`
245-
css_vars_brand_colors.append(f"--brand-{color_var}: {pal_color};")
255+
brand_css_vars.append(f"--brand-{color_var}: {pal_color};")
246256

247-
return {**sass_vars_brand_colors, **sass_vars_colors}, css_vars_brand_colors
257+
return {**brand_sass_vars, **mapped}, brand_css_vars
248258

249259
def _prepare_typography_vars(self) -> dict[str, str]:
250-
sass_vars_typography: dict[str, str] = {}
260+
"""Typography: Create a list of Bootstrap Sass variables"""
261+
mapped: dict[str, str] = {}
251262

252263
if not self.brand.typography:
253-
return sass_vars_typography
264+
return mapped
254265

255266
brand_typography = self.brand.typography.model_dump(
256267
exclude={"fonts"},
257268
exclude_none=True,
258269
)
259270

260-
for typ_field, typ_value in brand_typography.items():
261-
if typ_field not in typography_map:
262-
self._handle_unmapped_variable(f"typography.{typ_field}")
271+
for field, prop in brand_typography.items():
272+
if field not in typography_map:
273+
self._handle_unmapped_variable(f"typography.{field}")
263274
continue
264275

265-
for typ_field_key, typ_field_value in typ_value.items():
266-
if typ_field_key in typography_map[typ_field]:
267-
if typ_field == "base" and typ_field_key == "size":
268-
typ_field_value = str(
269-
maybe_convert_font_size_to_rem(typ_field_value)
270-
)
276+
for prop_key, prop_value in prop.items():
277+
if prop_key in typography_map[field]:
278+
if field == "base" and prop_key == "size":
279+
prop_value = str(maybe_convert_font_size_to_rem(prop_value))
271280

272-
typo_sass_vars = typography_map[typ_field][typ_field_key]
281+
typo_sass_vars = typography_map[field][prop_key]
273282
for typo_sass_var in typo_sass_vars:
274-
sass_vars_typography[typo_sass_var] = typ_field_value
283+
mapped[typo_sass_var] = prop_value
275284
else:
276-
self._handle_unmapped_variable(
277-
f"typography.{typ_field}.{typ_field_key}"
278-
)
285+
self._handle_unmapped_variable(f"typography.{field}.{prop_key}")
279286

280-
return sass_vars_typography
287+
return mapped
281288

282289
def _add_sass_ensure_variables(self):
283290
"""Ensure the variables we create to augment Bootstrap's variables exist"""
@@ -293,6 +300,7 @@ def _add_sass_ensure_variables(self):
293300
"link-weight": None,
294301
}
295302
)
303+
self.add_defaults("// *---- brand: added variables ---* //")
296304

297305
def _add_sass_brand_grays(self):
298306
"""
@@ -372,11 +380,13 @@ def _add_sass_brand_grays(self):
372380
}
373381
"""
374382
)
383+
self.add_defaults("// *---- brand: automatic gray gradient ----* //")
375384

376385
def _add_sass_brand_rules(self):
377386
"""Additional rules to fill in Bootstrap styles for Brand parameters"""
378387
self.add_rules(
379388
"""
389+
// *---- brand: brand rules to augment Bootstrap rules ----* //
380390
// https://github.com/twbs/bootstrap/blob/5c2f2e7e/scss/_root.scss#L82
381391
:root {
382392
--#{$prefix}link-bg: #{$link-bg};
@@ -401,7 +411,8 @@ def _add_sass_brand_rules(self):
401411
line-height: $code-block-line-height;
402412
}
403413
404-
@if variable-exists(brand--background) {
414+
$bslib-dashboard-design: false !default;
415+
@if $bslib-dashboard-design and variable-exists(brand--background) {
405416
// When brand makes dark mode, it usually hides card definition, so we add
406417
// back card borders in dark mode.
407418
[data-bs-theme="dark"] {

0 commit comments

Comments
 (0)