drawer.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. """Container to stack elements with spacing."""
  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.icon import Icon
  6. from pynecone.vars import Var
  7. class Drawer(ChakraComponent):
  8. """A drawer component."""
  9. tag = "Drawer"
  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. # If true and drawer's placement is top or bottom, the drawer will occupy the viewport height (100vh)
  25. is_full_height: Var[bool]
  26. # 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
  27. lock_focus_across_frames: Var[bool]
  28. # The placement of the drawer
  29. placement: Var[str]
  30. # 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
  31. preserve_scroll_bar_gap: Var[bool]
  32. # If true, the modal will return focus to the element that triggered it when it closes.
  33. return_focus_on_close: Var[bool]
  34. # "xs" | "sm" | "md" | "lg" | "xl" | "full"
  35. size: Var[str]
  36. # 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**
  37. use_inert: Var[bool]
  38. # Variant of drawer
  39. variant: Var[str]
  40. def get_triggers(self) -> Set[str]:
  41. """Get the event triggers for the component.
  42. Returns:
  43. The event triggers.
  44. """
  45. return super().get_triggers() | {
  46. "on_close",
  47. "on_close_complete",
  48. "on_esc",
  49. "on_overlay_click",
  50. }
  51. @classmethod
  52. def create(
  53. cls, *children, header=None, body=None, footer=None, close_button=None, **props
  54. ) -> Component:
  55. """Create a drawer component.
  56. Args:
  57. children: The children of the drawer component.
  58. header: The header of the drawer.
  59. body: The body of the drawer.
  60. footer: The footer of the drawer.
  61. close_button: The close button of the drawer.
  62. props: The properties of the drawer component.
  63. Raises:
  64. AttributeError: error that occurs if conflicting props are passed
  65. Returns:
  66. The drawer component.
  67. """
  68. if len(children) == 0:
  69. contents = []
  70. if header:
  71. contents.append(DrawerHeader.create(header))
  72. if body:
  73. contents.append(DrawerBody.create(body))
  74. if footer:
  75. contents.append(DrawerFooter.create(footer))
  76. if props.get("on_close"):
  77. # use default close button if undefined
  78. if not close_button:
  79. close_button = Icon.create(tag="close")
  80. contents.append(DrawerCloseButton.create(close_button))
  81. elif close_button:
  82. raise AttributeError(
  83. "Close button can not be used if on_close event handler is not defined"
  84. )
  85. children = [
  86. DrawerOverlay.create(
  87. DrawerContent.create(*contents),
  88. )
  89. ]
  90. return super().create(*children, **props)
  91. class DrawerBody(ChakraComponent):
  92. """Drawer body."""
  93. tag = "DrawerBody"
  94. class DrawerHeader(ChakraComponent):
  95. """Drawer header."""
  96. tag = "DrawerHeader"
  97. class DrawerFooter(ChakraComponent):
  98. """Drawer footer."""
  99. tag = "DrawerFooter"
  100. class DrawerOverlay(ChakraComponent):
  101. """Drawer overlay."""
  102. tag = "DrawerOverlay"
  103. class DrawerContent(ChakraComponent):
  104. """Drawer content."""
  105. tag = "DrawerContent"
  106. class DrawerCloseButton(ChakraComponent):
  107. """Drawer close button."""
  108. tag = "DrawerCloseButton"