style.py 1.1 KB

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