drawer.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. """Drawer components based on Radix primitives."""
  2. # Based on Vaul: https://github.com/emilkowalski/vaul
  3. # Style based on https://ui.shadcn.com/docs/components/drawer
  4. from __future__ import annotations
  5. from typing import Any, Dict, List, Literal, Optional, Union
  6. from reflex.components.radix.primitives.base import RadixPrimitiveComponentWithClassName
  7. from reflex.constants import EventTriggers
  8. from reflex.vars import Var
  9. class DrawerComponent(RadixPrimitiveComponentWithClassName):
  10. """A Drawer component."""
  11. library = "vaul"
  12. lib_dependencies: List[str] = ["@radix-ui/react-dialog@^1.0.5"]
  13. LiteralDirectionType = Literal[
  14. "top",
  15. "bottom",
  16. "left",
  17. "right",
  18. ]
  19. class DrawerRoot(DrawerComponent):
  20. """The Root component of a Drawer, contains all parts of a drawer."""
  21. tag = "Drawer.Root"
  22. # Whether the drawer is open or not.
  23. open: Var[bool]
  24. # Enable background scaling,
  25. # it requires an element with [vaul-drawer-wrapper] data attribute to scale its background.
  26. should_scale_background: Var[bool]
  27. # Number between 0 and 1 that determines when the drawer should be closed.
  28. close_threshold: Var[float]
  29. # Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up. Should go from least visible.
  30. # Also Accept px values, which doesn't take screen height into account.
  31. snap_points: Optional[List[Union[str, float]]]
  32. # Index of a snapPoint from which the overlay fade should be applied.
  33. # Defaults to the last snap point.
  34. # TODO: will it accept -1 then?
  35. fade_from_index: Var[int]
  36. # Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
  37. scroll_lock_timeout: Var[int]
  38. # When `False`, it allows to interact with elements outside of the drawer without closing it.
  39. # Defaults to `True`.
  40. modal: Var[bool]
  41. # Direction of the drawer. Defaults to `"bottom"`
  42. direction: Var[LiteralDirectionType]
  43. # When `True`, it prevents scroll restoration
  44. # when the drawer is closed after a navigation happens inside of it.
  45. # Defaults to `True`.
  46. preventScrollRestoration: Var[bool]
  47. def get_event_triggers(self) -> Dict[str, Any]:
  48. """Get the event triggers that pass the component's value to the handler.
  49. Returns:
  50. A dict mapping the event trigger to the var that is passed to the handler.
  51. """
  52. return {
  53. **super().get_event_triggers(),
  54. EventTriggers.ON_OPEN_CHANGE: lambda e0: [e0.target.value],
  55. }
  56. class DrawerTrigger(DrawerComponent):
  57. """The button that opens the dialog."""
  58. tag = "Drawer.Trigger"
  59. as_child: Var[bool]
  60. class DrawerPortal(DrawerComponent):
  61. """Portals your drawer into the body."""
  62. tag = "Drawer.Portal"
  63. # Based on https://www.radix-ui.com/primitives/docs/components/dialog#content
  64. class DrawerContent(DrawerComponent):
  65. """Content that should be rendered in the drawer."""
  66. tag = "Drawer.Content"
  67. # Style set partially based on the source code at https://ui.shadcn.com/docs/components/drawer
  68. def _get_style(self) -> dict:
  69. """Get the style for the component.
  70. Returns:
  71. The dictionary of the component style as value and the style notation as key.
  72. """
  73. base_style = {
  74. "left": "0",
  75. "right": "0",
  76. "bottom": "0",
  77. "top": "0",
  78. "position": "fixed",
  79. "z_index": 50,
  80. "display": "flex",
  81. }
  82. style = self.style or {}
  83. base_style.update(style)
  84. self.style.update(
  85. {
  86. "css": base_style,
  87. }
  88. )
  89. return self.style
  90. def get_event_triggers(self) -> Dict[str, Any]:
  91. """Get the events triggers signatures for the component.
  92. Returns:
  93. The signatures of the event triggers.
  94. """
  95. return {
  96. **super().get_event_triggers(),
  97. # DrawerContent is based on Radix DialogContent
  98. # These are the same triggers as DialogContent
  99. EventTriggers.ON_OPEN_AUTO_FOCUS: lambda e0: [e0.target.value],
  100. EventTriggers.ON_CLOSE_AUTO_FOCUS: lambda e0: [e0.target.value],
  101. EventTriggers.ON_ESCAPE_KEY_DOWN: lambda e0: [e0.target.value],
  102. EventTriggers.ON_POINTER_DOWN_OUTSIDE: lambda e0: [e0.target.value],
  103. EventTriggers.ON_INTERACT_OUTSIDE: lambda e0: [e0.target.value],
  104. }
  105. class DrawerOverlay(DrawerComponent):
  106. """A layer that covers the inert portion of the view when the dialog is open."""
  107. tag = "Drawer.Overlay"
  108. # Style set based on the source code at https://ui.shadcn.com/docs/components/drawer
  109. def _get_style(self) -> dict:
  110. """Get the style for the component.
  111. Returns:
  112. The dictionary of the component style as value and the style notation as key.
  113. """
  114. base_style = {
  115. "position": "fixed",
  116. "left": "0",
  117. "right": "0",
  118. "bottom": "0",
  119. "top": "0",
  120. "z_index": 50,
  121. "background": "rgba(0, 0, 0, 0.8)",
  122. }
  123. style = self.style or {}
  124. base_style.update(style)
  125. self.style.update(
  126. {
  127. "css": base_style,
  128. }
  129. )
  130. return self.style
  131. class DrawerClose(DrawerComponent):
  132. """A button that closes the drawer."""
  133. tag = "Drawer.Close"
  134. class DrawerTitle(DrawerComponent):
  135. """A title for the drawer."""
  136. tag = "Drawer.Title"
  137. # Style set based on the source code at https://ui.shadcn.com/docs/components/drawer
  138. def _get_style(self) -> dict:
  139. """Get the style for the component.
  140. Returns:
  141. The dictionary of the component style as value and the style notation as key.
  142. """
  143. base_style = {
  144. "font-size": "1.125rem",
  145. "font-weight": "600",
  146. "line-weight": "1",
  147. "letter-spacing": "-0.05em",
  148. }
  149. style = self.style or {}
  150. base_style.update(style)
  151. self.style.update(
  152. {
  153. "css": base_style,
  154. }
  155. )
  156. return self.style
  157. class DrawerDescription(DrawerComponent):
  158. """A description for the drawer."""
  159. tag = "Drawer.Description"
  160. # Style set based on the source code at https://ui.shadcn.com/docs/components/drawer
  161. def _get_style(self) -> dict:
  162. """Get the style for the component.
  163. Returns:
  164. The dictionary of the component style as value and the style notation as key.
  165. """
  166. base_style = {
  167. "font-size": "0.875rem",
  168. }
  169. style = self.style or {}
  170. base_style.update(style)
  171. self.style.update(
  172. {
  173. "css": base_style,
  174. }
  175. )
  176. return self.style
  177. drawer_root = DrawerRoot.create
  178. drawer_trigger = DrawerTrigger.create
  179. drawer_portal = DrawerPortal.create
  180. drawer_content = DrawerContent.create
  181. drawer_overlay = DrawerOverlay.create
  182. drawer_close = DrawerClose.create
  183. drawer_title = DrawerTitle.create
  184. drawer_description = DrawerDescription.create