simplify and fix set_color_mode (#4852)
* simplify and fix set_color_mode * woops
This commit is contained in:
parent
96086bcb0c
commit
c2917d46d5
@ -6,7 +6,7 @@ from typing import Any, Literal, Tuple, Type
|
|||||||
|
|
||||||
from reflex import constants
|
from reflex import constants
|
||||||
from reflex.components.core.breakpoints import Breakpoints, breakpoints_values
|
from reflex.components.core.breakpoints import Breakpoints, breakpoints_values
|
||||||
from reflex.event import EventChain, EventHandler
|
from reflex.event import EventChain, EventHandler, EventSpec, run_script
|
||||||
from reflex.utils import format
|
from reflex.utils import format
|
||||||
from reflex.utils.exceptions import ReflexError
|
from reflex.utils.exceptions import ReflexError
|
||||||
from reflex.utils.imports import ImportVar
|
from reflex.utils.imports import ImportVar
|
||||||
@ -49,9 +49,9 @@ def _color_mode_var(_js_expr: str, _var_type: Type = str) -> Var:
|
|||||||
|
|
||||||
|
|
||||||
def set_color_mode(
|
def set_color_mode(
|
||||||
new_color_mode: LiteralColorMode | Var[LiteralColorMode] | None = None,
|
new_color_mode: LiteralColorMode | Var[LiteralColorMode],
|
||||||
) -> Var[EventChain]:
|
) -> EventSpec:
|
||||||
"""Create an EventChain Var that sets the color mode to a specific value.
|
"""Create an EventSpec Var that sets the color mode to a specific value.
|
||||||
|
|
||||||
Note: `set_color_mode` is not a real event and cannot be triggered from a
|
Note: `set_color_mode` is not a real event and cannot be triggered from a
|
||||||
backend event handler.
|
backend event handler.
|
||||||
@ -60,24 +60,15 @@ def set_color_mode(
|
|||||||
new_color_mode: The color mode to set.
|
new_color_mode: The color mode to set.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
The EventChain Var that can be passed to an event trigger.
|
The EventSpec Var that can be passed to an event trigger.
|
||||||
"""
|
"""
|
||||||
base_setter = _color_mode_var(
|
base_setter = _color_mode_var(
|
||||||
_js_expr=constants.ColorMode.SET,
|
_js_expr=constants.ColorMode.SET,
|
||||||
_var_type=EventChain,
|
).to(FunctionVar)
|
||||||
|
|
||||||
|
return run_script(
|
||||||
|
base_setter.call(new_color_mode),
|
||||||
)
|
)
|
||||||
if new_color_mode is None:
|
|
||||||
return base_setter
|
|
||||||
|
|
||||||
if not isinstance(new_color_mode, Var):
|
|
||||||
new_color_mode = LiteralVar.create(new_color_mode)
|
|
||||||
|
|
||||||
return Var(
|
|
||||||
f"() => {base_setter!s}({new_color_mode!s})",
|
|
||||||
_var_data=VarData.merge(
|
|
||||||
base_setter._get_all_var_data(), new_color_mode._get_all_var_data()
|
|
||||||
),
|
|
||||||
).to(FunctionVar, EventChain)
|
|
||||||
|
|
||||||
|
|
||||||
# Var resolves to the current color mode for the app ("light", "dark" or "system")
|
# Var resolves to the current color mode for the app ("light", "dark" or "system")
|
||||||
|
@ -61,7 +61,7 @@ def ColorToggleApp():
|
|||||||
rx.icon(tag="moon", size=20),
|
rx.icon(tag="moon", size=20),
|
||||||
value="dark",
|
value="dark",
|
||||||
),
|
),
|
||||||
on_change=set_color_mode(),
|
on_change=set_color_mode, # pyright: ignore[reportArgumentType]
|
||||||
variant="classic",
|
variant="classic",
|
||||||
radius="large",
|
radius="large",
|
||||||
value=color_mode,
|
value=color_mode,
|
||||||
|
Loading…
Reference in New Issue
Block a user