@tamagui/toast 2.7.7 → 3.0.0-beta.643.1

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