@hanzogui/toast 2.0.0-rc.41-hanzoai.3

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 (295) hide show
  1. package/dist/cjs/Toast.cjs +178 -0
  2. package/dist/cjs/Toast.native.js +181 -0
  3. package/dist/cjs/Toast.native.js.map +1 -0
  4. package/dist/cjs/ToastAnnounce.cjs +103 -0
  5. package/dist/cjs/ToastAnnounce.native.js +111 -0
  6. package/dist/cjs/ToastAnnounce.native.js.map +1 -0
  7. package/dist/cjs/ToastComposable.cjs +933 -0
  8. package/dist/cjs/ToastComposable.native.js +1025 -0
  9. package/dist/cjs/ToastComposable.native.js.map +1 -0
  10. package/dist/cjs/ToastImperative.cjs +119 -0
  11. package/dist/cjs/ToastImperative.native.js +130 -0
  12. package/dist/cjs/ToastImperative.native.js.map +1 -0
  13. package/dist/cjs/ToastImpl.cjs +354 -0
  14. package/dist/cjs/ToastImpl.native.js +364 -0
  15. package/dist/cjs/ToastImpl.native.js.map +1 -0
  16. package/dist/cjs/ToastItemFrame.cjs +143 -0
  17. package/dist/cjs/ToastItemFrame.native.js +148 -0
  18. package/dist/cjs/ToastItemFrame.native.js.map +1 -0
  19. package/dist/cjs/ToastPortal.cjs +49 -0
  20. package/dist/cjs/ToastPortal.native.js +52 -0
  21. package/dist/cjs/ToastPortal.native.js.map +1 -0
  22. package/dist/cjs/ToastProvider.cjs +150 -0
  23. package/dist/cjs/ToastProvider.native.js +161 -0
  24. package/dist/cjs/ToastProvider.native.js.map +1 -0
  25. package/dist/cjs/ToastState.cjs +311 -0
  26. package/dist/cjs/ToastState.native.js +324 -0
  27. package/dist/cjs/ToastState.native.js.map +1 -0
  28. package/dist/cjs/ToastViewport.cjs +319 -0
  29. package/dist/cjs/ToastViewport.native.js +343 -0
  30. package/dist/cjs/ToastViewport.native.js.map +1 -0
  31. package/dist/cjs/Toaster.cjs +87 -0
  32. package/dist/cjs/Toaster.native.js +91 -0
  33. package/dist/cjs/Toaster.native.js.map +1 -0
  34. package/dist/cjs/constants.cjs +30 -0
  35. package/dist/cjs/constants.native.js +33 -0
  36. package/dist/cjs/constants.native.js.map +1 -0
  37. package/dist/cjs/createNativeToast.cjs +59 -0
  38. package/dist/cjs/createNativeToast.native.js +60 -0
  39. package/dist/cjs/createNativeToast.native.js.map +1 -0
  40. package/dist/cjs/dispatchNativeToast.cjs +47 -0
  41. package/dist/cjs/dispatchNativeToast.native.js +52 -0
  42. package/dist/cjs/dispatchNativeToast.native.js.map +1 -0
  43. package/dist/cjs/index.cjs +20 -0
  44. package/dist/cjs/index.native.js +23 -0
  45. package/dist/cjs/index.native.js.map +1 -0
  46. package/dist/cjs/types.cjs +18 -0
  47. package/dist/cjs/types.native.js +21 -0
  48. package/dist/cjs/types.native.js.map +1 -0
  49. package/dist/cjs/useAnimatedDragGesture.cjs +214 -0
  50. package/dist/cjs/useAnimatedDragGesture.native.js +224 -0
  51. package/dist/cjs/useAnimatedDragGesture.native.js.map +1 -0
  52. package/dist/cjs/useReducedMotion.cjs +74 -0
  53. package/dist/cjs/useReducedMotion.native.js +83 -0
  54. package/dist/cjs/useReducedMotion.native.js.map +1 -0
  55. package/dist/cjs/useToastAnimations.cjs +294 -0
  56. package/dist/cjs/useToastAnimations.native.js +304 -0
  57. package/dist/cjs/useToastAnimations.native.js.map +1 -0
  58. package/dist/cjs/v2.cjs +36 -0
  59. package/dist/cjs/v2.native.js +39 -0
  60. package/dist/cjs/v2.native.js.map +1 -0
  61. package/dist/esm/Toast.mjs +137 -0
  62. package/dist/esm/Toast.mjs.map +1 -0
  63. package/dist/esm/Toast.native.js +137 -0
  64. package/dist/esm/Toast.native.js.map +1 -0
  65. package/dist/esm/ToastAnnounce.mjs +66 -0
  66. package/dist/esm/ToastAnnounce.mjs.map +1 -0
  67. package/dist/esm/ToastAnnounce.native.js +71 -0
  68. package/dist/esm/ToastAnnounce.native.js.map +1 -0
  69. package/dist/esm/ToastComposable.mjs +895 -0
  70. package/dist/esm/ToastComposable.mjs.map +1 -0
  71. package/dist/esm/ToastComposable.native.js +984 -0
  72. package/dist/esm/ToastComposable.native.js.map +1 -0
  73. package/dist/esm/ToastImperative.mjs +80 -0
  74. package/dist/esm/ToastImperative.mjs.map +1 -0
  75. package/dist/esm/ToastImperative.native.js +88 -0
  76. package/dist/esm/ToastImperative.native.js.map +1 -0
  77. package/dist/esm/ToastImpl.mjs +316 -0
  78. package/dist/esm/ToastImpl.mjs.map +1 -0
  79. package/dist/esm/ToastImpl.native.js +323 -0
  80. package/dist/esm/ToastImpl.native.js.map +1 -0
  81. package/dist/esm/ToastItemFrame.mjs +114 -0
  82. package/dist/esm/ToastItemFrame.mjs.map +1 -0
  83. package/dist/esm/ToastItemFrame.native.js +116 -0
  84. package/dist/esm/ToastItemFrame.native.js.map +1 -0
  85. package/dist/esm/ToastPortal.mjs +24 -0
  86. package/dist/esm/ToastPortal.mjs.map +1 -0
  87. package/dist/esm/ToastPortal.native.js +24 -0
  88. package/dist/esm/ToastPortal.native.js.map +1 -0
  89. package/dist/esm/ToastProvider.mjs +110 -0
  90. package/dist/esm/ToastProvider.mjs.map +1 -0
  91. package/dist/esm/ToastProvider.native.js +118 -0
  92. package/dist/esm/ToastProvider.native.js.map +1 -0
  93. package/dist/esm/ToastState.mjs +285 -0
  94. package/dist/esm/ToastState.mjs.map +1 -0
  95. package/dist/esm/ToastState.native.js +295 -0
  96. package/dist/esm/ToastState.native.js.map +1 -0
  97. package/dist/esm/ToastViewport.mjs +280 -0
  98. package/dist/esm/ToastViewport.mjs.map +1 -0
  99. package/dist/esm/ToastViewport.native.js +301 -0
  100. package/dist/esm/ToastViewport.native.js.map +1 -0
  101. package/dist/esm/Toaster.mjs +51 -0
  102. package/dist/esm/Toaster.mjs.map +1 -0
  103. package/dist/esm/Toaster.native.js +52 -0
  104. package/dist/esm/Toaster.native.js.map +1 -0
  105. package/dist/esm/constants.mjs +4 -0
  106. package/dist/esm/constants.mjs.map +1 -0
  107. package/dist/esm/constants.native.js +4 -0
  108. package/dist/esm/constants.native.js.map +1 -0
  109. package/dist/esm/createNativeToast.mjs +32 -0
  110. package/dist/esm/createNativeToast.mjs.map +1 -0
  111. package/dist/esm/createNativeToast.native.js +30 -0
  112. package/dist/esm/createNativeToast.native.js.map +1 -0
  113. package/dist/esm/dispatchNativeToast.mjs +22 -0
  114. package/dist/esm/dispatchNativeToast.mjs.map +1 -0
  115. package/dist/esm/dispatchNativeToast.native.js +24 -0
  116. package/dist/esm/dispatchNativeToast.native.js.map +1 -0
  117. package/dist/esm/index.js +2 -0
  118. package/dist/esm/index.js.map +1 -0
  119. package/dist/esm/index.mjs +2 -0
  120. package/dist/esm/index.mjs.map +1 -0
  121. package/dist/esm/index.native.js +2 -0
  122. package/dist/esm/index.native.js.map +1 -0
  123. package/dist/esm/types.mjs +2 -0
  124. package/dist/esm/types.mjs.map +1 -0
  125. package/dist/esm/types.native.js +2 -0
  126. package/dist/esm/types.native.js.map +1 -0
  127. package/dist/esm/useAnimatedDragGesture.mjs +178 -0
  128. package/dist/esm/useAnimatedDragGesture.mjs.map +1 -0
  129. package/dist/esm/useAnimatedDragGesture.native.js +185 -0
  130. package/dist/esm/useAnimatedDragGesture.native.js.map +1 -0
  131. package/dist/esm/useReducedMotion.mjs +38 -0
  132. package/dist/esm/useReducedMotion.mjs.map +1 -0
  133. package/dist/esm/useReducedMotion.native.js +44 -0
  134. package/dist/esm/useReducedMotion.native.js.map +1 -0
  135. package/dist/esm/useToastAnimations.mjs +258 -0
  136. package/dist/esm/useToastAnimations.mjs.map +1 -0
  137. package/dist/esm/useToastAnimations.native.js +265 -0
  138. package/dist/esm/useToastAnimations.native.js.map +1 -0
  139. package/dist/esm/v2.mjs +6 -0
  140. package/dist/esm/v2.mjs.map +1 -0
  141. package/dist/esm/v2.native.js +6 -0
  142. package/dist/esm/v2.native.js.map +1 -0
  143. package/dist/jsx/Toast.mjs +137 -0
  144. package/dist/jsx/Toast.mjs.map +1 -0
  145. package/dist/jsx/Toast.native.js +181 -0
  146. package/dist/jsx/Toast.native.js.map +1 -0
  147. package/dist/jsx/ToastAnnounce.mjs +66 -0
  148. package/dist/jsx/ToastAnnounce.mjs.map +1 -0
  149. package/dist/jsx/ToastAnnounce.native.js +111 -0
  150. package/dist/jsx/ToastAnnounce.native.js.map +1 -0
  151. package/dist/jsx/ToastComposable.mjs +895 -0
  152. package/dist/jsx/ToastComposable.mjs.map +1 -0
  153. package/dist/jsx/ToastComposable.native.js +1025 -0
  154. package/dist/jsx/ToastComposable.native.js.map +1 -0
  155. package/dist/jsx/ToastImperative.mjs +80 -0
  156. package/dist/jsx/ToastImperative.mjs.map +1 -0
  157. package/dist/jsx/ToastImperative.native.js +130 -0
  158. package/dist/jsx/ToastImperative.native.js.map +1 -0
  159. package/dist/jsx/ToastImpl.mjs +316 -0
  160. package/dist/jsx/ToastImpl.mjs.map +1 -0
  161. package/dist/jsx/ToastImpl.native.js +364 -0
  162. package/dist/jsx/ToastImpl.native.js.map +1 -0
  163. package/dist/jsx/ToastItemFrame.mjs +114 -0
  164. package/dist/jsx/ToastItemFrame.mjs.map +1 -0
  165. package/dist/jsx/ToastItemFrame.native.js +148 -0
  166. package/dist/jsx/ToastItemFrame.native.js.map +1 -0
  167. package/dist/jsx/ToastPortal.mjs +24 -0
  168. package/dist/jsx/ToastPortal.mjs.map +1 -0
  169. package/dist/jsx/ToastPortal.native.js +52 -0
  170. package/dist/jsx/ToastPortal.native.js.map +1 -0
  171. package/dist/jsx/ToastProvider.mjs +110 -0
  172. package/dist/jsx/ToastProvider.mjs.map +1 -0
  173. package/dist/jsx/ToastProvider.native.js +161 -0
  174. package/dist/jsx/ToastProvider.native.js.map +1 -0
  175. package/dist/jsx/ToastState.mjs +285 -0
  176. package/dist/jsx/ToastState.mjs.map +1 -0
  177. package/dist/jsx/ToastState.native.js +324 -0
  178. package/dist/jsx/ToastState.native.js.map +1 -0
  179. package/dist/jsx/ToastViewport.mjs +280 -0
  180. package/dist/jsx/ToastViewport.mjs.map +1 -0
  181. package/dist/jsx/ToastViewport.native.js +343 -0
  182. package/dist/jsx/ToastViewport.native.js.map +1 -0
  183. package/dist/jsx/Toaster.mjs +51 -0
  184. package/dist/jsx/Toaster.mjs.map +1 -0
  185. package/dist/jsx/Toaster.native.js +91 -0
  186. package/dist/jsx/Toaster.native.js.map +1 -0
  187. package/dist/jsx/constants.mjs +4 -0
  188. package/dist/jsx/constants.mjs.map +1 -0
  189. package/dist/jsx/constants.native.js +33 -0
  190. package/dist/jsx/constants.native.js.map +1 -0
  191. package/dist/jsx/createNativeToast.mjs +32 -0
  192. package/dist/jsx/createNativeToast.mjs.map +1 -0
  193. package/dist/jsx/createNativeToast.native.js +60 -0
  194. package/dist/jsx/createNativeToast.native.js.map +1 -0
  195. package/dist/jsx/dispatchNativeToast.mjs +22 -0
  196. package/dist/jsx/dispatchNativeToast.mjs.map +1 -0
  197. package/dist/jsx/dispatchNativeToast.native.js +52 -0
  198. package/dist/jsx/dispatchNativeToast.native.js.map +1 -0
  199. package/dist/jsx/index.js +2 -0
  200. package/dist/jsx/index.js.map +1 -0
  201. package/dist/jsx/index.mjs +2 -0
  202. package/dist/jsx/index.mjs.map +1 -0
  203. package/dist/jsx/index.native.js +23 -0
  204. package/dist/jsx/index.native.js.map +1 -0
  205. package/dist/jsx/types.mjs +2 -0
  206. package/dist/jsx/types.mjs.map +1 -0
  207. package/dist/jsx/types.native.js +21 -0
  208. package/dist/jsx/types.native.js.map +1 -0
  209. package/dist/jsx/useAnimatedDragGesture.mjs +178 -0
  210. package/dist/jsx/useAnimatedDragGesture.mjs.map +1 -0
  211. package/dist/jsx/useAnimatedDragGesture.native.js +224 -0
  212. package/dist/jsx/useAnimatedDragGesture.native.js.map +1 -0
  213. package/dist/jsx/useReducedMotion.mjs +38 -0
  214. package/dist/jsx/useReducedMotion.mjs.map +1 -0
  215. package/dist/jsx/useReducedMotion.native.js +83 -0
  216. package/dist/jsx/useReducedMotion.native.js.map +1 -0
  217. package/dist/jsx/useToastAnimations.mjs +258 -0
  218. package/dist/jsx/useToastAnimations.mjs.map +1 -0
  219. package/dist/jsx/useToastAnimations.native.js +304 -0
  220. package/dist/jsx/useToastAnimations.native.js.map +1 -0
  221. package/dist/jsx/v2.mjs +6 -0
  222. package/dist/jsx/v2.mjs.map +1 -0
  223. package/dist/jsx/v2.native.js +39 -0
  224. package/dist/jsx/v2.native.js.map +1 -0
  225. package/package.json +83 -0
  226. package/src/Toast.tsx +219 -0
  227. package/src/ToastAnnounce.tsx +103 -0
  228. package/src/ToastComposable.tsx +1380 -0
  229. package/src/ToastImperative.tsx +192 -0
  230. package/src/ToastImpl.tsx +514 -0
  231. package/src/ToastItemFrame.tsx +136 -0
  232. package/src/ToastPortal.tsx +19 -0
  233. package/src/ToastProvider.tsx +204 -0
  234. package/src/ToastState.ts +398 -0
  235. package/src/ToastViewport.tsx +430 -0
  236. package/src/Toaster.tsx +181 -0
  237. package/src/constants.ts +2 -0
  238. package/src/createNativeToast.native.tsx +33 -0
  239. package/src/createNativeToast.tsx +42 -0
  240. package/src/dispatchNativeToast.ts +43 -0
  241. package/src/index.ts +3 -0
  242. package/src/types.ts +71 -0
  243. package/src/useAnimatedDragGesture.native.ts +255 -0
  244. package/src/useAnimatedDragGesture.ts +319 -0
  245. package/src/useReducedMotion.ts +59 -0
  246. package/src/useToastAnimations.ts +377 -0
  247. package/src/v2.ts +31 -0
  248. package/types/Toast.d.ts +84 -0
  249. package/types/Toast.d.ts.map +1 -0
  250. package/types/ToastAnnounce.d.ts +18 -0
  251. package/types/ToastAnnounce.d.ts.map +1 -0
  252. package/types/ToastComposable.d.ts +199 -0
  253. package/types/ToastComposable.d.ts.map +1 -0
  254. package/types/ToastImperative.d.ts +95 -0
  255. package/types/ToastImperative.d.ts.map +1 -0
  256. package/types/ToastImpl.d.ts +109 -0
  257. package/types/ToastImpl.d.ts.map +1 -0
  258. package/types/ToastItemFrame.d.ts +25 -0
  259. package/types/ToastItemFrame.d.ts.map +1 -0
  260. package/types/ToastPortal.d.ts +8 -0
  261. package/types/ToastPortal.d.ts.map +1 -0
  262. package/types/ToastProvider.d.ts +92 -0
  263. package/types/ToastProvider.d.ts.map +1 -0
  264. package/types/ToastState.d.ts +179 -0
  265. package/types/ToastState.d.ts.map +1 -0
  266. package/types/ToastViewport.d.ts +75 -0
  267. package/types/ToastViewport.d.ts.map +1 -0
  268. package/types/Toaster.d.ts +112 -0
  269. package/types/Toaster.d.ts.map +1 -0
  270. package/types/constants.d.ts +3 -0
  271. package/types/constants.d.ts.map +1 -0
  272. package/types/createNativeToast.d.ts +9 -0
  273. package/types/createNativeToast.d.ts.map +1 -0
  274. package/types/createNativeToast.native.d.ts +5 -0
  275. package/types/createNativeToast.native.d.ts.map +1 -0
  276. package/types/dispatchNativeToast.d.ts +12 -0
  277. package/types/dispatchNativeToast.d.ts.map +1 -0
  278. package/types/index.d.ts +4 -0
  279. package/types/index.d.ts.map +1 -0
  280. package/types/types.d.ts +61 -0
  281. package/types/types.d.ts.map +1 -0
  282. package/types/useAnimatedDragGesture.d.ts +33 -0
  283. package/types/useAnimatedDragGesture.d.ts.map +1 -0
  284. package/types/useAnimatedDragGesture.native.d.ts +33 -0
  285. package/types/useAnimatedDragGesture.native.d.ts.map +1 -0
  286. package/types/useReducedMotion.d.ts +6 -0
  287. package/types/useReducedMotion.d.ts.map +1 -0
  288. package/types/useToastAnimations.d.ts +50 -0
  289. package/types/useToastAnimations.d.ts.map +1 -0
  290. package/types/v2.d.ts +10 -0
  291. package/types/v2.d.ts.map +1 -0
  292. package/v2/index.cjs +2 -0
  293. package/v2/index.js +2 -0
  294. package/v2/index.native.cjs +2 -0
  295. package/v2/index.native.js +2 -0
