@tamagui/toast 1.14.0 → 1.14.2

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 (103) hide show
  1. package/dist/cjs/Toast.js +185 -1
  2. package/dist/cjs/Toast.js.map +2 -2
  3. package/dist/cjs/ToastAnnounce.js +92 -1
  4. package/dist/cjs/ToastAnnounce.js.map +2 -2
  5. package/dist/cjs/ToastImperative.js +110 -1
  6. package/dist/cjs/ToastImperative.js.map +2 -2
  7. package/dist/cjs/ToastImpl.js +352 -1
  8. package/dist/cjs/ToastImpl.js.map +2 -2
  9. package/dist/cjs/ToastProvider.js +119 -1
  10. package/dist/cjs/ToastProvider.js.map +2 -2
  11. package/dist/cjs/ToastViewport.js +312 -1
  12. package/dist/cjs/ToastViewport.js.map +2 -2
  13. package/dist/cjs/constants.js +28 -1
  14. package/dist/cjs/constants.js.map +2 -2
  15. package/dist/cjs/createNativeToast.js +69 -1
  16. package/dist/cjs/createNativeToast.js.map +2 -2
  17. package/dist/cjs/createNativeToast.native.js +43 -1
  18. package/dist/cjs/createNativeToast.native.js.map +2 -2
  19. package/dist/cjs/index.js +18 -1
  20. package/dist/cjs/index.js.map +2 -2
  21. package/dist/cjs/types.js +16 -1
  22. package/dist/cjs/types.js.map +2 -2
  23. package/dist/esm/Toast.js +163 -1
  24. package/dist/esm/Toast.js.map +2 -2
  25. package/dist/esm/Toast.mjs +163 -1
  26. package/dist/esm/Toast.mjs.map +2 -2
  27. package/dist/esm/ToastAnnounce.js +63 -1
  28. package/dist/esm/ToastAnnounce.js.map +2 -2
  29. package/dist/esm/ToastAnnounce.mjs +63 -1
  30. package/dist/esm/ToastAnnounce.mjs.map +2 -2
  31. package/dist/esm/ToastImperative.js +73 -1
  32. package/dist/esm/ToastImperative.js.map +2 -2
  33. package/dist/esm/ToastImperative.mjs +73 -1
  34. package/dist/esm/ToastImperative.mjs.map +2 -2
  35. package/dist/esm/ToastImpl.js +328 -1
  36. package/dist/esm/ToastImpl.js.map +2 -2
  37. package/dist/esm/ToastImpl.mjs +328 -1
  38. package/dist/esm/ToastImpl.mjs.map +2 -2
  39. package/dist/esm/ToastProvider.js +80 -1
  40. package/dist/esm/ToastProvider.js.map +2 -2
  41. package/dist/esm/ToastProvider.mjs +80 -1
  42. package/dist/esm/ToastProvider.mjs.map +2 -2
  43. package/dist/esm/ToastViewport.js +279 -1
  44. package/dist/esm/ToastViewport.js.map +2 -2
  45. package/dist/esm/ToastViewport.mjs +279 -1
  46. package/dist/esm/ToastViewport.mjs.map +2 -2
  47. package/dist/esm/constants.js +4 -1
  48. package/dist/esm/constants.js.map +2 -2
  49. package/dist/esm/constants.mjs +4 -1
  50. package/dist/esm/constants.mjs.map +2 -2
  51. package/dist/esm/createNativeToast.js +44 -1
  52. package/dist/esm/createNativeToast.js.map +2 -2
  53. package/dist/esm/createNativeToast.mjs +44 -1
  54. package/dist/esm/createNativeToast.mjs.map +2 -2
  55. package/dist/esm/createNativeToast.native.js +18 -1
  56. package/dist/esm/createNativeToast.native.js.map +2 -2
  57. package/dist/esm/createNativeToast.native.mjs +18 -1
  58. package/dist/esm/createNativeToast.native.mjs.map +2 -2
  59. package/dist/esm/index.js +1 -1
  60. package/dist/esm/index.js.map +1 -1
  61. package/dist/esm/index.mjs +1 -1
  62. package/dist/esm/index.mjs.map +1 -1
  63. package/dist/jsx/Toast.js +156 -1
  64. package/dist/jsx/Toast.js.map +2 -2
  65. package/dist/jsx/Toast.mjs +156 -1
  66. package/dist/jsx/Toast.mjs.map +2 -2
  67. package/dist/jsx/ToastAnnounce.js +59 -1
  68. package/dist/jsx/ToastAnnounce.js.map +2 -2
  69. package/dist/jsx/ToastAnnounce.mjs +59 -1
  70. package/dist/jsx/ToastAnnounce.mjs.map +2 -2
  71. package/dist/jsx/ToastImperative.js +72 -1
  72. package/dist/jsx/ToastImperative.js.map +2 -2
  73. package/dist/jsx/ToastImperative.mjs +72 -1
  74. package/dist/jsx/ToastImperative.mjs.map +2 -2
  75. package/dist/jsx/ToastImpl.js +307 -1
  76. package/dist/jsx/ToastImpl.js.map +2 -2
  77. package/dist/jsx/ToastImpl.mjs +307 -1
  78. package/dist/jsx/ToastImpl.mjs.map +2 -2
  79. package/dist/jsx/ToastProvider.js +75 -1
  80. package/dist/jsx/ToastProvider.js.map +2 -2
  81. package/dist/jsx/ToastProvider.mjs +75 -1
  82. package/dist/jsx/ToastProvider.mjs.map +2 -2
  83. package/dist/jsx/ToastViewport.js +259 -1
  84. package/dist/jsx/ToastViewport.js.map +2 -2
  85. package/dist/jsx/ToastViewport.mjs +259 -1
  86. package/dist/jsx/ToastViewport.mjs.map +2 -2
  87. package/dist/jsx/constants.js +4 -1
  88. package/dist/jsx/constants.js.map +2 -2
  89. package/dist/jsx/constants.mjs +4 -1
  90. package/dist/jsx/constants.mjs.map +2 -2
  91. package/dist/jsx/createNativeToast.js +44 -1
  92. package/dist/jsx/createNativeToast.js.map +2 -2
  93. package/dist/jsx/createNativeToast.mjs +44 -1
  94. package/dist/jsx/createNativeToast.mjs.map +2 -2
  95. package/dist/jsx/createNativeToast.native.js +18 -1
  96. package/dist/jsx/createNativeToast.native.js.map +2 -2
  97. package/dist/jsx/createNativeToast.native.mjs +18 -1
  98. package/dist/jsx/createNativeToast.native.mjs.map +2 -2
  99. package/dist/jsx/index.js +1 -1
  100. package/dist/jsx/index.js.map +1 -1
  101. package/dist/jsx/index.mjs +1 -1
  102. package/dist/jsx/index.mjs.map +1 -1
  103. package/package.json +13 -13
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/ToastAnnounce.tsx"],
4
4
  "sourcesContent": ["import {\n GetProps,\n Stack,\n TamaguiElement,\n Text,\n styled,\n useEvent,\n useIsomorphicLayoutEffect,\n} from '@tamagui/core'\nimport { Portal } from '@tamagui/portal'\nimport { VisuallyHidden } from '@tamagui/visually-hidden'\nimport * as React from 'react'\n\nimport { ScopedProps, useToastProviderContext } from './ToastProvider'\nimport { TOAST_NAME } from './constants'\n\nconst ToastAnnounceExcludeFrame = styled(Stack, {\n name: 'ToastAnnounceExclude',\n})\ntype ToastAnnounceExcludeFrameProps = GetProps<typeof ToastAnnounceExcludeFrame>\ntype ToastAnnounceExcludeProps = ToastAnnounceExcludeFrameProps & {\n altText?: string\n}\n\nconst ToastAnnounceExclude = React.forwardRef<TamaguiElement, ToastAnnounceExcludeProps>(\n (props: ScopedProps<ToastAnnounceExcludeProps>, forwardedRef) => {\n const { __scopeToast, altText, ...announceExcludeProps } = props\n\n return (\n <ToastAnnounceExcludeFrame\n data-toast-announce-exclude=\"\"\n data-toast-announce-alt={altText || undefined}\n {...announceExcludeProps}\n ref={forwardedRef}\n />\n )\n }\n)\n\n/* -----------------------------------------------------------------------------------------------*/\n\ninterface ToastAnnounceProps\n extends Omit<GetProps<typeof VisuallyHidden>, 'children'>,\n ScopedProps<{ children: string[] }> {}\n\nconst ToastAnnounce: React.FC<ToastAnnounceProps> = (\n props: ScopedProps<ToastAnnounceProps>\n) => {\n const { __scopeToast, children, ...announceProps } = props\n const context = useToastProviderContext(TOAST_NAME, __scopeToast)\n const [renderAnnounceText, setRenderAnnounceText] = React.useState(false)\n const [isAnnounced, setIsAnnounced] = React.useState(false)\n\n // render text content in the next frame to ensure toast is announced in NVDA\n useNextFrame(() => setRenderAnnounceText(true))\n\n // cleanup after announcing\n React.useEffect(() => {\n const timer = setTimeout(() => setIsAnnounced(true), 1000)\n return () => clearTimeout(timer)\n }, [])\n\n return isAnnounced ? null : (\n <Portal asChild>\n <VisuallyHidden {...announceProps}>\n {renderAnnounceText && (\n <Text>\n {context.label} {children}\n </Text>\n )}\n </VisuallyHidden>\n </Portal>\n )\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction useNextFrame(callback = () => {}) {\n const fn = useEvent(callback)\n useIsomorphicLayoutEffect(() => {\n let raf1 = 0\n let raf2 = 0\n raf1 = requestAnimationFrame(() => (raf2 = requestAnimationFrame(fn)))\n return () => {\n cancelAnimationFrame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AM,IAAEF,GACnB,EAEJ,EACF,CAEJ,EAIA,SAASO,EAAaE,EAAW,IAAM,CAAC,EAAG,CACzC,MAAMC,EAAKzB,EAASwB,CAAQ,EAC5BvB,EAA0B,IAAM,CAC9B,IAAIyB,EAAO,EACPC,EAAO,EACX,OAAAD,EAAO,sBAAsB,IAAOC,EAAO,sBAAsBF,CAAE,CAAE,EAC9D,IAAM,CACX,qBAAqBC,CAAI,EACzB,qBAAqBC,CAAI,CAC3B,CACF,EAAG,CAACF,CAAE,CAAC,CACT",
6
- "names": ["jsx", "jsxs", "Stack", "Text", "styled", "useEvent", "useIsomorphicLayoutEffect", "Portal", "VisuallyHidden", "React", "useToastProviderContext", "TOAST_NAME", "ToastAnnounceExcludeFrame", "ToastAnnounceExclude", "props", "forwardedRef", "__scopeToast", "altText", "announceExcludeProps", "ToastAnnounce", "children", "announceProps", "context", "renderAnnounceText", "setRenderAnnounceText", "isAnnounced", "setIsAnnounced", "useNextFrame", "timer", "callback", "fn", "raf1", "raf2"]
5
+ "mappings": "AA6BM,cAqCI,YArCJ;AA7BN;AAAA,EAEE;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,SAAS,cAAc;AACvB,SAAS,sBAAsB;AAC/B,YAAY,WAAW;AAEvB,SAAsB,+BAA+B;AACrD,SAAS,kBAAkB;AAE3B,MAAM,4BAA4B,OAAO,OAAO;AAAA,EAC9C,MAAM;AACR,CAAC;AAMD,MAAM,uBAAuB,MAAM;AAAA,EACjC,CAAC,OAA+C,iBAAiB;AAC/D,UAAM,EAAE,cAAc,SAAS,GAAG,qBAAqB,IAAI;AAE3D,WACE;AAAA,MAAC;AAAA;AAAA,QACC,+BAA4B;AAAA,QAC5B,2BAAyB,WAAW;AAAA,QACnC,GAAG;AAAA,QACJ,KAAK;AAAA;AAAA,IACP;AAAA,EAEJ;AACF;AAQA,MAAM,gBAA8C,CAClD,UACG;AACH,QAAM,EAAE,cAAc,UAAU,GAAG,cAAc,IAAI;AACrD,QAAM,UAAU,wBAAwB,YAAY,YAAY;AAChE,QAAM,CAAC,oBAAoB,qBAAqB,IAAI,MAAM,SAAS,KAAK;AACxE,QAAM,CAAC,aAAa,cAAc,IAAI,MAAM,SAAS,KAAK;AAG1D,eAAa,MAAM,sBAAsB,IAAI,CAAC;AAG9C,QAAM,UAAU,MAAM;AACpB,UAAM,QAAQ,WAAW,MAAM,eAAe,IAAI,GAAG,GAAI;AACzD,WAAO,MAAM,aAAa,KAAK;AAAA,EACjC,GAAG,CAAC,CAAC;AAEL,SAAO,cAAc,OACnB,oBAAC,UAAO,SAAO,MACb,8BAAC,kBAAgB,GAAG,eACjB,gCACC,qBAAC,QACE;AAAA,YAAQ;AAAA,IAAM;AAAA,IAAE;AAAA,KACnB,GAEJ,GACF;AAEJ;AAIA,SAAS,aAAa,WAAW,MAAM;AAAC,GAAG;AACzC,QAAM,KAAK,SAAS,QAAQ;AAC5B,4BAA0B,MAAM;AAC9B,QAAI,OAAO;AACX,QAAI,OAAO;AACX,WAAO,sBAAsB,MAAO,OAAO,sBAAsB,EAAE,CAAE;AACrE,WAAO,MAAM;AACX,2BAAqB,IAAI;AACzB,2BAAqB,IAAI;AAAA,IAC3B;AAAA,EACF,GAAG,CAAC,EAAE,CAAC;AACT;",
6
+ "names": []
7
7
  }
