style.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Handle styling."""
  2. from __future__ import annotations
  3. from reflex import constants
  4. from reflex.event import EventChain
  5. from reflex.utils import format
  6. from reflex.vars import BaseVar, Var
  7. color_mode = BaseVar(_var_name=constants.ColorMode.NAME, _var_type="str")
  8. toggle_color_mode = BaseVar(_var_name=constants.ColorMode.TOGGLE, _var_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: dict | None = 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))