@@ -0,0 +1,136 @@
1
+ /**
2
+ * Shared visual styled components for toast items.
3
+ * Shared across web and native — imported by ToastComposable.
4
+ */
5
+
6
+ import { styled } from '@hanzogui/core'
7
+ import { XStack, YStack } from '@hanzogui/stacks'
8
+ import { SizableText } from '@hanzogui/text'
9
+
10
+ /* -------------------------------------------------------------------------------------------------
11
+ * ToastPositionWrapper - handles absolute positioning and stacking animations
12
+ * On web: uses Hanzogui transition/enterStyle/exitStyle
13
+ * On native: replaced by Animated.View with useAnimatedStyle
14
+ * -----------------------------------------------------------------------------------------------*/
15
+
16
+ export const ToastPositionWrapper = styled(YStack, {
17
+ name: 'ToastPositionWrapper',
18
+ pointerEvents: 'auto',
19
+ position: 'absolute',
20
+ left: 0,
21
+ right: 0,
22
+ opacity: 1,
23
+ scale: 1,
24
+ y: 0,
25
+ x: 0,
26
+ })
27
+
28
+ /* -------------------------------------------------------------------------------------------------
29
+ * ToastItemFrame - visual styling for the toast
30
+ * Shared across web and native — the visual appearance of the toast card.
31
+ * -----------------------------------------------------------------------------------------------*/
32
+
33
+ export const ToastItemFrame = styled(YStack, {
34
+ name: 'ToastItem',
35
+ userSelect: 'none',
36
+ cursor: 'default',
37
+ focusable: true,
38
+
39
+ variants: {
40
+ unstyled: {
41
+ false: {
42
+ backgroundColor: '$background',
43
+ borderRadius: '$6',
44
+ paddingHorizontal: '$4',
45
+ paddingVertical: '$3',
46
+ borderWidth: 1,
47
+ borderColor: '$borderColor',
48
+ shadowColor: 'rgba(0, 0, 0, 0.15)',
49
+ shadowOffset: { width: 0, height: 4 },
50
+ shadowRadius: 12,
51
+ focusVisibleStyle: {
52
+ outlineWidth: 2,
53
+ outlineColor: '$color8',
54
+ outlineStyle: 'solid',
55
+ },
56
+ },
57
+ },
58
+ } as const,
59
+
60
+ defaultVariants: {
61
+ unstyled: process.env.HANZOGUI_HEADLESS === '1',
62
+ },
63
+ })
64
+
65
+ /* -------------------------------------------------------------------------------------------------
66
+ * ToastCloseFrame
67
+ * -----------------------------------------------------------------------------------------------*/
68
+
69
+ export const ToastCloseFrame = styled(XStack, {
70
+ name: 'ToastClose',
71
+ render: 'button',
72
+ alignItems: 'center',
73
+ justifyContent: 'center',
74
+ cursor: 'pointer',
75
+
76
+ variants: {
77
+ unstyled: {
78
+ false: {
79
+ width: 18,
80
+ height: 18,
81
+ borderRadius: '$10',
82
+ backgroundColor: '$background',
83
+ borderWidth: 1,
84
+ borderColor: '$borderColor',
85
+ shadowColor: 'rgba(0, 0, 0, 0.08)',
86
+ shadowOffset: { width: 0, height: 1 },
87
+ shadowRadius: 3,
88
+ hoverStyle: { backgroundColor: '$color3' },
89
+ pressStyle: { backgroundColor: '$color4' },
90
+ },
91
+ },
92
+ } as const,
93
+
94
+ defaultVariants: {
95
+ unstyled: process.env.HANZOGUI_HEADLESS === '1',
96
+ },
97
+ })
98
+
99
+ /* -------------------------------------------------------------------------------------------------
100
+ * ToastActionFrame - for action/cancel buttons with text
101
+ * -----------------------------------------------------------------------------------------------*/
102
+
103
+ export const ToastActionFrame = styled(XStack, {
104
+ name: 'ToastAction',
105
+ render: 'button',
106
+ alignItems: 'center',
107
+ justifyContent: 'center',
108
+ cursor: 'pointer',
109
+
110
+ variants: {
111
+ unstyled: {
112
+ false: {
113
+ borderRadius: '$2',
114
+ paddingHorizontal: '$2',
115
+ height: 24,
116
+ backgroundColor: '$color5',
117
+ hoverStyle: { backgroundColor: '$color6' },
118
+ pressStyle: { backgroundColor: '$color7' },
119
+ },
120
+ },
121
+ } as const,
122
+
123
+ defaultVariants: {
124
+ unstyled: process.env.HANZOGUI_HEADLESS === '1',
125
+ },
126
+ })
127
+
128
+ /* -------------------------------------------------------------------------------------------------
129
+ * DefaultCloseIcon
130
+ * -----------------------------------------------------------------------------------------------*/
131
+
132
+ export const DefaultCloseIcon = () => (
133
+ <SizableText size="$1" color="$color11">
134
+ ✕
135
+ </SizableText>
136
+ )
@@ -0,0 +1,19 @@
1
+ import { isWeb } from '@hanzogui/constants'
2
+ import { Portal } from '@hanzogui/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
+ }
@@ -0,0 +1,204 @@
1
+ import { createCollection } from '@hanzogui/collection'
2
+ import type { NativeValue, HanzoguiElement } from '@hanzogui/core'
3
+ import { createStyledContext } from '@hanzogui/core'
4
+ import { startTransition } from '@hanzogui/start-transition'
5
+ import * as React from 'react'
6
+
7
+ import { TOAST_CONTEXT } from './constants'
8
+ import type { ToastImperativeOptions } from './ToastImperative'
9
+ import { ToastImperativeProvider } from './ToastImperative'
10
+ import type { BurntToastOptions } from './types'
11
+
12
+ /* -------------------------------------------------------------------------------------------------
13
+ * ToastProvider
14
+ * -----------------------------------------------------------------------------------------------*/
15
+
16
+ const PROVIDER_NAME = 'ToastProvider'
17
+
18
+ const [Collection, useCollection] = createCollection<HanzoguiElement>('Toast')
19
+
20
+ export type SwipeDirection =
21
+ | 'vertical'
22
+ | 'up'
23
+ | 'down'
24
+ | 'horizontal'
25
+ | 'left'
26
+ | 'right'
27
+ | 'auto'
28
+
29
+ export type ToastProviderContextValue = {
30
+ id: string
31
+ toastScope: string
32
+ label: string
33
+ duration: number
34
+ swipeDirection: SwipeDirection
35
+ swipeThreshold: number
36
+ toastCount: number
37
+ viewports: Record<string, HanzoguiElement | null>
38
+ onViewportChange(name: string, viewport: HanzoguiElement): void
39
+ onToastAdd(): void
40
+ onToastRemove(): void
41
+ isFocusedToastEscapeKeyDownRef: React.MutableRefObject<boolean>
42
+ isClosePausedRef: React.MutableRefObject<boolean>
43
+ options: ToastImperativeOptions
44
+ }
45
+
46
+ export type ToastScopes = string
47
+
48
+ type ScopedProps<P> = Omit<P, 'scope'> & { scope?: ToastScopes }
49
+
50
+ const { Provider: ToastProviderProvider, useStyledContext: useToastProviderContext } =
51
+ createStyledContext<ToastProviderContextValue>(
52
+ // since we always provide this we can avoid setting here
53
+ {} as ToastProviderContextValue,
54
+ 'Toast__'
55
+ )
56
+
57
+ interface ToastProviderProps {
58
+ children?: React.ReactNode
59
+ /**
60
+ * An author-localized label for each toast. Used to help screen reader users
61
+ * associate the interruption with a toast.
62
+ * @defaultValue 'Notification'
63
+ */
64
+ label?: string
65
+ /**
66
+ * Time in milliseconds that each toast should remain visible for.
67
+ * @defaultValue 5000
68
+ */
69
+ duration?: number
70
+ /**
71
+ * Direction of pointer swipe that should close the toast.
72
+ * @defaultValue 'right'
73
+ */
74
+ swipeDirection?: SwipeDirection
75
+ /**
76
+ * Distance in pixels that the swipe must pass before a close is triggered.
77
+ * @defaultValue 50
78
+ */
79
+ swipeThreshold?: number
80
+ /**
81
+ * @defaultValue unique generated identifier
82
+ */
83
+ id?: string
84
+ /**
85
+ * Will show a native toast if is true or is set to the current platform. On iOS, it wraps `SPIndicator` and `SPAlert`. On Android, it wraps `ToastAndroid`. On web, it wraps Notification API. Mobile's native features are handled by `burnt`.
86
+ * Only works with the imperative `useToast` hook.
87
+ */
88
+ native?: NativeValue
89
+ /**
90
+ * Options for the burnt package if you're using native toasts on mobile
91
+ */
92
+ burntOptions?: Omit<BurntToastOptions, 'title' | 'message' | 'duration'>
93
+ /**
94
+ * Options for the notification API if you're using native toasts on web
95
+ */
96
+ notificationOptions?: NotificationOptions
97
+ }
98
+
99
+ const ToastProvider: React.FC<ToastProviderProps> = (
100
+ props: ScopedProps<ToastProviderProps>
101
+ ) => {
102
+ const {
103
+ scope = TOAST_CONTEXT,
104
+ id: providedId,
105
+ burntOptions,
106
+ native,
107
+ notificationOptions,
108
+ label = 'Notification',
109
+ duration = 5000,
110
+ swipeDirection = 'right',
111
+ swipeThreshold = 50,
112
+ children,
113
+ } = props
114
+ const backupId = React.useId()
115
+ const id = providedId ?? backupId
116
+ const [viewports, setViewports] = React.useState<
117
+ ToastProviderContextValue['viewports']
118
+ >({})
119
+ const [toastCount, setToastCount] = React.useState(0)
120
+ const isFocusedToastEscapeKeyDownRef = React.useRef(false)
121
+ const isClosePausedRef = React.useRef(false)
122
+
123
+ const handleViewportChange = React.useCallback(
124
+ (name: string, viewport: HanzoguiElement | null) => {
125
+ startTransition(() => {
126
+ setViewports((prev) => ({ ...prev, [name]: viewport }))
127
+ })
128
+ },
129
+ []
130
+ )
131
+
132
+ // memo context to avoid expensive re-renders
133
+ const options = React.useMemo(() => {
134
+ return {
135
+ duration,
136
+ burntOptions,
137
+ native,
138
+ notificationOptions,
139
+ }
140
+ // nested simple object so JSON.stringify
141
+ }, [JSON.stringify([duration, burntOptions, native, notificationOptions])])
142
+
143
+ return (
144
+ <Collection.Provider scope={scope}>
145
+ <ToastProviderProvider
146
+ scope={scope}
147
+ id={id}
148
+ label={label}
149
+ duration={duration}
150
+ swipeDirection={swipeDirection}
151
+ swipeThreshold={swipeThreshold}
152
+ toastCount={toastCount}
153
+ viewports={viewports}
154
+ onViewportChange={handleViewportChange}
155
+ onToastAdd={React.useCallback(() => {
156
+ startTransition(() => {
157
+ setToastCount((prevCount) => prevCount + 1)
158
+ })
159
+ }, [])}
160
+ onToastRemove={React.useCallback(() => {
161
+ startTransition(() => {
162
+ setToastCount((prevCount) => prevCount - 1)
163
+ })
164
+ }, [])}
165
+ isFocusedToastEscapeKeyDownRef={isFocusedToastEscapeKeyDownRef}
166
+ isClosePausedRef={isClosePausedRef}
167
+ options={options}
168
+ >
169
+ <ToastImperativeProvider options={options}>{children}</ToastImperativeProvider>
170
+ </ToastProviderProvider>
171
+ </Collection.Provider>
172
+ )
173
+ }
174
+
175
+ export function ReprogapateToastProvider(props: {
176
+ children: React.ReactNode
177
+ context: ToastProviderContextValue
178
+ }) {
179
+ const { children, context } = props
180
+ return (
181
+ <Collection.Provider scope={context.toastScope}>
182
+ <ToastProviderProvider {...context}>
183
+ <ToastImperativeProvider options={context.options}>
184
+ {children}
185
+ </ToastImperativeProvider>
186
+ </ToastProviderProvider>
187
+ </Collection.Provider>
188
+ )
189
+ }
190
+
191
+ ToastProvider.propTypes = {
192
+ label(props) {
193
+ if (props.label && typeof props.label === 'string' && !props.label.trim()) {
194
+ const error = `Invalid prop \`label\` supplied to \`${PROVIDER_NAME}\`. Expected non-empty \`string\`.`
195
+ return new Error(error)
196
+ }
197
+ return null
198
+ },
199
+ }
200
+
201
+ ToastProvider.displayName = PROVIDER_NAME
202
+
203
+ export { Collection, ToastProvider, useCollection, useToastProviderContext }
204
+ export type { ScopedProps, ToastProviderProps }