@@ -1,2 +1,74 @@
1
- import{jsx as y}from"react/jsx-runtime";import{isWeb as d}from"@tamagui/core";import s,{createContext as p,useContext as m,useMemo as f,useRef as O}from"react";import{Platform as N}from"react-native";import{createNativeToast as V}from"./createNativeToast";const C=p({}),x=p(null),D=()=>m(C),h=()=>m(x),B=()=>({...D(),currentToast:h()}),E=({children:P,options:o})=>{const l=O(0),[I,r]=s.useState(null),[e,b]=s.useState(null),v=s.useCallback((c,a)=>{const t=(a==null?void 0:a.native)??o.native,A=Array.isArray(t)?t.includes("web"):t==="web",i=Array.isArray(t)?t.includes("mobile"):t==="mobile",S=i||(Array.isArray(t)?t.includes("android"):t==="android"),g=i||(Array.isArray(t)?t.includes("ios"):t==="ios"),T=t===!0||d&&A||!d&&i||N.OS==="android"&&S||N.OS==="ios"&&g;if(T){const n=V(c,a??{});typeof n=="object"&&n.nativeToastRef&&b(n.nativeToastRef)}return l.current++,r({title:c,id:l.current.toString(),...a,isHandledNatively:T}),!0},[r,o.native]),u=s.useCallback(()=>{e==null||e.close(),r(null)},[r,e]),R=f(()=>({show:v,hide:u,nativeToast:e,options:o}),[v,u,e,JSON.stringify(o||null)]),k=f(()=>{},[]);return y(C.Provider,{value:R,children:y(x.Provider,{value:I,children:P})})};export{E as ToastImperativeProvider,B as useToast,D as useToastController,h as useToastState};
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { isWeb } from "@tamagui/core";
3
+ import React, { createContext, useContext, useMemo, useRef } from "react";
4
+ import { Platform } from "react-native";
5
+ import { createNativeToast } from "./createNativeToast";
6
+ const ToastContext = createContext({});
7
+ const ToastCurrentContext = createContext(null);
8
+ const useToastController = () => {
9
+ return useContext(ToastContext);
10
+ };
11
+ const useToastState = () => {
12
+ return useContext(ToastCurrentContext);
13
+ };
14
+ const useToast = () => {
15
+ return {
16
+ ...useToastController(),
17
+ currentToast: useToastState()
18
+ };
19
+ };
20
+ const ToastImperativeProvider = ({
21
+ children,
22
+ options
23
+ }) => {
24
+ const counterRef = useRef(0);
25
+ const [toast, setToast] = React.useState(null);
26
+ const [lastNativeToastRef, setLastNativeToastRef] = React.useState(null);
27
+ const show = React.useCallback(
28
+ (title, showOptions) => {
29
+ const native = (showOptions == null ? void 0 : showOptions.native) ?? options.native;
30
+ const isWebNative = Array.isArray(native) ? native.includes("web") : native === "web";
31
+ const isMobileNative = Array.isArray(native) ? native.includes("mobile") : native === "mobile";
32
+ const isAndroidNative = isMobileNative || (Array.isArray(native) ? native.includes("android") : native === "android");
33
+ const isIosNative = isMobileNative || (Array.isArray(native) ? native.includes("ios") : native === "ios");
34
+ const isHandledNatively = native === true || isWeb && isWebNative || !isWeb && isMobileNative || Platform.OS === "android" && isAndroidNative || Platform.OS === "ios" && isIosNative;
35
+ if (isHandledNatively) {
36
+ const nativeToastResult = createNativeToast(title, showOptions ?? {});
37
+ if (typeof nativeToastResult === "object" && nativeToastResult.nativeToastRef) {
38
+ setLastNativeToastRef(nativeToastResult.nativeToastRef);
39
+ }
40
+ }
41
+ counterRef.current++;
42
+ setToast({
43
+ title,
44
+ id: counterRef.current.toString(),
45
+ ...showOptions,
46
+ isHandledNatively
47
+ });
48
+ return true;
49
+ },
50
+ [setToast, options.native]
51
+ );
52
+ const hide = React.useCallback(() => {
53
+ lastNativeToastRef == null ? void 0 : lastNativeToastRef.close();
54
+ setToast(null);
55
+ }, [setToast, lastNativeToastRef]);
56
+ const contextValue = useMemo(() => {
57
+ return {
58
+ show,
59
+ hide,
60
+ nativeToast: lastNativeToastRef,
61
+ options
62
+ };
63
+ }, [show, hide, lastNativeToastRef, JSON.stringify(options || null)]);
64
+ const currentContextValue = useMemo(() => {
65
+ }, []);
66
+ return /* @__PURE__ */ jsx(ToastContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(ToastCurrentContext.Provider, { value: toast, children }) });
67
+ };
68
+ export {
69
+ ToastImperativeProvider,
70
+ useToast,
71
+ useToastController,
72
+ useToastState
73
+ };
2
74
  //# sourceMappingURL=ToastImperative.js.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/ToastImperative.tsx"],
