drawer.py 7.3 KB

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