1
0

style.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """Handle styling."""
  2. from typing import Optional
  3. from pynecone import constants, utils
  4. from pynecone.event import EventChain
  5. from pynecone.var import BaseVar, Var
  6. toggle_color_mode = BaseVar(name=constants.TOGGLE_COLOR_MODE, type_=EventChain)
  7. def convert(style_dict):
  8. """Format a style dictionary.
  9. Args:
  10. style_dict: The style dictionary to format.
  11. Returns:
  12. The formatted style dictionary.
  13. """
  14. out = {}
  15. for key, value in style_dict.items():
  16. key = utils.to_camel_case(key)
  17. if isinstance(value, dict):
  18. out[key] = convert(value)
  19. elif isinstance(value, Var):
  20. out[key] = str(value)
  21. else:
  22. out[key] = value
  23. return out
  24. class Style(dict):
  25. """A style dictionary."""
  26. def __init__(self, style_dict: Optional[dict] = None):
  27. """Initialize the style.
  28. Args:
  29. style_dict: The style dictionary.
  30. """
  31. style_dict = style_dict or {}
  32. super().__init__(convert(style_dict))