modal.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. """Modal components."""
  2. from __future__ import annotations
  3. from typing import Literal, Optional, Union
  4. from reflex.components.chakra import ChakraComponent
  5. from reflex.components.chakra.media import Icon
  6. from reflex.components.component import Component
  7. from reflex.event import EventHandler
  8. from reflex.vars import Var
  9. ModalSizes = Literal["xs", "sm", "md", "lg", "xl", "full"]
  10. class Modal(ChakraComponent):
  11. """The wrapper that provides context for its children."""
  12. tag = "Modal"
  13. # If true, the modal will be open.
  14. is_open: Var[bool]
  15. # Handle zoom/pinch gestures on iOS devices when scroll locking is enabled. Defaults to false.
  16. allow_pinch_zoom: Var[bool]
  17. # If true, the modal will autofocus the first enabled and interactive element within the ModalContent
  18. auto_focus: Var[bool]
  19. # If true, scrolling will be disabled on the body when the modal opens.
  20. block_scroll_on_mount: Var[bool]
  21. # If true, the modal will close when the Esc key is pressed
  22. close_on_esc: Var[bool]
  23. # If true, the modal will close when the overlay is clicked
  24. close_on_overlay_click: Var[bool]
  25. # If true, the modal will be centered on screen.
  26. is_centered: Var[bool]
  27. # Enables aggressive focus capturing within iframes. - If true: keep focus in the lock, no matter where lock is active - If false: allows focus to move outside of iframe
  28. lock_focus_across_frames: Var[bool]
  29. # The transition that should be used for the modal
  30. motion_preset: Var[str]
  31. # If true, a `padding-right` will be applied to the body element that's equal to the width of the scrollbar. This can help prevent some unpleasant flickering effect and content adjustment when the modal opens
  32. preserve_scroll_bar_gap: Var[bool]
  33. # If true, the modal will return focus to the element that triggered it when it closes.
  34. return_focus_on_close: Var[bool]
  35. # "xs" | "sm" | "md" | "lg" | "xl" | "full"
  36. size: Var[ModalSizes]
  37. # A11y: If true, the siblings of the modal will have `aria-hidden` set to true so that screen readers can only see the modal. This is commonly known as making the other elements **inert**
  38. use_inert: Var[bool]
  39. # Fired when the modal is closing.
  40. on_close: EventHandler[lambda: []]
  41. # Fired when the modal is closed and the exit animation is complete.
  42. on_close_complete: EventHandler[lambda: []]
  43. # Fired when the Esc key is pressed.
  44. on_esc: EventHandler[lambda: []]
  45. # Fired when the overlay is clicked.
  46. on_overlay_click: EventHandler[lambda: []]
  47. @classmethod
  48. def create(
  49. cls,
  50. *children,
  51. header: Optional[Union[Component, str]] = None,
  52. body: Optional[Union[Component, str]] = None,
  53. footer: Optional[Union[Component, str]] = None,
  54. close_button: Optional[Component] = None,
  55. **props,
  56. ) -> Component:
  57. """Create a modal component.
  58. Args:
  59. *children: The children of the component.
  60. header: The header of the modal.
  61. body: The body of the modal.
  62. footer: The footer of the modal.
  63. close_button: The close button of the modal.
  64. **props: The properties of the component.
  65. Raises:
  66. AttributeError: error that occurs if conflicting props are passed
  67. Returns:
  68. The modal component.
  69. """
  70. if len(children) == 0:
  71. contents = []
  72. # add header if present in props
  73. if header:
  74. contents.append(ModalHeader.create(header))
  75. # add ModalBody if present in props
  76. if body:
  77. contents.append(ModalBody.create(body))
  78. # add ModalFooter if present in props
  79. if footer:
  80. contents.append(ModalFooter.create(footer))
  81. # add ModalCloseButton if either a prop for one was passed, or if
  82. if props.get("on_close"):
  83. # get user defined close button or use default one
  84. if not close_button:
  85. close_button = Icon.create(tag="close")
  86. contents.append(ModalCloseButton.create(close_button))
  87. elif close_button:
  88. raise AttributeError(
  89. "Close button can not be used if on_close event handler is not defined"
  90. )
  91. children = [
  92. ModalOverlay.create(
  93. ModalContent.create(*contents),
  94. )
  95. ]
  96. return super().create(*children, **props)
  97. class ModalOverlay(ChakraComponent):
  98. """The dimmed overlay behind the modal dialog."""
  99. tag = "ModalOverlay"
  100. class ModalHeader(ChakraComponent):
  101. """The header that labels the modal dialog."""
  102. tag = "ModalHeader"
  103. class ModalFooter(ChakraComponent):
  104. """The footer that houses the modal events."""
  105. tag = "ModalFooter"
  106. class ModalContent(ChakraComponent):
  107. """The container for the modal dialog's content."""
  108. tag = "ModalContent"
  109. class ModalBody(ChakraComponent):
  110. """The wrapper that houses the modal's main content."""
  111. tag = "ModalBody"
  112. class ModalCloseButton(ChakraComponent):
  113. """The button that closes the modal."""
  114. tag = "ModalCloseButton"