base.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """Base classes for radix-themes components."""
  2. from __future__ import annotations
  3. from typing import Literal
  4. from reflex.components import Component
  5. from reflex.utils import imports
  6. from reflex.vars import ImportVar, Var
  7. LiteralAlign = Literal["start", "center", "end", "baseline", "stretch"]
  8. LiteralJustify = Literal["start", "center", "end", "between"]
  9. LiteralSize = Literal["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
  10. LiteralVariant = Literal["solid", "soft", "outline", "ghost"]
  11. class CommonMarginProps(Component):
  12. """Many radix-themes elements accept shorthand margin props."""
  13. # Margin: "0" - "9"
  14. m: Var[LiteralSize]
  15. # Margin horizontal: "0" - "9"
  16. mx: Var[LiteralSize]
  17. # Margin vertical: "0" - "9"
  18. my: Var[LiteralSize]
  19. # Margin top: "0" - "9"
  20. mt: Var[LiteralSize]
  21. # Margin right: "0" - "9"
  22. mr: Var[LiteralSize]
  23. # Margin bottom: "0" - "9"
  24. mb: Var[LiteralSize]
  25. # Margin left: "0" - "9"
  26. ml: Var[LiteralSize]
  27. class RadixThemesComponent(Component):
  28. """Base class for all @radix-ui/themes components."""
  29. library = "@radix-ui/themes@^2.0.0"
  30. @classmethod
  31. def create(cls, *children, **props) -> Component:
  32. """Create a new component instance.
  33. Will prepend "RadixThemes" to the component tag to avoid conflicts with
  34. other UI libraries for common names, like Text and Button.
  35. Args:
  36. *children: Child components.
  37. **props: Component properties.
  38. Returns:
  39. A new component instance.
  40. """
  41. component = super().create(*children, **props)
  42. if component.library is None:
  43. component.library = RadixThemesComponent.__fields__["library"].default
  44. component.alias = "RadixThemes" + (
  45. component.tag or component.__class__.__name__
  46. )
  47. return component
  48. @staticmethod
  49. def _get_app_wrap_components() -> dict[tuple[int, str], Component]:
  50. return {
  51. (45, "RadixThemesColorModeProvider"): RadixThemesColorModeProvider.create(),
  52. }
  53. def _get_style(self) -> dict:
  54. return {"style": self.style}
  55. LiteralAccentColor = Literal[
  56. "tomato",
  57. "red",
  58. "ruby",
  59. "crimson",
  60. "pink",
  61. "plum",
  62. "purple",
  63. "violet",
  64. "iris",
  65. "indigo",
  66. "blue",
  67. "cyan",
  68. "teal",
  69. "jade",
  70. "green",
  71. "grass",
  72. "brown",
  73. "orange",
  74. "sky",
  75. "mint",
  76. "lime",
  77. "yellow",
  78. "amber",
  79. "gold",
  80. "bronze",
  81. "gray",
  82. ]
  83. LiteralAppearance = Literal["inherit", "light", "dark"]
  84. LiteralGrayColor = Literal["gray", "mauve", "slate", "sage", "olive", "sand", "auto"]
  85. LiteralPanelBackground = Literal["solid", "transparent"]
  86. LiteralRadius = Literal["none", "small", "medium", "large", "full"]
  87. LiteralScaling = Literal["90%", "95%", "100%", "105%", "110%"]
  88. class Theme(RadixThemesComponent):
  89. """A theme provider for radix components.
  90. This should be applied as `App.theme` to apply the theme to all radix
  91. components in the app with the given settings.
  92. It can also be used in a normal page to apply specified properties to all
  93. child elements as an override of the main theme.
  94. """
  95. tag = "Theme"
  96. # Whether to apply the themes background color to the theme node.
  97. has_background: Var[bool]
  98. # Override light or dark mode theme: "inherit" | "light" | "dark"
  99. appearance: Var[LiteralAppearance]
  100. # The color used for default buttons, typography, backgrounds, etc
  101. accent_color: Var[LiteralAccentColor]
  102. # The shade of gray
  103. gray_color: Var[LiteralGrayColor]
  104. # Whether panel backgrounds are transparent: "solid" | "transparent" (default)
  105. panel_background: Var[LiteralPanelBackground]
  106. # Element border radius: "none" | "small" | "medium" | "large" | "full"
  107. radius: Var[LiteralRadius]
  108. # Scale of all theme items: "90%" | "95%" | "100%" | "105%" | "110%"
  109. scaling: Var[LiteralScaling]
  110. def _get_imports(self) -> imports.ImportDict:
  111. return {
  112. **super()._get_imports(),
  113. "": [ImportVar(tag="@radix-ui/themes/styles.css", install=False)],
  114. }
  115. class ThemePanel(RadixThemesComponent):
  116. """Visual editor for creating and editing themes.
  117. Include as a child component of Theme to use in your app.
  118. """
  119. tag = "ThemePanel"
  120. default_open: Var[bool]
  121. class RadixThemesColorModeProvider(Component):
  122. """Next-themes integration for radix themes components."""
  123. library = "/components/reflex/radix_themes_color_mode_provider.js"
  124. tag = "RadixThemesColorModeProvider"
  125. is_default = True