Skip to content

Commit 7855a59

Browse files
adhami3310masenf
authored andcommitted
simplify and fix set_color_mode (#4852)
* simplify and fix set_color_mode * woops
1 parent 2745160 commit 7855a59

File tree

2 files changed

+10
-19
lines changed

2 files changed

+10
-19
lines changed

reflex/style.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from reflex import constants
88
from reflex.components.core.breakpoints import Breakpoints, breakpoints_values
9-
from reflex.event import EventChain, EventHandler
9+
from reflex.event import EventChain, EventHandler, EventSpec, run_script
1010
from reflex.utils import format
1111
from reflex.utils.exceptions import ReflexError
1212
from reflex.utils.imports import ImportVar
@@ -49,9 +49,9 @@ def _color_mode_var(_js_expr: str, _var_type: Type = str) -> Var:
4949

5050

5151
def set_color_mode(
52-
new_color_mode: LiteralColorMode | Var[LiteralColorMode] | None = None,
53-
) -> Var[EventChain]:
54-
"""Create an EventChain Var that sets the color mode to a specific value.
52+
new_color_mode: LiteralColorMode | Var[LiteralColorMode],
53+
) -> EventSpec:
54+
"""Create an EventSpec Var that sets the color mode to a specific value.
5555
5656
Note: `set_color_mode` is not a real event and cannot be triggered from a
5757
backend event handler.
@@ -60,24 +60,15 @@ def set_color_mode(
6060
new_color_mode: The color mode to set.
6161
6262
Returns:
63-
The EventChain Var that can be passed to an event trigger.
63+
The EventSpec Var that can be passed to an event trigger.
6464
"""
6565
base_setter = _color_mode_var(
6666
_js_expr=constants.ColorMode.SET,
67-
_var_type=EventChain,
68-
)
69-
if new_color_mode is None:
70-
return base_setter
71-
72-
if not isinstance(new_color_mode, Var):
73-
new_color_mode = LiteralVar.create(new_color_mode)
67+
).to(FunctionVar)
7468

75-
return Var(
76-
f"() => {base_setter!s}({new_color_mode!s})",
77-
_var_data=VarData.merge(
78-
base_setter._get_all_var_data(), new_color_mode._get_all_var_data()
79-
),
80-
).to(FunctionVar, EventChain)
69+
return run_script(
70+
base_setter.call(new_color_mode),
71+
)
8172

8273

8374
# Var resolves to the current color mode for the app ("light", "dark" or "system")

tests/integration/tests_playwright/test_appearance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def index():
6161
rx.icon(tag="moon", size=20),
6262
value="dark",
6363
),
64-
on_change=set_color_mode(),
64+
on_change=set_color_mode, # pyright: ignore[reportArgumentType]
6565
variant="classic",
6666
radius="large",
6767
value=color_mode,

0 commit comments

Comments
 (0)