modal.py 5.2 KB

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