Skip to content

Commit

Permalink
Radix Themes + Tailwind Harmony (reflex-dev#3355)
Browse files Browse the repository at this point in the history
  • Loading branch information
masenf authored and benedikt-bartscher committed Jun 3, 2024
1 parent 5b79ae4 commit 8d677c8
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
1 change: 1 addition & 0 deletions reflex/.templates/web/postcss.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
plugins: {
"postcss-import": {},
tailwindcss: {},
autoprefixer: {},
},
Expand Down
5 changes: 4 additions & 1 deletion reflex/.templates/web/styles/tailwind.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
@tailwind base;
@import "tailwindcss/base";

@import "@radix-ui/themes/styles.css";

@tailwind components;
@tailwind utilities;
46 changes: 26 additions & 20 deletions reflex/components/radix/themes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from reflex.components import Component
from reflex.components.tags import Tag
from reflex.utils import imports
from reflex.config import get_config
from reflex.utils.imports import ImportVar
from reflex.vars import Var

LiteralAlign = Literal["start", "center", "end", "baseline", "stretch"]
Expand Down Expand Up @@ -208,18 +209,23 @@ def create(
children = [ThemePanel.create(), *children]
return super().create(*children, **props)

def _get_imports(self) -> imports.ImportDict:
return imports.merge_imports(
super()._get_imports(),
{
"": [
imports.ImportVar(tag="@radix-ui/themes/styles.css", install=False)
],
"/utils/theme.js": [
imports.ImportVar(tag="theme", is_default=True),
],
},
)
def add_imports(self) -> dict[str, list[ImportVar] | ImportVar]:
"""Add imports for the Theme component.
Returns:
The import dict.
"""
_imports: dict[str, list[ImportVar] | ImportVar] = {
"/utils/theme.js": [ImportVar(tag="theme", is_default=True)],
}
if get_config().tailwind is None:
# When tailwind is disabled, import the radix-ui styles directly because they will
# not be included in the tailwind.css file.
_imports[""] = ImportVar(
tag="@radix-ui/themes/styles.css",
install=False,
)
return _imports

def _render(self, props: dict[str, Any] | None = None) -> Tag:
tag = super()._render(props)
Expand All @@ -243,13 +249,13 @@ class ThemePanel(RadixThemesComponent):
# Whether the panel is open. Defaults to False.
default_open: Var[bool]

def _get_imports(self) -> dict[str, list[imports.ImportVar]]:
return imports.merge_imports(
super()._get_imports(),
{
"react": [imports.ImportVar(tag="useEffect")],
},
)
def add_imports(self) -> dict[str, str]:
"""Add imports for the ThemePanel component.
Returns:
The import dict.
"""
return {"react": "useEffect"}

def _get_hooks(self) -> str | None:
# The panel freezes the tab if the user color preference differs from the
Expand Down
5 changes: 4 additions & 1 deletion reflex/components/radix/themes/base.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ from reflex.style import Style
from typing import Any, Dict, Literal
from reflex.components import Component
from reflex.components.tags import Tag
from reflex.utils import imports
from reflex.config import get_config
from reflex.utils.imports import ImportVar
from reflex.vars import Var

LiteralAlign = Literal["start", "center", "end", "baseline", "stretch"]
Expand Down Expand Up @@ -579,8 +580,10 @@ class Theme(RadixThemesComponent):
A new component instance.
"""
...
def add_imports(self) -> dict[str, list[ImportVar] | ImportVar]: ...

class ThemePanel(RadixThemesComponent):
def add_imports(self) -> dict[str, str]: ...
@overload
@classmethod
def create( # type: ignore
Expand Down
1 change: 1 addition & 0 deletions reflex/constants/installer.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,5 @@ class Commands(SimpleNamespace):
DEV_DEPENDENCIES = {
"autoprefixer": "10.4.14",
"postcss": "8.4.31",
"postcss-import": "16.1.0",
}

0 comments on commit 8d677c8

Please sign in to comment.