Skip to content

Commit 6a0bd28

Browse files
fix: reject string CodeBlock theme to surface invalid values at create time (#6419)
* fix: reject string CodeBlock theme to surface invalid values at create time * Update packages/reflex-components-code/src/reflex_components_code/code.py Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * docs: document CodeBlock themes namespace and color_mode_cond usage --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 76ff38c commit 6a0bd28

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

‎docs/library/data-display/code_block.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,28 @@ rx.code_block(
2323
show_line_numbers=True,
2424
)
2525
```
26+
27+
## Themes
28+
29+
The `theme` prop must be set to a `Theme` value accessed from the `rx.code_block.themes` namespace; strings are not accepted. By default, the code block uses `one_light` in light mode and `one_dark` in dark mode.
30+
31+
```python demo
32+
rx.code_block(
33+
"""print("Hello, world!")""",
34+
language="python",
35+
theme=rx.code_block.themes.dracula,
36+
)
37+
```
38+
39+
To pick a theme that responds to the global color mode, pass `rx.color_mode_cond` with the desired light and dark variants:
40+
41+
```python demo
42+
rx.code_block(
43+
"""print("Hello, world!")""",
44+
language="python",
45+
theme=rx.color_mode_cond(
46+
light=rx.code_block.themes.one_light,
47+
dark=rx.code_block.themes.one_dark,
48+
),
49+
)
50+
```

‎packages/reflex-components-code/src/reflex_components_code/code.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,8 +387,9 @@ class CodeBlock(Component, MarkdownComponentMap):
387387

388388
alias = "SyntaxHighlighter"
389389

390-
theme: Var[Theme | str] = field(
391-
default=Theme.one_light, doc='The theme to use ("light" or "dark").'
390+
theme: Var[Theme] = field(
391+
default=Theme.one_light,
392+
doc="The theme to use, accessed via rx.code_block.themes (e.g. rx.code_block.themes.one_dark).",
392393
)
393394

394395
language: Var[LiteralCodeLanguage] = field(

‎pyi_hashes.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"packages/reflex-components-code/src/reflex_components_code/code.pyi": "a879ccd253e901964a7ab7ea7154f904",
2+
"packages/reflex-components-code/src/reflex_components_code/code.pyi": "6ef91a4a4976e66b2761539e16d4f28e",
33
"packages/reflex-components-code/src/reflex_components_code/shiki_code_block.pyi": "d3e0c33fdc34f5c154ac387d550c0d29",
44
"packages/reflex-components-core/src/reflex_components_core/__init__.pyi": "82b29d23f2490161d42fd21021bd39c3",
55
"packages/reflex-components-core/src/reflex_components_core/base/__init__.pyi": "7009187aaaf191814d031e5462c48318",

‎tests/units/components/datadisplay/test_code.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
from reflex_components_code.code import CodeBlock, Theme
33

4+
import reflex as rx
5+
46

57
@pytest.mark.parametrize(
68
("theme", "expected"),
@@ -10,3 +12,27 @@ def test_code_light_dark_theme(theme, expected):
1012
code_block = CodeBlock.create(theme=theme)
1113

1214
assert code_block.theme._js_expr == expected # pyright: ignore [reportAttributeAccessIssue]
15+
16+
17+
def test_code_block_rejects_string_theme():
18+
with pytest.raises(TypeError, match=r"CodeBlock\.theme"):
19+
CodeBlock.create("print('Hello')", theme="one_dark") # pyright: ignore[reportArgumentType]
20+
21+
22+
def test_code_block_accepts_color_mode_cond_theme():
23+
theme = rx.color_mode_cond(
24+
light=rx.code_block.themes.one_light,
25+
dark=rx.code_block.themes.one_dark,
26+
)
27+
code_block = CodeBlock.create("print('Hello')", theme=theme)
28+
29+
rendered = code_block.render()
30+
style_prop = next((p for p in rendered["props"] if p.startswith("style:")), None)
31+
assert style_prop is not None, "code block did not render a style prop"
32+
assert "resolvedColorMode" in style_prop
33+
assert "oneLight" in style_prop
34+
assert "oneDark" in style_prop
35+
36+
imports = code_block._get_all_imports()
37+
assert "react-syntax-highlighter/dist/esm/styles/prism/one-light" in imports
38+
assert "react-syntax-highlighter/dist/esm/styles/prism/one-dark" in imports

0 commit comments

Comments
 (0)