posit-dev / brand-yml

Unified branding with a single yaml file.
https://posit-dev.github.io/brand-yml/
MIT License
35 stars 4 forks source link

Add `size_as_rem()` to `BaseTypographyOptionsSize` #41

Open gadenbuie opened 1 month ago

gadenbuie commented 1 month ago

Often useful in particular for typography.base.size.

gadenbuie commented 3 weeks ago

In Python, I implemented this in e59ea9d9fdee0dea4d8efdc5e15e6a06789d0416. I was able to use pydantic's serialization context feature, which is where users can provide serialization options to the context argument of .model_dump().

from brand_yml import Brand

brand = Brand.from_yaml_str(
    f"""
    typography:
      base:
        size: 18px
    """
)

brand.typography.model_dump(
  exclude_none = True,
  context={"typography_base_size_unit": "rem"}
)
#> {'fonts': [], 'base': {'size': '1.125rem'}}

(From the example in the docs.)