drawer.py 7.5 KB

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