@tamagui/v2-toast 2.0.0-1769464493958

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 (307) hide show
  1. package/dist/cjs/Toast.cjs +170 -0
  2. package/dist/cjs/Toast.js +119 -0
  3. package/dist/cjs/Toast.js.map +6 -0
  4. package/dist/cjs/Toast.native.js +174 -0
  5. package/dist/cjs/Toast.native.js.map +1 -0
  6. package/dist/cjs/ToastAnnounce.cjs +97 -0
  7. package/dist/cjs/ToastAnnounce.js +72 -0
  8. package/dist/cjs/ToastAnnounce.js.map +6 -0
  9. package/dist/cjs/ToastAnnounce.native.js +105 -0
  10. package/dist/cjs/ToastAnnounce.native.js.map +1 -0
  11. package/dist/cjs/ToastImperative.cjs +100 -0
  12. package/dist/cjs/ToastImperative.js +71 -0
  13. package/dist/cjs/ToastImperative.js.map +6 -0
  14. package/dist/cjs/ToastImperative.native.js +122 -0
  15. package/dist/cjs/ToastImperative.native.js.map +1 -0
  16. package/dist/cjs/ToastImpl.cjs +292 -0
  17. package/dist/cjs/ToastImpl.js +227 -0
  18. package/dist/cjs/ToastImpl.js.map +6 -0
  19. package/dist/cjs/ToastImpl.native.js +327 -0
  20. package/dist/cjs/ToastImpl.native.js.map +1 -0
  21. package/dist/cjs/ToastItem.cjs +466 -0
  22. package/dist/cjs/ToastItem.js +356 -0
  23. package/dist/cjs/ToastItem.js.map +6 -0
  24. package/dist/cjs/ToastItem.native.js +547 -0
  25. package/dist/cjs/ToastItem.native.js.map +1 -0
  26. package/dist/cjs/ToastPortal.cjs +44 -0
  27. package/dist/cjs/ToastPortal.js +26 -0
  28. package/dist/cjs/ToastPortal.js.map +6 -0
  29. package/dist/cjs/ToastPortal.native.js +47 -0
  30. package/dist/cjs/ToastPortal.native.js.map +1 -0
  31. package/dist/cjs/ToastProvider.cjs +146 -0
  32. package/dist/cjs/ToastProvider.js +105 -0
  33. package/dist/cjs/ToastProvider.js.map +6 -0
  34. package/dist/cjs/ToastProvider.native.js +159 -0
  35. package/dist/cjs/ToastProvider.native.js.map +1 -0
  36. package/dist/cjs/ToastState.cjs +248 -0
  37. package/dist/cjs/ToastState.js +160 -0
  38. package/dist/cjs/ToastState.js.map +6 -0
  39. package/dist/cjs/ToastState.native.js +257 -0
  40. package/dist/cjs/ToastState.native.js.map +1 -0
  41. package/dist/cjs/ToastViewport.cjs +278 -0
  42. package/dist/cjs/ToastViewport.js +263 -0
  43. package/dist/cjs/ToastViewport.js.map +6 -0
  44. package/dist/cjs/ToastViewport.native.js +316 -0
  45. package/dist/cjs/ToastViewport.native.js.map +1 -0
  46. package/dist/cjs/Toaster.cjs +219 -0
  47. package/dist/cjs/Toaster.js +177 -0
  48. package/dist/cjs/Toaster.js.map +6 -0
  49. package/dist/cjs/Toaster.native.js +279 -0
  50. package/dist/cjs/Toaster.native.js.map +1 -0
  51. package/dist/cjs/constants.cjs +28 -0
  52. package/dist/cjs/constants.js +22 -0
  53. package/dist/cjs/constants.js.map +6 -0
  54. package/dist/cjs/constants.native.js +31 -0
  55. package/dist/cjs/constants.native.js.map +1 -0
  56. package/dist/cjs/createNativeToast.cjs +51 -0
  57. package/dist/cjs/createNativeToast.js +44 -0
  58. package/dist/cjs/createNativeToast.js.map +6 -0
  59. package/dist/cjs/createNativeToast.native.js +47 -0
  60. package/dist/cjs/createNativeToast.native.js.map +1 -0
  61. package/dist/cjs/index.cjs +28 -0
  62. package/dist/cjs/index.js +22 -0
  63. package/dist/cjs/index.js.map +6 -0
  64. package/dist/cjs/index.native.js +31 -0
  65. package/dist/cjs/index.native.js.map +1 -0
  66. package/dist/cjs/types.cjs +16 -0
  67. package/dist/cjs/types.js +14 -0
  68. package/dist/cjs/types.js.map +6 -0
  69. package/dist/cjs/types.native.js +19 -0
  70. package/dist/cjs/types.native.js.map +1 -0
  71. package/dist/cjs/useDragGesture.cjs +129 -0
  72. package/dist/cjs/useDragGesture.js +100 -0
  73. package/dist/cjs/useDragGesture.js.map +6 -0
  74. package/dist/cjs/useDragGesture.native.js +146 -0
  75. package/dist/cjs/useDragGesture.native.js.map +1 -0
  76. package/dist/esm/Toast.js +107 -0
  77. package/dist/esm/Toast.js.map +6 -0
  78. package/dist/esm/Toast.mjs +131 -0
  79. package/dist/esm/Toast.mjs.map +1 -0
  80. package/dist/esm/Toast.native.js +132 -0
  81. package/dist/esm/Toast.native.js.map +1 -0
  82. package/dist/esm/ToastAnnounce.js +55 -0
  83. package/dist/esm/ToastAnnounce.js.map +6 -0
  84. package/dist/esm/ToastAnnounce.mjs +62 -0
  85. package/dist/esm/ToastAnnounce.mjs.map +1 -0
  86. package/dist/esm/ToastAnnounce.native.js +67 -0
  87. package/dist/esm/ToastAnnounce.native.js.map +1 -0
  88. package/dist/esm/ToastImperative.js +50 -0
  89. package/dist/esm/ToastImperative.js.map +6 -0
  90. package/dist/esm/ToastImperative.mjs +63 -0
  91. package/dist/esm/ToastImperative.mjs.map +1 -0
  92. package/dist/esm/ToastImperative.native.js +82 -0
  93. package/dist/esm/ToastImperative.native.js.map +1 -0
  94. package/dist/esm/ToastImpl.js +225 -0
  95. package/dist/esm/ToastImpl.js.map +6 -0
  96. package/dist/esm/ToastImpl.mjs +256 -0
  97. package/dist/esm/ToastImpl.mjs.map +1 -0
  98. package/dist/esm/ToastImpl.native.js +288 -0
  99. package/dist/esm/ToastImpl.native.js.map +1 -0
  100. package/dist/esm/ToastItem.js +339 -0
  101. package/dist/esm/ToastItem.js.map +6 -0
  102. package/dist/esm/ToastItem.mjs +432 -0
  103. package/dist/esm/ToastItem.mjs.map +1 -0
  104. package/dist/esm/ToastItem.native.js +510 -0
  105. package/dist/esm/ToastItem.native.js.map +1 -0
  106. package/dist/esm/ToastPortal.js +13 -0
  107. package/dist/esm/ToastPortal.js.map +6 -0
  108. package/dist/esm/ToastPortal.mjs +21 -0
  109. package/dist/esm/ToastPortal.mjs.map +1 -0
  110. package/dist/esm/ToastPortal.native.js +21 -0
  111. package/dist/esm/ToastPortal.native.js.map +1 -0
  112. package/dist/esm/ToastProvider.js +87 -0
  113. package/dist/esm/ToastProvider.js.map +6 -0
  114. package/dist/esm/ToastProvider.mjs +108 -0
  115. package/dist/esm/ToastProvider.mjs.map +1 -0
  116. package/dist/esm/ToastProvider.native.js +118 -0
  117. package/dist/esm/ToastProvider.native.js.map +1 -0
  118. package/dist/esm/ToastState.js +144 -0
  119. package/dist/esm/ToastState.js.map +6 -0
  120. package/dist/esm/ToastState.mjs +224 -0
  121. package/dist/esm/ToastState.mjs.map +1 -0
  122. package/dist/esm/ToastState.native.js +230 -0
  123. package/dist/esm/ToastState.native.js.map +1 -0
  124. package/dist/esm/ToastViewport.js +250 -0
  125. package/dist/esm/ToastViewport.js.map +6 -0
  126. package/dist/esm/ToastViewport.mjs +241 -0
  127. package/dist/esm/ToastViewport.mjs.map +1 -0
  128. package/dist/esm/ToastViewport.native.js +276 -0
  129. package/dist/esm/ToastViewport.native.js.map +1 -0
  130. package/dist/esm/Toaster.js +160 -0
  131. package/dist/esm/Toaster.js.map +6 -0
  132. package/dist/esm/Toaster.mjs +185 -0
  133. package/dist/esm/Toaster.mjs.map +1 -0
  134. package/dist/esm/Toaster.native.js +242 -0
  135. package/dist/esm/Toaster.native.js.map +1 -0
  136. package/dist/esm/constants.js +6 -0
  137. package/dist/esm/constants.js.map +6 -0
  138. package/dist/esm/constants.mjs +4 -0
  139. package/dist/esm/constants.mjs.map +1 -0
  140. package/dist/esm/constants.native.js +4 -0
  141. package/dist/esm/constants.native.js.map +1 -0
  142. package/dist/esm/createNativeToast.js +28 -0
  143. package/dist/esm/createNativeToast.js.map +6 -0
  144. package/dist/esm/createNativeToast.mjs +27 -0
  145. package/dist/esm/createNativeToast.mjs.map +1 -0
  146. package/dist/esm/createNativeToast.native.js +20 -0
  147. package/dist/esm/createNativeToast.native.js.map +1 -0
  148. package/dist/esm/index.js +7 -0
  149. package/dist/esm/index.js.map +6 -0
  150. package/dist/esm/index.mjs +4 -0
  151. package/dist/esm/index.mjs.map +1 -0
  152. package/dist/esm/index.native.js +4 -0
  153. package/dist/esm/index.native.js.map +1 -0
  154. package/dist/esm/types.js +1 -0
  155. package/dist/esm/types.js.map +6 -0
  156. package/dist/esm/types.mjs +2 -0
  157. package/dist/esm/types.mjs.map +1 -0
  158. package/dist/esm/types.native.js +2 -0
  159. package/dist/esm/types.native.js.map +1 -0
  160. package/dist/esm/useDragGesture.js +76 -0
  161. package/dist/esm/useDragGesture.js.map +6 -0
  162. package/dist/esm/useDragGesture.mjs +95 -0
  163. package/dist/esm/useDragGesture.mjs.map +1 -0
  164. package/dist/esm/useDragGesture.native.js +109 -0
  165. package/dist/esm/useDragGesture.native.js.map +1 -0
  166. package/dist/jsx/Toast.js +107 -0
  167. package/dist/jsx/Toast.js.map +6 -0
  168. package/dist/jsx/Toast.mjs +131 -0
  169. package/dist/jsx/Toast.mjs.map +1 -0
  170. package/dist/jsx/Toast.native.js +174 -0
  171. package/dist/jsx/Toast.native.js.map +1 -0
  172. package/dist/jsx/ToastAnnounce.js +55 -0
  173. package/dist/jsx/ToastAnnounce.js.map +6 -0
  174. package/dist/jsx/ToastAnnounce.mjs +62 -0
  175. package/dist/jsx/ToastAnnounce.mjs.map +1 -0
  176. package/dist/jsx/ToastAnnounce.native.js +105 -0
  177. package/dist/jsx/ToastAnnounce.native.js.map +1 -0
  178. package/dist/jsx/ToastImperative.js +50 -0
  179. package/dist/jsx/ToastImperative.js.map +6 -0
  180. package/dist/jsx/ToastImperative.mjs +63 -0
  181. package/dist/jsx/ToastImperative.mjs.map +1 -0
  182. package/dist/jsx/ToastImperative.native.js +122 -0
  183. package/dist/jsx/ToastImperative.native.js.map +1 -0
  184. package/dist/jsx/ToastImpl.js +225 -0
  185. package/dist/jsx/ToastImpl.js.map +6 -0
  186. package/dist/jsx/ToastImpl.mjs +256 -0
  187. package/dist/jsx/ToastImpl.mjs.map +1 -0
  188. package/dist/jsx/ToastImpl.native.js +327 -0
  189. package/dist/jsx/ToastImpl.native.js.map +1 -0
  190. package/dist/jsx/ToastItem.js +339 -0
  191. package/dist/jsx/ToastItem.js.map +6 -0
  192. package/dist/jsx/ToastItem.mjs +432 -0
  193. package/dist/jsx/ToastItem.mjs.map +1 -0
  194. package/dist/jsx/ToastItem.native.js +547 -0
  195. package/dist/jsx/ToastItem.native.js.map +1 -0
  196. package/dist/jsx/ToastPortal.js +13 -0
  197. package/dist/jsx/ToastPortal.js.map +6 -0
  198. package/dist/jsx/ToastPortal.mjs +21 -0
  199. package/dist/jsx/ToastPortal.mjs.map +1 -0
  200. package/dist/jsx/ToastPortal.native.js +47 -0
  201. package/dist/jsx/ToastPortal.native.js.map +1 -0
  202. package/dist/jsx/ToastProvider.js +87 -0
  203. package/dist/jsx/ToastProvider.js.map +6 -0
  204. package/dist/jsx/ToastProvider.mjs +108 -0
  205. package/dist/jsx/ToastProvider.mjs.map +1 -0
  206. package/dist/jsx/ToastProvider.native.js +159 -0
  207. package/dist/jsx/ToastProvider.native.js.map +1 -0
  208. package/dist/jsx/ToastState.js +144 -0
  209. package/dist/jsx/ToastState.js.map +6 -0
  210. package/dist/jsx/ToastState.mjs +224 -0
  211. package/dist/jsx/ToastState.mjs.map +1 -0
  212. package/dist/jsx/ToastState.native.js +257 -0
  213. package/dist/jsx/ToastState.native.js.map +1 -0
  214. package/dist/jsx/ToastViewport.js +250 -0
  215. package/dist/jsx/ToastViewport.js.map +6 -0
  216. package/dist/jsx/ToastViewport.mjs +241 -0
  217. package/dist/jsx/ToastViewport.mjs.map +1 -0
  218. package/dist/jsx/ToastViewport.native.js +316 -0
  219. package/dist/jsx/ToastViewport.native.js.map +1 -0
  220. package/dist/jsx/Toaster.js +160 -0
  221. package/dist/jsx/Toaster.js.map +6 -0
  222. package/dist/jsx/Toaster.mjs +185 -0
  223. package/dist/jsx/Toaster.mjs.map +1 -0
  224. package/dist/jsx/Toaster.native.js +279 -0
  225. package/dist/jsx/Toaster.native.js.map +1 -0
  226. package/dist/jsx/constants.js +6 -0
  227. package/dist/jsx/constants.js.map +6 -0
  228. package/dist/jsx/constants.mjs +4 -0
  229. package/dist/jsx/constants.mjs.map +1 -0
  230. package/dist/jsx/constants.native.js +31 -0
  231. package/dist/jsx/constants.native.js.map +1 -0
  232. package/dist/jsx/createNativeToast.js +28 -0
  233. package/dist/jsx/createNativeToast.js.map +6 -0
  234. package/dist/jsx/createNativeToast.mjs +27 -0
  235. package/dist/jsx/createNativeToast.mjs.map +1 -0
  236. package/dist/jsx/createNativeToast.native.js +47 -0
  237. package/dist/jsx/createNativeToast.native.js.map +1 -0
  238. package/dist/jsx/index.js +7 -0
  239. package/dist/jsx/index.js.map +6 -0
  240. package/dist/jsx/index.mjs +4 -0
  241. package/dist/jsx/index.mjs.map +1 -0
  242. package/dist/jsx/index.native.js +31 -0
  243. package/dist/jsx/index.native.js.map +1 -0
  244. package/dist/jsx/types.js +1 -0
  245. package/dist/jsx/types.js.map +6 -0
  246. package/dist/jsx/types.mjs +2 -0
  247. package/dist/jsx/types.mjs.map +1 -0
  248. package/dist/jsx/types.native.js +19 -0
  249. package/dist/jsx/types.native.js.map +1 -0
  250. package/dist/jsx/useDragGesture.js +76 -0
  251. package/dist/jsx/useDragGesture.js.map +6 -0
  252. package/dist/jsx/useDragGesture.mjs +95 -0
  253. package/dist/jsx/useDragGesture.mjs.map +1 -0
  254. package/dist/jsx/useDragGesture.native.js +146 -0
  255. package/dist/jsx/useDragGesture.native.js.map +1 -0
  256. package/package.json +77 -0
  257. package/src/Toast.tsx +219 -0
  258. package/src/ToastAnnounce.tsx +102 -0
  259. package/src/ToastImperative.tsx +190 -0
  260. package/src/ToastImpl.tsx +503 -0
  261. package/src/ToastItem.tsx +694 -0
  262. package/src/ToastPortal.tsx +19 -0
  263. package/src/ToastProvider.tsx +197 -0
  264. package/src/ToastState.ts +397 -0
  265. package/src/ToastViewport.tsx +430 -0
  266. package/src/Toaster.tsx +445 -0
  267. package/src/constants.ts +2 -0
  268. package/src/createNativeToast.native.tsx +22 -0
  269. package/src/createNativeToast.tsx +48 -0
  270. package/src/index.ts +17 -0
  271. package/src/types.ts +71 -0
  272. package/src/useDragGesture.native.ts +199 -0
  273. package/src/useDragGesture.ts +218 -0
  274. package/types/Toast.d.ts +84 -0
  275. package/types/Toast.d.ts.map +1 -0
  276. package/types/ToastAnnounce.d.ts +18 -0
  277. package/types/ToastAnnounce.d.ts.map +1 -0
  278. package/types/ToastImperative.d.ts +95 -0
  279. package/types/ToastImperative.d.ts.map +1 -0
  280. package/types/ToastImpl.d.ts +109 -0
  281. package/types/ToastImpl.d.ts.map +1 -0
  282. package/types/ToastItem.d.ts +34 -0
  283. package/types/ToastItem.d.ts.map +1 -0
  284. package/types/ToastPortal.d.ts +8 -0
  285. package/types/ToastPortal.d.ts.map +1 -0
  286. package/types/ToastProvider.d.ts +92 -0
  287. package/types/ToastProvider.d.ts.map +1 -0
  288. package/types/ToastState.d.ts +177 -0
  289. package/types/ToastState.d.ts.map +1 -0
  290. package/types/ToastViewport.d.ts +75 -0
  291. package/types/ToastViewport.d.ts.map +1 -0
  292. package/types/Toaster.d.ts +120 -0
  293. package/types/Toaster.d.ts.map +1 -0
  294. package/types/constants.d.ts +3 -0
  295. package/types/constants.d.ts.map +1 -0
  296. package/types/createNativeToast.d.ts +4 -0
  297. package/types/createNativeToast.d.ts.map +1 -0
  298. package/types/createNativeToast.native.d.ts +4 -0
  299. package/types/createNativeToast.native.d.ts.map +1 -0
  300. package/types/index.d.ts +7 -0
  301. package/types/index.d.ts.map +1 -0
  302. package/types/types.d.ts +61 -0
  303. package/types/types.d.ts.map +1 -0
  304. package/types/useDragGesture.d.ts +32 -0
  305. package/types/useDragGesture.d.ts.map +1 -0
  306. package/types/useDragGesture.native.d.ts +26 -0
  307. package/types/useDragGesture.native.d.ts.map +1 -0