4
4
  "sourcesContent": ["import { isWeb } from '@tamagui/core'\nimport React, { createContext, useContext, useMemo, useRef } from 'react'\nimport { Platform } from 'react-native'\n\nimport { createNativeToast } from './createNativeToast'\nimport {\n CreateNativeToastOptions,\n NativeToastRef,\n ToastNativePlatform,\n ToastNativeValue,\n} from './types'\n\ninterface ToastImperativeOptions extends Omit<CreateNativeToastOptions, 'message'> {\n /**\n * 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`.\n */\n native?: ToastNativeValue\n}\ninterface ShowOptions extends CreateNativeToastOptions {\n /**\n * Used when need custom data\n */\n additionalInfo?: Record<string, any>\n /**\n * Overrides the native option on `ToastImperativeProvider`\n */\n native?: ToastNativeValue\n}\n\ntype ToastData = { title: string; id: string } & CreateNativeToastOptions & {\n isHandledNatively: boolean\n }\n\ninterface ToastContextI {\n nativeToast: NativeToastRef | null\n\n /**\n * Call it to show a new toast. If you're using native toasts, you can pass native options using \\`burntOptions\\` or \\`notificationOptions\\` depending on the native platform (mobile/web).\n */\n show: (title: string, options?: ShowOptions) => boolean\n\n /**\n * Call it to hide the currently displayed toast.\n *\n * _NOTE_: does not work on Android native toasts\n *\n * _NOTE_: hides the last toast on web notification toasts\n */\n hide: () => void\n\n options?: ToastImperativeOptions\n}\n\nconst ToastContext = createContext<ToastContextI>({} as any)\nconst ToastCurrentContext = createContext<ToastData | null>(null)\n\nexport const useToastController = () => {\n return useContext(ToastContext)\n}\n\nexport const useToastState = () => {\n return useContext(ToastCurrentContext)\n}\n\n/** @deprecated use `useToastController` and `useToastState` instead to avoid performance pitfalls */\nexport const useToast = () => {\n return {\n ...useToastController(),\n currentToast: useToastState(),\n }\n}\n\ninterface ToastImperativeProviderProps {\n children: React.ReactNode\n /**\n * Used to provide defaults to imperative API. Options can be overwritten when calling `show()`.\n */\n options: ToastImperativeOptions\n}\n\nexport const ToastImperativeProvider = ({\n children,\n options,\n}: ToastImperativeProviderProps) => {\n const counterRef = useRef(0)\n\n const [toast, setToast] = React.useState<ToastData | null>(null)\n\n const [lastNativeToastRef, setLastNativeToastRef] =\n React.useState<ToastContextI['nativeToast']>(null)\n\n const show = React.useCallback<ToastContextI['show']>(\n (title, showOptions) => {\n const native = showOptions?.native ?? options.native\n const isWebNative = Array.isArray(native)\n ? native.includes('web')\n : native === 'web'\n const isMobileNative = Array.isArray(native)\n ? native.includes('mobile')\n : native === 'mobile'\n const isAndroidNative =\n isMobileNative ||\n (Array.isArray(native) ? native.includes('android') : native === 'android')\n const isIosNative =\n isMobileNative ||\n (Array.isArray(native) ? native.includes('ios') : native === 'ios')\n\n const isHandledNatively =\n native === true ||\n (isWeb && isWebNative) ||\n (!isWeb && isMobileNative) ||\n (Platform.OS === 'android' && isAndroidNative) ||\n (Platform.OS === 'ios' && isIosNative)\n\n if (isHandledNatively) {\n const nativeToastResult = createNativeToast(title, showOptions ?? {})\n if (typeof nativeToastResult === 'object' && nativeToastResult.nativeToastRef) {\n setLastNativeToastRef(nativeToastResult.nativeToastRef)\n }\n }\n counterRef.current++\n setToast({\n title,\n id: counterRef.current.toString(),\n ...showOptions,\n isHandledNatively,\n })\n return true\n },\n [setToast, options.native]\n )\n const hide = React.useCallback(() => {\n lastNativeToastRef?.close()\n setToast(null)\n }, [setToast, lastNativeToastRef])\n\n const contextValue = useMemo(() => {\n return {\n show,\n hide,\n nativeToast: lastNativeToastRef,\n options,\n }\n }, [show, hide, lastNativeToastRef, JSON.stringify(options || null)])\n\n const currentContextValue = useMemo(() => {}, [])\n\n return (\n <ToastContext.Provider value={contextValue}>\n <ToastCurrentContext.Provider value={toast}>\n {children}\n </ToastCurrentContext.Provider>\n </ToastContext.Provider>\n )\n}\n\nexport type { ToastImperativeProviderProps, ToastNativePlatform, ToastNativeValue }\n"],
