base.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """Base classes for radix-themes components."""
  2. from __future__ import annotations
  3. from reflex.components import Component
  4. from reflex.utils import imports
  5. from reflex.vars import ImportVar, Var
  6. class CommonMarginProps(Component):
  7. """Many radix-themes elements accept shorthand margin props."""
  8. # Margin: "0" - "9"
  9. m: Var[str]
  10. # Margin horizontal: "0" - "9"
  11. mx: Var[str]
  12. # Margin vertical: "0" - "9"
  13. my: Var[str]
  14. # Margin top: "0" - "9"
  15. mt: Var[str]
  16. # Margin right: "0" - "9"
  17. mr: Var[str]
  18. # Margin bottom: "0" - "9"
  19. mb: Var[str]
  20. # Margin left: "0" - "9"
  21. ml: Var[str]
  22. class RadixThemesComponent(Component):
  23. """Base class for all @radix-ui/themes components."""
  24. library = "@radix-ui/themes@^2.0.0"
  25. @classmethod
  26. def create(cls, *children, **props) -> Component:
  27. """Create a new component instance.
  28. Will prepend "RadixThemes" to the component tag to avoid conflicts with
  29. other UI libraries for common names, like Text and Button.
  30. Args:
  31. *children: Child components.
  32. **props: Component properties.
  33. Returns:
  34. A new component instance.
  35. """
  36. component = super().create(*children, **props)
  37. if component.library is None:
  38. component.library = RadixThemesComponent.__fields__["library"].default
  39. component.alias = "RadixThemes" + (
  40. component.tag or component.__class__.__name__
  41. )
  42. return component
  43. def _get_app_wrap_components(self) -> dict[tuple[int, str], Component]:
  44. return {
  45. **super()._get_app_wrap_components(),
  46. (45, "RadixThemesColorModeProvider"): RadixThemesColorModeProvider.create(),
  47. }
  48. def _get_style(self) -> dict:
  49. return {"style": self.style}
  50. class Theme(RadixThemesComponent):
  51. """A theme provider for radix components.
  52. This should be applied as `App.theme` to apply the theme to all radix
  53. components in the app with the given settings.
  54. It can also be used in a normal page to apply specified properties to all
  55. child elements as an override of the main theme.
  56. """
  57. tag = "Theme"
  58. # Whether to apply the themes background color to the theme node.
  59. has_background: Var[bool]
  60. # Override light or dark mode theme: "inherit" | "light" | "dark"
  61. appearance: Var[str]
  62. # The color used for default buttons, typography, backgrounds, etc
  63. accent_color: Var[str]
  64. # The shade of gray
  65. gray_color: Var[str]
  66. # Whether panel backgrounds are transparent: "solid" | "transparent" (default)
  67. panel_background: Var[str]
  68. # Element border radius: "none" | "small" | "medium" | "large" | "full"
  69. radius: Var[str]
  70. # Scale of all theme items: "90%" | "95%" | "100%" | "105%" | "110%"
  71. scaling: Var[str]
  72. def _get_imports(self) -> imports.ImportDict:
  73. return {
  74. **super()._get_imports(),
  75. "": {ImportVar(tag="@radix-ui/themes/styles.css", install=False)},
  76. }
  77. class ThemePanel(RadixThemesComponent):
  78. """Visual editor for creating and editing themes.
  79. Include as a child component of Theme to use in your app.
  80. """
  81. tag = "ThemePanel"
  82. default_open: Var[bool]
  83. class RadixThemesColorModeProvider(Component):
  84. """Next-themes integration for radix themes components."""
  85. library = "/components/reflex/radix_themes_color_mode_provider.js"
  86. tag = "RadixThemesColorModeProvider"
  87. is_default = True