Skip to content

Commit 7a6c712

Browse files
authored
treat hyphen as underscore in keys of styles (#4810)
* treat hyphen as underscore in keys of styles * fix tests * more nuanced conversions
1 parent abab18e commit 7a6c712

File tree

6 files changed

+18
-16
lines changed

6 files changed

+18
-16
lines changed

reflex/components/tags/tag.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ def add_props(self, **kwargs: Optional[Any]) -> Tag:
101101
"""
102102
self.props.update(
103103
{
104-
format.to_camel_case(name, allow_hyphens=True): (
104+
format.to_camel_case(name, treat_hyphens_as_underscores=False): (
105105
prop
106106
if types._isinstance(prop, (EventChain, Mapping))
107107
else LiteralVar.create(prop)

reflex/style.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ def update_out_dict(
190190
for key, value in style_dict.items():
191191
keys = (
192192
format_style_key(key)
193-
if not isinstance(value, (dict, ObjectVar))
193+
if not isinstance(value, (dict, ObjectVar, list))
194194
or (
195195
isinstance(value, Breakpoints)
196196
and all(not isinstance(v, dict) for v in value.values())
197197
)
198+
or (isinstance(value, list) and all(not isinstance(v, dict) for v in value))
198199
or (
199200
isinstance(value, ObjectVar)
200201
and not issubclass(get_origin(value._var_type) or value._var_type, dict)
@@ -236,7 +237,9 @@ def format_style_key(key: str) -> Tuple[str, ...]:
236237
Returns:
237238
Tuple of css style names corresponding to the key provided.
238239
"""
239-
key = format.to_camel_case(key, allow_hyphens=True)
240+
if key.startswith("--"):
241+
return (key,)
242+
key = format.to_camel_case(key)
240243
return STYLE_PROP_SHORTHAND_MAPPING.get(key, (key,))
241244

242245

reflex/utils/format.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,24 @@ def to_snake_case(text: str) -> str:
168168
return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower().replace("-", "_")
169169

170170

171-
def to_camel_case(text: str, allow_hyphens: bool = False) -> str:
171+
def to_camel_case(text: str, treat_hyphens_as_underscores: bool = True) -> str:
172172
"""Convert a string to camel case.
173173
174174
The first word in the text is converted to lowercase and
175175
the rest of the words are converted to title case, removing underscores.
176176
177177
Args:
178178
text: The string to convert.
179-
allow_hyphens: Whether to allow hyphens in the string.
179+
treat_hyphens_as_underscores: Whether to allow hyphens in the string.
180180
181181
Returns:
182182
The camel case string.
183183
"""
184-
char = "_" if allow_hyphens else "-_"
185-
words = re.split(f"[{char}]", text.lstrip(char))
186-
leading_underscores_or_hyphens = "".join(re.findall(rf"^[{char}]+", text))
184+
char = "_" if not treat_hyphens_as_underscores else "-_"
185+
words = re.split(f"[{char}]", text)
187186
# Capitalize the first letter of each word except the first one
188187
converted_word = words[0] + "".join(x.capitalize() for x in words[1:])
189-
return leading_underscores_or_hyphens + converted_word
188+
return converted_word
190189

191190

192191
def to_title_case(text: str, sep: str = "") -> str:

tests/units/components/markdown/test_markdown.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def test_create_map_fn_var_subclass(cls, fn_body, fn_args, explicit_return, expe
157157
value, **props
158158
)
159159
},
160-
r"""(({node, inline, className, children, ...props}) => { const match = (className || '').match(/language-(?<lang>.*)/); let _language = match ? match[1] : ''; ; return inline ? ( <RadixThemesCode {...props}>{children}</RadixThemesCode> ) : ( <RadixThemesBox css={({ ["pre"] : ({ ["margin"] : "0", ["padding"] : "24px", ["background"] : "transparent", ["overflow-x"] : "auto", ["border-radius"] : "6px" }) })} {...props}><ShikiCode code={((Array.isArray(children)) ? children.join("\n") : children)} decorations={[]} language={_language} theme={((resolvedColorMode === "light") ? "one-light" : "one-dark-pro")} transformers={[]}/></RadixThemesBox> ); })""",
160+
r"""(({node, inline, className, children, ...props}) => { const match = (className || '').match(/language-(?<lang>.*)/); let _language = match ? match[1] : ''; ; return inline ? ( <RadixThemesCode {...props}>{children}</RadixThemesCode> ) : ( <RadixThemesBox css={({ ["pre"] : ({ ["margin"] : "0", ["padding"] : "24px", ["background"] : "transparent", ["overflowX"] : "auto", ["borderRadius"] : "6px" }) })} {...props}><ShikiCode code={((Array.isArray(children)) ? children.join("\n") : children)} decorations={[]} language={_language} theme={((resolvedColorMode === "light") ? "one-light" : "one-dark-pro")} transformers={[]}/></RadixThemesBox> ); })""",
161161
),
162162
(
163163
"h1",

tests/units/components/test_component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ def test_create_filters_none_props(test_component):
651651

652652
# Assert that the style prop is present in the component's props
653653
assert str(component.style["color"]) == '"white"'
654-
assert str(component.style["text-align"]) == '"center"'
654+
assert str(component.style["textAlign"]) == '"center"'
655655

656656

657657
@pytest.mark.parametrize(

tests/units/utils/test_format.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ def test_to_snake_case(input: str, output: str):
189189
("kebab-case", "kebabCase"),
190190
("kebab-case-two", "kebabCaseTwo"),
191191
("snake_kebab-case", "snakeKebabCase"),
192-
("_hover", "_hover"),
193-
("-starts-with-hyphen", "-startsWithHyphen"),
194-
("--starts-with-double-hyphen", "--startsWithDoubleHyphen"),
195-
("_starts_with_underscore", "_startsWithUnderscore"),
196-
("__starts_with_double_underscore", "__startsWithDoubleUnderscore"),
192+
("_hover", "Hover"),
193+
("-starts-with-hyphen", "StartsWithHyphen"),
194+
("--starts-with-double-hyphen", "StartsWithDoubleHyphen"),
195+
("_starts_with_underscore", "StartsWithUnderscore"),
196+
("__starts_with_double_underscore", "StartsWithDoubleUnderscore"),
197197
(":start-with-colon", ":startWithColon"),
198198
(":-start-with-colon-dash", ":StartWithColonDash"),
199199
],

0 commit comments

Comments
 (0)