5
- "mappings": "AAqJM,cAAAA,MAAA,oBArJN,OAAS,SAAAC,MAAa,gBACtB,OAAOC,GAAS,iBAAAC,EAAe,cAAAC,EAAY,WAAAC,EAAS,UAAAC,MAAc,QAClE,OAAS,YAAAC,MAAgB,eAEzB,OAAS,qBAAAC,MAAyB,sBAiDlC,MAAMC,EAAeN,EAA6B,CAAC,CAAQ,EACrDO,EAAsBP,EAAgC,IAAI,EAEnDQ,EAAqB,IACzBP,EAAWK,CAAY,EAGnBG,EAAgB,IACpBR,EAAWM,CAAmB,EAI1BG,EAAW,KACf,CACL,GAAGF,EAAmB,EACtB,aAAcC,EAAc,CAC9B,GAWWE,EAA0B,CAAC,CACtC,SAAAC,EACA,QA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AAUJ,GAAW,IAAI,CAAC,CAAC,EAE9DkB,EAAsB7B,EAAQ,IAAM,CAAC,EAAG,CAAC,CAAC,EAEhD,OACEL,EAACS,EAAa,SAAb,CAAsB,MAAOwB,EAC5B,SAAAjC,EAACU,EAAoB,SAApB,CAA6B,MAAOQ,EAClC,SAAAH,EACH,EACF,CAEJ",
6
- "names": ["jsx", "isWeb", "React", "createContext", "useContext", "useMemo", "useRef", "Platform", "createNativeToast", "ToastContext", "ToastCurrentContext", "useToastController", "useToastState", "useToast", "ToastImperativeProvider", "children", "options", "counterRef", "toast", "setToast", "lastNativeToastRef", "setLastNativeToastRef", "show", "title", "showOptions", "native", "isWebNative", "isMobileNative", "isAndroidNative", "isIosNative", "isHandledNatively", "nativeToastResult", "hide", "contextValue", "currentContextValue"]
5
+ "mappings": "AAqJM;AArJN,SAAS,aAAa;AACtB,OAAO,SAAS,eAAe,YAAY,SAAS,cAAc;AAClE,SAAS,gBAAgB;AAEzB,SAAS,yBAAyB;AAiDlC,MAAM,eAAe,cAA6B,CAAC,CAAQ;AAC3D,MAAM,sBAAsB,cAAgC,IAAI;AAEzD,MAAM,qBAAqB,MAAM;AACtC,SAAO,WAAW,YAAY;AAChC;AAEO,MAAM,gBAAgB,MAAM;AACjC,SAAO,WAAW,mBAAmB;AACvC;AAGO,MAAM,WAAW,MAAM;AAC5B,SAAO;AAAA,IACL,GAAG,mBAAmB;AAAA,IACtB,cAAc,cAAc;AAAA,EAC9B;AACF;AAUO,MAAM,0BAA0B,CAAC;AAAA,EACtC;AAAA,EACA;AACF,MAAoC;AAClC,QAAM,aAAa,OAAO,CAAC;AAE3B,QAAM,CAAC,OAAO,QAAQ,IAAI,MAAM,SAA2B,IAAI;AAE/D,QAAM,CAAC,oBAAoB,qBAAqB,IAC9C,MAAM,SAAuC,IAAI;AAEnD,QAAM,OAAO,MAAM;AAAA,IACjB,CAAC,OAAO,gBAAgB;AACtB,YAAM,UAAS,2CAAa,WAAU,QAAQ;AAC9C,YAAM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;",
6
+ "names": []
7
7
  }
