callout.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Interactive components provided by @radix-ui/themes."""
  2. from types import SimpleNamespace
  3. from typing import Literal, Union
  4. import reflex as rx
  5. from reflex import el
  6. from reflex.components.component import Component
  7. from reflex.components.lucide.icon import Icon
  8. from reflex.vars import Var
  9. from ..base import (
  10. LiteralAccentColor,
  11. RadixThemesComponent,
  12. )
  13. CalloutVariant = Literal["soft", "surface", "outline"]
  14. class CalloutRoot(el.Div, RadixThemesComponent):
  15. """Groups Icon and Text parts of a Callout."""
  16. tag = "Callout.Root"
  17. # Change the default rendered element for the one passed as a child, merging their props and behavior.
  18. as_child: Var[bool]
  19. # Size "1" - "3"
  20. size: Var[Literal["1", "2", "3"]]
  21. # Variant of button: "soft" | "surface" | "outline"
  22. variant: Var[CalloutVariant]
  23. # Override theme color for button
  24. color_scheme: Var[LiteralAccentColor]
  25. # Whether to render the button with higher contrast color against background
  26. high_contrast: Var[bool]
  27. class CalloutIcon(el.Div, RadixThemesComponent):
  28. """Provides width and height for the icon associated with the callout."""
  29. tag = "Callout.Icon"
  30. class CalloutText(el.P, RadixThemesComponent):
  31. """Renders the callout text. This component is based on the p element."""
  32. tag = "Callout.Text"
  33. class Callout(CalloutRoot):
  34. """A short message to attract user's attention."""
  35. # The text of the callout.
  36. text: Var[str]
  37. # The icon of the callout.
  38. icon: Var[str]
  39. @classmethod
  40. def create(cls, text: Union[str, Var[str]], **props) -> Component:
  41. """Create a callout component.
  42. Args:
  43. text: The text of the callout.
  44. **props: The properties of the component.
  45. Returns:
  46. The callout component.
  47. """
  48. return CalloutRoot.create(
  49. (
  50. CalloutIcon.create(Icon.create(tag=props["icon"]))
  51. if "icon" in props
  52. else rx.fragment()
  53. ),
  54. CalloutText.create(text),
  55. **props,
  56. )
  57. class CalloutNamespace(SimpleNamespace):
  58. """Callout components namespace."""
  59. root = staticmethod(CalloutRoot.create)
  60. icon = staticmethod(CalloutIcon.create)
  61. text = staticmethod(CalloutText.create)
  62. __call__ = staticmethod(Callout.create)
  63. callout = CalloutNamespace()