drawer.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 RadixPrimitiveComponent
  8. from reflex.constants import EventTriggers
  9. from reflex.vars import Var
  10. class DrawerComponent(RadixPrimitiveComponent):
  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, 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. 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. Defaults to the last snap point.
  33. fade_from_index: Var[int]
  34. # Duration for which the drawer is not draggable after scrolling content inside of the drawer. Defaults to 500ms
  35. scroll_lock_timeout: Var[int]
  36. # When `False`, it allows to interact with elements outside of the drawer without closing it. Defaults to `True`.
  37. modal: Var[bool]
  38. # Direction of the drawer. Defaults to `"bottom"`
  39. direction: Var[LiteralDirectionType]
  40. # When `True`, it prevents scroll restoration. Defaults to `True`.
  41. preventScrollRestoration: Var[bool]
  42. def get_event_triggers(self) -> Dict[str, Any]:
  43. """Get the event triggers that pass the component's value to the handler.
  44. Returns:
  45. A dict mapping the event trigger to the var that is passed to the handler.
  46. """
  47. return {
  48. **super().get_event_triggers(),
  49. EventTriggers.ON_OPEN_CHANGE: lambda e0: [e0.target.value],
  50. }
  51. class DrawerTrigger(DrawerComponent):
  52. """The button that opens the dialog."""
  53. tag = "Drawer.Trigger"
  54. alias = "Vaul" + tag
  55. # Defaults to true, if the first child acts as the trigger.
  56. as_child: Var[bool] = True # type: ignore
  57. class DrawerPortal(DrawerComponent):
  58. """Portals your drawer into the body."""
  59. tag = "Drawer.Portal"
  60. alias = "Vaul" + tag
  61. # Based on https://www.radix-ui.com/primitives/docs/components/dialog#content
  62. class DrawerContent(DrawerComponent):
  63. """Content that should be rendered in the drawer."""
  64. tag = "Drawer.Content"
  65. alias = "Vaul" + tag
  66. # Style set partially based on the source code at https://ui.shadcn.com/docs/components/drawer
  67. def _get_style(self) -> dict:
  68. """Get the style for the component.
  69. Returns:
  70. The dictionary of the component style as value and the style notation as key.
  71. """
  72. base_style = {
  73. "left": "0",
  74. "right": "0",
  75. "bottom": "0",
  76. "top": "0",
  77. "position": "fixed",
  78. "z_index": 50,
  79. "display": "flex",
  80. }
  81. style = self.style or {}
  82. base_style.update(style)
  83. self.style.update(
  84. {
  85. "css": base_style,
  86. }
  87. )
  88. return self.style
  89. def get_event_triggers(self) -> Dict[str, Any]:
  90. """Get the events triggers signatures for the component.
  91. Returns:
  92. The signatures of the event triggers.
  93. """
  94. return {
  95. **super().get_event_triggers(),
  96. # DrawerContent is based on Radix DialogContent
  97. # These are the same triggers as DialogContent
  98. EventTriggers.ON_OPEN_AUTO_FOCUS: lambda e0: [e0.target.value],
  99. EventTriggers.ON_CLOSE_AUTO_FOCUS: lambda e0: [e0.target.value],
  100. EventTriggers.ON_ESCAPE_KEY_DOWN: lambda e0: [e0.target.value],
  101. EventTriggers.ON_POINTER_DOWN_OUTSIDE: lambda e0: [e0.target.value],
  102. EventTriggers.ON_INTERACT_OUTSIDE: lambda e0: [e0.target.value],
  103. }
  104. class DrawerOverlay(DrawerComponent):
  105. """A layer that covers the inert portion of the view when the dialog is open."""
  106. tag = "Drawer.Overlay"
  107. alias = "Vaul" + tag
  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.5)",
  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. alias = "Vaul" + tag
  135. class DrawerTitle(DrawerComponent):
  136. """A title for the drawer."""
  137. tag = "Drawer.Title"
  138. alias = "Vaul" + tag
  139. # Style set based on the source code at https://ui.shadcn.com/docs/components/drawer
  140. def _get_style(self) -> dict:
  141. """Get the style for the component.
  142. Returns:
  143. The dictionary of the component style as value and the style notation as key.
  144. """
  145. base_style = {
  146. "font-size": "1.125rem",
  147. "font-weight": "600",
  148. "line-weight": "1",
  149. "letter-spacing": "-0.05em",
  150. }
  151. style = self.style or {}
  152. base_style.update(style)
  153. self.style.update(
  154. {
  155. "css": base_style,
  156. }
  157. )
  158. return self.style
  159. class DrawerDescription(DrawerComponent):
  160. """A description for the drawer."""
  161. tag = "Drawer.Description"
  162. alias = "Vaul" + tag
  163. # Style set based on the source code at https://ui.shadcn.com/docs/components/drawer
  164. def _get_style(self) -> dict:
  165. """Get the style for the component.
  166. Returns:
  167. The dictionary of the component style as value and the style notation as key.
  168. """
  169. base_style = {
  170. "font-size": "0.875rem",
  171. }
  172. style = self.style or {}
  173. base_style.update(style)
  174. self.style.update(
  175. {
  176. "css": base_style,
  177. }
  178. )
  179. return self.style
  180. class Drawer(SimpleNamespace):
  181. """A namespace for Drawer components."""
  182. root = __call__ = staticmethod(DrawerRoot.create)
  183. trigger = staticmethod(DrawerTrigger.create)
  184. portal = staticmethod(DrawerPortal.create)
  185. content = staticmethod(DrawerContent.create)
  186. overlay = staticmethod(DrawerOverlay.create)
  187. close = staticmethod(DrawerClose.create)
  188. title = staticmethod(DrawerTitle.create)
  189. description = staticmethod(DrawerDescription.create)
  190. drawer = Drawer()