popover.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Interactive components provided by @radix-ui/themes."""
  2. from typing import Dict, Literal, Union
  3. from reflex.components.component import ComponentNamespace
  4. from reflex.components.core.breakpoints import Responsive
  5. from reflex.components.el import elements
  6. from reflex.event import EventHandler, no_args_event_spec, passthrough_event_spec
  7. from reflex.vars.base import Var
  8. from ..base import RadixThemesComponent, RadixThemesTriggerComponent
  9. class PopoverRoot(RadixThemesComponent):
  10. """Floating element for displaying rich content, triggered by a button."""
  11. tag = "Popover.Root"
  12. # The controlled open state of the popover.
  13. open: Var[bool]
  14. # The modality of the popover. When set to true, interaction with outside elements will be disabled and only popover content will be visible to screen readers.
  15. modal: Var[bool]
  16. # Fired when the open state changes.
  17. on_open_change: EventHandler[passthrough_event_spec(bool)]
  18. # The open state of the popover when it is initially rendered. Use when you do not need to control its open state.
  19. default_open: Var[bool]
  20. class PopoverTrigger(RadixThemesTriggerComponent):
  21. """Wraps the control that will open the popover."""
  22. tag = "Popover.Trigger"
  23. class PopoverContent(elements.Div, RadixThemesComponent):
  24. """Contains content to be rendered in the open popover."""
  25. tag = "Popover.Content"
  26. # Size of the button: "1" | "2" | "3" | "4"
  27. size: Var[Responsive[Literal["1", "2", "3", "4"]]]
  28. # The preferred side of the anchor to render against when open. Will be reversed when collisions occur and avoidCollisions is enabled.
  29. side: Var[Literal["top", "right", "bottom", "left"]]
  30. # The distance in pixels from the anchor.
  31. side_offset: Var[int]
  32. # The preferred alignment against the anchor. May change when collisions occur.
  33. align: Var[Literal["start", "center", "end"]]
  34. # The vertical distance in pixels from the anchor.
  35. align_offset: Var[int]
  36. # When true, overrides the side andalign preferences to prevent collisions with boundary edges.
  37. avoid_collisions: Var[bool]
  38. # The distance in pixels from the boundary edges where collision detection should occur. Accepts a number (same for all sides), or a partial padding object, for example: { "top": 20, "left": 20 }. Defaults to 0.
  39. collision_padding: Var[Union[float, int, Dict[str, Union[float, int]]]]
  40. # The sticky behavior on the align axis. "partial" will keep the content in the boundary as long as the trigger is at least partially in the boundary whilst "always" will keep the content in the boundary regardless. Defaults to "partial".
  41. sticky: Var[Literal["partial", "always"]]
  42. # Whether to hide the content when the trigger becomes fully occluded. Defaults to False.
  43. hide_when_detached: Var[bool]
  44. # Fired when the dialog is opened.
  45. on_open_auto_focus: EventHandler[no_args_event_spec]
  46. # Fired when the dialog is closed.
  47. on_close_auto_focus: EventHandler[no_args_event_spec]
  48. # Fired when the escape key is pressed.
  49. on_escape_key_down: EventHandler[no_args_event_spec]
  50. # Fired when the pointer is down outside the dialog.
  51. on_pointer_down_outside: EventHandler[no_args_event_spec]
  52. # Fired when focus moves outside the dialog.
  53. on_focus_outside: EventHandler[no_args_event_spec]
  54. # Fired when the pointer interacts outside the dialog.
  55. on_interact_outside: EventHandler[no_args_event_spec]
  56. class PopoverClose(RadixThemesTriggerComponent):
  57. """Wraps the control that will close the popover."""
  58. tag = "Popover.Close"
  59. class Popover(ComponentNamespace):
  60. """Floating element for displaying rich content, triggered by a button."""
  61. root = staticmethod(PopoverRoot.create)
  62. trigger = staticmethod(PopoverTrigger.create)
  63. content = staticmethod(PopoverContent.create)
  64. close = staticmethod(PopoverClose.create)
  65. popover = Popover()