[REF-1158] Move chakra-only deps to chakra lib (#2171)

This commit is contained in:
Masen Furer 2023-11-28 00:04:07 -08:00 committed by GitHub
parent ee87e62efa
commit 0c55723df4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 52 additions and 56 deletions

View File

@ -3,6 +3,8 @@
{% block declaration %} {% block declaration %}
import { EventLoopProvider, StateProvider } from "/utils/context.js"; import { EventLoopProvider, StateProvider } from "/utils/context.js";
import { ThemeProvider } from 'next-themes' import { ThemeProvider } from 'next-themes'
import '/styles/styles.css'
{% for custom_code in custom_codes %} {% for custom_code in custom_codes %}
{{custom_code}} {{custom_code}}

View File

@ -563,7 +563,6 @@ class App(Base):
for i, tags in imports.items() for i, tags in imports.items()
if i if i
not in [ not in [
*compiler.DEFAULT_IMPORTS.keys(),
*constants.PackageJson.DEPENDENCIES.keys(), *constants.PackageJson.DEPENDENCIES.keys(),
*constants.PackageJson.DEV_DEPENDENCIES.keys(), *constants.PackageJson.DEV_DEPENDENCIES.keys(),
] ]

View File

@ -16,12 +16,7 @@ from reflex.components.component import (
) )
from reflex.config import get_config from reflex.config import get_config
from reflex.state import State from reflex.state import State
from reflex.utils.imports import ImportDict, ImportVar from reflex.utils.imports import ImportVar
# Imports to be included in every Reflex app.
DEFAULT_IMPORTS: ImportDict = {
"": [ImportVar(tag="focus-visible/dist/focus-visible", install=False)],
}
def _compile_document_root(root: Component) -> str: def _compile_document_root(root: Component) -> str:
@ -103,8 +98,7 @@ def _compile_page(
Returns: Returns:
The compiled component. The compiled component.
""" """
# Merge the default imports with the app-specific imports. imports = component.get_imports()
imports = utils.merge_imports(DEFAULT_IMPORTS, component.get_imports())
imports = utils.compile_imports(imports) imports = utils.compile_imports(imports)
# Compile the code to render the component. # Compile the code to render the component.

View File

@ -1,10 +1,10 @@
"""Top-level component that wraps the entire app.""" """Top-level component that wraps the entire app."""
from reflex.components.component import Component from reflex.components.component import Component
from reflex.components.layout.fragment import Fragment
from .bare import Bare from reflex.vars import Var
class AppWrap(Bare): class AppWrap(Fragment):
"""Top-level component that wraps the entire app.""" """Top-level component that wraps the entire app."""
@classmethod @classmethod
@ -14,4 +14,6 @@ class AppWrap(Bare):
Returns: Returns:
A new AppWrap component containing {children}. A new AppWrap component containing {children}.
""" """
return super().create(contents="{children}") return super().create(
Var.create("{children}", _var_is_local=False, _var_is_string=False)
)

View File

@ -8,15 +8,15 @@ from reflex.vars import Var, BaseVar, ComputedVar
from reflex.event import EventChain, EventHandler, EventSpec from reflex.event import EventChain, EventHandler, EventSpec
from reflex.style import Style from reflex.style import Style
from reflex.components.component import Component from reflex.components.component import Component
from .bare import Bare from reflex.components.layout.fragment import Fragment
from reflex.vars import Var
class AppWrap(Bare): class AppWrap(Fragment):
@overload @overload
@classmethod @classmethod
def create( # type: ignore def create( # type: ignore
cls, cls,
*children, *children,
contents: Optional[Union[Var[str], str]] = None,
style: Optional[Style] = None, style: Optional[Style] = None,
key: Optional[Any] = None, key: Optional[Any] = None,
id: Optional[Any] = None, id: Optional[Any] = None,

View File

@ -591,7 +591,7 @@ class Component(BaseComponent, ABC):
Returns: Returns:
The dictionary of the component style as value and the style notation as key. The dictionary of the component style as value and the style notation as key.
""" """
return {"sx": self.style} return {"style": self.style}
def render(self) -> Dict: def render(self) -> Dict:
"""Render the component. """Render the component.

View File

@ -1,39 +1,11 @@
"""Base class definition for raw HTML elements.""" """Base class definition for raw HTML elements."""
from typing import Dict
from reflex.components.component import Component from reflex.components.component import Component
class Element(Component): class Element(Component):
"""The base class for all raw HTML elements. """The base class for all raw HTML elements."""
The key difference between `Element` and `Component` is that elements do not
use Chakra's `sx` prop, instead passing styles directly to the React style
prop.
"""
def render(self) -> Dict:
"""Render the element.
Returns:
The code to render the element.
"""
tag = self._render()
return dict(
tag.add_props(
**self.event_triggers,
key=self.key,
id=self.id,
style=self.style,
class_name=self.class_name,
**self.custom_attrs,
).set(
contents=str(tag.contents),
children=[child.render() for child in self.children],
props=tag.format_props(),
)
)
def __eq__(self, other): def __eq__(self, other):
"""Two elements are equal if they have the same tag. """Two elements are equal if they have the same tag.

View File

@ -7,11 +7,9 @@ from typing import Any, Dict, Literal, Optional, Union, overload
from reflex.vars import Var, BaseVar, ComputedVar from reflex.vars import Var, BaseVar, ComputedVar
from reflex.event import EventChain, EventHandler, EventSpec from reflex.event import EventChain, EventHandler, EventSpec
from reflex.style import Style from reflex.style import Style
from typing import Dict
from reflex.components.component import Component from reflex.components.component import Component
class Element(Component): class Element(Component):
def render(self) -> Dict: ...
@overload @overload
@classmethod @classmethod
def create( # type: ignore def create( # type: ignore

View File

@ -113,7 +113,9 @@ class DebounceInput(Component):
), ),
) )
return super().create(**props) component = super().create(**props)
component._get_style = child._get_style
return component
def get_event_triggers(self) -> dict[str, Any]: def get_event_triggers(self) -> dict[str, Any]:
"""Get the event triggers that pass the component's value to the handler. """Get the event triggers that pass the component's value to the handler.

