reflex/pynecone/style.py
advo-kat 41fffe677b
Add support for toggling color mode ('night/day mode') (#382)
Co-authored-by: g0ee <0@g0.ee>
Co-authored-by: g0ee <adbokat.b.a.s.e@gmail.com>
2023-01-30 12:39:32 -08:00

50 lines
1.1 KiB
Python

"""Handle styling."""
from typing import Optional
from pynecone import utils
from pynecone.var import Var
def toggle_color_mode():
"""Toggle the color mode.
Returns:
Toggle color mode JS event as a variable
"""
return Var.create(value="toggleColorMode", is_local=False, is_string=False)
def convert(style_dict):
"""Format a style dictionary.
Args:
style_dict: The style dictionary to format.
Returns:
The formatted style dictionary.
"""
out = {}
for key, value in style_dict.items():
key = utils.to_camel_case(key)
if isinstance(value, dict):
out[key] = convert(value)
elif isinstance(value, Var):
out[key] = str(value)
else:
out[key] = value
return out
class Style(dict):
"""A style dictionary."""
def __init__(self, style_dict: Optional[dict] = None):
"""Initialize the style.
Args:
style_dict: The style dictionary.
"""
style_dict = style_dict or {}
super().__init__(convert(style_dict))