Skip to content

Commit 35c5d57

Browse files
authored
ENG-7583: check against base component for style (#5795)
* ENG-7583: check against base component for style * add tests
1 parent 367b24d commit 35c5d57

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

reflex/style.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,11 @@ def convert_item(
120120
Raises:
121121
ReflexError: If an EventHandler is used as a style value
122122
"""
123-
if isinstance(style_item, EventHandler):
123+
from reflex.components.component import BaseComponent
124+
125+
if isinstance(style_item, (EventHandler, BaseComponent)):
124126
msg = (
125-
"EventHandlers cannot be used as style values. "
127+
f"{type(style_item)} cannot be used as style values. "
126128
"Please use a Var or a literal value."
127129
)
128130
raise ReflexError(msg)

tests/units/test_style.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from reflex import style
99
from reflex.components.component import evaluate_style_namespaces
1010
from reflex.style import Style
11+
from reflex.utils.exceptions import ReflexError
1112
from reflex.vars import VarData
1213
from reflex.vars.base import LiteralVar, Var
1314

@@ -552,3 +553,31 @@ def test_style_update_with_var_data():
552553
s3 = s1 | s2
553554
assert s3._var_data is not None
554555
assert "_varData" not in s3
556+
557+
558+
def test_component_as_css_value_raises_error():
559+
"""Test that passing a component as a CSS prop value raises ReflexError."""
560+
with pytest.raises(ReflexError, match="cannot be used as style values"):
561+
rx.el.button(
562+
"Import",
563+
class_name="px-3 py-2 text-sm font-medium text-gray-600",
564+
left_icon=rx.icon(tag="cloud_upload", class_name="w-4 h-4"),
565+
)
566+
567+
# Test other CSS props that might receive components
568+
with pytest.raises(ReflexError, match="cannot be used as style values"):
569+
rx.el.div(
570+
right_icon=rx.icon(tag="settings"),
571+
)
572+
573+
# Test components passed to style dict
574+
with pytest.raises(ReflexError, match="cannot be used as style values"):
575+
rx.el.div(
576+
style={"background": rx.icon(tag="star")},
577+
)
578+
579+
# Test nested style with component
580+
with pytest.raises(ReflexError, match="cannot be used as style values"):
581+
rx.el.div(
582+
style={"_hover": {"content": rx.text("hover")}},
583+
)

0 commit comments

Comments
 (0)