View File

@ -15,6 +15,7 @@ class ChakraComponent(Component):
library = "@chakra-ui/react@2.6.1" library = "@chakra-ui/react@2.6.1"
lib_dependencies: List[str] = [ lib_dependencies: List[str] = [
"@chakra-ui/system@2.5.7", "@chakra-ui/system@2.5.7",
"focus-visible@5.2.0",
"framer-motion@10.16.4", "framer-motion@10.16.4",
] ]
@ -25,6 +26,33 @@ class ChakraComponent(Component):
(60, "ChakraProvider"): chakra_provider, (60, "ChakraProvider"): chakra_provider,
} }
def get_imports(self) -> imports.ImportDict:
"""Chakra requires focus-visible and imported into each page.
This allows the GlobalStyle defined by the ChakraProvider to hide the blue border.
Returns:
The imports for the component.
"""
return imports.merge_imports(
super().get_imports(),
{
"": {
imports.ImportVar(
tag="focus-visible/dist/focus-visible", install=False
)
}
},
)
def _get_style(self) -> dict:
"""Get the style for the component.
Returns:
The dictionary of the component style as value and the style notation as key.
"""
return {"sx": self.style}
@classmethod @classmethod
@lru_cache(maxsize=None) @lru_cache(maxsize=None)
def _get_dependencies_imports(cls) -> imports.ImportDict: def _get_dependencies_imports(cls) -> imports.ImportDict:
@ -37,6 +65,7 @@ class ChakraComponent(Component):
dep: [imports.ImportVar(tag=None, render=False)] dep: [imports.ImportVar(tag=None, render=False)]
for dep in [ for dep in [
"@chakra-ui/system@2.5.7", "@chakra-ui/system@2.5.7",
"focus-visible@5.2.0",
"framer-motion@10.16.4", "framer-motion@10.16.4",
] ]
} }
@ -89,8 +118,6 @@ class ChakraProvider(ChakraComponent):
def _get_custom_code(self) -> str | None: def _get_custom_code(self) -> str | None:
return """ return """
import '/styles/styles.css'
const GlobalStyles = css` const GlobalStyles = css`
/* Hide the blue border around Chakra components. */ /* Hide the blue border around Chakra components. */
.js-focus-visible :focus:not([data-focus-visible-added]) { .js-focus-visible :focus:not([data-focus-visible-added]) {

View File

@ -14,6 +14,7 @@ from reflex.utils import imports
from reflex.vars import Var from reflex.vars import Var
class ChakraComponent(Component): class ChakraComponent(Component):
def get_imports(self) -> imports.ImportDict: ...
@overload @overload
@classmethod @classmethod
def create( # type: ignore def create( # type: ignore

View File

@ -72,9 +72,6 @@ class RadixThemesComponent(Component):
(45, "RadixThemesColorModeProvider"): RadixThemesColorModeProvider.create(), (45, "RadixThemesColorModeProvider"): RadixThemesColorModeProvider.create(),
} }
def _get_style(self) -> dict:
return {"style": self.style}
LiteralAccentColor = Literal[ LiteralAccentColor = Literal[
"tomato", "tomato",

View File

@ -103,8 +103,6 @@ class PackageJson(SimpleNamespace):
DEPENDENCIES = { DEPENDENCIES = {
"axios": "1.4.0", "axios": "1.4.0",
"focus-visible": "5.2.0",
"framer-motion": "10.16.4",
"json5": "2.2.3", "json5": "2.2.3",
"next": "14.0.1", "next": "14.0.1",
"next-sitemap": "4.1.8", "next-sitemap": "4.1.8",

View File

@ -1210,7 +1210,9 @@ def test_app_wrap_compile_theme(compilable_app):
"function AppWrap({children}) {" "function AppWrap({children}) {"
"return (" "return ("
"<RadixThemesTheme accentColor={`plum`}>" "<RadixThemesTheme accentColor={`plum`}>"
"<Fragment>"
"{children}" "{children}"
"</Fragment>"
"</RadixThemesTheme>" "</RadixThemesTheme>"
")" ")"
"}" "}"
@ -1261,7 +1263,9 @@ def test_app_wrap_priority(compilable_app):
"<ChakraColorModeProvider>" "<ChakraColorModeProvider>"
"<Text>" "<Text>"
"<Fragment2>" "<Fragment2>"
"<Fragment>"
"{children}" "{children}"
"</Fragment>"
"</Fragment2>" "</Fragment2>"
"</Text>" "</Text>"
"</ChakraColorModeProvider>" "</ChakraColorModeProvider>"