base.py 4.5 KB

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