@@ -0,0 +1,503 @@
1
+ import { useIsPresent } from '@tamagui/animate-presence'
2
+ import { useComposedRefs } from '@tamagui/compose-refs'
3
+ import { isWeb } from '@tamagui/constants'
4
+ import type { GetProps, TamaguiElement } from '@tamagui/core'
5
+ import {
6
+ View,
7
+ Theme,
8
+ createStyledContext,
9
+ styled,
10
+ useConfiguration,
11
+ useEvent,
12
+ useThemeName,
13
+ } from '@tamagui/core'
14
+ import type { DismissableProps } from '@tamagui/dismissable'
15
+ import { Dismissable } from '@tamagui/dismissable'
16
+ import { composeEventHandlers } from '@tamagui/helpers'
17
+ import { PortalItem } from '@tamagui/portal'
18
+ import { YStack } from '@tamagui/stacks'
19
+ import * as React from 'react'
20
+ import type {
21
+ Animated,
22
+ GestureResponderEvent,
23
+ PanResponderGestureState,
24
+ } from 'react-native'
25
+ import { PanResponder } from 'react-native'
26
+ import { TOAST_CONTEXT, TOAST_NAME } from './constants'
27
+ import { ToastAnnounce } from './ToastAnnounce'
28
+ import type { ScopedProps, SwipeDirection } from './ToastProvider'
29
+ import { Collection, useToastProviderContext } from './ToastProvider'
30
+ import { VIEWPORT_PAUSE, VIEWPORT_RESUME } from './ToastViewport'
31
+
32
+ const ToastImplFrame = styled(YStack, {
33
+ name: 'ToastImpl',
34
+ focusable: true,
35
+
36
+ variants: {
37
+ unstyled: {
38
+ false: {
39
+ focusStyle: {
40
+ outlineStyle: 'solid',
41
+ outlineWidth: 2,
42
+ outlineColor: '$outlineColor',
43
+ },
44
+ backgroundColor: '$color6',
45
+ borderRadius: '$4',
46
+ paddingHorizontal: '$4',
47
+ paddingVertical: '$3',
48
+ marginHorizontal: 'auto',
49
+ marginVertical: '$1',
50
+ elevation: '$3',
51
+ },
52
+ },
53
+ } as const,
54
+
55
+ defaultVariants: {
56
+ unstyled: process.env.TAMAGUI_HEADLESS === '1',
57
+ },
58
+ })
59
+
60
+ type ToastProps = Omit<ToastImplProps, keyof ToastImplPrivateProps>
61
+
62
+ type SwipeEvent = GestureResponderEvent
63
+
64
+ const {
65
+ Provider: ToastInteractiveProvider,
66
+ useStyledContext: useToastInteractiveContext,
67
+ } = createStyledContext({
68
+ onClose() {},
69
+ })
70
+
71
+ type ToastImplPrivateProps = {
72
+ open?: boolean
73
+ onClose(): void
74
+ }
75
+
76
+ type ToastImplFrameProps = GetProps<typeof ToastImplFrame>
77
+
78
+ export type ToastExtraProps = {
79
+ /**
80
+ * The controlled open state of the dialog. Must be used in conjunction with `onOpenChange`.
81
+ */
82
+ open?: boolean
83
+
84
+ /**
85
+ * The open state of the dialog when it is initially rendered. Use when you do not need to control its open state.
86
+ */
87
+ defaultOpen?: boolean
88
+ /**
89
+ * Event handler called when the open state of the dialog changes.
90
+ */
91
+ onOpenChange?(open: boolean): void
92
+ /**
93
+ * Used to force mounting when more control is needed. Useful when
94
+ * controlling animation with React animation libraries.
95
+ */
96
+ forceMount?: true
97
+
98
+ /**
99
+ * Control the sensitivity of the toast for accessibility purposes.
100
+ * For toasts that are the result of a user action, choose foreground. Toasts generated from background tasks should use background.
101
+ */
102
+ type?: 'foreground' | 'background'
103
+ /**
104
+ * Time in milliseconds that toast should remain visible for. Overrides value given to `ToastProvider`.
105
+ */
106
+ duration?: number
107
+ /**
108
+ * Event handler called when the escape key is down. It can be prevented by calling `event.preventDefault`.
109
+ */
110
+ onEscapeKeyDown?: DismissableProps['onEscapeKeyDown']
111
+ /**
112
+ * Event handler called when the dismiss timer is paused.
113
+ * On web, this occurs when the pointer is moved over the viewport, the viewport is focused or when the window is blurred.
114
+ * On mobile, this occurs when the toast is touched.
115
+ */
116
+ onPause?(): void
117
+ /**
118
+ * Event handler called when the dismiss timer is resumed.
119
+ * On web, this occurs when the pointer is moved away from the viewport, the viewport is blurred or when the window is focused.
120
+ * On mobile, this occurs when the toast is released.
121
+ */
122
+ onResume?(): void
123
+ /**
124
+ * Event handler called when starting a swipe interaction. It can be prevented by calling `event.preventDefault`.
125
+ */
126
+ onSwipeStart?(event: SwipeEvent): void
127
+ /**
128
+ * Event handler called during a swipe interaction. It can be prevented by calling `event.preventDefault`.
129
+ */
130
+ onSwipeMove?(event: SwipeEvent): void
131
+ /**
132
+ * Event handler called at the cancellation of a swipe interaction. It can be prevented by calling `event.preventDefault`.
133
+ */
134
+ onSwipeCancel?(event: SwipeEvent): void
135
+ /**
136
+ * Event handler called at the end of a swipe interaction. It can be prevented by calling `event.preventDefault`.
137
+ */
138
+ onSwipeEnd?(event: SwipeEvent): void
139
+ /**
140
+ * The viewport's name to send the toast to. Used when using multiple viewports and want to forward toasts to different ones.
141
+ *
142
+ * @default "default"
143
+ */
144
+ viewportName?: string
145
+ /**
146
+ *
147
+ */
148
+ id?: string
149
+ }
150
+
151
+ type ToastImplProps = ScopedProps<
152
+ ToastImplPrivateProps & ToastImplFrameProps & ToastExtraProps
153
+ >
154
+
155
+ const ToastImpl = React.forwardRef<TamaguiElement, ToastImplProps>(
156
+ (props, forwardedRef) => {
157
+ const {
158
+ scope,
159
+ type = 'foreground',
160
+ duration: durationProp,
161
+ open,
162
+ onClose,
163
+ onEscapeKeyDown,
164
+ onPause,
165
+ onResume,
166
+ onSwipeStart,
167
+ onSwipeMove,
168
+ onSwipeCancel,
169
+ onSwipeEnd,
170
+ viewportName = 'default',
171
+ ...toastProps
172
+ } = props
173
+ const isPresent = useIsPresent()
174
+ const context = useToastProviderContext(scope)
175
+ const [node, setNode] = React.useState<TamaguiElement | null>(null)
176
+ const composedRefs = useComposedRefs(forwardedRef, setNode)
177
+ const duration = durationProp || context.duration
178
+ const closeTimerStartTimeRef = React.useRef(0)
179
+ const closeTimerRemainingTimeRef = React.useRef(duration)
180
+ const closeTimerRef = React.useRef(0)
181
+ const { onToastAdd, onToastRemove } = context
182
+
183
+ const viewport = React.useMemo(() => {
184
+ return context.viewports[viewportName] as HTMLElement | null | undefined
185
+ }, [context.viewports, viewportName])
186
+
187
+ const handleClose = useEvent(() => {
188
+ if (!isPresent) {
189
+ // already removed from the react tree
190
+ return
191
+ }
192
+ // focus viewport if focus is within toast to read the remaining toast
193
+ // count to SR users and ensure focus isn't lost
194
+ if (isWeb) {
195
+ const isFocusInToast = (node as unknown as HTMLDivElement)?.contains(
196
+ document.activeElement
197
+ )
198
+ if (isFocusInToast) viewport?.focus()
199
+ }
200
+ onClose()
201
+ })
202
+
203
+ const startTimer = React.useCallback(
204
+ (duration: number) => {
205
+ if (!duration || duration === Number.POSITIVE_INFINITY) return
206
+ clearTimeout(closeTimerRef.current)
207
+ closeTimerStartTimeRef.current = new Date().getTime()
208
+ closeTimerRef.current = setTimeout(handleClose, duration) as unknown as number
209
+ },
210
+ [handleClose]
211
+ )
212
+
213
+ const handleResume = React.useCallback(() => {
214
+ startTimer(closeTimerRemainingTimeRef.current)
215
+ onResume?.()
216
+ }, [onResume, startTimer])
217
+
218
+ const handlePause = React.useCallback(() => {
219
+ const elapsedTime = new Date().getTime() - closeTimerStartTimeRef.current
220
+ closeTimerRemainingTimeRef.current =
221
+ closeTimerRemainingTimeRef.current - elapsedTime
222
+ window.clearTimeout(closeTimerRef.current)
223
+ onPause?.()
224
+ }, [onPause])
225
+
226
+ React.useEffect(() => {
227
+ if (!isWeb) return
228
+
229
+ if (viewport) {
230
+ viewport.addEventListener(VIEWPORT_PAUSE, handlePause)
231
+ viewport.addEventListener(VIEWPORT_RESUME, handleResume)
232
+ return () => {
233
+ viewport.removeEventListener(VIEWPORT_PAUSE, handlePause)
234
+ viewport.removeEventListener(VIEWPORT_RESUME, handleResume)
235
+ }
236
+ }
237
+ }, [viewport, duration, onPause, onResume, startTimer])
238
+
239
+ // start timer when toast opens or duration changes.
240
+ // we include `open` in deps because closed !== unmounted when animating
241
+ // so it could reopen before being completely unmounted
242
+ React.useEffect(() => {
243
+ if (open && !context.isClosePausedRef.current) {
244
+ startTimer(duration)
245
+ }
246
+ }, [open, duration, context.isClosePausedRef, startTimer])
247
+
248
+ React.useEffect(() => {
249
+ onToastAdd()
250
+ return () => onToastRemove()
251
+ }, [onToastAdd, onToastRemove])
252
+
253
+ const announceTextContent = React.useMemo(() => {
254
+ if (!isWeb) return null
255
+ return node ? getAnnounceTextContent(node as unknown as HTMLDivElement) : null
256
+ }, [node])
257
+
258
+ const isHorizontalSwipe = ['left', 'right', 'horizontal'].includes(
259
+ context.swipeDirection
260
+ )
261
+
262
+ const { animationDriver } = useConfiguration()
263
+ if (!animationDriver) {
264
+ throw new Error('Must set animations in tamagui.config.ts')
265
+ }
266
+
267
+ const { useAnimatedNumber, useAnimatedNumberStyle } = animationDriver
268
+
269
+ const animatedNumber = useAnimatedNumber(0)
270
+
271
+ // temp until reanimated useAnimatedNumber fix
272
+ const AnimatedView = (animationDriver['NumberView'] ??
273
+ animationDriver.View ??
274
+ View) as typeof Animated.View
275
+
276
+ const animatedStyles = useAnimatedNumberStyle(animatedNumber, (val) => {
277
+ 'worklet'
278
+ return {
279
+ transform: [isHorizontalSwipe ? { translateX: val } : { translateY: val }],
280
+ }
281
+ })
282
+
283
+ const panResponder = React.useMemo(() => {
284
+ return PanResponder.create({
285
+ onMoveShouldSetPanResponder: (e, gesture) => {
286
+ const shouldMove = shouldGrantGestureMove(context.swipeDirection, gesture)
287
+ if (shouldMove) {
288
+ onSwipeStart?.(e)
289
+ return true
290
+ }
291
+ return false
292
+ },
293
+ onPanResponderGrant: (e) => {
294
+ if (!isWeb) {
295
+ handlePause?.()
296
+ }
297
+ },
298
+ onPanResponderMove: (e, gesture) => {
299
+ const { x, y } = getGestureDistance(context.swipeDirection, gesture)
300
+ const delta = { x, y }
301
+ animatedNumber.setValue(isHorizontalSwipe ? x : y, { type: 'direct' })
302
+ if (isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)) {
303
+ onSwipeEnd?.(e)
304
+ }
305
+ onSwipeMove?.(e)
306
+ },
307
+ onPanResponderEnd: (e, { dx, dy }) => {
308
+ if (
309
+ !isDeltaInDirection(
310
+ { x: dx, y: dy },
311
+ context.swipeDirection,
312
+ context.swipeThreshold
313
+ )
314
+ ) {
315
+ if (!isWeb) {
316
+ handleResume?.()
317
+ }
318
+ onSwipeCancel?.(e)
319
+ animatedNumber.setValue(0, { type: 'spring' })
320
+ }
321
+ },
322
+ })
323
+ }, [handlePause, handleResume])
324
+
325
+ // need to get the theme name from context and apply it again since portals don't retain the theme
326
+ const themeName = useThemeName()
327
+
328
+ return (
329
+ <>
330
+ {announceTextContent && (
331
+ <ToastAnnounce
332
+ scope={scope}
333
+ // Toasts are always role=status to avoid stuttering issues with role=alert in SRs.
334
+ // biome-ignore lint/a11y/useSemanticElements: <explanation>
335
+ role="status"
336
+ aria-live={type === 'foreground' ? 'assertive' : 'polite'}
337
+ aria-atomic
338
+ >
339
+ {announceTextContent}
340
+ </ToastAnnounce>
341
+ )}
342
+
343
+ <PortalItem hostName={viewportName ?? 'default'}>
344
+ <ToastInteractiveProvider
345
+ key={props.id}
346
+ scope={scope}
347
+ onClose={() => {
348
+ handleClose()
349
+ }}
350
+ >
351
+ <Dismissable
352
+ // asChild
353
+ onEscapeKeyDown={composeEventHandlers(onEscapeKeyDown, () => {
354
+ if (!context.isFocusedToastEscapeKeyDownRef.current) {
355
+ handleClose()
356
+ }
357
+ context.isFocusedToastEscapeKeyDownRef.current = false
358
+ })}
359
+ >
360
+ <Theme contain forceClassName name={themeName}>
361
+ <AnimatedView
362
+ {...panResponder?.panHandlers}
363
+ style={[{ margin: 'auto' }, animatedStyles]}
364
+ >
365
+ <Collection.ItemSlot scope={context.toastScope}>
366
+ <ToastImplFrame
367
+ // Ensure toasts are announced as status list or status when focused
368
+ role="status"
369
+ aria-live="off"
370
+ aria-atomic
371
+ data-state={open ? 'open' : 'closed'}
372
+ data-swipe-direction={context.swipeDirection}
373
+ pointerEvents="auto"
374
+ $platform-web={{
375
+ touchAction: 'none',
376
+ userSelect: 'none',
377
+ }}
378
+ {...toastProps}
379
+ ref={composedRefs}
380
+ {...(isWeb && {
381
+ onKeyDown: composeEventHandlers(props.onKeyDown, (event) => {
382
+ if (event.key !== 'Escape') return
383
+ onEscapeKeyDown?.(event)
384
+ if (!event.defaultPrevented) {
385
+ context.isFocusedToastEscapeKeyDownRef.current = true
386
+ handleClose()
387
+ }
388
+ }),
389
+ })}
390
+ />
391
+ </Collection.ItemSlot>
392
+ </AnimatedView>
393
+ </Theme>
394
+ </Dismissable>
395
+ </ToastInteractiveProvider>
396
+ </PortalItem>
397
+ </>
398
+ )
399
+ }
400
+ )
401
+
402
+ ToastImpl.propTypes = {
403
+ type(props) {
404
+ if (props.type && !['foreground', 'background'].includes(props.type)) {
405
+ const error = `Invalid prop \`type\` supplied to \`${TOAST_NAME}\`. Expected \`foreground | background\`.`
406
+ return new Error(error)
407
+ }
408
+ return null
409
+ },
410
+ }
411
+
412
+ /* ---------------------------------------------------------------------------------------------- */
413
+
414
+ const isDeltaInDirection = (
415
+ delta: { x: number; y: number },
416
+ direction: SwipeDirection,
417
+ threshold = 0
418
+ ) => {
419
+ const deltaX = Math.abs(delta.x)
420
+ const deltaY = Math.abs(delta.y)
421
+ const isDeltaX = deltaX > deltaY
422
+ if (direction === 'left' || direction === 'right' || direction === 'horizontal') {
423
+ return isDeltaX && deltaX > threshold
424
+ }
425
+ return !isDeltaX && deltaY > threshold
426
+ }
427
+
428
+ function getAnnounceTextContent(container: HTMLElement) {
429
+ if (!isWeb) return ''
430
+ const textContent: string[] = []
431
+ const childNodes = Array.from(container.childNodes)
432
+
433
+ childNodes.forEach((node) => {
434
+ if (node.nodeType === node.TEXT_NODE && node.textContent)
435
+ textContent.push(node.textContent)
436
+ if (isHTMLElement(node)) {
437
+ const isHidden = node.ariaHidden || node.hidden || node.style.display === 'none'
438
+ const isExcluded = node.dataset.toastAnnounceExclude === ''
439
+
440
+ if (!isHidden) {
441
+ if (isExcluded) {
442
+ const altText = node.dataset.toastAnnounceAlt
443
+ if (altText) textContent.push(altText)
444
+ } else {
445
+ textContent.push(...getAnnounceTextContent(node))
446
+ }
447
+ }
448
+ }
449
+ })
450
+
451
+ // We return a collection of text rather than a single concatenated string.
452
+ // This allows SR VO to naturally pause break between nodes while announcing.
453
+ return textContent
454
+ }
455
+
456
+ function isHTMLElement(node: any): node is HTMLElement {
457
+ return node.nodeType === node.ELEMENT_NODE
458
+ }
459
+
460
+ const GESTURE_GRANT_THRESHOLD = 10
461
+
462
+ const shouldGrantGestureMove = (
463
+ dir: SwipeDirection,
464
+ { dx, dy }: PanResponderGestureState
465
+ ) => {
466
+ if ((dir === 'horizontal' || dir === 'left') && dx < -GESTURE_GRANT_THRESHOLD) {
467
+ return true
468
+ }
469
+ if ((dir === 'horizontal' || dir === 'right') && dx > GESTURE_GRANT_THRESHOLD) {
470
+ return true
471
+ }
472
+ if ((dir === 'vertical' || dir === 'up') && dy > -GESTURE_GRANT_THRESHOLD) {
473
+ return true
474
+ }
475
+ if ((dir === 'vertical' || dir === 'down') && dy < GESTURE_GRANT_THRESHOLD) {
476
+ return true
477
+ }
478
+
479
+ return false
480
+ }
481
+
482
+ const getGestureDistance = (
483
+ dir: SwipeDirection,
484
+ { dx, dy }: PanResponderGestureState
485
+ ) => {
486
+ let y = 0
487
+ let x = 0
488
+
489
+ if (dir === 'horizontal') x = dx
490
+ else if (dir === 'left') x = Math.min(0, dx)
491
+ else if (dir === 'right') x = Math.max(0, dx)
492
+ else if (dir === 'vertical') y = dy
493
+ else if (dir === 'up') y = Math.min(0, dy)
494
+ else if (dir === 'down') y = Math.max(0, dy)
495
+
496
+ return {
497
+ x,
498
+ y,
499
+ }
500
+ }
501
+
502
+ export { ToastImpl, ToastImplFrame, useToastInteractiveContext, type ToastImplProps }
503
+ export type { ToastProps }