base.py 4.4 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["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. LiteralAccentColor = Literal[
  54. "tomato",
  55. "red",
  56. "ruby",
  57. "crimson",
  58. "pink",
  59. "plum",
  60. "purple",
  61. "violet",
  62. "iris",
  63. "indigo",
  64. "blue",
  65. "cyan",
  66. "teal",
  67. "jade",
  68. "green",
  69. "grass",
  70. "brown",
  71. "orange",
  72. "sky",
  73. "mint",
  74. "lime",
  75. "yellow",
  76. "amber",
  77. "gold",
  78. "bronze",
  79. "gray",
  80. ]
  81. LiteralAppearance = Literal["inherit", "light", "dark"]
  82. LiteralGrayColor = Literal["gray", "mauve", "slate", "sage", "olive", "sand", "auto"]
  83. LiteralPanelBackground = Literal["solid", "transparent"]
  84. LiteralRadius = Literal["none", "small", "medium", "large", "full"]
  85. LiteralScaling = Literal["90%", "95%", "100%", "105%", "110%"]
  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 = "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 = "ThemePanel"
  118. default_open: Var[bool]
  119. class RadixThemesColorModeProvider(Component):
  120. """Next-themes integration for radix themes components."""
  121. library = "/components/reflex/radix_themes_color_mode_provider.js"
  122. tag = "RadixThemesColorModeProvider"
  123. is_default = True