@lodev09/react-native-true-sheet 4.0.0-beta.0 → 4.0.0-beta.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (430) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +62 -30
  3. package/RNTrueSheet.podspec +10 -2
  4. package/android/build.gradle +24 -2
  5. package/android/src/main/java/com/lodev09/truesheet/TrueSheetContainerView.kt +189 -47
  6. package/android/src/main/java/com/lodev09/truesheet/TrueSheetContainerViewManager.kt +7 -0
  7. package/android/src/main/java/com/lodev09/truesheet/TrueSheetContentView.kt +401 -19
  8. package/android/src/main/java/com/lodev09/truesheet/TrueSheetContentViewManager.kt +18 -0
  9. package/android/src/main/java/com/lodev09/truesheet/TrueSheetFooterView.kt +145 -21
  10. package/android/src/main/java/com/lodev09/truesheet/TrueSheetFooterViewManager.kt +14 -0
  11. package/android/src/main/java/com/lodev09/truesheet/TrueSheetHeaderView.kt +38 -0
  12. package/android/src/main/java/com/lodev09/truesheet/TrueSheetHeaderViewManager.kt +28 -0
  13. package/android/src/main/java/com/lodev09/truesheet/TrueSheetModule.kt +84 -24
  14. package/android/src/main/java/com/lodev09/truesheet/TrueSheetOverlayView.kt +106 -0
  15. package/android/src/main/java/com/lodev09/truesheet/TrueSheetOverlayViewManager.kt +45 -0
  16. package/android/src/main/java/com/lodev09/truesheet/TrueSheetPackage.kt +6 -4
  17. package/android/src/main/java/com/lodev09/truesheet/TrueSheetPeekView.kt +72 -0
  18. package/android/src/main/java/com/lodev09/truesheet/TrueSheetPeekViewManager.kt +33 -0
  19. package/android/src/main/java/com/lodev09/truesheet/TrueSheetStateUpdater.kt +63 -0
  20. package/android/src/main/java/com/lodev09/truesheet/TrueSheetView.kt +564 -226
  21. package/android/src/main/java/com/lodev09/truesheet/TrueSheetViewController.kt +1399 -598
  22. package/android/src/main/java/com/lodev09/truesheet/TrueSheetViewManager.kt +161 -29
  23. package/android/src/main/java/com/lodev09/truesheet/core/RNScreensEventObserver.kt +63 -0
  24. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetBottomSheetBehavior.kt +36 -0
  25. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetBottomSheetView.kt +255 -0
  26. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetCoordinatorLayout.kt +375 -0
  27. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetDetentCalculator.kt +299 -0
  28. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetDimView.kt +162 -0
  29. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetGrabberView.kt +180 -0
  30. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetKeyboardObserver.kt +207 -0
  31. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetOverlayRootView.kt +137 -0
  32. package/android/src/main/java/com/lodev09/truesheet/core/TrueSheetStackManager.kt +220 -0
  33. package/android/src/main/java/com/lodev09/truesheet/events/TrueSheetDragEvents.kt +71 -0
  34. package/android/src/main/java/com/lodev09/truesheet/events/TrueSheetFocusEvents.kt +65 -0
  35. package/android/src/main/java/com/lodev09/truesheet/events/TrueSheetLifecycleEvents.kt +127 -0
  36. package/android/src/main/java/com/lodev09/truesheet/events/TrueSheetStateEvents.kt +56 -0
  37. package/android/src/main/java/com/lodev09/truesheet/utils/KeyboardUtils.kt +84 -0
  38. package/android/src/main/java/com/lodev09/truesheet/utils/ScreenUtils.kt +93 -67
  39. package/android/src/main/java/com/lodev09/truesheet/utils/TouchEventDeduper.kt +31 -0
  40. package/android/src/main/java/com/lodev09/truesheet/utils/ViewUtils.kt +42 -0
  41. package/android/src/main/jni/CMakeLists.txt +63 -0
  42. package/android/src/main/jni/TrueSheetSpec.h +20 -0
  43. package/android/src/main/jni/TrueSheetStateUpdater.cpp +96 -0
  44. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetContentViewComponentDescriptor.h +16 -0
  45. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetContentViewShadowNode.cpp +68 -0
  46. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetContentViewShadowNode.h +33 -0
  47. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetContentViewState.cpp +11 -0
  48. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetContentViewState.h +39 -0
  49. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetFooterViewComponentDescriptor.h +24 -0
  50. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetFooterViewShadowNode.cpp +63 -0
  51. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetFooterViewShadowNode.h +28 -0
  52. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetFooterViewState.cpp +11 -0
  53. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetFooterViewState.h +41 -0
  54. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetInsets.cpp +18 -0
  55. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetInsets.h +16 -0
  56. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetLayoutUtils.h +46 -0
  57. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetOverlayViewComponentDescriptor.h +21 -0
  58. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetOverlayViewShadowNode.cpp +17 -0
  59. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetOverlayViewShadowNode.h +36 -0
  60. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetViewComponentDescriptor.h +28 -0
  61. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetViewShadowNode.cpp +30 -0
  62. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetViewShadowNode.h +45 -0
  63. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetViewState.cpp +23 -0
  64. package/common/cpp/react/renderer/components/TrueSheetSpec/TrueSheetViewState.h +52 -0
  65. package/ios/TrueSheetContainerView.h +88 -6
  66. package/ios/TrueSheetContainerView.mm +278 -21
  67. package/ios/TrueSheetContentView.h +54 -1
  68. package/ios/TrueSheetContentView.mm +371 -47
  69. package/ios/TrueSheetFooterView.h +33 -2
  70. package/ios/TrueSheetFooterView.mm +187 -35
  71. package/ios/{events/OnWillDismissEvent.h → TrueSheetHeaderView.h} +10 -7
  72. package/ios/TrueSheetHeaderView.mm +64 -0
  73. package/ios/TrueSheetModule.h +2 -7
  74. package/ios/TrueSheetModule.mm +98 -20
  75. package/ios/TrueSheetOverlayView.h +32 -0
  76. package/ios/TrueSheetOverlayView.mm +161 -0
  77. package/ios/TrueSheetPeekView.h +32 -0
  78. package/ios/TrueSheetPeekView.mm +99 -0
  79. package/ios/TrueSheetView.h +7 -15
  80. package/ios/TrueSheetView.mm +775 -215
  81. package/ios/TrueSheetViewController.h +94 -16
  82. package/ios/TrueSheetViewController.mm +1316 -331
  83. package/ios/core/RNScreensEventObserver.h +46 -0
  84. package/ios/core/RNScreensEventObserver.mm +280 -0
  85. package/ios/core/TrueSheetBlurView.h +27 -0
  86. package/ios/core/TrueSheetBlurView.mm +82 -0
  87. package/ios/core/TrueSheetDetentCalculator.h +107 -0
  88. package/ios/core/TrueSheetDetentCalculator.mm +197 -0
  89. package/ios/core/TrueSheetGrabberView.h +72 -0
  90. package/ios/core/TrueSheetGrabberView.mm +188 -0
  91. package/ios/core/TrueSheetKeyboardObserver.h +39 -0
  92. package/ios/core/TrueSheetKeyboardObserver.mm +112 -0
  93. package/ios/core/TrueSheetOverlayContainerView.h +31 -0
  94. package/ios/core/TrueSheetOverlayContainerView.mm +35 -0
  95. package/ios/events/TrueSheetDragEvents.h +39 -0
  96. package/ios/events/TrueSheetDragEvents.mm +62 -0
  97. package/ios/events/{OnPositionChangeEvent.h → TrueSheetFocusEvents.h} +8 -5
  98. package/ios/events/TrueSheetFocusEvents.mm +49 -0
  99. package/ios/events/TrueSheetLifecycleEvents.h +42 -0
  100. package/ios/events/TrueSheetLifecycleEvents.mm +79 -0
  101. package/ios/events/TrueSheetStateEvents.h +35 -0
  102. package/ios/events/TrueSheetStateEvents.mm +49 -0
  103. package/ios/tvos/TrueSheetStubs.mm +166 -0
  104. package/ios/{events/OnMountEvent.h → utils/BlurUtil.h} +5 -7
  105. package/ios/utils/BlurUtil.mm +69 -0
  106. package/ios/utils/GestureUtil.h +7 -0
  107. package/ios/utils/GestureUtil.mm +12 -0
  108. package/ios/utils/PlatformUtil.h +15 -0
  109. package/ios/utils/UIView+FirstResponder.h +15 -0
  110. package/ios/utils/UIView+FirstResponder.mm +26 -0
  111. package/ios/utils/UIView+ScrollEdgeInteraction.h +26 -0
  112. package/ios/utils/UIView+ScrollEdgeInteraction.mm +84 -0
  113. package/ios/utils/WindowUtil.mm +17 -1
  114. package/lib/module/TrueSheet.js +349 -74
  115. package/lib/module/TrueSheet.js.map +1 -1
  116. package/lib/module/TrueSheet.web.js +1270 -0
  117. package/lib/module/TrueSheet.web.js.map +1 -0
  118. package/lib/module/TrueSheetOverlay.js +30 -0
  119. package/lib/module/TrueSheetOverlay.js.map +1 -0
  120. package/lib/module/TrueSheetOverlay.web.js +35 -0
  121. package/lib/module/TrueSheetOverlay.web.js.map +1 -0
  122. package/lib/module/TrueSheetPeek.js +15 -0
  123. package/lib/module/TrueSheetPeek.js.map +1 -0
  124. package/lib/module/TrueSheetPeek.web.js +55 -0
  125. package/lib/module/TrueSheetPeek.web.js.map +1 -0
  126. package/lib/module/TrueSheetProvider.js +28 -0
  127. package/lib/module/TrueSheetProvider.js.map +1 -0
  128. package/lib/module/TrueSheetProvider.web.js +157 -0
  129. package/lib/module/TrueSheetProvider.web.js.map +1 -0
  130. package/lib/module/fabric/TrueSheetContainerViewNativeComponent.ts +1 -1
  131. package/lib/module/fabric/TrueSheetContentViewNativeComponent.ts +5 -1
  132. package/lib/module/fabric/TrueSheetFooterViewNativeComponent.ts +10 -2
  133. package/lib/module/fabric/TrueSheetHeaderViewNativeComponent.ts +8 -0
  134. package/lib/module/fabric/TrueSheetOverlayViewNativeComponent.ts +12 -0
  135. package/lib/module/fabric/TrueSheetPeekViewNativeComponent.ts +8 -0
  136. package/lib/module/fabric/TrueSheetViewNativeComponent.ts +110 -18
  137. package/lib/module/index.js +4 -3
  138. package/lib/module/index.js.map +1 -1
  139. package/lib/module/mocks/index.js +103 -0
  140. package/lib/module/mocks/index.js.map +1 -0
  141. package/lib/module/mocks/navigation.js +95 -0
  142. package/lib/module/mocks/navigation.js.map +1 -0
  143. package/lib/module/mocks/navigationExpoRouter.js +21 -0
  144. package/lib/module/mocks/navigationExpoRouter.js.map +1 -0
  145. package/lib/module/mocks/reanimated.js +91 -0
  146. package/lib/module/mocks/reanimated.js.map +1 -0
  147. package/lib/module/navigation/TrueSheetNavigatorContent.js +73 -0
  148. package/lib/module/navigation/TrueSheetNavigatorContent.js.map +1 -0
  149. package/lib/module/navigation/actions.js +50 -0
  150. package/lib/module/navigation/actions.js.map +1 -0
  151. package/lib/module/navigation/base-types.js +2 -0
  152. package/lib/module/navigation/base-types.js.map +1 -0
  153. package/lib/module/navigation/createTrueSheetRouter.js +157 -0
  154. package/lib/module/navigation/createTrueSheetRouter.js.map +1 -0
  155. package/lib/module/navigation/expo-router/base-types.js +2 -0
  156. package/lib/module/navigation/expo-router/base-types.js.map +1 -0
  157. package/lib/module/navigation/expo-router/index.js +50 -0
  158. package/lib/module/navigation/expo-router/index.js.map +1 -0
  159. package/lib/module/navigation/expo-router/types.js +4 -0
  160. package/lib/module/navigation/expo-router/types.js.map +1 -0
  161. package/lib/module/navigation/index.js +47 -0
  162. package/lib/module/navigation/index.js.map +1 -0
  163. package/lib/module/navigation/navigator.js +10 -0
  164. package/lib/module/navigation/navigator.js.map +1 -0
  165. package/lib/module/navigation/screen/ReanimatedTrueSheetScreen.js +56 -0
  166. package/lib/module/navigation/screen/ReanimatedTrueSheetScreen.js.map +1 -0
  167. package/lib/module/navigation/screen/TrueSheetScreen.js +49 -0
  168. package/lib/module/navigation/screen/TrueSheetScreen.js.map +1 -0
  169. package/lib/module/navigation/screen/index.js +5 -0
  170. package/lib/module/navigation/screen/index.js.map +1 -0
  171. package/lib/module/navigation/screen/types.js +4 -0
  172. package/lib/module/navigation/screen/types.js.map +1 -0
  173. package/lib/module/navigation/screen/useSheetScreenState.js +69 -0
  174. package/lib/module/navigation/screen/useSheetScreenState.js.map +1 -0
  175. package/lib/module/navigation/types.js +4 -0
  176. package/lib/module/navigation/types.js.map +1 -0
  177. package/lib/module/navigation/useTrueSheetNavigation.js +24 -0
  178. package/lib/module/navigation/useTrueSheetNavigation.js.map +1 -0
  179. package/lib/module/reanimated/ReanimatedTrueSheet.js +15 -9
  180. package/lib/module/reanimated/ReanimatedTrueSheet.js.map +1 -1
  181. package/lib/module/reanimated/ReanimatedTrueSheet.web.js +81 -0
  182. package/lib/module/reanimated/ReanimatedTrueSheet.web.js.map +1 -0
  183. package/lib/module/reanimated/ReanimatedTrueSheetProvider.js +4 -2
  184. package/lib/module/reanimated/ReanimatedTrueSheetProvider.js.map +1 -1
  185. package/lib/module/reanimated/index.js +2 -2
  186. package/lib/module/reanimated/index.js.map +1 -1
  187. package/lib/module/reanimated/useReanimatedPositionChangeHandler.web.js +20 -0
  188. package/lib/module/reanimated/useReanimatedPositionChangeHandler.web.js.map +1 -0
  189. package/lib/module/specs/NativeTrueSheetModule.js.map +1 -1
  190. package/lib/module/web/constants.js +16 -0
  191. package/lib/module/web/constants.js.map +1 -0
  192. package/lib/module/web/dom.js +10 -0
  193. package/lib/module/web/dom.js.map +1 -0
  194. package/lib/module/web/vaul/README.md +9 -0
  195. package/lib/module/web/vaul/browser.js +30 -0
  196. package/lib/module/web/vaul/browser.js.map +1 -0
  197. package/lib/module/web/vaul/constants.js +19 -0
  198. package/lib/module/web/vaul/constants.js.map +1 -0
  199. package/lib/module/web/vaul/context.js +59 -0
  200. package/lib/module/web/vaul/context.js.map +1 -0
  201. package/lib/module/web/vaul/helpers.js +99 -0
  202. package/lib/module/web/vaul/helpers.js.map +1 -0
  203. package/lib/module/web/vaul/index.js +1342 -0
  204. package/lib/module/web/vaul/index.js.map +1 -0
  205. package/lib/module/web/vaul/style.css +287 -0
  206. package/lib/module/web/vaul/style.css.d.js +2 -0
  207. package/lib/module/web/vaul/style.css.d.js.map +1 -0
  208. package/lib/module/web/vaul/types.js +2 -0
  209. package/lib/module/web/vaul/types.js.map +1 -0
  210. package/lib/module/web/vaul/use-composed-refs.js +34 -0
  211. package/lib/module/web/vaul/use-composed-refs.js.map +1 -0
  212. package/lib/module/web/vaul/use-controllable-state.js +54 -0
  213. package/lib/module/web/vaul/use-controllable-state.js.map +1 -0
  214. package/lib/module/web/vaul/use-position-fixed.js +123 -0
  215. package/lib/module/web/vaul/use-position-fixed.js.map +1 -0
  216. package/lib/module/web/vaul/use-prevent-scroll.js +258 -0
  217. package/lib/module/web/vaul/use-prevent-scroll.js.map +1 -0
  218. package/lib/module/web/vaul/use-scale-background.js +57 -0
  219. package/lib/module/web/vaul/use-scale-background.js.map +1 -0
  220. package/lib/module/web/vaul/use-snap-points.js +305 -0
  221. package/lib/module/web/vaul/use-snap-points.js.map +1 -0
  222. package/lib/typescript/src/TrueSheet.d.ts +57 -14
  223. package/lib/typescript/src/TrueSheet.d.ts.map +1 -1
  224. package/lib/typescript/src/TrueSheet.types.d.ts +535 -55
  225. package/lib/typescript/src/TrueSheet.types.d.ts.map +1 -1
  226. package/lib/typescript/src/TrueSheet.web.d.ts +5 -0
  227. package/lib/typescript/src/TrueSheet.web.d.ts.map +1 -0
  228. package/lib/typescript/src/TrueSheetOverlay.d.ts +8 -0
  229. package/lib/typescript/src/TrueSheetOverlay.d.ts.map +1 -0
  230. package/lib/typescript/src/TrueSheetOverlay.web.d.ts +8 -0
  231. package/lib/typescript/src/TrueSheetOverlay.web.d.ts.map +1 -0
  232. package/lib/typescript/src/TrueSheetPeek.d.ts +9 -0
  233. package/lib/typescript/src/TrueSheetPeek.d.ts.map +1 -0
  234. package/lib/typescript/src/TrueSheetPeek.web.d.ts +28 -0
  235. package/lib/typescript/src/TrueSheetPeek.web.d.ts.map +1 -0
  236. package/lib/typescript/src/TrueSheetProvider.d.ts +18 -0
  237. package/lib/typescript/src/TrueSheetProvider.d.ts.map +1 -0
  238. package/lib/typescript/src/TrueSheetProvider.web.d.ts +27 -0
  239. package/lib/typescript/src/TrueSheetProvider.web.d.ts.map +1 -0
  240. package/lib/typescript/src/fabric/TrueSheetContainerViewNativeComponent.d.ts.map +1 -1
  241. package/lib/typescript/src/fabric/TrueSheetContentViewNativeComponent.d.ts.map +1 -1
  242. package/lib/typescript/src/fabric/TrueSheetFooterViewNativeComponent.d.ts +2 -0
  243. package/lib/typescript/src/fabric/TrueSheetFooterViewNativeComponent.d.ts.map +1 -1
  244. package/lib/typescript/src/fabric/TrueSheetHeaderViewNativeComponent.d.ts +6 -0
  245. package/lib/typescript/src/fabric/TrueSheetHeaderViewNativeComponent.d.ts.map +1 -0
  246. package/lib/typescript/src/fabric/TrueSheetOverlayViewNativeComponent.d.ts +6 -0
  247. package/lib/typescript/src/fabric/TrueSheetOverlayViewNativeComponent.d.ts.map +1 -0
  248. package/lib/typescript/src/fabric/TrueSheetPeekViewNativeComponent.d.ts +6 -0
  249. package/lib/typescript/src/fabric/TrueSheetPeekViewNativeComponent.d.ts.map +1 -0
  250. package/lib/typescript/src/fabric/TrueSheetViewNativeComponent.d.ts +71 -16
  251. package/lib/typescript/src/fabric/TrueSheetViewNativeComponent.d.ts.map +1 -1
  252. package/lib/typescript/src/index.d.ts +3 -2
  253. package/lib/typescript/src/index.d.ts.map +1 -1
  254. package/lib/typescript/src/mocks/index.d.ts +47 -0
  255. package/lib/typescript/src/mocks/index.d.ts.map +1 -0
  256. package/lib/typescript/src/mocks/navigation.d.ts +70 -0
  257. package/lib/typescript/src/mocks/navigation.d.ts.map +1 -0
  258. package/lib/typescript/src/mocks/navigationExpoRouter.d.ts +22 -0
  259. package/lib/typescript/src/mocks/navigationExpoRouter.d.ts.map +1 -0
  260. package/lib/typescript/src/mocks/reanimated.d.ts +48 -0
  261. package/lib/typescript/src/mocks/reanimated.d.ts.map +1 -0
  262. package/lib/typescript/src/navigation/TrueSheetNavigatorContent.d.ts +5 -0
  263. package/lib/typescript/src/navigation/TrueSheetNavigatorContent.d.ts.map +1 -0
  264. package/lib/typescript/src/navigation/actions.d.ts +71 -0
  265. package/lib/typescript/src/navigation/actions.d.ts.map +1 -0
  266. package/lib/typescript/src/navigation/base-types.d.ts +9 -0
  267. package/lib/typescript/src/navigation/base-types.d.ts.map +1 -0
  268. package/lib/typescript/src/navigation/createTrueSheetRouter.d.ts +3 -0
  269. package/lib/typescript/src/navigation/createTrueSheetRouter.d.ts.map +1 -0
  270. package/lib/typescript/src/navigation/expo-router/base-types.d.ts +8 -0
  271. package/lib/typescript/src/navigation/expo-router/base-types.d.ts.map +1 -0
  272. package/lib/typescript/src/navigation/expo-router/index.d.ts +37 -0
  273. package/lib/typescript/src/navigation/expo-router/index.d.ts.map +1 -0
  274. package/lib/typescript/src/navigation/expo-router/types.d.ts +44 -0
  275. package/lib/typescript/src/navigation/expo-router/types.d.ts.map +1 -0
  276. package/lib/typescript/src/navigation/index.d.ts +45 -0
  277. package/lib/typescript/src/navigation/index.d.ts.map +1 -0
  278. package/lib/typescript/src/navigation/navigator.d.ts +7 -0
  279. package/lib/typescript/src/navigation/navigator.d.ts.map +1 -0
  280. package/lib/typescript/src/navigation/screen/ReanimatedTrueSheetScreen.d.ts +3 -0
  281. package/lib/typescript/src/navigation/screen/ReanimatedTrueSheetScreen.d.ts.map +1 -0
  282. package/lib/typescript/src/navigation/screen/TrueSheetScreen.d.ts +3 -0
  283. package/lib/typescript/src/navigation/screen/TrueSheetScreen.d.ts.map +1 -0
  284. package/lib/typescript/src/navigation/screen/index.d.ts +4 -0
  285. package/lib/typescript/src/navigation/screen/index.d.ts.map +1 -0
  286. package/lib/typescript/src/navigation/screen/types.d.ts +14 -0
  287. package/lib/typescript/src/navigation/screen/types.d.ts.map +1 -0
  288. package/lib/typescript/src/navigation/screen/useSheetScreenState.d.ts +34 -0
  289. package/lib/typescript/src/navigation/screen/useSheetScreenState.d.ts.map +1 -0
  290. package/lib/typescript/src/navigation/types.d.ts +213 -0
  291. package/lib/typescript/src/navigation/types.d.ts.map +1 -0
  292. package/lib/typescript/src/navigation/useTrueSheetNavigation.d.ts +23 -0
  293. package/lib/typescript/src/navigation/useTrueSheetNavigation.d.ts.map +1 -0
  294. package/lib/typescript/src/reanimated/ReanimatedTrueSheet.d.ts.map +1 -1
  295. package/lib/typescript/src/reanimated/ReanimatedTrueSheet.web.d.ts +41 -0
  296. package/lib/typescript/src/reanimated/ReanimatedTrueSheet.web.d.ts.map +1 -0
  297. package/lib/typescript/src/reanimated/ReanimatedTrueSheetProvider.d.ts +8 -2
  298. package/lib/typescript/src/reanimated/ReanimatedTrueSheetProvider.d.ts.map +1 -1
  299. package/lib/typescript/src/reanimated/useReanimatedPositionChangeHandler.web.d.ts +15 -0
  300. package/lib/typescript/src/reanimated/useReanimatedPositionChangeHandler.web.d.ts.map +1 -0
  301. package/lib/typescript/src/specs/NativeTrueSheetModule.d.ts +25 -3
  302. package/lib/typescript/src/specs/NativeTrueSheetModule.d.ts.map +1 -1
  303. package/lib/typescript/src/web/constants.d.ts +14 -0
  304. package/lib/typescript/src/web/constants.d.ts.map +1 -0
  305. package/lib/typescript/src/web/dom.d.ts +7 -0
  306. package/lib/typescript/src/web/dom.d.ts.map +1 -0
  307. package/lib/typescript/src/web/vaul/browser.d.ts +8 -0
  308. package/lib/typescript/src/web/vaul/browser.d.ts.map +1 -0
  309. package/lib/typescript/src/web/vaul/constants.d.ts +14 -0
  310. package/lib/typescript/src/web/vaul/constants.d.ts.map +1 -0
  311. package/lib/typescript/src/web/vaul/context.d.ts +54 -0
  312. package/lib/typescript/src/web/vaul/context.d.ts.map +1 -0
  313. package/lib/typescript/src/web/vaul/helpers.d.ts +17 -0
  314. package/lib/typescript/src/web/vaul/helpers.d.ts.map +1 -0
  315. package/lib/typescript/src/web/vaul/index.d.ts +234 -0
  316. package/lib/typescript/src/web/vaul/index.d.ts.map +1 -0
  317. package/lib/typescript/src/web/vaul/types.d.ts +7 -0
  318. package/lib/typescript/src/web/vaul/types.d.ts.map +1 -0
  319. package/lib/typescript/src/web/vaul/use-composed-refs.d.ts +14 -0
  320. package/lib/typescript/src/web/vaul/use-composed-refs.d.ts.map +1 -0
  321. package/lib/typescript/src/web/vaul/use-controllable-state.d.ts +9 -0
  322. package/lib/typescript/src/web/vaul/use-controllable-state.d.ts.map +1 -0
  323. package/lib/typescript/src/web/vaul/use-position-fixed.d.ts +19 -0
  324. package/lib/typescript/src/web/vaul/use-position-fixed.d.ts.map +1 -0
  325. package/lib/typescript/src/web/vaul/use-prevent-scroll.d.ts +18 -0
  326. package/lib/typescript/src/web/vaul/use-prevent-scroll.d.ts.map +1 -0
  327. package/lib/typescript/src/web/vaul/use-scale-background.d.ts +2 -0
  328. package/lib/typescript/src/web/vaul/use-scale-background.d.ts.map +1 -0
  329. package/lib/typescript/src/web/vaul/use-snap-points.d.ts +38 -0
  330. package/lib/typescript/src/web/vaul/use-snap-points.d.ts.map +1 -0
  331. package/package.json +104 -37
  332. package/react-native.config.js +10 -2
  333. package/src/TrueSheet.tsx +424 -70
  334. package/src/TrueSheet.types.ts +602 -72
  335. package/src/TrueSheet.web.tsx +1437 -0
  336. package/src/TrueSheetOverlay.tsx +24 -0
  337. package/src/TrueSheetOverlay.web.tsx +33 -0
  338. package/src/TrueSheetPeek.tsx +11 -0
  339. package/src/TrueSheetPeek.web.tsx +64 -0
  340. package/src/TrueSheetProvider.tsx +33 -0
  341. package/src/TrueSheetProvider.web.tsx +220 -0
  342. package/src/fabric/TrueSheetContainerViewNativeComponent.ts +1 -1
  343. package/src/fabric/TrueSheetContentViewNativeComponent.ts +5 -1
  344. package/src/fabric/TrueSheetFooterViewNativeComponent.ts +10 -2
  345. package/src/fabric/TrueSheetHeaderViewNativeComponent.ts +8 -0
  346. package/src/fabric/TrueSheetOverlayViewNativeComponent.ts +12 -0
  347. package/src/fabric/TrueSheetPeekViewNativeComponent.ts +8 -0
  348. package/src/fabric/TrueSheetViewNativeComponent.ts +110 -18
  349. package/src/index.ts +3 -2
  350. package/src/mocks/index.ts +96 -0
  351. package/src/mocks/navigation.ts +79 -0
  352. package/src/mocks/navigationExpoRouter.ts +41 -0
  353. package/src/mocks/reanimated.ts +100 -0
  354. package/src/navigation/TrueSheetNavigatorContent.tsx +106 -0
  355. package/src/navigation/actions.ts +78 -0
  356. package/src/navigation/base-types.ts +20 -0
  357. package/src/navigation/createTrueSheetRouter.ts +194 -0
  358. package/src/navigation/expo-router/base-types.ts +18 -0
  359. package/src/navigation/expo-router/index.ts +90 -0
  360. package/src/navigation/expo-router/types.ts +116 -0
  361. package/src/navigation/index.ts +80 -0
  362. package/src/navigation/navigator.ts +18 -0
  363. package/src/navigation/screen/ReanimatedTrueSheetScreen.tsx +61 -0
  364. package/src/navigation/screen/TrueSheetScreen.tsx +54 -0
  365. package/src/navigation/screen/index.ts +3 -0
  366. package/src/navigation/screen/types.ts +19 -0
  367. package/src/navigation/screen/useSheetScreenState.ts +102 -0
  368. package/src/navigation/types.ts +302 -0
  369. package/src/navigation/useTrueSheetNavigation.ts +26 -0
  370. package/src/reanimated/ReanimatedTrueSheet.tsx +12 -7
  371. package/src/reanimated/ReanimatedTrueSheet.web.tsx +75 -0
  372. package/src/reanimated/ReanimatedTrueSheetProvider.tsx +12 -8
  373. package/src/reanimated/useReanimatedPositionChangeHandler.web.ts +31 -0
  374. package/src/specs/NativeTrueSheetModule.ts +28 -3
  375. package/src/web/constants.ts +13 -0
  376. package/src/web/dom.ts +9 -0
  377. package/src/web/vaul/README.md +9 -0
  378. package/src/web/vaul/browser.ts +38 -0
  379. package/src/web/vaul/constants.ts +24 -0
  380. package/src/web/vaul/context.ts +102 -0
  381. package/src/web/vaul/helpers.ts +131 -0
  382. package/src/web/vaul/index.tsx +1785 -0
  383. package/src/web/vaul/style.css +287 -0
  384. package/src/web/vaul/style.css.d.ts +1 -0
  385. package/src/web/vaul/types.ts +7 -0
  386. package/src/web/vaul/use-composed-refs.ts +35 -0
  387. package/src/web/vaul/use-controllable-state.ts +66 -0
  388. package/src/web/vaul/use-position-fixed.ts +147 -0
  389. package/src/web/vaul/use-prevent-scroll.ts +309 -0
  390. package/src/web/vaul/use-scale-background.ts +64 -0
  391. package/src/web/vaul/use-snap-points.ts +455 -0
  392. package/android/src/main/java/com/lodev09/truesheet/events/DetentChangeEvent.kt +0 -26
  393. package/android/src/main/java/com/lodev09/truesheet/events/DidDismissEvent.kt +0 -20
  394. package/android/src/main/java/com/lodev09/truesheet/events/DidPresentEvent.kt +0 -26
  395. package/android/src/main/java/com/lodev09/truesheet/events/DragBeginEvent.kt +0 -26
  396. package/android/src/main/java/com/lodev09/truesheet/events/DragChangeEvent.kt +0 -26
  397. package/android/src/main/java/com/lodev09/truesheet/events/DragEndEvent.kt +0 -26
  398. package/android/src/main/java/com/lodev09/truesheet/events/MountEvent.kt +0 -20
  399. package/android/src/main/java/com/lodev09/truesheet/events/PositionChangeEvent.kt +0 -32
  400. package/android/src/main/java/com/lodev09/truesheet/events/SizeChangeEvent.kt +0 -27
  401. package/android/src/main/java/com/lodev09/truesheet/events/WillDismissEvent.kt +0 -20
  402. package/android/src/main/java/com/lodev09/truesheet/events/WillPresentEvent.kt +0 -26
  403. package/android/src/main/res/values/styles.xml +0 -8
  404. package/ios/events/OnDetentChangeEvent.h +0 -28
  405. package/ios/events/OnDetentChangeEvent.mm +0 -30
  406. package/ios/events/OnDidDismissEvent.h +0 -26
  407. package/ios/events/OnDidDismissEvent.mm +0 -25
  408. package/ios/events/OnDidPresentEvent.h +0 -28
  409. package/ios/events/OnDidPresentEvent.mm +0 -30
  410. package/ios/events/OnDragBeginEvent.h +0 -28
  411. package/ios/events/OnDragBeginEvent.mm +0 -30
  412. package/ios/events/OnDragChangeEvent.h +0 -28
  413. package/ios/events/OnDragChangeEvent.mm +0 -30
  414. package/ios/events/OnDragEndEvent.h +0 -28
  415. package/ios/events/OnDragEndEvent.mm +0 -30
  416. package/ios/events/OnMountEvent.mm +0 -25
  417. package/ios/events/OnPositionChangeEvent.mm +0 -32
  418. package/ios/events/OnSizeChangeEvent.h +0 -28
  419. package/ios/events/OnSizeChangeEvent.mm +0 -30
  420. package/ios/events/OnWillDismissEvent.mm +0 -25
  421. package/ios/events/OnWillPresentEvent.h +0 -28
  422. package/ios/events/OnWillPresentEvent.mm +0 -30
  423. package/ios/utils/LayoutUtil.h +0 -44
  424. package/ios/utils/LayoutUtil.mm +0 -50
  425. package/lib/module/TrueSheetGrabber.js +0 -51
  426. package/lib/module/TrueSheetGrabber.js.map +0 -1
  427. package/lib/typescript/src/TrueSheetGrabber.d.ts +0 -39
  428. package/lib/typescript/src/TrueSheetGrabber.d.ts.map +0 -1
  429. package/src/TrueSheetGrabber.tsx +0 -82
  430. package/src/__mocks__/index.js +0 -67