@@ -1,2 +1,74 @@
1
- import{jsx as y}from"react/jsx-runtime";import{isWeb as d}from"@tamagui/core";import s,{createContext as p,useContext as m,useMemo as f,useRef as O}from"react";import{Platform as N}from"react-native";import{createNativeToast as V}from"./createNativeToast";const C=p({}),x=p(null),D=()=>m(C),h=()=>m(x),B=()=>({...D(),currentToast:h()}),E=({children:P,options:o})=>{const l=O(0),[I,r]=s.useState(null),[e,b]=s.useState(null),v=s.useCallback((c,a)=>{const t=(a==null?void 0:a.native)??o.native,A=Array.isArray(t)?t.includes("web"):t==="web",i=Array.isArray(t)?t.includes("mobile"):t==="mobile",S=i||(Array.isArray(t)?t.includes("android"):t==="android"),g=i||(Array.isArray(t)?t.includes("ios"):t==="ios"),T=t===!0||d&&A||!d&&i||N.OS==="android"&&S||N.OS==="ios"&&g;if(T){const n=V(c,a??{});typeof n=="object"&&n.nativeToastRef&&b(n.nativeToastRef)}return l.current++,r({title:c,id:l.current.toString(),...a,isHandledNatively:T}),!0},[r,o.native]),u=s.useCallback(()=>{e==null||e.close(),r(null)},[r,e]),R=f(()=>({show:v,hide:u,nativeToast:e,options:o}),[v,u,e,JSON.stringify(o||null)]),k=f(()=>{},[]);return y(C.Provider,{value:R,children:y(x.Provider,{value:I,children:P})})};export{E as ToastImperativeProvider,B as useToast,D as useToastController,h as useToastState};
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { isWeb } from "@tamagui/core";
3
+ import React, { createContext, useContext, useMemo, useRef } from "react";
4
+ import { Platform } from "react-native";
5
+ import { createNativeToast } from "./createNativeToast";
6
+ const ToastContext = createContext({});
7
+ const ToastCurrentContext = createContext(null);
8
+ const useToastController = () => {
9
+ return useContext(ToastContext);
10
+ };
11
+ const useToastState = () => {
12
+ return useContext(ToastCurrentContext);
13
+ };
14
+ const useToast = () => {
15
+ return {
16
+ ...useToastController(),
17
+ currentToast: useToastState()
18
+ };
19
+ };
20
+ const ToastImperativeProvider = ({
21
+ children,
22
+ options
23
+ }) => {
24
+ const counterRef = useRef(0);
25
+ const [toast, setToast] = React.useState(null);
26
+ const [lastNativeToastRef, setLastNativeToastRef] = React.useState(null);
27
+ const show = React.useCallback(
28
+ (title, showOptions) => {
29
+ const native = (showOptions == null ? void 0 : showOptions.native) ?? options.native;
30
+ const isWebNative = Array.isArray(native) ? native.includes("web") : native === "web";
31
+ const isMobileNative = Array.isArray(native) ? native.includes("mobile") : native === "mobile";
32
+ const isAndroidNative = isMobileNative || (Array.isArray(native) ? native.includes("android") : native === "android");
33
+ const isIosNative = isMobileNative || (Array.isArray(native) ? native.includes("ios") : native === "ios");
34
+ const isHandledNatively = native === true || isWeb && isWebNative || !isWeb && isMobileNative || Platform.OS === "android" && isAndroidNative || Platform.OS === "ios" && isIosNative;
35
+ if (isHandledNatively) {
36
+ const nativeToastResult = createNativeToast(title, showOptions ?? {});
37
+ if (typeof nativeToastResult === "object" && nativeToastResult.nativeToastRef) {
38
+ setLastNativeToastRef(nativeToastResult.nativeToastRef);
39
+ }
40
+ }
41
+ counterRef.current++;
42
+ setToast({
43
+ title,
44
+ id: counterRef.current.toString(),
45
+ ...showOptions,
46
+ isHandledNatively
47
+ });
48
+ return true;
49
+ },
50
+ [setToast, options.native]
51
+ );
52
+ const hide = React.useCallback(() => {
53
+ lastNativeToastRef == null ? void 0 : lastNativeToastRef.close();
54
+ setToast(null);
55
+ }, [setToast, lastNativeToastRef]);
56
+ const contextValue = useMemo(() => {
57
+ return {
58
+ show,
59
+ hide,
60
+ nativeToast: lastNativeToastRef,
61
+ options
62
+ };
63
+ }, [show, hide, lastNativeToastRef, JSON.stringify(options || null)]);
64
+ const currentContextValue = useMemo(() => {
65
+ }, []);
66
+ return /* @__PURE__ */ jsx(ToastContext.Provider, { value: contextValue, children: /* @__PURE__ */ jsx(ToastCurrentContext.Provider, { value: toast, children }) });
67
+ };
68
+ export {
69
+ ToastImperativeProvider,
70
+ useToast,
71
+ useToastController,
72
+ useToastState
73
+ };
2
74
  //# sourceMappingURL=ToastImperative.mjs.map
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/ToastImperative.tsx"],
4
4
  "sourcesContent": ["import { isWeb } from '@tamagui/core'\nimport React, { createContext, useContext, useMemo, useRef } from 'react'\nimport { Platform } from 'react-native'\n\nimport { createNativeToast } from './createNativeToast'\nimport {\n CreateNativeToastOptions,\n NativeToastRef,\n ToastNativePlatform,\n ToastNativeValue,\n} from './types'\n\ninterface ToastImperativeOptions extends Omit<CreateNativeToastOptions, 'message'> {\n /**\n * 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`.\n */\n native?: ToastNativeValue\n}\ninterface ShowOptions extends CreateNativeToastOptions {\n /**\n * Used when need custom data\n */\n additionalInfo?: Record<string, any>\n /**\n * Overrides the native option on `ToastImperativeProvider`\n */\n native?: ToastNativeValue\n}\n\ntype ToastData = { title: string; id: string } & CreateNativeToastOptions & {\n isHandledNatively: boolean\n }\n\ninterface ToastContextI {\n nativeToast: NativeToastRef | null\n\n /**\n * Call it to show a new toast. If you're using native toasts, you can pass native options using \\`burntOptions\\` or \\`notificationOptions\\` depending on the native platform (mobile/web).\n */\n show: (title: string, options?: ShowOptions) => boolean\n\n /**\n * Call it to hide the currently displayed toast.\n *\n * _NOTE_: does not work on Android native toasts\n *\n * _NOTE_: hides the last toast on web notification toasts\n */\n hide: () => void\n\n options?: ToastImperativeOptions\n}\n\nconst ToastContext = createContext<ToastContextI>({} as any)\nconst ToastCurrentContext = createContext<ToastData | null>(null)\n\nexport const useToastController = () => {\n return useContext(ToastContext)\n}\n\nexport const useToastState = () => {\n return useContext(ToastCurrentContext)\n}\n\n/** @deprecated use `useToastController` and `useToastState` instead to avoid performance pitfalls */\nexport const useToast = () => {\n return {\n ...useToastController(),\n currentToast: useToastState(),\n }\n}\n\ninterface ToastImperativeProviderProps {\n children: React.ReactNode\n /**\n * Used to provide defaults to imperative API. Options can be overwritten when calling `show()`.\n */\n options: ToastImperativeOptions\n}\n\nexport const ToastImperativeProvider = ({\n children,\n options,\n}: ToastImperativeProviderProps) => {\n const counterRef = useRef(0)\n\n const [toast, setToast] = React.useState<ToastData | null>(null)\n\n const [lastNativeToastRef, setLastNativeToastRef] =\n React.useState<ToastContextI['nativeToast']>(null)\n\n const show = React.useCallback<ToastContextI['show']>(\n (title, showOptions) => {\n const native = showOptions?.native ?? options.native\n const isWebNative = Array.isArray(native)\n ? native.includes('web')\n : native === 'web'\n const isMobileNative = Array.isArray(native)\n ? native.includes('mobile')\n : native === 'mobile'\n const isAndroidNative =\n isMobileNative ||\n (Array.isArray(native) ? native.includes('android') : native === 'android')\n const isIosNative =\n isMobileNative ||\n (Array.isArray(native) ? native.includes('ios') : native === 'ios')\n\n const isHandledNatively =\n native === true ||\n (isWeb && isWebNative) ||\n (!isWeb && isMobileNative) ||\n (Platform.OS === 'android' && isAndroidNative) ||\n (Platform.OS === 'ios' && isIosNative)\n\n if (isHandledNatively) {\n const nativeToastResult = createNativeToast(title, showOptions ?? {})\n if (typeof nativeToastResult === 'object' && nativeToastResult.nativeToastRef) {\n setLastNativeToastRef(nativeToastResult.nativeToastRef)\n }\n }\n counterRef.current++\n setToast({\n title,\n id: counterRef.current.toString(),\n ...showOptions,\n isHandledNatively,\n })\n return true\n },\n [setToast, options.native]\n )\n const hide = React.useCallback(() => {\n lastNativeToastRef?.close()\n setToast(null)\n }, [setToast, lastNativeToastRef])\n\n const contextValue = useMemo(() => {\n return {\n show,\n hide,\n nativeToast: lastNativeToastRef,\n options,\n }\n }, [show, hide, lastNativeToastRef, JSON.stringify(options || null)])\n\n const currentContextValue = useMemo(() => {}, [])\n\n return (\n <ToastContext.Provider value={contextValue}>\n <ToastCurrentContext.Provider value={toast}>\n {children}\n </ToastCurrentContext.Provider>\n </ToastContext.Provider>\n )\n}\n\nexport type { ToastImperativeProviderProps, ToastNativePlatform, ToastNativeValue }\n"],
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C7DV,EAAsBU,EAAkB,cAAc,CAE1D,CACA,OAAAd,EAAW,UACXE,EAAS,CACP,MAAAI,EACA,GAAIN,EAAW,QAAQ,SAAS,EAChC,GAAGO,EACH,kBAAAM,CACF,CAAC,EACM,EACT,EACA,CAACX,EAAUH,EAAQ,MAAM,CAC3B,EACMgB,EAAO9B,EAAM,YAAY,IAAM,CACnCkB,GAAA,MAAAA,EAAoB,QACpBD,EAAS,IAAI,CACf,EAAG,CAACA,EAAUC,CAAkB,CAAC,EAE3Ba,EAAe5B,EAAQ,KACpB,CACL,KAAAiB,EACA,KAAAU,EACA,YAAaZ,EACb,QAAAJ,CACF,GACC,CAACM,EAAMU,EAAMZ,EAAoB,KAAK,UAAUJ,GAAW,IAAI,CAAC,CAAC,EAE9DkB,EAAsB7B,EAAQ,IAAM,CAAC,EAAG,CAAC,CAAC,EAEhD,OACEL,EAACS,EAAa,SAAb,CAAsB,MAAOwB,EAC5B,SAAAjC,EAACU,EAAoB,SAApB,CAA6B,MAAOQ,EAClC,SAAAH,EACH,EACF,CAEJ",
6
- "names": ["jsx", "isWeb", "React", "createContext", "useContext", "useMemo", "useRef", "Platform", "createNativeToast", "ToastContext", "ToastCurrentContext", "useToastController", "useToastState", "useToast", "ToastImperativeProvider", "children", "options", "counterRef", "toast", "setToast", "lastNativeToastRef", "setLastNativeToastRef", "show", "title", "showOptions", "native", "isWebNative", "isMobileNative", "isAndroidNative", "isIosNative", "isHandledNatively", "nativeToastResult", "hide", "contextValue", "currentContextValue"]
5
+ "mappings": "AAqJM;AArJN,SAAS,aAAa;AACtB,OAAO,SAAS,eAAe,YAAY,SAAS,cAAc;AAClE,SAAS,gBAAgB;AAEzB,SAAS,yBAAyB;AAiDlC,MAAM,eAAe,cAA6B,CAAC,CAAQ;AAC3D,MAAM,sBAAsB,cAAgC,IAAI;AAEzD,MAAM,qBAAqB,MAAM;AACtC,SAAO,WAAW,YAAY;AAChC;AAEO,MAAM,gBAAgB,MAAM;AACjC,SAAO,WAA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AY,MAAM;AACnC,6DAAoB;AACpB,aAAS,IAAI;AAAA,EACf,GAAG,CAAC,UAAU,kBAAkB,CAAC;AAEjC,QAAM,eAAe,QAAQ,MAAM;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA,aAAa;AAAA,MACb;AAAA,IACF;AAAA,EACF,GAAG,CAAC,MAAM,MAAM,oBAAoB,KAAK,UAAU,WAAW,IAAI,CAAC,CAAC;AAEpE,QAAM,sBAAsB,QAAQ,MAAM;AAAA,EAAC,GAAG,CAAC,CAAC;AAEhD,SACE,oBAAC,aAAa,UAAb,EAAsB,OAAO,cAC5B,8BAAC,oBAAoB,UAApB,EAA6B,OAAO,OAClC,UACH,GACF;AAEJ;",
6
+ "names": []
7
7
  }
