Skip to content

Commit 51f9f2c

Browse files
authored
put reflexGlobalStyles before everything and add style to radix theme root (#5763)
* put reflexGlobalStyles before everything and add style to radix theme root * fix tests
1 parent 737e427 commit 51f9f2c

File tree

4 files changed

+46
-16
lines changed

4 files changed

+46
-16
lines changed

reflex/.templates/web/components/reflex/radix_themes_color_mode_provider.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useTheme } from "$/utils/react-theme";
2-
import { createElement } from "react";
2+
import { createElement, useEffect } from "react";
33
import { ColorModeContext, defaultColorMode } from "$/utils/context";
44

55
export default function RadixThemesColorModeProvider({ children }) {
@@ -20,6 +20,16 @@ export default function RadixThemesColorModeProvider({ children }) {
2020
setTheme(mode);
2121
};
2222

23+
useEffect(() => {
24+
const radixRoot = document.querySelector(
25+
'.radix-themes[data-is-root-theme="true"]',
26+
);
27+
if (radixRoot) {
28+
radixRoot.classList.remove("light", "dark");
29+
radixRoot.classList.add(resolvedTheme);
30+
}
31+
}, [resolvedTheme]);
32+
2333
return createElement(
2434
ColorModeContext.Provider,
2535
{

reflex/compiler/templates.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,6 @@ def app_root_template(
200200
)
201201

202202
return f"""
203-
import reflexGlobalStyles from '$/styles/__reflex_global_styles.css?url';
204203
{imports_str}
205204
{dynamic_imports_str}
206205
import {{ EventLoopProvider, StateProvider, defaultColorMode }} from "$/utils/context";
@@ -211,10 +210,6 @@ def app_root_template(
211210
212211
{custom_code_str}
213212
214-
export const links = () => [
215-
{{ rel: 'stylesheet', href: reflexGlobalStyles, type: 'text/css' }}
216-
];
217-
218213
function AppWrap({{children}}) {{
219214
{_render_hooks(hooks)}
220215
return ({_RenderUtils.render(render)})

reflex/compiler/utils.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from reflex.components.base.document import Links, ScrollRestoration
1717
from reflex.components.base.document import Meta as ReactMeta
1818
from reflex.components.component import Component, ComponentStyle, CustomComponent
19-
from reflex.components.el.elements.metadata import Head, Meta, Title
19+
from reflex.components.el.elements.metadata import Head, Link, Meta, Title
2020
from reflex.components.el.elements.other import Html
2121
from reflex.components.el.elements.sectioning import Body
2222
from reflex.constants.state import FIELD_MARKER
@@ -26,7 +26,7 @@
2626
from reflex.utils import format, imports, path_ops
2727
from reflex.utils.imports import ImportVar, ParsedImportDict
2828
from reflex.utils.prerequisites import get_web_dir
29-
from reflex.vars.base import Field, Var
29+
from reflex.vars.base import Field, Var, VarData
3030

3131
# To re-export this function.
3232
merge_imports = imports.merge_imports
@@ -382,6 +382,20 @@ def create_document_root(
382382
# Always include the framework meta and link tags.
383383
always_head_components = [
384384
ReactMeta.create(),
385+
Link.create(
386+
rel="stylesheet",
387+
type="text/css",
388+
href=Var(
389+
"reflexGlobalStyles",
390+
_var_data=VarData(
391+
imports={
392+
"$/styles/__reflex_global_styles.css?url": [
393+
ImportVar(tag="reflexGlobalStyles", is_default=True)
394+
]
395+
}
396+
),
397+
),
398+
),
385399
Links.create(),
386400
]
387401
maybe_head_components = []

tests/units/compiler/test_compiler.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from reflex import constants
99
from reflex.compiler import compiler, utils
1010
from reflex.components.base import document
11+
from reflex.components.el.elements.metadata import Link
1112
from reflex.constants.compiler import PageNames
1213
from reflex.utils.imports import ImportVar, ParsedImportDict
1314
from reflex.vars.base import Var
@@ -364,7 +365,7 @@ def test_create_document_root():
364365
assert isinstance(lang, LiteralStringVar)
365366
assert lang.equals(Var.create("en"))
366367
# No children in head.
367-
assert len(root.children[0].children) == 5
368+
assert len(root.children[0].children) == 6
368369
assert isinstance(root.children[0].children[1], utils.Meta)
369370
char_set = root.children[0].children[1].char_set # pyright: ignore [reportAttributeAccessIssue]
370371
assert isinstance(char_set, LiteralStringVar)
@@ -374,7 +375,8 @@ def test_create_document_root():
374375
assert isinstance(name, LiteralStringVar)
375376
assert name.equals(Var.create("viewport"))
376377
assert isinstance(root.children[0].children[3], document.Meta)
377-
assert isinstance(root.children[0].children[4], document.Links)
378+
assert isinstance(root.children[0].children[4], Link)
379+
assert isinstance(root.children[0].children[5], document.Links)
378380

379381

380382
def test_create_document_root_with_scripts():
@@ -389,9 +391,18 @@ def test_create_document_root_with_scripts():
389391
html_custom_attrs={"project": "reflex"},
390392
)
391393
assert isinstance(root, utils.Html)
392-
assert len(root.children[0].children) == 7
394+
assert len(root.children[0].children) == 8
393395
names = [c.tag for c in root.children[0].children]
394-
assert names == ["script", "Scripts", "Scripts", "meta", "meta", "Meta", "Links"]
396+
assert names == [
397+
"script",
398+
"Scripts",
399+
"Scripts",
400+
"meta",
401+
"meta",
402+
"Meta",
403+
"link",
404+
"Links",
405+
]
395406
lang = root.lang # pyright: ignore [reportAttributeAccessIssue]
396407
assert isinstance(lang, LiteralStringVar)
397408
assert lang.equals(Var.create("rx"))
@@ -408,9 +419,9 @@ def test_create_document_root_with_meta_char_set():
408419
head_components=comps,
409420
)
410421
assert isinstance(root, utils.Html)
411-
assert len(root.children[0].children) == 5
422+
assert len(root.children[0].children) == 6
412423
names = [c.tag for c in root.children[0].children]
413-
assert names == ["script", "meta", "meta", "Meta", "Links"]
424+
assert names == ["script", "meta", "meta", "Meta", "link", "Links"]
414425
assert str(root.children[0].children[1].char_set) == '"cp1252"' # pyright: ignore [reportAttributeAccessIssue]
415426

416427

@@ -424,9 +435,9 @@ def test_create_document_root_with_meta_viewport():
424435
head_components=comps,
425436
)
426437
assert isinstance(root, utils.Html)
427-
assert len(root.children[0].children) == 6
438+
assert len(root.children[0].children) == 7
428439
names = [c.tag for c in root.children[0].children]
429-
assert names == ["script", "meta", "meta", "meta", "Meta", "Links"]
440+
assert names == ["script", "meta", "meta", "meta", "Meta", "link", "Links"]
430441
assert str(root.children[0].children[1].http_equiv) == '"refresh"' # pyright: ignore [reportAttributeAccessIssue]
431442
assert str(root.children[0].children[2].name) == '"viewport"' # pyright: ignore [reportAttributeAccessIssue]
432443
assert str(root.children[0].children[2].content) == '"foo"' # pyright: ignore [reportAttributeAccessIssue]

0 commit comments

Comments
 (0)