style.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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.var import BaseVar, Var
  7. toggle_color_mode = BaseVar(name=constants.TOGGLE_COLOR_MODE, type_=EventChain)
  8. def convert(style_dict):
  9. """Format a style dictionary.
  10. Args:
  11. style_dict: The style dictionary to format.
  12. Returns:
  13. The formatted style dictionary.
  14. """
  15. out = {}
  16. for key, value in style_dict.items():
  17. key = format.to_camel_case(key)
  18. if isinstance(value, dict):
  19. out[key] = convert(value)
  20. elif isinstance(value, Var):
  21. out[key] = str(value)
  22. else:
  23. out[key] = value
  24. return out
  25. class Style(dict):
  26. """A style dictionary."""
  27. def __init__(self, style_dict: Optional[dict] = None):
  28. """Initialize the style.
  29. Args:
  30. style_dict: The style dictionary.
  31. """
  32. style_dict = style_dict or {}
  33. super().__init__(convert(style_dict))