@@ -1,2 +1,329 @@
1
- import{Fragment as he,jsx as c,jsxs as be}from"react/jsx-runtime";import{useIsPresent as se}from"@tamagui/animate-presence";import{useComposedRefs as re}from"@tamagui/compose-refs";import{Theme as ie,composeEventHandlers as z,isWeb as l,styled as ae,useAnimationDriver as ce,useEvent as ue,useThemeName as le}from"@tamagui/core";import{Dismissable as pe}from"@tamagui/dismissable";import{PortalItem as me}from"@tamagui/portal";import{ThemeableStack as de}from"@tamagui/stacks";import*as s from"react";import{PanResponder as fe}from"react-native";import{TOAST_NAME as H}from"./constants";import{ToastAnnounce as Te}from"./ToastAnnounce";import{Collection as ve,createToastContext as Ee,useToastProviderContext as we}from"./ToastProvider";import{VIEWPORT_PAUSE as K,VIEWPORT_RESUME as $}from"./ToastViewport";const G=ae(de,{name:"ToastImpl",variants:{backgrounded:{true:{backgroundColor:"$color6"}},unstyled:{false:{borderRadius:"$10",paddingHorizontal:"$5",paddingVertical:"$2",marginHorizontal:"auto",marginVertical:"$1"}}},defaultVariants:{backgrounded:!0,unstyled:!1}}),[ye,Pe]=Ee(H,{onClose(){}}),X=s.forwardRef((n,r)=>{const{__scopeToast:i,type:o="foreground",duration:p,open:a,onClose:b,onEscapeKeyDown:u,onPause:v,onResume:E,onSwipeStart:R,onSwipeMove:C,onSwipeCancel:S,onSwipeEnd:M,viewportName:Y,...q}=n,B=se(),t=we(H,i),[m,J]=s.useState(null),Q=re(r,e=>J(e)),D=p||t.duration,V=s.useRef(0),x=s.useRef(D),A=s.useRef(0),{onToastAdd:L,onToastRemove:F}=t,w=ue(()=>{var e;B&&(l&&(m!=null&&m.contains(document.activeElement))&&((e=t.viewport)==null||e.focus()),b())}),y=s.useCallback(e=>{!e||e===1/0||(clearTimeout(A.current),V.current=new Date().getTime(),A.current=setTimeout(w,e))},[w]),d=s.useCallback(()=>{y(x.current),E==null||E()},[E,y]),f=s.useCallback(()=>{const e=new Date().getTime()-V.current;x.current=x.current-e,window.clearTimeout(A.current),v==null||v()},[v]);s.useEffect(()=>{if(!l)return;const e=t.viewport;if(e)return e.addEventListener(K,f),e.addEventListener($,d),()=>{e.removeEventListener(K,f),e.removeEventListener($,d)}},[t.viewport,D,v,E,y]),s.useEffect(()=>{a&&!t.isClosePausedRef.current&&y(D)},[a,D,t.isClosePausedRef,y]),s.useEffect(()=>(L(),()=>F()),[L,F]);const _=s.useMemo(()=>l&&m?U(m):null,[m]),O=["left","right","horizontal"].includes(t.swipeDirection),I=ce();if(!I)throw new Error("Must set animations in tamagui.config.ts");const{useAnimatedNumber:Z,useAnimatedNumberStyle:j}=I,N=Z(0),ee=I.NumberView??I.View,te=j(N,e=>({transform:[O?{translateX:e}:{translateY:e}]})),k=s.useMemo(()=>fe.create({onMoveShouldSetPanResponder:e=>(R==null||R(e),!0),onPanResponderGrant:e=>{l||f==null||f()},onPanResponderMove:(e,{dy:T,dx:P})=>{let g=0,h=0;t.swipeDirection==="horizontal"?h=P:t.swipeDirection==="left"?h=Math.min(0,P):t.swipeDirection==="right"?h=Math.max(0,P):t.swipeDirection==="vertical"?g=T:t.swipeDirection==="up"?g=Math.min(0,T):t.swipeDirection==="down"&&(g=Math.max(0,T)),C==null||C(e);const ne={x:h,y:g};N.setValue(O?h:g,{type:"direct"}),W(ne,t.swipeDirection,t.swipeThreshold)&&(M==null||M(e))},onPanResponderEnd:(e,{dx:T,dy:P})=>{W({x:T,y:P},t.swipeDirection,t.swipeThreshold)||(l||d==null||d(),S==null||S(e),N.setValue(0,{type:"spring"}))}}),[f,d]),oe=le();return be(he,{children:[_&&c(Te,{__scopeToast:i,role:"status","aria-live":o==="foreground"?"assertive":"polite","aria-atomic":!0,children:_}),c(me,{hostName:Y??"default",children:c(ye,{scope:i,onClose:()=>{w()},children:c(pe,{onEscapeKeyDown:z(u,()=>{t.isFocusedToastEscapeKeyDownRef.current||w(),t.isFocusedToastEscapeKeyDownRef.current=!1}),children:c(ie,{forceClassName:!0,name:oe,children:c(ee,{...k==null?void 0:k.panHandlers,style:[{margin:"auto"},te],children:c(ve.ItemSlot,{scope:i,children:c(G,{role:"status","aria-live":"off","aria-atomic":!0,tabIndex:0,"data-state":a?"open":"closed","data-swipe-direction":t.swipeDirection,pointerEvents:"auto",userSelect:"none",...q,ref:Q,...l&&{onKeyDown:z(n.onKeyDown,e=>{e.key==="Escape"&&(u==null||u(e),u==null||u(e),e.defaultPrevented||(t.isFocusedToastEscapeKeyDownRef.current=!0,w()))})}})})})})})},n.id)})]})});X.propTypes={type(n){if(n.type&&!["foreground","background"].includes(n.type)){const r=`Invalid prop \`type\` supplied to \`${H}\`. Expected \`foreground | background\`.`;return new Error(r)}return null}};const W=(n,r,i=0)=>{const o=Math.abs(n.x),p=Math.abs(n.y),a=o>p;return r==="left"||r==="right"||r==="horizontal"?a&&o>i:!a&&p>i};function U(n){if(!l)return"";const r=[];return Array.from(n.childNodes).forEach(o=>{if(o.nodeType===o.TEXT_NODE&&o.textContent&&r.push(o.textContent),ge(o)){const p=o.ariaHidden||o.hidden||o.style.display==="none",a=o.dataset.toastAnnounceExclude==="";if(!p)if(a){const b=o.dataset.toastAnnounceAlt;b&&r.push(b)}else r.push(...U(o))}}),r}function ge(n){return n.nodeType===n.ELEMENT_NODE}export{X as ToastImpl,G as ToastImplFrame,Pe as useToastInteractiveContext};
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useIsPresent } from "@tamagui/animate-presence";
3
+ import { useComposedRefs } from "@tamagui/compose-refs";
4
+ import {
5
+ Theme,
6
+ composeEventHandlers,
7
+ isWeb,
8
+ styled,
9
+ useAnimationDriver,
10
+ useEvent,
11
+ useThemeName
12
+ } from "@tamagui/core";
13
+ import { Dismissable } from "@tamagui/dismissable";
14
+ import { PortalItem } from "@tamagui/portal";
15
+ import { ThemeableStack } from "@tamagui/stacks";
16
+ import * as React from "react";
17
+ import { PanResponder } from "react-native";
18
+ import { TOAST_NAME } from "./constants";
19
+ import { ToastAnnounce } from "./ToastAnnounce";
20
+ import {
21
+ Collection,
22
+ createToastContext,
23
+ useToastProviderContext
24
+ } from "./ToastProvider";
25
+ import { VIEWPORT_PAUSE, VIEWPORT_RESUME } from "./ToastViewport";
26
+ const ToastImplFrame = styled(ThemeableStack, {
27
+ name: "ToastImpl",
28
+ variants: {
29
+ backgrounded: {
30
+ true: {
31
+ backgroundColor: "$color6"
32
+ }
33
+ },
34
+ unstyled: {
35
+ false: {
36
+ borderRadius: "$10",
37
+ paddingHorizontal: "$5",
38
+ paddingVertical: "$2",
39
+ marginHorizontal: "auto",
40
+ marginVertical: "$1"
41
+ }
42
+ }
43
+ },
44
+ defaultVariants: {
45
+ backgrounded: true,
46
+ unstyled: false
47
+ }
48
+ });
49
+ const [ToastInteractiveProvider, useToastInteractiveContext] = createToastContext(
50
+ TOAST_NAME,
51
+ {
52
+ onClose() {
53
+ }
54
+ }
55
+ );
56
+ const ToastImpl = React.forwardRef(
57
+ (props, forwardedRef) => {
58
+ const {
59
+ __scopeToast,
60
+ type = "foreground",
61
+ duration: durationProp,
62
+ open,
63
+ onClose,
64
+ onEscapeKeyDown,
65
+ onPause,
66
+ onResume,
67
+ onSwipeStart,
68
+ onSwipeMove,
69
+ onSwipeCancel,
70
+ onSwipeEnd,
71
+ viewportName,
72
+ ...toastProps
73
+ } = props;
74
+ const isPresent = useIsPresent();
75
+ const context = useToastProviderContext(TOAST_NAME, __scopeToast);
76
+ const [node, setNode] = React.useState(null);
77
+ const composedRefs = useComposedRefs(forwardedRef, (node2) => setNode(node2));
78
+ const duration = durationProp || context.duration;
79
+ const closeTimerStartTimeRef = React.useRef(0);
80
+ const closeTimerRemainingTimeRef = React.useRef(duration);
81
+ const closeTimerRef = React.useRef(0);
82
+ const { onToastAdd, onToastRemove } = context;
83
+ const handleClose = useEvent(() => {
84
+ var _a;
85
+ if (!isPresent) {
86
+ return;
87
+ }
88
+ if (isWeb) {
89
+ const isFocusInToast = node == null ? void 0 : node.contains(document.activeElement);
90
+ if (isFocusInToast)
91
+ (_a = context.viewport) == null ? void 0 : _a.focus();
92
+ }
93
+ onClose();
94
+ });
95
+ const startTimer = React.useCallback(
96
+ (duration2) => {
97
+ if (!duration2 || duration2 === Infinity)
98
+ return;
99
+ clearTimeout(closeTimerRef.current);
100
+ closeTimerStartTimeRef.current = (/* @__PURE__ */ new Date()).getTime();
101
+ closeTimerRef.current = setTimeout(handleClose, duration2);
102
+ },
103
+ [handleClose]
104
+ );
105
+ const handleResume = React.useCallback(() => {
106
+ startTimer(closeTimerRemainingTimeRef.current);
107
+ onResume == null ? void 0 : onResume();
108
+ }, [onResume, startTimer]);
109
+ const handlePause = React.useCallback(() => {
110
+ const elapsedTime = (/* @__PURE__ */ new Date()).getTime() - closeTimerStartTimeRef.current;
111
+ closeTimerRemainingTimeRef.current = closeTimerRemainingTimeRef.current - elapsedTime;
112
+ window.clearTimeout(closeTimerRef.current);
113
+ onPause == null ? void 0 : onPause();
114
+ }, [onPause]);
115
+ React.useEffect(() => {
116
+ if (!isWeb)
117
+ return;
118
+ const viewport = context.viewport;
119
+ if (viewport) {
120
+ viewport.addEventListener(VIEWPORT_PAUSE, handlePause);
121
+ viewport.addEventListener(VIEWPORT_RESUME, handleResume);
122
+ return () => {
123
+ viewport.removeEventListener(VIEWPORT_PAUSE, handlePause);
124
+ viewport.removeEventListener(VIEWPORT_RESUME, handleResume);
125
+ };
126
+ }
127
+ }, [context.viewport, duration, onPause, onResume, startTimer]);
128
+ React.useEffect(() => {
129
+ if (open && !context.isClosePausedRef.current) {
130
+ startTimer(duration);
131
+ }
132
+ }, [open, duration, context.isClosePausedRef, startTimer]);
133
+ React.useEffect(() => {
134
+ onToastAdd();
135
+ return () => onToastRemove();
136
+ }, [onToastAdd, onToastRemove]);
137
+ const announceTextContent = React.useMemo(() => {
138
+ if (!isWeb)
139
+ return null;
140
+ return node ? getAnnounceTextContent(node) : null;
141
+ }, [node]);
142
+ const isHorizontalSwipe = ["left", "right", "horizontal"].includes(
143
+ context.swipeDirection
144
+ );
145
+ const driver = useAnimationDriver();
146
+ if (!driver) {
147
+ throw new Error("Must set animations in tamagui.config.ts");
148
+ }
149
+ const { useAnimatedNumber, useAnimatedNumberStyle } = driver;
150
+ const animatedNumber = useAnimatedNumber(0);
151
+ const AnimatedView = driver["NumberView"] ?? driver.View;
152
+ const animatedStyles = useAnimatedNumberStyle(animatedNumber, (val) => {
153
+ return {
154
+ transform: [isHorizontalSwipe ? { translateX: val } : { translateY: val }]
155
+ };
156
+ });
157
+ const panResponder = React.useMemo(() => {
158
+ return PanResponder.create({
159
+ onMoveShouldSetPanResponder: (e) => {
160
+ onSwipeStart == null ? void 0 : onSwipeStart(e);
161
+ return true;
162
+ },
163
+ onPanResponderGrant: (e) => {
164
+ if (!isWeb) {
165
+ handlePause == null ? void 0 : handlePause();
166
+ }
167
+ },
168
+ onPanResponderMove: (e, { dy, dx }) => {
169
+ let y = 0;
170
+ let x = 0;
171
+ if (context.swipeDirection === "horizontal")
172
+ x = dx;
173
+ else if (context.swipeDirection === "left")
174
+ x = Math.min(0, dx);
175
+ else if (context.swipeDirection === "right")
176
+ x = Math.max(0, dx);
177
+ else if (context.swipeDirection === "vertical")
178
+ y = dy;
179
+ else if (context.swipeDirection === "up")
180
+ y = Math.min(0, dy);
181
+ else if (context.swipeDirection === "down")
182
+ y = Math.max(0, dy);
183
+ onSwipeMove == null ? void 0 : onSwipeMove(e);
184
+ const delta = { x, y };
185
+ animatedNumber.setValue(isHorizontalSwipe ? x : y, { type: "direct" });
186
+ if (isDeltaInDirection(delta, context.swipeDirection, context.swipeThreshold)) {
187
+ onSwipeEnd == null ? void 0 : onSwipeEnd(e);
188
+ }
189
+ },
190
+ onPanResponderEnd: (e, { dx, dy }) => {
191
+ if (!isDeltaInDirection(
192
+ { x: dx, y: dy },
193
+ context.swipeDirection,
194
+ context.swipeThreshold
195
+ )) {
196
+ if (!isWeb) {
197
+ handleResume == null ? void 0 : handleResume();
198
+ }
199
+ onSwipeCancel == null ? void 0 : onSwipeCancel(e);
200
+ animatedNumber.setValue(0, { type: "spring" });
201
+ }
202
+ }
203
+ });
204
+ }, [handlePause, handleResume]);
205
+ const themeName = useThemeName();
206
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
207
+ announceTextContent && /* @__PURE__ */ jsx(
208
+ ToastAnnounce,
209
+ {
210
+ __scopeToast,
211
+ role: "status",
212
+ "aria-live": type === "foreground" ? "assertive" : "polite",
213
+ "aria-atomic": true,
214
+ children: announceTextContent
215
+ }
216
+ ),
217
+ /* @__PURE__ */ jsx(PortalItem, { hostName: viewportName ?? "default", children: /* @__PURE__ */ jsx(
218
+ ToastInteractiveProvider,
219
+ {
220
+ scope: __scopeToast,
221
+ onClose: () => {
222
+ handleClose();
223
+ },
224
+ children: /* @__PURE__ */ jsx(
225
+ Dismissable,
226
+ {
227
+ onEscapeKeyDown: composeEventHandlers(onEscapeKeyDown, () => {
228
+ if (!context.isFocusedToastEscapeKeyDownRef.current) {
229
+ handleClose();
230
+ }
231
+ context.isFocusedToastEscapeKeyDownRef.current = false;
232
+ }),
233
+ children: /* @__PURE__ */ jsx(Theme, { forceClassName: true, name: themeName, children: /* @__PURE__ */ jsx(
234
+ AnimatedView,
235
+ {
236
+ ...panResponder == null ? void 0 : panResponder.panHandlers,
237
+ style: [{ margin: "auto" }, animatedStyles],
238
+ children: /* @__PURE__ */ jsx(Collection.ItemSlot, { scope: __scopeToast, children: /* @__PURE__ */ jsx(
239
+ ToastImplFrame,
240
+ {
241
+ role: "status",
242
+ "aria-live": "off",
243
+ "aria-atomic": true,
244
+ tabIndex: 0,
245
+ "data-state": open ? "open" : "closed",
246
+ "data-swipe-direction": context.swipeDirection,
247
+ pointerEvents: "auto",
248
+ userSelect: "none",
249
+ ...toastProps,
250
+ ref: composedRefs,
251
+ ...isWeb && {
252
+ onKeyDown: composeEventHandlers(
253
+ props.onKeyDown,
254
+ (event) => {
255
+ if (event.key !== "Escape")
256
+ return;
257
+ onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event);
258
+ onEscapeKeyDown == null ? void 0 : onEscapeKeyDown(event);
259
+ if (!event.defaultPrevented) {
260
+ context.isFocusedToastEscapeKeyDownRef.current = true;
261
+ handleClose();
262
+ }
263
+ }
264
+ )
265
+ }
266
+ }
267
+ ) })
268
+ }
269
+ ) })
270
+ }
271
+ )
272
+ },
273
+ props.id
274
+ ) })
275
+ ] });
276
+ }
277
+ );
278
+ ToastImpl.propTypes = {
279
+ type(props) {
280
+ if (props.type && !["foreground", "background"].includes(props.type)) {
281
+ const error = `Invalid prop \`type\` supplied to \`${TOAST_NAME}\`. Expected \`foreground | background\`.`;
282
+ return new Error(error);
283
+ }
284
+ return null;
285
+ }
286
+ };
287
+ const isDeltaInDirection = (delta, direction, threshold = 0) => {
288
+ const deltaX = Math.abs(delta.x);
289
+ const deltaY = Math.abs(delta.y);
290
+ const isDeltaX = deltaX > deltaY;
291
+ if (direction === "left" || direction === "right" || direction === "horizontal") {
292
+ return isDeltaX && deltaX > threshold;
293
+ } else {
294
+ return !isDeltaX && deltaY > threshold;
295
+ }
296
+ };
297
+ function getAnnounceTextContent(container) {
298
+ if (!isWeb)
299
+ return "";
300
+ const textContent = [];
301
+ const childNodes = Array.from(container.childNodes);
302
+ childNodes.forEach((node) => {
303
+ if (node.nodeType === node.TEXT_NODE && node.textContent)
304
+ textContent.push(node.textContent);
305
+ if (isHTMLElement(node)) {
306
+ const isHidden = node.ariaHidden || node.hidden || node.style.display === "none";
307
+ const isExcluded = node.dataset.toastAnnounceExclude === "";
308
+ if (!isHidden) {
309
+ if (isExcluded) {
310
+ const altText = node.dataset.toastAnnounceAlt;
311
+ if (altText)
312
+ textContent.push(altText);
313
+ } else {
314
+ textContent.push(...getAnnounceTextContent(node));
315
+ }
316
+ }
317
+ }
318
+ });
319
+ return textContent;
320
+ }
321
+ function isHTMLElement(node) {
322
+ return node.nodeType === node.ELEMENT_NODE;
323
+ }
324
+ export {
325
+ ToastImpl,
326
+ ToastImplFrame,
327
+ useToastInteractiveContext
328
+ };
2
329
  //# sourceMappingURL=ToastImpl.js.map