modal.py 4.8 KB

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