@@ -0,0 +1,1785 @@
1
+ 'use client';
2
+
3
+ import React from 'react';
4
+
5
+ import * as DialogPrimitive from '@radix-ui/react-dialog';
6
+ import { Presence } from '@radix-ui/react-presence';
7
+
8
+ import { DrawerContext, useDrawerContext, type DragEvent } from './context';
9
+ import './style.css';
10
+
11
+ import { isIOS, isMobileFirefox } from './browser';
12
+ import {
13
+ BORDER_RADIUS,
14
+ CLOSE_THRESHOLD,
15
+ DISMISS_ATTEMPT_THRESHOLD,
16
+ DRAG_CLASS,
17
+ NESTED_DISPLACEMENT,
18
+ SCROLL_LOCK_TIMEOUT,
19
+ TRANSITIONS,
20
+ VELOCITY_THRESHOLD,
21
+ WINDOW_TOP_OFFSET,
22
+ } from './constants';
23
+ import { dampenValue, getTranslate, isVertical, reset, set } from './helpers';
24
+ import type { DrawerDirection } from './types';
25
+ import { useComposedRefs } from './use-composed-refs';
26
+ import { useControllableState } from './use-controllable-state';
27
+ import { usePositionFixed } from './use-position-fixed';
28
+ import { isInput, isScrollable, usePreventScroll } from './use-prevent-scroll';
29
+ import { useScaleBackground } from './use-scale-background';
30
+ import { useSnapPoints } from './use-snap-points';
31
+
32
+ export interface WithFadeFromProps {
33
+ /**
34
+ * Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
35
+ * Should go from least visible. Example `[0.2, 0.5, 0.8]`.
36
+ * You can also use px values, which doesn't take screen height into account.
37
+ */
38
+ snapPoints: (number | string)[];
39
+ /**
40
+ * Index of a `snapPoint` from which the overlay fade should be applied. Defaults to the last snap point.
41
+ */
42
+ fadeFromIndex: number;
43
+ }
44
+
45
+ export interface WithoutFadeFromProps {
46
+ /**
47
+ * Array of numbers from 0 to 100 that corresponds to % of the screen a given snap point should take up.
48
+ * Should go from least visible. Example `[0.2, 0.5, 0.8]`.
49
+ * You can also use px values, which doesn't take screen height into account.
50
+ */
51
+ snapPoints?: (number | string)[];
52
+ fadeFromIndex?: never;
53
+ }
54
+
55
+ export type DialogProps = {
56
+ activeSnapPoint?: number | string | null;
57
+ setActiveSnapPoint?: (snapPoint: number | string | null) => void;
58
+ children?: React.ReactNode;
59
+ open?: boolean;
60
+ /**
61
+ * Number between 0 and 1 that determines when the drawer should be closed.
62
+ * Example: threshold of 0.5 would close the drawer if the user swiped for 50% of the height of the drawer or more.
63
+ * @default 0.25
64
+ */
65
+ closeThreshold?: number;
66
+ /**
67
+ * When `true` the `body` doesn't get any styles assigned from Vaul
68
+ */
69
+ noBodyStyles?: boolean;
70
+ onOpenChange?: (open: boolean) => void;
71
+ shouldScaleBackground?: boolean;
72
+ /**
73
+ * When `false` we don't change body's background color when the drawer is open.
74
+ * @default true
75
+ */
76
+ setBackgroundColorOnScale?: boolean;
77
+ /**
78
+ * Duration for which the drawer is not draggable after scrolling content inside of the drawer.
79
+ * @default 500ms
80
+ */
81
+ scrollLockTimeout?: number;
82
+ /**
83
+ * When `true`, don't move the drawer upwards if there's space, but rather only change it's height so it's fully scrollable when the keyboard is open
84
+ */
85
+ fixed?: boolean;
86
+ /**
87
+ * When `true` only allows the drawer to be dragged by the `<Drawer.Handle />` component.
88
+ * @default false
89
+ */
90
+ handleOnly?: boolean;
91
+ /**
92
+ * When `false` dragging, clicking outside, pressing esc, etc. will not close the drawer.
93
+ * Use this in comination with the `open` prop, otherwise you won't be able to open/close the drawer.
94
+ * @default true
95
+ */
96
+ dismissible?: boolean;
97
+ /**
98
+ * Called when the user tries to close a non-dismissible drawer — dragging down
99
+ * at the first snap point, pressing escape, or tapping the handle of a
100
+ * single-snap-point drawer. Overlay clicks don't count, mirroring iOS.
101
+ */
102
+ onDismissAttempt?: () => void;
103
+ /**
104
+ * When `false` the drawer can't be dragged by the user. Programmatic snap-point
105
+ * changes still animate. Defaults to `true`.
106
+ */
107
+ draggable?: boolean;
108
+ onDrag?: (event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) => void;
109
+ onRelease?: (event: React.PointerEvent<HTMLDivElement>, open: boolean) => void;
110
+ /**
111
+ * When `false` it allows to interact with elements outside of the drawer without closing it.
112
+ * @default true
113
+ */
114
+ modal?: boolean;
115
+ nested?: boolean;
116
+ onClose?: () => void;
117
+ /**
118
+ * Direction of the drawer. Can be `top` or `bottom`, `left`, `right`.
119
+ * @default 'bottom'
120
+ */
121
+ direction?: 'top' | 'bottom' | 'left' | 'right';
122
+ /**
123
+ * Opened by default, skips initial enter animation. Still reacts to `open` state changes
124
+ * @default false
125
+ */
126
+ defaultOpen?: boolean;
127
+ /**
128
+ * When set to `true` prevents scrolling on the document body on mount, and restores it on unmount.
129
+ * @default false
130
+ */
131
+ disablePreventScroll?: boolean;
132
+ /**
133
+ * When `true` Vaul will reposition inputs rather than scroll then into view if the keyboard is in the way.
134
+ * Setting it to `false` will fall back to the default browser behavior.
135
+ * @default true when {@link snapPoints} is defined
136
+ */
137
+ repositionInputs?: boolean;
138
+ /**
139
+ * Disabled velocity based swiping for snap points.
140
+ * This means that a snap point won't be skipped even if the velocity is high enough.
141
+ * Useful if each snap point in a drawer is equally important.
142
+ * @default false
143
+ */
144
+ snapToSequentialPoint?: boolean;
145
+ container?: HTMLElement | null;
146
+ /**
147
+ * Gets triggered after the open or close animation ends, it receives an `open` argument with the `open` state of the drawer by the time the function was triggered.
148
+ * Useful to revert any state changes for example.
149
+ */
150
+ onAnimationEnd?: (open: boolean) => void;
151
+ /**
152
+ * Fires on every animation frame while the drawer is open with the drawer's
153
+ * current top edge in the viewport (px). Emits during drag, snap, and the
154
+ * open/close transitions.
155
+ */
156
+ onPositionChange?: (position: number) => void;
157
+ preventScrollRestoration?: boolean;
158
+ autoFocus?: boolean;
159
+ /**
160
+ * Renders the drawer as a floating card offset from the bottom edge so it
161
+ * doesn't visually attach to the viewport. Snap points are computed against
162
+ * the reduced floating area, so {@link detachedOffset} is preserved during
163
+ * drag and snap.
164
+ * @default false
165
+ */
166
+ detached?: boolean;
167
+ /**
168
+ * Gap (in px) between the drawer's bottom edge and the viewport bottom when
169
+ * {@link detached} is `true`.
170
+ * @default 0
171
+ */
172
+ detachedOffset?: number;
173
+ /**
174
+ * Corner radius (in px) applied to the clip wrapper's bottom when
175
+ * {@link detached} is `true`, so the floating card's bottom edge stays
176
+ * rounded while it's clipped at the floating anchor.
177
+ * @default 0
178
+ */
179
+ detachedRadius?: number;
180
+ /**
181
+ * Extra styles merged into the detached clip wrapper. Use this to match the
182
+ * wrapper's horizontal bounds to the drawer's on desktop (e.g. `maxWidth` +
183
+ * auto margins) so its rounded-bottom clip aligns with the drawer.
184
+ */
185
+ detachedWrapperStyle?: React.CSSProperties;
186
+ /**
187
+ * Caps the visible sheet height (in px) so full and 'auto' snap points
188
+ * respect a user-specified ceiling.
189
+ */
190
+ maxContentHeight?: number;
191
+ /**
192
+ * Resolved height (in px) for the 'peek' snap point — the caller measures
193
+ * its header/footer and passes the combined height.
194
+ */
195
+ peekHeight?: number;
196
+ /**
197
+ * When `false` the first snap on mount is applied without a transition so
198
+ * the sheet appears at its target detent instantly. Defaults to `true`.
199
+ */
200
+ initialAnimated?: boolean;
201
+ /**
202
+ * Fires when the auto-size wrapper's measured content height changes.
203
+ * Useful for callers that want to size the surrounding card to fit
204
+ * content (e.g. iPad-style form sheets).
205
+ */
206
+ onContentHeightChange?: (height: number) => void;
207
+ /**
208
+ * Overrides the measured content height used to resolve the 'auto' snap
209
+ * point. Used when the caller bounds the content to the visible sheet
210
+ * (e.g. a plugged scrollable) so the auto-size wrapper can't be measured
211
+ * from natural flow.
212
+ */
213
+ contentHeight?: number;
214
+ } & (WithFadeFromProps | WithoutFadeFromProps);
215
+
216
+ export function Root({
217
+ open: openProp,
218
+ onOpenChange,
219
+ children,
220
+ onDrag: onDragProp,
221
+ onRelease: onReleaseProp,
222
+ snapPoints,
223
+ shouldScaleBackground = false,
224
+ setBackgroundColorOnScale = true,
225
+ closeThreshold = CLOSE_THRESHOLD,
226
+ scrollLockTimeout = SCROLL_LOCK_TIMEOUT,
227
+ dismissible = true,
228
+ onDismissAttempt,
229
+ draggable = true,
230
+ handleOnly = false,
231
+ fadeFromIndex = snapPoints && snapPoints.length - 1,
232
+ activeSnapPoint: activeSnapPointProp,
233
+ setActiveSnapPoint: setActiveSnapPointProp,
234
+ fixed,
235
+ modal = true,
236
+ onClose,
237
+ nested,
238
+ noBodyStyles = false,
239
+ direction = 'bottom',
240
+ defaultOpen = false,
241
+ disablePreventScroll = true,
242
+ snapToSequentialPoint = false,
243
+ preventScrollRestoration = false,
244
+ repositionInputs = true,
245
+ onAnimationEnd,
246
+ onPositionChange,
247
+ container,
248
+ autoFocus = false,
249
+ detached = false,
250
+ detachedOffset = 0,
251
+ detachedRadius = 0,
252
+ detachedWrapperStyle,
253
+ maxContentHeight,
254
+ peekHeight,
255
+ initialAnimated = true,
256
+ onContentHeightChange,
257
+ contentHeight: contentHeightProp,
258
+ }: DialogProps) {
259
+ const [isOpen = false, setIsOpen] = useControllableState({
260
+ defaultProp: defaultOpen,
261
+ prop: openProp,
262
+ onChange: (o: boolean) => {
263
+ onOpenChange?.(o);
264
+
265
+ if (!o && !nested) {
266
+ restorePositionSetting();
267
+ }
268
+
269
+ setTimeout(() => {
270
+ onAnimationEnd?.(o);
271
+ }, TRANSITIONS.DURATION * 1000);
272
+ },
273
+ });
274
+ const [hasBeenOpened, setHasBeenOpened] = React.useState<boolean>(false);
275
+ const [isDragging, setIsDragging] = React.useState<boolean>(false);
276
+ const [justReleased, setJustReleased] = React.useState<boolean>(false);
277
+ const overlayRef = React.useRef<HTMLDivElement>(null);
278
+ const openTime = React.useRef<Date | null>(null);
279
+ const dragStartTime = React.useRef<Date | null>(null);
280
+ const dragEndTime = React.useRef<Date | null>(null);
281
+ const lastTimeDragPrevented = React.useRef<Date | null>(null);
282
+ const isAllowedToDrag = React.useRef<boolean>(false);
283
+ const nestedOpenChangeTimer = React.useRef<NodeJS.Timeout | null>(null);
284
+ const pointerStart = React.useRef(0);
285
+ const dismissAttempted = React.useRef(false);
286
+ const keyboardIsOpen = React.useRef(false);
287
+ const shouldAnimate = React.useRef(!defaultOpen);
288
+ const previousDiffFromInitial = React.useRef(0);
289
+ const drawerRef = React.useRef<HTMLDivElement>(null);
290
+ const drawerHeightRef = React.useRef(drawerRef.current?.getBoundingClientRect().height || 0);
291
+ const drawerWidthRef = React.useRef(drawerRef.current?.getBoundingClientRect().width || 0);
292
+ const initialDrawerHeight = React.useRef(0);
293
+ const [measuredContentHeight, setContentHeight] = React.useState(0);
294
+ const contentHeight = contentHeightProp ?? measuredContentHeight;
295
+
296
+ const onContentHeightChangeRef = React.useRef(onContentHeightChange);
297
+ React.useEffect(() => {
298
+ onContentHeightChangeRef.current = onContentHeightChange;
299
+ });
300
+ React.useEffect(() => {
301
+ onContentHeightChangeRef.current?.(measuredContentHeight);
302
+ }, [measuredContentHeight]);
303
+
304
+ const onSnapPointChange = React.useCallback((activeSnapPointIndex: number) => {
305
+ // Change openTime ref when we reach the last snap point to prevent dragging for 500ms incase it's scrollable.
306
+ if (snapPoints && activeSnapPointIndex === snapPointsOffset.length - 1)
307
+ openTime.current = new Date();
308
+ }, []);
309
+
310
+ const {
311
+ activeSnapPoint,
312
+ activeSnapPointIndex,
313
+ setActiveSnapPoint,
314
+ onRelease: onReleaseSnapPoints,
315
+ snapPointsOffset,
316
+ onDrag: onDragSnapPoints,
317
+ shouldFade,
318
+ getPercentageDragged: getSnapPointsPercentageDragged,
319
+ } = useSnapPoints({
320
+ snapPoints,
321
+ activeSnapPointProp,
322
+ setActiveSnapPointProp,
323
+ drawerRef,
324
+ fadeFromIndex,
325
+ overlayRef,
326
+ onSnapPointChange,
327
+ direction,
328
+ container,
329
+ snapToSequentialPoint,
330
+ isOpen,
331
+ contentHeight,
332
+ detachedOffset: detached ? detachedOffset : 0,
333
+ maxContentHeight,
334
+ peekHeight,
335
+ initialAnimated,
336
+ });
337
+
338
+ usePreventScroll({
339
+ isDisabled:
340
+ !isOpen ||
341
+ isDragging ||
342
+ !modal ||
343
+ justReleased ||
344
+ !hasBeenOpened ||
345
+ !repositionInputs ||
346
+ !disablePreventScroll,
347
+ });
348
+
349
+ const { restorePositionSetting } = usePositionFixed({
350
+ isOpen,
351
+ modal,
352
+ nested: nested ?? false,
353
+ hasBeenOpened,
354
+ preventScrollRestoration,
355
+ noBodyStyles,
356
+ });
357
+
358
+ // While the sheet itself is being dragged, scrollables in the touched chain
359
+ // are frozen (overflow: hidden). Touch browsers latch the scroll gesture at
360
+ // touchstart and ignore preventDefault once scrolling has started, so making
361
+ // the scroller non-scrollable mid-gesture is the only reliable way to keep
362
+ // the content from panning along with the sheet.
363
+ const frozenScrollablesRef = React.useRef<
364
+ { element: HTMLElement; overflowX: string; overflowY: string }[] | null
365
+ >(null);
366
+
367
+ function freezeScrollables(target: EventTarget) {
368
+ if (frozenScrollablesRef.current) return;
369
+ const frozen: { element: HTMLElement; overflowX: string; overflowY: string }[] = [];
370
+ let element = target instanceof HTMLElement ? target : null;
371
+ while (element && element !== drawerRef.current) {
372
+ if (isScrollable(element)) {
373
+ frozen.push({
374
+ element,
375
+ overflowX: element.style.overflowX,
376
+ overflowY: element.style.overflowY,
377
+ });
378
+ element.style.overflowX = 'hidden';
379
+ element.style.overflowY = 'hidden';
380
+ }
381
+ element = element.parentElement;
382
+ }
383
+ frozenScrollablesRef.current = frozen;
384
+ }
385
+
386
+ function unfreezeScrollables() {
387
+ const frozen = frozenScrollablesRef.current;
388
+ if (!frozen) return;
389
+ frozenScrollablesRef.current = null;
390
+ for (const { element, overflowX, overflowY } of frozen) {
391
+ element.style.overflowX = overflowX;
392
+ element.style.overflowY = overflowY;
393
+ }
394
+ }
395
+
396
+ function getScale() {
397
+ return (window.innerWidth - WINDOW_TOP_OFFSET) / window.innerWidth;
398
+ }
399
+
400
+ function onPress(event: React.PointerEvent<HTMLDivElement>) {
401
+ if (!draggable) return;
402
+ if (!dismissible && !snapPoints) return;
403
+ if (drawerRef.current && !drawerRef.current.contains(event.target as Node)) return;
404
+
405
+ drawerHeightRef.current = drawerRef.current?.getBoundingClientRect().height || 0;
406
+ drawerWidthRef.current = drawerRef.current?.getBoundingClientRect().width || 0;
407
+ setIsDragging(true);
408
+ dragStartTime.current = new Date();
409
+
410
+ // iOS doesn't trigger mouseUp after scrolling so we need to listen to touched in order to disallow dragging
411
+ if (isIOS()) {
412
+ window.addEventListener('touchend', () => (isAllowedToDrag.current = false), { once: true });
413
+ }
414
+ // Ensure we maintain correct pointer capture even when going outside of the drawer
415
+ (event.target as HTMLElement).setPointerCapture(event.pointerId);
416
+
417
+ pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
418
+ }
419
+
420
+ function shouldDrag(el: EventTarget, isDraggingInDirection: boolean) {
421
+ let element = el as HTMLElement;
422
+ const highlightedText = window.getSelection()?.toString();
423
+ const swipeAmount = drawerRef.current ? getTranslate(drawerRef.current, direction) : null;
424
+ const date = new Date();
425
+
426
+ // Fixes https://github.com/emilkowalski/vaul/issues/483
427
+ if (element.tagName === 'SELECT') {
428
+ return false;
429
+ }
430
+
431
+ if (element.hasAttribute('data-vaul-no-drag') || element.closest('[data-vaul-no-drag]')) {
432
+ return false;
433
+ }
434
+
435
+ if (direction === 'right' || direction === 'left') {
436
+ return true;
437
+ }
438
+
439
+ // Allow scrolling when animating
440
+ if (openTime.current && date.getTime() - openTime.current.getTime() < 500) {
441
+ return false;
442
+ }
443
+
444
+ if (swipeAmount !== null) {
445
+ // Translated past the drag threshold → keep dragging. Below the last
446
+ // snap point the threshold is 0, so the sheet always wins over content
447
+ // scrolling. At the last snap point the threshold is its resting
448
+ // translate — which can still be > 0 when the max detent < 1 — so an
449
+ // at-rest sheet falls through to the scroll checks and content
450
+ // scrolling wins, matching native. Mid-drag/mid-animation the sheet is
451
+ // displaced past rest and still wins.
452
+ const atLastSnapPoint = snapPoints && activeSnapPointIndex === snapPoints.length - 1;
453
+ const restOffset = atLastSnapPoint ? (snapPointsOffset?.[activeSnapPointIndex!] ?? 0) : 0;
454
+ if (direction === 'bottom' ? swipeAmount > restOffset + 1 : swipeAmount < restOffset - 1) {
455
+ return true;
456
+ }
457
+ }
458
+
459
+ // Don't drag if there's highlighted text
460
+ if (highlightedText && highlightedText.length > 0) {
461
+ return false;
462
+ }
463
+
464
+ // Disallow dragging if drawer was scrolled within `scrollLockTimeout`.
465
+ // Don't re-arm the timestamp here — a prevented drag attempt is not a
466
+ // scroll, and re-arming would keep the lock alive for as long as the
467
+ // finger moves, deadening the whole gesture instead of just the first
468
+ // `scrollLockTimeout` ms after the last real scroll.
469
+ if (
470
+ lastTimeDragPrevented.current &&
471
+ date.getTime() - lastTimeDragPrevented.current.getTime() < scrollLockTimeout &&
472
+ swipeAmount === 0
473
+ ) {
474
+ return false;
475
+ }
476
+
477
+ if (isDraggingInDirection) {
478
+ lastTimeDragPrevented.current = date;
479
+
480
+ // We are dragging down so we should allow scrolling
481
+ return false;
482
+ }
483
+
484
+ // Keep climbing up the DOM tree as long as there's a parent
485
+ while (element) {
486
+ // Check if the element is scrollable
487
+ if (element.scrollHeight > element.clientHeight) {
488
+ // `> 0`, not `!== 0`: Safari reports a negative scrollTop during the
489
+ // rubber-band bounce at the top — that's "at the top" for drag
490
+ // purposes, and treating it as scrolled would arm the scroll lock and
491
+ // delay the sheet drag until the bounce fully settles.
492
+ if (element.scrollTop > 0) {
493
+ lastTimeDragPrevented.current = new Date();
494
+
495
+ // The element is scrollable and not scrolled to the top, so don't drag
496
+ return false;
497
+ }
498
+
499
+ if (element.getAttribute('role') === 'dialog') {
500
+ return true;
501
+ }
502
+ }
503
+
504
+ // Move up to the parent element
505
+ element = element.parentNode as HTMLElement;
506
+ }
507
+
508
+ // No scrollable parents not scrolled to the top found, so drag
509
+ return true;
510
+ }
511
+
512
+ function drag(event: DragEvent, pointerEvent?: React.PointerEvent<HTMLDivElement>) {
513
+ if (!drawerRef.current) {
514
+ return;
515
+ }
516
+
517
+ // We need to know how much of the drawer has been dragged in percentages so that we can transform background accordingly
518
+ if (isDragging) {
519
+ const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
520
+ const draggedDistance =
521
+ (pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX)) *
522
+ directionMultiplier;
523
+ const isDraggingInDirection = draggedDistance > 0;
524
+
525
+ // Pre condition for disallowing dragging in the close direction.
526
+ const noCloseSnapPointsPreCondition = snapPoints && !dismissible && !isDraggingInDirection;
527
+
528
+ // Disallow dragging down to close when first snap point is the active one and dismissible prop is set to false.
529
+ if (noCloseSnapPointsPreCondition && activeSnapPointIndex === 0) {
530
+ // The drawer stays put. Remember a deliberate pull that would otherwise
531
+ // have dragged it (content at its top) so release() can report it.
532
+ if (
533
+ -draggedDistance > DISMISS_ATTEMPT_THRESHOLD &&
534
+ (isAllowedToDrag.current || shouldDrag(event.target, isDraggingInDirection))
535
+ ) {
536
+ dismissAttempted.current = true;
537
+ }
538
+ return;
539
+ }
540
+
541
+ // We need to capture last time when drag with scroll was triggered and have a timeout between
542
+ const absDraggedDistance = Math.abs(draggedDistance);
543
+ const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
544
+ const drawerDimension =
545
+ direction === 'bottom' || direction === 'top'
546
+ ? drawerHeightRef.current
547
+ : drawerWidthRef.current;
548
+
549
+ // Calculate the percentage dragged, where 1 is the closed position
550
+ let percentageDragged = absDraggedDistance / drawerDimension;
551
+ const snapPointPercentageDragged = getSnapPointsPercentageDragged(
552
+ absDraggedDistance,
553
+ isDraggingInDirection
554
+ );
555
+
556
+ if (snapPointPercentageDragged !== null) {
557
+ percentageDragged = snapPointPercentageDragged;
558
+ }
559
+
560
+ // Disallow close dragging beyond the smallest snap point.
561
+ if (noCloseSnapPointsPreCondition && percentageDragged >= 1) {
562
+ return;
563
+ }
564
+
565
+ if (!isAllowedToDrag.current) {
566
+ if (!shouldDrag(event.target, isDraggingInDirection)) return;
567
+ drawerRef.current.classList.add(DRAG_CLASS);
568
+ // If shouldDrag gave true once after pressing down on the drawer, we set isAllowedToDrag to true and it will remain true until we let go, there's no reason to disable dragging mid way, ever, and that's the solution to it
569
+ isAllowedToDrag.current = true;
570
+ // Touch pans latched onto a scroller would keep scrolling the content
571
+ // along with the sheet drag — freeze them for the drag's duration.
572
+ if (event.pointerType !== 'mouse') freezeScrollables(event.target);
573
+ // Drag can engage mid-gesture (content scrolled back to its top under
574
+ // the same finger, or the scroll lock expiring). Re-anchor and start
575
+ // moving on the next tick so the sheet tracks the finger from here
576
+ // instead of jumping by the distance the gesture already consumed.
577
+ pointerStart.current = isVertical(direction) ? event.pageY : event.pageX;
578
+ return;
579
+ }
580
+ set(drawerRef.current, {
581
+ transition: 'none',
582
+ });
583
+
584
+ set(overlayRef.current, {
585
+ transition: 'none',
586
+ });
587
+
588
+ if (pointerEvent) onDragProp?.(pointerEvent, percentageDragged);
589
+
590
+ if (snapPoints) {
591
+ onDragSnapPoints({ draggedDistance });
592
+ }
593
+
594
+ // Run this only if snapPoints are not defined or if we are at the last snap point (highest one)
595
+ if (isDraggingInDirection && !snapPoints) {
596
+ const dampenedDraggedDistance = dampenValue(draggedDistance);
597
+
598
+ const translateValue = Math.min(dampenedDraggedDistance * -1, 0) * directionMultiplier;
599
+ set(drawerRef.current, {
600
+ transform: isVertical(direction)
601
+ ? `translate3d(0, ${translateValue}px, 0)`
602
+ : `translate3d(${translateValue}px, 0, 0)`,
603
+ });
604
+ return;
605
+ }
606
+
607
+ const opacityValue = 1 - percentageDragged;
608
+
609
+ if (shouldFade || (fadeFromIndex && activeSnapPointIndex === fadeFromIndex - 1)) {
610
+ set(
611
+ overlayRef.current,
612
+ {
613
+ opacity: `${opacityValue}`,
614
+ transition: 'none',
615
+ },
616
+ true
617
+ );
618
+ }
619
+
620
+ if (wrapper && overlayRef.current && shouldScaleBackground) {
621
+ // Calculate percentageDragged as a fraction (0 to 1)
622
+ const scaleValue = Math.min(getScale() + percentageDragged * (1 - getScale()), 1);
623
+ const borderRadiusValue = 8 - percentageDragged * 8;
624
+
625
+ const translateValue = Math.max(0, 14 - percentageDragged * 14);
626
+
627
+ set(
628
+ wrapper,
629
+ {
630
+ borderRadius: `${borderRadiusValue}px`,
631
+ transform: isVertical(direction)
632
+ ? `scale(${scaleValue}) translate3d(0, ${translateValue}px, 0)`
633
+ : `scale(${scaleValue}) translate3d(${translateValue}px, 0, 0)`,
634
+ transition: 'none',
635
+ },
636
+ true
637
+ );
638
+ }
639
+
640
+ if (!snapPoints) {
641
+ const translateValue = absDraggedDistance * directionMultiplier;
642
+
643
+ set(drawerRef.current, {
644
+ transform: isVertical(direction)
645
+ ? `translate3d(0, ${translateValue}px, 0)`
646
+ : `translate3d(${translateValue}px, 0, 0)`,
647
+ });
648
+ }
649
+ }
650
+ }
651
+
652
+ React.useEffect(() => {
653
+ window.requestAnimationFrame(() => {
654
+ shouldAnimate.current = true;
655
+ });
656
+ }, []);
657
+
658
+ // Reset to the first snap point on every open so a prior session's active snap
659
+ // (kept in state by `useControllableState` after dismissal) doesn't leak into
660
+ // the next present. Skipped when `activeSnapPoint` is controlled externally.
661
+ React.useEffect(() => {
662
+ if (isOpen && snapPoints && activeSnapPointProp === undefined) {
663
+ setActiveSnapPoint(snapPoints[0]);
664
+ }
665
+ }, [isOpen]);
666
+
667
+ const onPositionChangeRef = React.useRef(onPositionChange);
668
+ React.useEffect(() => {
669
+ onPositionChangeRef.current = onPositionChange;
670
+ });
671
+
672
+ React.useEffect(() => {
673
+ function onVisualViewportChange() {
674
+ if (!drawerRef.current || !repositionInputs) return;
675
+
676
+ const focusedElement = document.activeElement as HTMLElement;
677
+ if (isInput(focusedElement) || keyboardIsOpen.current) {
678
+ const visualViewportHeight = window.visualViewport?.height || 0;
679
+ const totalHeight = window.innerHeight;
680
+ // This is the height of the keyboard
681
+ let diffFromInitial = totalHeight - visualViewportHeight;
682
+ const drawerHeight = drawerRef.current.getBoundingClientRect().height || 0;
683
+ // Adjust drawer height only if it's tall enough
684
+ const isTallEnough = drawerHeight > totalHeight * 0.8;
685
+
686
+ if (!initialDrawerHeight.current) {
687
+ initialDrawerHeight.current = drawerHeight;
688
+ }
689
+ const offsetFromTop = drawerRef.current.getBoundingClientRect().top;
690
+
691
+ // visualViewport height may change due to somq e subtle changes to the keyboard. Checking if the height changed by 60 or more will make sure that they keyboard really changed its open state.
692
+ if (Math.abs(previousDiffFromInitial.current - diffFromInitial) > 60) {
693
+ keyboardIsOpen.current = !keyboardIsOpen.current;
694
+ }
695
+
696
+ if (snapPoints && snapPoints.length > 0 && snapPointsOffset && activeSnapPointIndex) {
697
+ const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0;
698
+ diffFromInitial += activeSnapPointHeight;
699
+ }
700
+ previousDiffFromInitial.current = diffFromInitial;
701
+ // We don't have to change the height if the input is in view, when we are here we are in the opened keyboard state so we can correctly check if the input is in view
702
+ if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
703
+ const height = drawerRef.current.getBoundingClientRect().height;
704
+ let newDrawerHeight = height;
705
+
706
+ if (height > visualViewportHeight) {
707
+ newDrawerHeight =
708
+ visualViewportHeight - (isTallEnough ? offsetFromTop : WINDOW_TOP_OFFSET);
709
+ }
710
+ // When fixed, don't move the drawer upwards if there's space, but rather only change it's height so it's fully scrollable when the keyboard is open
711
+ if (fixed) {
712
+ drawerRef.current.style.height = `${height - Math.max(diffFromInitial, 0)}px`;
713
+ } else {
714
+ drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
715
+ }
716
+ } else if (!isMobileFirefox()) {
717
+ drawerRef.current.style.height = `${initialDrawerHeight.current}px`;
718
+ }
719
+
720
+ if (snapPoints && snapPoints.length > 0 && !keyboardIsOpen.current) {
721
+ drawerRef.current.style.bottom = '0px';
722
+ } else {
723
+ // Negative bottom value would never make sense
724
+ drawerRef.current.style.bottom = `${Math.max(diffFromInitial, 0)}px`;
725
+ }
726
+ }
727
+ }
728
+
729
+ window.visualViewport?.addEventListener('resize', onVisualViewportChange);
730
+ return () => window.visualViewport?.removeEventListener('resize', onVisualViewportChange);
731
+ }, [activeSnapPointIndex, snapPoints, snapPointsOffset]);
732
+
733
+ function closeDrawer(fromWithin?: boolean) {
734
+ cancelDrag();
735
+ onClose?.();
736
+
737
+ if (!fromWithin) {
738
+ setIsOpen(false);
739
+ }
740
+ }
741
+
742
+ function resetDrawer() {
743
+ if (!drawerRef.current) return;
744
+ const wrapper = document.querySelector('[data-vaul-drawer-wrapper]');
745
+ const currentSwipeAmount = getTranslate(drawerRef.current, direction);
746
+
747
+ set(drawerRef.current, {
748
+ transform: 'translate3d(0, 0, 0)',
749
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
750
+ });
751
+
752
+ set(overlayRef.current, {
753
+ transition: `opacity ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
754
+ opacity: '1',
755
+ });
756
+
757
+ // Don't reset background if swiped upwards
758
+ if (shouldScaleBackground && currentSwipeAmount && currentSwipeAmount > 0 && isOpen) {
759
+ set(
760
+ wrapper,
761
+ {
762
+ borderRadius: `${BORDER_RADIUS}px`,
763
+ overflow: 'hidden',
764
+ ...(isVertical(direction)
765
+ ? {
766
+ transform: `scale(${getScale()}) translate3d(0, calc(env(safe-area-inset-top) + 14px), 0)`,
767
+ transformOrigin: 'top',
768
+ }
769
+ : {
770
+ transform: `scale(${getScale()}) translate3d(calc(env(safe-area-inset-top) + 14px), 0, 0)`,
771
+ transformOrigin: 'left',
772
+ }),
773
+ transitionProperty: 'transform, border-radius',
774
+ transitionDuration: `${TRANSITIONS.DURATION}s`,
775
+ transitionTimingFunction: `cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
776
+ },
777
+ true
778
+ );
779
+ }
780
+ }
781
+
782
+ function cancelDrag() {
783
+ if (!isDragging || !drawerRef.current) return;
784
+
785
+ drawerRef.current.classList.remove(DRAG_CLASS);
786
+ isAllowedToDrag.current = false;
787
+ unfreezeScrollables();
788
+ setIsDragging(false);
789
+ dragEndTime.current = new Date();
790
+ }
791
+
792
+ function release(event: DragEvent | null, pointerEvent?: React.PointerEvent<HTMLDivElement>) {
793
+ if (!isDragging || !drawerRef.current) return;
794
+
795
+ drawerRef.current.classList.remove(DRAG_CLASS);
796
+ isAllowedToDrag.current = false;
797
+ unfreezeScrollables();
798
+ setIsDragging(false);
799
+ dragEndTime.current = new Date();
800
+
801
+ if (dismissAttempted.current) {
802
+ dismissAttempted.current = false;
803
+ if (isOpen) onDismissAttempt?.();
804
+ }
805
+
806
+ // The press that started this "drag" may have dismissed the drawer (e.g. a
807
+ // back button acting on pointerdown). Snapping now would overwrite the
808
+ // dismiss transform that closeDrawer just wrote and freeze the sheet in
809
+ // place until it unmounts.
810
+ if (!isOpen) {
811
+ if (pointerEvent) onReleaseProp?.(pointerEvent, false);
812
+ return;
813
+ }
814
+
815
+ const swipeAmount = getTranslate(drawerRef.current, direction);
816
+
817
+ // Drag overshoot below the lowest snap point translates the detached
818
+ // wrapper while the drawer stays pinned (see onDrag in use-snap-points) —
819
+ // common when a clamped 'auto' puts the lowest snap point at 0. Count the
820
+ // wrapper's translation as movement, or the release is swallowed and the
821
+ // sheet is left stuck at the overshoot position.
822
+ const wrapper = drawerRef.current.closest<HTMLElement>('[data-vaul-detached-wrapper]');
823
+ const wrapperSwipeAmount = wrapper ? getTranslate(wrapper, direction) : null;
824
+ const hasMoved = Boolean(swipeAmount) || Boolean(wrapperSwipeAmount);
825
+
826
+ if (!event || !shouldDrag(event.target, false) || !hasMoved || Number.isNaN(swipeAmount))
827
+ return;
828
+
829
+ if (dragStartTime.current === null) return;
830
+
831
+ const timeTaken = dragEndTime.current.getTime() - dragStartTime.current.getTime();
832
+ const distMoved = pointerStart.current - (isVertical(direction) ? event.pageY : event.pageX);
833
+ const velocity = Math.abs(distMoved) / timeTaken;
834
+
835
+ if (velocity > 0.05) {
836
+ // `justReleased` is needed to prevent the drawer from focusing on an input when the drag ends, as it's not the intent most of the time.
837
+ setJustReleased(true);
838
+
839
+ setTimeout(() => {
840
+ setJustReleased(false);
841
+ }, 200);
842
+ }
843
+
844
+ if (snapPoints) {
845
+ const directionMultiplier = direction === 'bottom' || direction === 'right' ? 1 : -1;
846
+ onReleaseSnapPoints({
847
+ draggedDistance: distMoved * directionMultiplier,
848
+ closeDrawer,
849
+ velocity,
850
+ dismissible,
851
+ });
852
+ if (pointerEvent) onReleaseProp?.(pointerEvent, true);
853
+ return;
854
+ }
855
+
856
+ // Moved upwards, don't do anything
857
+ if (direction === 'bottom' || direction === 'right' ? distMoved > 0 : distMoved < 0) {
858
+ resetDrawer();
859
+ if (pointerEvent) onReleaseProp?.(pointerEvent, true);
860
+ return;
861
+ }
862
+
863
+ if (velocity > VELOCITY_THRESHOLD) {
864
+ closeDrawer();
865
+ if (pointerEvent) onReleaseProp?.(pointerEvent, false);
866
+ return;
867
+ }
868
+
869
+ const visibleDrawerHeight = Math.min(
870
+ drawerRef.current.getBoundingClientRect().height ?? 0,
871
+ window.innerHeight
872
+ );
873
+ const visibleDrawerWidth = Math.min(
874
+ drawerRef.current.getBoundingClientRect().width ?? 0,
875
+ window.innerWidth
876
+ );
877
+
878
+ const isHorizontalSwipe = direction === 'left' || direction === 'right';
879
+ if (
880
+ Math.abs(swipeAmount ?? 0) >=
881
+ (isHorizontalSwipe ? visibleDrawerWidth : visibleDrawerHeight) * closeThreshold
882
+ ) {
883
+ closeDrawer();
884
+ if (pointerEvent) onReleaseProp?.(pointerEvent, false);
885
+ return;
886
+ }
887
+
888
+ if (pointerEvent) onReleaseProp?.(pointerEvent, true);
889
+ resetDrawer();
890
+ }
891
+
892
+ React.useEffect(() => {
893
+ // Trigger enter animation without using CSS animation
894
+ if (isOpen) {
895
+ set(document.documentElement, {
896
+ scrollBehavior: 'auto',
897
+ });
898
+
899
+ openTime.current = new Date();
900
+ }
901
+
902
+ return () => {
903
+ reset(document.documentElement, 'scrollBehavior');
904
+ };
905
+ }, [isOpen]);
906
+
907
+ function onNestedOpenChange(o: boolean) {
908
+ const scale = o ? (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth : 1;
909
+
910
+ const initialTranslate = o ? -NESTED_DISPLACEMENT : 0;
911
+
912
+ if (nestedOpenChangeTimer.current) {
913
+ window.clearTimeout(nestedOpenChangeTimer.current);
914
+ }
915
+
916
+ set(drawerRef.current, {
917
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
918
+ transform: isVertical(direction)
919
+ ? `scale(${scale}) translate3d(0, ${initialTranslate}px, 0)`
920
+ : `scale(${scale}) translate3d(${initialTranslate}px, 0, 0)`,
921
+ });
922
+
923
+ if (!o && drawerRef.current) {
924
+ nestedOpenChangeTimer.current = setTimeout(() => {
925
+ const translateValue = getTranslate(drawerRef.current as HTMLElement, direction);
926
+ set(drawerRef.current, {
927
+ transition: 'none',
928
+ transform: isVertical(direction)
929
+ ? `translate3d(0, ${translateValue}px, 0)`
930
+ : `translate3d(${translateValue}px, 0, 0)`,
931
+ });
932
+ }, 500);
933
+ }
934
+ }
935
+
936
+ function onNestedDrag(_event: React.PointerEvent<HTMLDivElement>, percentageDragged: number) {
937
+ if (percentageDragged < 0) return;
938
+
939
+ const initialScale = (window.innerWidth - NESTED_DISPLACEMENT) / window.innerWidth;
940
+ const newScale = initialScale + percentageDragged * (1 - initialScale);
941
+ const newTranslate = -NESTED_DISPLACEMENT + percentageDragged * NESTED_DISPLACEMENT;
942
+
943
+ set(drawerRef.current, {
944
+ transform: isVertical(direction)
945
+ ? `scale(${newScale}) translate3d(0, ${newTranslate}px, 0)`
946
+ : `scale(${newScale}) translate3d(${newTranslate}px, 0, 0)`,
947
+ transition: 'none',
948
+ });
949
+ }
950
+
951
+ function onNestedRelease(_event: React.PointerEvent<HTMLDivElement>, o: boolean) {
952
+ const dim = isVertical(direction) ? window.innerHeight : window.innerWidth;
953
+ const scale = o ? (dim - NESTED_DISPLACEMENT) / dim : 1;
954
+ const translate = o ? -NESTED_DISPLACEMENT : 0;
955
+
956
+ if (o) {
957
+ set(drawerRef.current, {
958
+ transition: `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`,
959
+ transform: isVertical(direction)
960
+ ? `scale(${scale}) translate3d(0, ${translate}px, 0)`
961
+ : `scale(${scale}) translate3d(${translate}px, 0, 0)`,
962
+ });
963
+ }
964
+ }
965
+
966
+ return (
967
+ <DialogPrimitive.Root
968
+ defaultOpen={defaultOpen}
969
+ onOpenChange={(open) => {
970
+ if (!dismissible && !open) return;
971
+ if (open) {
972
+ setHasBeenOpened(true);
973
+ } else {
974
+ closeDrawer(true);
975
+ }
976
+
977
+ setIsOpen(open);
978
+ }}
979
+ open={isOpen}
980
+ // Always non-modal at the Radix level — Radix's modal mode locks
981
+ // body.style.pointerEvents and its restore on layer unmount is buggy
982
+ // (see git history). Vaul's own overlay handles click-trapping; the
983
+ // overlay sets pointer-events: none below fadeFromIndex so clicks
984
+ // pass through to the page (matches native dimmedDetentIndex). Vaul's
985
+ // own `modal` prop still drives overlay rendering and click-outside
986
+ // behavior below.
987
+ modal={false}
988
+ >
989
+ <DrawerContext.Provider
990
+ value={{
991
+ activeSnapPoint,
992
+ snapPoints,
993
+ setActiveSnapPoint,
994
+ drawerRef,
995
+ overlayRef,
996
+ onOpenChange,
997
+ onPress,
998
+ onRelease: (event) => release(event, event ?? undefined),
999
+ onDrag: (event) => drag(event, event),
1000
+ onTouchDrag: (event, pointerEvent) => drag(event, pointerEvent),
1001
+ onTouchRelease: (event, pointerEvent) => release(event, pointerEvent),
1002
+ dismissible,
1003
+ onDismissAttempt,
1004
+ shouldAnimate,
1005
+ handleOnly,
1006
+ isOpen,
1007
+ isDragging,
1008
+ shouldFade,
1009
+ closeDrawer,
1010
+ onNestedDrag,
1011
+ onNestedOpenChange,
1012
+ onNestedRelease,
1013
+ keyboardIsOpen,
1014
+ modal,
1015
+ snapPointsOffset,
1016
+ activeSnapPointIndex,
1017
+ fadeFromIndex,
1018
+ direction,
1019
+ shouldScaleBackground,
1020
+ setBackgroundColorOnScale,
1021
+ noBodyStyles,
1022
+ container,
1023
+ autoFocus,
1024
+ onPositionChangeRef,
1025
+ setContentHeight,
1026
+ detached,
1027
+ detachedOffset: detached ? detachedOffset : 0,
1028
+ detachedRadius: detached ? detachedRadius : 0,
1029
+ detachedWrapperStyle,
1030
+ }}
1031
+ >
1032
+ {children}
1033
+ </DrawerContext.Provider>
1034
+ </DialogPrimitive.Root>
1035
+ );
1036
+ }
1037
+
1038
+ export const Overlay = React.forwardRef<
1039
+ HTMLDivElement,
1040
+ React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
1041
+ >(({ style, ...rest }, ref) => {
1042
+ const {
1043
+ overlayRef,
1044
+ snapPoints,
1045
+ onRelease,
1046
+ shouldFade,
1047
+ isOpen,
1048
+ modal,
1049
+ shouldAnimate,
1050
+ activeSnapPointIndex,
1051
+ fadeFromIndex,
1052
+ } = useDrawerContext();
1053
+ const composedRef = useComposedRefs(ref, overlayRef);
1054
+ const hasSnapPoints = snapPoints && snapPoints.length > 0;
1055
+ const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
1056
+ const onMouseUp = React.useCallback(
1057
+ (event: React.PointerEvent<HTMLDivElement>) => onRelease(event),
1058
+ [onRelease]
1059
+ );
1060
+
1061
+ React.useEffect(() => {
1062
+ if (hasSnapPoints) {
1063
+ window.requestAnimationFrame(() => {
1064
+ setDelayedSnapPoints(true);
1065
+ });
1066
+ }
1067
+ }, []);
1068
+
1069
+ // Overlay is the component that is locking scroll, removing it will unlock the scroll without having to dig into Radix's Dialog library
1070
+ if (!modal) {
1071
+ return null;
1072
+ }
1073
+
1074
+ // When the active snap is below fadeFromIndex, the overlay is fully
1075
+ // transparent (CSS opacity: 0). Radix's DialogOverlayImpl sets inline
1076
+ // `pointer-events: auto` which would still catch clicks on the invisible
1077
+ // layer — drive this via React so it stays correct even before
1078
+ // `snapToPoint` runs (Presence defers the drawer/overlay mount, leaving
1079
+ // refs null on the first open effect pass).
1080
+ const isBelowFade =
1081
+ hasSnapPoints &&
1082
+ fadeFromIndex !== undefined &&
1083
+ typeof activeSnapPointIndex === 'number' &&
1084
+ activeSnapPointIndex < fadeFromIndex;
1085
+
1086
+ // Render our own overlay element instead of `DialogPrimitive.Overlay`:
1087
+ // Radix's Overlay returns null when the Dialog is non-modal, and we run
1088
+ // Radix as `modal={false}` to keep it from locking body pointer-events.
1089
+ // `Presence` keeps the node mounted through the closing animation
1090
+ // (`fadeOut`) before unmounting.
1091
+ return (
1092
+ <Presence present={isOpen}>
1093
+ <div
1094
+ onMouseUp={onMouseUp}
1095
+ ref={composedRef}
1096
+ data-state={isOpen ? 'open' : 'closed'}
1097
+ data-vaul-overlay=""
1098
+ data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
1099
+ data-vaul-delayed-snap-points={delayedSnapPoints ? 'true' : 'false'}
1100
+ data-vaul-snap-points-overlay={isOpen && shouldFade ? 'true' : 'false'}
1101
+ data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
1102
+ {...rest}
1103
+ style={overlayStyle(style, !!isBelowFade)}
1104
+ />
1105
+ </Presence>
1106
+ );
1107
+ });
1108
+
1109
+ Overlay.displayName = 'Drawer.Overlay';
1110
+
1111
+ const overlayStyle = (
1112
+ style: React.CSSProperties | undefined,
1113
+ isBelowFade: boolean
1114
+ ): React.CSSProperties => ({
1115
+ ...style,
1116
+ pointerEvents: isBelowFade ? 'none' : 'auto',
1117
+ });
1118
+
1119
+ export type ContentProps = React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content> & {
1120
+ /**
1121
+ * Extra nodes rendered inside the detached clip wrapper as siblings of the
1122
+ * drawer. Use for floating elements (e.g. a footer) that should slide with
1123
+ * the wrapper on dismiss and drag-overshoot instead of staying pinned.
1124
+ */
1125
+ detachedSiblings?: React.ReactNode;
1126
+ };
1127
+
1128
+ export const Content = React.forwardRef<HTMLDivElement, ContentProps>(
1129
+ (
1130
+ {
1131
+ onPointerDownOutside,
1132
+ onEscapeKeyDown,
1133
+ style,
1134
+ onOpenAutoFocus,
1135
+ children,
1136
+ detachedSiblings,
1137
+ ...rest
1138
+ },
1139
+ ref
1140
+ ) => {
1141
+ const {
1142
+ drawerRef,
1143
+ onPress,
1144
+ onRelease,
1145
+ onDrag,
1146
+ onTouchDrag,
1147
+ onTouchRelease,
1148
+ keyboardIsOpen,
1149
+ snapPointsOffset,
1150
+ activeSnapPointIndex,
1151
+ fadeFromIndex,
1152
+ modal,
1153
+ isOpen,
1154
+ isDragging,
1155
+ direction,
1156
+ snapPoints,
1157
+ setActiveSnapPoint,
1158
+ dismissible,
1159
+ onDismissAttempt,
1160
+ container,
1161
+ handleOnly,
1162
+ shouldAnimate,
1163
+ autoFocus,
1164
+ onPositionChangeRef,
1165
+ setContentHeight,
1166
+ detached,
1167
+ detachedOffset,
1168
+ detachedRadius,
1169
+ detachedWrapperStyle: detachedWrapperStyleProp,
1170
+ } = useDrawerContext();
1171
+ // Always measure the inner wrapper's natural height. The drawer itself may
1172
+ // be styled to a fixed viewport height, so we measure an inner wrapper
1173
+ // instead. Ref callback starts the observer as soon as the node mounts
1174
+ // (Radix Presence defers the portal mount past useEffect). The measurement
1175
+ // drives the 'auto' snap point and is also exposed via
1176
+ // `onContentHeightChange` for callers that size the surrounding card.
1177
+ const autoRoRef = React.useRef<ResizeObserver | null>(null);
1178
+ const setAutoSizeNode = React.useCallback(
1179
+ (node: HTMLDivElement | null) => {
1180
+ autoRoRef.current?.disconnect();
1181
+ if (!node) {
1182
+ autoRoRef.current = null;
1183
+ return;
1184
+ }
1185
+ const measure = () => setContentHeight(node.offsetHeight);
1186
+ measure();
1187
+ const ro = new ResizeObserver(measure);
1188
+ ro.observe(node);
1189
+ autoRoRef.current = ro;
1190
+ },
1191
+ [setContentHeight]
1192
+ );
1193
+
1194
+ const isBelowFade =
1195
+ snapPoints !== undefined &&
1196
+ snapPoints !== null &&
1197
+ fadeFromIndex !== undefined &&
1198
+ typeof activeSnapPointIndex === 'number' &&
1199
+ activeSnapPointIndex < fadeFromIndex;
1200
+ // Needed to use transition instead of animations
1201
+ const [delayedSnapPoints, setDelayedSnapPoints] = React.useState(false);
1202
+ const composedRef = useComposedRefs(ref, drawerRef);
1203
+ const pointerStartRef = React.useRef<{ x: number; y: number } | null>(null);
1204
+ const lastKnownPointerEventRef = React.useRef<React.PointerEvent<HTMLDivElement> | null>(null);
1205
+ const continuingCanceledTouchRef = React.useRef(false);
1206
+ const wasBeyondThePointRef = React.useRef(false);
1207
+ const hasSnapPoints = snapPoints && snapPoints.length > 0;
1208
+ useScaleBackground();
1209
+
1210
+ const isDeltaInDirection = (
1211
+ delta: { x: number; y: number },
1212
+ dir: DrawerDirection,
1213
+ threshold = 0
1214
+ ) => {
1215
+ if (wasBeyondThePointRef.current) return true;
1216
+
1217
+ const deltaY = Math.abs(delta.y);
1218
+ const deltaX = Math.abs(delta.x);
1219
+ const isDeltaX = deltaX > deltaY;
1220
+ const dFactor = ['bottom', 'right'].includes(dir) ? 1 : -1;
1221
+
1222
+ if (dir === 'left' || dir === 'right') {
1223
+ const isReverseDirection = delta.x * dFactor < 0;
1224
+ if (!isReverseDirection && deltaX >= 0 && deltaX <= threshold) {
1225
+ return isDeltaX;
1226
+ }
1227
+ } else {
1228
+ const isReverseDirection = delta.y * dFactor < 0;
1229
+ if (!isReverseDirection && deltaY >= 0 && deltaY <= threshold) {
1230
+ return !isDeltaX;
1231
+ }
1232
+ }
1233
+
1234
+ wasBeyondThePointRef.current = true;
1235
+ return true;
1236
+ };
1237
+
1238
+ React.useEffect(() => {
1239
+ if (hasSnapPoints) {
1240
+ window.requestAnimationFrame(() => {
1241
+ setDelayedSnapPoints(true);
1242
+ });
1243
+ }
1244
+ }, []);
1245
+
1246
+ // Event-driven position tracking. We only tick RAF while the drawer is
1247
+ // actually moving (drag / CSS transition / CSS animation). When it's idle at
1248
+ // a snap, no frames run at all.
1249
+ const positionTrackingRef = React.useRef<{
1250
+ rafId: number | null;
1251
+ movingCount: number;
1252
+ lastPosition: number;
1253
+ start: () => void;
1254
+ stop: () => void;
1255
+ } | null>(null);
1256
+
1257
+ React.useEffect(() => {
1258
+ const drawer = drawerRef.current;
1259
+ if (!drawer) return;
1260
+
1261
+ const state = {
1262
+ rafId: null as number | null,
1263
+ movingCount: 0,
1264
+ lastPosition: Number.NaN,
1265
+ start: () => {},
1266
+ stop: () => {},
1267
+ };
1268
+
1269
+ const emit = () => {
1270
+ const cb = onPositionChangeRef.current;
1271
+ if (!cb) return;
1272
+ const position = drawer.getBoundingClientRect().top;
1273
+ if (position !== state.lastPosition) {
1274
+ state.lastPosition = position;
1275
+ cb(position);
1276
+ }
1277
+ };
1278
+
1279
+ const tick = () => {
1280
+ emit();
1281
+ state.rafId = state.movingCount > 0 ? window.requestAnimationFrame(tick) : null;
1282
+ };
1283
+
1284
+ state.start = () => {
1285
+ state.movingCount += 1;
1286
+ if (state.rafId === null) {
1287
+ state.rafId = window.requestAnimationFrame(tick);
1288
+ }
1289
+ };
1290
+
1291
+ state.stop = () => {
1292
+ state.movingCount = Math.max(0, state.movingCount - 1);
1293
+ // RAF loop will exit on its next tick; emit the settled position now.
1294
+ if (state.movingCount === 0) emit();
1295
+ };
1296
+
1297
+ const wrapper = drawer.closest<HTMLElement>('[data-vaul-detached-wrapper]');
1298
+
1299
+ // Listen on the wrapper too: drag-overshoot snap-back animates the wrapper
1300
+ // alone when the drawer's target is unchanged (e.g. snapping back to the
1301
+ // same detent). Without this, position goes stale mid-animation because
1302
+ // the drawer's `transitionrun` never fires.
1303
+ const onTransitionRun = (e: TransitionEvent) => {
1304
+ if ((e.target === drawer || e.target === wrapper) && e.propertyName === 'transform')
1305
+ state.start();
1306
+ };
1307
+ const onTransitionDone = (e: TransitionEvent) => {
1308
+ if ((e.target === drawer || e.target === wrapper) && e.propertyName === 'transform')
1309
+ state.stop();
1310
+ };
1311
+ const onAnimationStart = (e: AnimationEvent) => {
1312
+ if (e.target === drawer || e.target === wrapper) state.start();
1313
+ };
1314
+ const onAnimationDone = (e: AnimationEvent) => {
1315
+ if (e.target === drawer || e.target === wrapper) state.stop();
1316
+ };
1317
+
1318
+ drawer.addEventListener('transitionrun', onTransitionRun);
1319
+ drawer.addEventListener('transitionend', onTransitionDone);
1320
+ drawer.addEventListener('transitioncancel', onTransitionDone);
1321
+ drawer.addEventListener('animationstart', onAnimationStart);
1322
+ drawer.addEventListener('animationend', onAnimationDone);
1323
+ drawer.addEventListener('animationcancel', onAnimationDone);
1324
+ wrapper?.addEventListener('transitionrun', onTransitionRun);
1325
+ wrapper?.addEventListener('transitionend', onTransitionDone);
1326
+ wrapper?.addEventListener('transitioncancel', onTransitionDone);
1327
+
1328
+ positionTrackingRef.current = state;
1329
+ emit();
1330
+
1331
+ return () => {
1332
+ drawer.removeEventListener('transitionrun', onTransitionRun);
1333
+ drawer.removeEventListener('transitionend', onTransitionDone);
1334
+ drawer.removeEventListener('transitioncancel', onTransitionDone);
1335
+ drawer.removeEventListener('animationstart', onAnimationStart);
1336
+ drawer.removeEventListener('animationend', onAnimationDone);
1337
+ drawer.removeEventListener('animationcancel', onAnimationDone);
1338
+ wrapper?.removeEventListener('transitionrun', onTransitionRun);
1339
+ wrapper?.removeEventListener('transitionend', onTransitionDone);
1340
+ wrapper?.removeEventListener('transitioncancel', onTransitionDone);
1341
+ if (state.rafId !== null) window.cancelAnimationFrame(state.rafId);
1342
+ positionTrackingRef.current = null;
1343
+ };
1344
+ }, []);
1345
+
1346
+ React.useEffect(() => {
1347
+ const state = positionTrackingRef.current;
1348
+ if (!state) return;
1349
+ if (isDragging) state.start();
1350
+ else state.stop();
1351
+ }, [isDragging]);
1352
+
1353
+ function handleOnPointerUp(event: React.PointerEvent<HTMLDivElement> | null) {
1354
+ pointerStartRef.current = null;
1355
+ wasBeyondThePointRef.current = false;
1356
+ onRelease(event);
1357
+ }
1358
+
1359
+ // The drawer always sits inside a fixed clip wrapper. `contain: paint`
1360
+ // establishes the wrapper as the containing block so the drawer's own
1361
+ // `position: fixed` is constrained here, and `overflow: hidden` keeps any
1362
+ // overshoot out of the viewport. When `detached` the wrapper also floats
1363
+ // with a bottom gap and rounded bottom corners; otherwise it sits flush.
1364
+ // Transform/transition are managed imperatively (via drag overshoot and the
1365
+ // dismiss effect) so React doesn't skip DOM writes for values it thinks it
1366
+ // already owns.
1367
+ const wrapperStyle = React.useMemo<React.CSSProperties>(
1368
+ () => ({
1369
+ position: 'fixed',
1370
+ top: 0,
1371
+ left: 0,
1372
+ right: 0,
1373
+ bottom: detached ? detachedOffset : 0,
1374
+ overflow: 'hidden',
1375
+ contain: 'paint',
1376
+ pointerEvents: 'none',
1377
+ borderBottomLeftRadius: detached ? detachedRadius : 0,
1378
+ borderBottomRightRadius: detached ? detachedRadius : 0,
1379
+ ...detachedWrapperStyleProp,
1380
+ }),
1381
+ [detached, detachedOffset, detachedRadius, detachedWrapperStyleProp]
1382
+ );
1383
+
1384
+ // Translate the wrapper off-screen on dismiss so the whole card slides out
1385
+ // as one. The reset-then-target pattern on open forces the browser to
1386
+ // record a starting value so the transition actually animates.
1387
+ // Start at `false` so a mount with `isOpen=true` (autopresent via
1388
+ // `initialDetentIndex`) is detected as a false→true transition and runs
1389
+ // the slide-up animation instead of snapping straight to rest.
1390
+ const wasOpenRef = React.useRef(false);
1391
+ React.useEffect(() => {
1392
+ if (!drawerRef.current) {
1393
+ wasOpenRef.current = isOpen;
1394
+ return;
1395
+ }
1396
+ const wrapper = drawerRef.current.closest<HTMLElement>('[data-vaul-detached-wrapper]');
1397
+ if (wrapper) {
1398
+ const transition = `transform ${TRANSITIONS.DURATION}s cubic-bezier(${TRANSITIONS.EASE.join(',')})`;
1399
+ const viewportH = typeof window !== 'undefined' ? window.innerHeight : 0;
1400
+ if (!isOpen && wasOpenRef.current) {
1401
+ wrapper.style.transition = transition;
1402
+ wrapper.style.transform = `translate3d(0, ${viewportH}px, 0)`;
1403
+ } else if (isOpen && !wasOpenRef.current) {
1404
+ wrapper.style.transition = 'none';
1405
+ wrapper.style.transform = `translate3d(0, ${viewportH}px, 0)`;
1406
+ // eslint-disable-next-line no-void
1407
+ void wrapper.offsetHeight;
1408
+ wrapper.style.transition = transition;
1409
+ wrapper.style.transform = 'translate3d(0, 0, 0)';
1410
+ }
1411
+ }
1412
+ wasOpenRef.current = isOpen;
1413
+ }, [isOpen, detached, drawerRef]);
1414
+
1415
+ const contentNode = (
1416
+ <DialogPrimitive.Content
1417
+ data-vaul-drawer-direction={direction}
1418
+ data-vaul-drawer=""
1419
+ data-vaul-detached={detached ? 'true' : 'false'}
1420
+ data-vaul-delayed-snap-points={delayedSnapPoints ? 'true' : 'false'}
1421
+ data-vaul-snap-points={isOpen && hasSnapPoints ? 'true' : 'false'}
1422
+ data-vaul-custom-container={container ? 'true' : 'false'}
1423
+ data-vaul-animate={shouldAnimate?.current ? 'true' : 'false'}
1424
+ {...rest}
1425
+ ref={composedRef}
1426
+ style={
1427
+ snapPointsOffset && snapPointsOffset.length > 0
1428
+ ? ({
1429
+ '--snap-point-height': `${snapPointsOffset[activeSnapPointIndex ?? 0]!}px`,
1430
+ ...style,
1431
+ 'pointerEvents': 'auto',
1432
+ } as React.CSSProperties)
1433
+ : ({ ...style, pointerEvents: 'auto' } as React.CSSProperties)
1434
+ }
1435
+ onPointerDown={(event) => {
1436
+ if (handleOnly) return;
1437
+ rest.onPointerDown?.(event);
1438
+ continuingCanceledTouchRef.current = false;
1439
+ pointerStartRef.current = { x: event.pageX, y: event.pageY };
1440
+ onPress(event);
1441
+ }}
1442
+ onOpenAutoFocus={(e) => {
1443
+ onOpenAutoFocus?.(e);
1444
+
1445
+ if (!autoFocus) {
1446
+ e.preventDefault();
1447
+ }
1448
+ }}
1449
+ onPointerDownOutside={(e) => {
1450
+ onPointerDownOutside?.(e);
1451
+
1452
+ if (!modal || e.defaultPrevented || isBelowFade) {
1453
+ e.preventDefault();
1454
+ return;
1455
+ }
1456
+
1457
+ // Non-dismissible + dimmed: collapse to the highest non-dimmed
1458
+ // snap point instead of silently swallowing the dismiss attempt.
1459
+ // Mirrors the native Android dim-tap behavior.
1460
+ if (
1461
+ !dismissible &&
1462
+ hasSnapPoints &&
1463
+ fadeFromIndex !== undefined &&
1464
+ fadeFromIndex > 0 &&
1465
+ typeof activeSnapPointIndex === 'number' &&
1466
+ activeSnapPointIndex >= fadeFromIndex
1467
+ ) {
1468
+ e.preventDefault();
1469
+ setActiveSnapPoint(snapPoints![fadeFromIndex - 1]!);
1470
+ return;
1471
+ }
1472
+
1473
+ if (keyboardIsOpen.current) {
1474
+ keyboardIsOpen.current = false;
1475
+ }
1476
+ }}
1477
+ onEscapeKeyDown={(e) => {
1478
+ onEscapeKeyDown?.(e);
1479
+ if (e.defaultPrevented) return;
1480
+
1481
+ // Non-dismissible: collapse to the first snap point (if above it)
1482
+ // instead of silently swallowing the dismiss attempt. Mirrors the
1483
+ // native Android back-press behavior.
1484
+ if (!dismissible) {
1485
+ e.preventDefault();
1486
+ if (
1487
+ hasSnapPoints &&
1488
+ typeof activeSnapPointIndex === 'number' &&
1489
+ activeSnapPointIndex > 0
1490
+ ) {
1491
+ setActiveSnapPoint(snapPoints![0]!);
1492
+ } else {
1493
+ onDismissAttempt?.();
1494
+ }
1495
+ }
1496
+ }}
1497
+ onFocusOutside={(e) => {
1498
+ // Never auto-dismiss the sheet on focus shifts. Without Radix's
1499
+ // FocusScope trap (we run modal={false}), this fires whenever
1500
+ // focus naturally leaves the drawer — e.g. React Navigation's
1501
+ // web driver hides the previous screen via display:none, which
1502
+ // moves focus off the trigger and dismisses the sheet.
1503
+ e.preventDefault();
1504
+ }}
1505
+ onPointerMove={(event) => {
1506
+ lastKnownPointerEventRef.current = event;
1507
+ if (handleOnly) return;
1508
+ rest.onPointerMove?.(event);
1509
+ if (!pointerStartRef.current) return;
1510
+ const yPosition = event.pageY - pointerStartRef.current.y;
1511
+ const xPosition = event.pageX - pointerStartRef.current.x;
1512
+
1513
+ const swipeStartThreshold = event.pointerType === 'touch' ? 10 : 2;
1514
+ const delta = { x: xPosition, y: yPosition };
1515
+
1516
+ const isAllowedToSwipe = isDeltaInDirection(delta, direction, swipeStartThreshold);
1517
+ if (isAllowedToSwipe) onDrag(event);
1518
+ else if (
1519
+ Math.abs(xPosition) > swipeStartThreshold ||
1520
+ Math.abs(yPosition) > swipeStartThreshold
1521
+ ) {
1522
+ pointerStartRef.current = null;
1523
+ }
1524
+ }}
1525
+ onPointerUp={(event) => {
1526
+ rest.onPointerUp?.(event);
1527
+ continuingCanceledTouchRef.current = false;
1528
+ pointerStartRef.current = null;
1529
+ wasBeyondThePointRef.current = false;
1530
+ onRelease(event);
1531
+ }}
1532
+ onPointerCancel={(event) => {
1533
+ rest.onPointerCancel?.(event);
1534
+ if (
1535
+ event.pointerType === 'touch' &&
1536
+ snapPoints &&
1537
+ activeSnapPointIndex === snapPoints.length - 1
1538
+ ) {
1539
+ continuingCanceledTouchRef.current = true;
1540
+ return;
1541
+ }
1542
+ handleOnPointerUp(null);
1543
+ }}
1544
+ onPointerOut={(event) => {
1545
+ rest.onPointerOut?.(event);
1546
+ if (continuingCanceledTouchRef.current) return;
1547
+ handleOnPointerUp(lastKnownPointerEventRef.current);
1548
+ }}
1549
+ onTouchMove={(event) => {
1550
+ rest.onTouchMove?.(event);
1551
+ if (!continuingCanceledTouchRef.current) return;
1552
+ const touch = event.touches[0];
1553
+ if (!touch) return;
1554
+ onTouchDrag(
1555
+ {
1556
+ target: event.target,
1557
+ pageX: touch.pageX,
1558
+ pageY: touch.pageY,
1559
+ pointerType: 'touch',
1560
+ },
1561
+ lastKnownPointerEventRef.current ?? undefined
1562
+ );
1563
+ }}
1564
+ onTouchEnd={(event) => {
1565
+ rest.onTouchEnd?.(event);
1566
+ if (!continuingCanceledTouchRef.current) return;
1567
+ continuingCanceledTouchRef.current = false;
1568
+ pointerStartRef.current = null;
1569
+ wasBeyondThePointRef.current = false;
1570
+ const touch = event.changedTouches[0];
1571
+ onTouchRelease(
1572
+ touch
1573
+ ? {
1574
+ target: event.target,
1575
+ pageX: touch.pageX,
1576
+ pageY: touch.pageY,
1577
+ pointerType: 'touch',
1578
+ }
1579
+ : null,
1580
+ lastKnownPointerEventRef.current ?? undefined
1581
+ );
1582
+ }}
1583
+ onTouchCancel={(event) => {
1584
+ rest.onTouchCancel?.(event);
1585
+ if (!continuingCanceledTouchRef.current) return;
1586
+ continuingCanceledTouchRef.current = false;
1587
+ handleOnPointerUp(null);
1588
+ }}
1589
+ onContextMenu={(event) => {
1590
+ rest.onContextMenu?.(event);
1591
+ if (lastKnownPointerEventRef.current) {
1592
+ handleOnPointerUp(lastKnownPointerEventRef.current);
1593
+ }
1594
+ }}
1595
+ >
1596
+ <div ref={setAutoSizeNode} data-vaul-auto-size-wrapper="" style={autoSizeWrapperStyle}>
1597
+ {children}
1598
+ </div>
1599
+ </DialogPrimitive.Content>
1600
+ );
1601
+
1602
+ return (
1603
+ <div data-vaul-detached-wrapper="" style={wrapperStyle}>
1604
+ {contentNode}
1605
+ {detachedSiblings}
1606
+ </div>
1607
+ );
1608
+ }
1609
+ );
1610
+
1611
+ Content.displayName = 'Drawer.Content';
1612
+
1613
+ // `flow-root` establishes a new block formatting context so the first child's
1614
+ // margin-top (e.g. a grabber) stays inside the wrapper instead of collapsing
1615
+ // out — otherwise `offsetHeight` under-reports and the drawer positions the
1616
+ // wrapper below where the content actually ends.
1617
+ const autoSizeWrapperStyle: React.CSSProperties = { display: 'flow-root' };
1618
+
1619
+ export type HandleProps = React.ComponentPropsWithoutRef<'div'> & {
1620
+ preventCycle?: boolean;
1621
+ };
1622
+
1623
+ const LONG_HANDLE_PRESS_TIMEOUT = 250;
1624
+ const DOUBLE_TAP_TIMEOUT = 120;
1625
+
1626
+ export const Handle = React.forwardRef<HTMLDivElement, HandleProps>(
1627
+ ({ preventCycle = false, children, ...rest }, ref) => {
1628
+ const {
1629
+ closeDrawer,
1630
+ isDragging,
1631
+ snapPoints,
1632
+ activeSnapPoint,
1633
+ setActiveSnapPoint,
1634
+ dismissible,
1635
+ onDismissAttempt,
1636
+ handleOnly,
1637
+ isOpen,
1638
+ onPress,
1639
+ onDrag,
1640
+ } = useDrawerContext();
1641
+
1642
+ const closeTimeoutIdRef = React.useRef<number | null>(null);
1643
+ const shouldCancelInteractionRef = React.useRef(false);
1644
+
1645
+ function handleStartCycle() {
1646
+ // Stop if this is the second click of a double click
1647
+ if (shouldCancelInteractionRef.current) {
1648
+ handleCancelInteraction();
1649
+ return;
1650
+ }
1651
+ window.setTimeout(() => {
1652
+ handleCycleSnapPoints();
1653
+ }, DOUBLE_TAP_TIMEOUT);
1654
+ }
1655
+
1656
+ function handleCycleSnapPoints() {
1657
+ // Prevent accidental taps while resizing drawer
1658
+ if (isDragging || preventCycle || shouldCancelInteractionRef.current) {
1659
+ handleCancelInteraction();
1660
+ return;
1661
+ }
1662
+ // Make sure to clear the timeout id if the user releases the handle before the cancel timeout
1663
+ handleCancelInteraction();
1664
+
1665
+ if (!snapPoints || snapPoints.length === 0) {
1666
+ if (!dismissible) {
1667
+ closeDrawer();
1668
+ }
1669
+ return;
1670
+ }
1671
+
1672
+ const isLastSnapPoint = activeSnapPoint === snapPoints[snapPoints.length - 1];
1673
+
1674
+ if (isLastSnapPoint && dismissible) {
1675
+ closeDrawer();
1676
+ return;
1677
+ }
1678
+
1679
+ // Mirrors native: a single-detent sheet's handle tap is a dismiss.
1680
+ if (isLastSnapPoint && snapPoints.length === 1) {
1681
+ onDismissAttempt?.();
1682
+ return;
1683
+ }
1684
+
1685
+ const currentSnapIndex = snapPoints.findIndex((point) => point === activeSnapPoint);
1686
+ if (currentSnapIndex === -1) return; // activeSnapPoint not found in snapPoints
1687
+ const nextSnapPoint = snapPoints[currentSnapIndex + 1];
1688
+ if (nextSnapPoint === undefined) return;
1689
+ setActiveSnapPoint(nextSnapPoint);
1690
+ }
1691
+
1692
+ function handleStartInteraction() {
1693
+ closeTimeoutIdRef.current = window.setTimeout(() => {
1694
+ // Cancel click interaction on a long press
1695
+ shouldCancelInteractionRef.current = true;
1696
+ }, LONG_HANDLE_PRESS_TIMEOUT);
1697
+ }
1698
+
1699
+ function handleCancelInteraction() {
1700
+ if (closeTimeoutIdRef.current) {
1701
+ window.clearTimeout(closeTimeoutIdRef.current);
1702
+ }
1703
+ shouldCancelInteractionRef.current = false;
1704
+ }
1705
+
1706
+ return (
1707
+ <div
1708
+ onClick={handleStartCycle}
1709
+ onPointerCancel={handleCancelInteraction}
1710
+ onPointerDown={(e) => {
1711
+ if (handleOnly) onPress(e);
1712
+ handleStartInteraction();
1713
+ }}
1714
+ onPointerMove={(e) => {
1715
+ if (handleOnly) onDrag(e);
1716
+ }}
1717
+ // onPointerUp is already handled by the content component
1718
+ ref={ref}
1719
+ data-vaul-drawer-visible={isOpen ? 'true' : 'false'}
1720
+ data-vaul-handle=""
1721
+ aria-hidden="true"
1722
+ {...rest}
1723
+ >
1724
+ {/* Expand handle's hit area beyond what's visible to ensure a 44x44 tap target for touch devices */}
1725
+ <span data-vaul-handle-hitarea="" aria-hidden="true">
1726
+ {children}
1727
+ </span>
1728
+ </div>
1729
+ );
1730
+ }
1731
+ );
1732
+
1733
+ Handle.displayName = 'Drawer.Handle';
1734
+
1735
+ export function NestedRoot({ onDrag, onOpenChange, open: nestedIsOpen, ...rest }: DialogProps) {
1736
+ const { onNestedDrag, onNestedOpenChange, onNestedRelease } = useDrawerContext();
1737
+
1738
+ if (!onNestedDrag) {
1739
+ throw new Error('Drawer.NestedRoot must be placed in another drawer');
1740
+ }
1741
+
1742
+ return (
1743
+ <Root
1744
+ nested
1745
+ open={nestedIsOpen}
1746
+ onClose={() => {
1747
+ onNestedOpenChange(false);
1748
+ }}
1749
+ onDrag={(e, p) => {
1750
+ onNestedDrag(e, p);
1751
+ onDrag?.(e, p);
1752
+ }}
1753
+ onOpenChange={(o) => {
1754
+ if (o) {
1755
+ onNestedOpenChange(o);
1756
+ }
1757
+ onOpenChange?.(o);
1758
+ }}
1759
+ onRelease={onNestedRelease}
1760
+ {...rest}
1761
+ />
1762
+ );
1763
+ }
1764
+
1765
+ type PortalProps = React.ComponentPropsWithoutRef<typeof DialogPrimitive.Portal>;
1766
+
1767
+ export function Portal(props: PortalProps) {
1768
+ const context = useDrawerContext();
1769
+ const { container = context.container, ...portalProps } = props;
1770
+
1771
+ return <DialogPrimitive.Portal container={container} {...portalProps} />;
1772
+ }
1773
+
1774
+ export const Drawer = {
1775
+ Root,
1776
+ NestedRoot,
1777
+ Content,
1778
+ Overlay,
1779
+ Trigger: DialogPrimitive.Trigger,
1780
+ Portal,
1781
+ Handle,
1782
+ Close: DialogPrimitive.Close,
1783
+ Title: DialogPrimitive.Title,
1784
+ Description: DialogPrimitive.Description,
1785
+ };