@tamagui/toast 1.7.11 → 1.8.0

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 (131) hide show
  1. package/dist/cjs/Toast.js +95 -543
  2. package/dist/cjs/Toast.js.map +3 -3
  3. package/dist/cjs/ToastAnnounce.js +93 -0
  4. package/dist/cjs/ToastAnnounce.js.map +7 -0
  5. package/dist/cjs/ToastImpl.js +354 -0
  6. package/dist/cjs/ToastImpl.js.map +7 -0
  7. package/dist/cjs/ToastProvider.js +109 -0
  8. package/dist/cjs/ToastProvider.js.map +7 -0
  9. package/dist/cjs/ToastViewport.js +307 -0
  10. package/dist/cjs/ToastViewport.js.map +7 -0
  11. package/dist/cjs/constants.js +29 -0
  12. package/dist/cjs/constants.js.map +7 -0
  13. package/dist/cjs/createNativeToast.js +49 -0
  14. package/dist/cjs/createNativeToast.js.map +7 -0
  15. package/dist/cjs/createNativeToast.native.js +37 -0
  16. package/dist/cjs/createNativeToast.native.js.map +7 -0
  17. package/dist/cjs/createToast.js +88 -0
  18. package/dist/cjs/createToast.js.map +7 -0
  19. package/dist/cjs/types.js +17 -0
  20. package/dist/cjs/types.js.map +7 -0
  21. package/dist/esm/Toast.js +101 -531
  22. package/dist/esm/Toast.js.map +3 -3
  23. package/dist/esm/Toast.mjs +101 -531
  24. package/dist/esm/Toast.mjs.map +3 -3
  25. package/dist/esm/ToastAnnounce.js +64 -0
  26. package/dist/esm/ToastAnnounce.js.map +7 -0
  27. package/dist/esm/ToastAnnounce.mjs +64 -0
  28. package/dist/esm/ToastAnnounce.mjs.map +7 -0
  29. package/dist/esm/ToastImpl.js +330 -0
  30. package/dist/esm/ToastImpl.js.map +7 -0
  31. package/dist/esm/ToastImpl.mjs +330 -0
  32. package/dist/esm/ToastImpl.mjs.map +7 -0
  33. package/dist/esm/ToastProvider.js +70 -0
  34. package/dist/esm/ToastProvider.js.map +7 -0
  35. package/dist/esm/ToastProvider.mjs +70 -0
  36. package/dist/esm/ToastProvider.mjs.map +7 -0
  37. package/dist/esm/ToastViewport.js +274 -0
  38. package/dist/esm/ToastViewport.js.map +7 -0
  39. package/dist/esm/ToastViewport.mjs +274 -0
  40. package/dist/esm/ToastViewport.mjs.map +7 -0
  41. package/dist/esm/constants.js +5 -0
  42. package/dist/esm/constants.js.map +7 -0
  43. package/dist/esm/constants.mjs +5 -0
  44. package/dist/esm/constants.mjs.map +7 -0
  45. package/dist/esm/createNativeToast.js +25 -0
  46. package/dist/esm/createNativeToast.js.map +7 -0
  47. package/dist/esm/createNativeToast.mjs +25 -0
  48. package/dist/esm/createNativeToast.mjs.map +7 -0
  49. package/dist/esm/createNativeToast.native.js +13 -0
  50. package/dist/esm/createNativeToast.native.js.map +7 -0
  51. package/dist/esm/createNativeToast.native.mjs +13 -0
  52. package/dist/esm/createNativeToast.native.mjs.map +7 -0
  53. package/dist/esm/createToast.js +54 -0
  54. package/dist/esm/createToast.js.map +7 -0
  55. package/dist/esm/createToast.mjs +54 -0
  56. package/dist/esm/createToast.mjs.map +7 -0
  57. package/dist/esm/types.js +1 -0
  58. package/dist/esm/types.js.map +7 -0
  59. package/dist/esm/types.mjs +1 -0
  60. package/dist/esm/types.mjs.map +7 -0
  61. package/dist/jsx/Toast.js +97 -509
  62. package/dist/jsx/Toast.js.map +3 -3
  63. package/dist/jsx/Toast.mjs +97 -509
  64. package/dist/jsx/Toast.mjs.map +3 -3
  65. package/dist/jsx/ToastAnnounce.js +60 -0
  66. package/dist/jsx/ToastAnnounce.js.map +7 -0
  67. package/dist/jsx/ToastAnnounce.mjs +60 -0
  68. package/dist/jsx/ToastAnnounce.mjs.map +7 -0
  69. package/dist/jsx/ToastImpl.js +309 -0
  70. package/dist/jsx/ToastImpl.js.map +7 -0
  71. package/dist/jsx/ToastImpl.mjs +309 -0
  72. package/dist/jsx/ToastImpl.mjs.map +7 -0
  73. package/dist/jsx/ToastProvider.js +65 -0
  74. package/dist/jsx/ToastProvider.js.map +7 -0
  75. package/dist/jsx/ToastProvider.mjs +65 -0
  76. package/dist/jsx/ToastProvider.mjs.map +7 -0
  77. package/dist/jsx/ToastViewport.js +254 -0
  78. package/dist/jsx/ToastViewport.js.map +7 -0
  79. package/dist/jsx/ToastViewport.mjs +254 -0
  80. package/dist/jsx/ToastViewport.mjs.map +7 -0
  81. package/dist/jsx/constants.js +5 -0
  82. package/dist/jsx/constants.js.map +7 -0
  83. package/dist/jsx/constants.mjs +5 -0
  84. package/dist/jsx/constants.mjs.map +7 -0
  85. package/dist/jsx/createNativeToast.js +25 -0
  86. package/dist/jsx/createNativeToast.js.map +7 -0
  87. package/dist/jsx/createNativeToast.mjs +25 -0
  88. package/dist/jsx/createNativeToast.mjs.map +7 -0
  89. package/dist/jsx/createNativeToast.native.js +13 -0
  90. package/dist/jsx/createNativeToast.native.js.map +7 -0
  91. package/dist/jsx/createNativeToast.native.mjs +13 -0
  92. package/dist/jsx/createNativeToast.native.mjs.map +7 -0
  93. package/dist/jsx/createToast.js +49 -0
  94. package/dist/jsx/createToast.js.map +7 -0
  95. package/dist/jsx/createToast.mjs +49 -0
  96. package/dist/jsx/createToast.mjs.map +7 -0
  97. package/dist/jsx/types.js +1 -0
  98. package/dist/jsx/types.js.map +7 -0
  99. package/dist/jsx/types.mjs +1 -0
  100. package/dist/jsx/types.mjs.map +7 -0
  101. package/package.json +14 -13
  102. package/src/Toast.tsx +139 -908
  103. package/src/ToastAnnounce.tsx +97 -0
  104. package/src/ToastImpl.tsx +439 -0
  105. package/src/ToastProvider.tsx +134 -0
  106. package/src/ToastViewport.tsx +354 -0
  107. package/src/constants.ts +1 -0
  108. package/src/createNativeToast.native.tsx +16 -0
  109. package/src/createNativeToast.tsx +25 -0
  110. package/src/createToast.tsx +93 -0
  111. package/src/types.ts +21 -0
  112. package/types/Toast.d.ts +429 -106
  113. package/types/Toast.d.ts.map +1 -1
  114. package/types/ToastAnnounce.d.ts +35 -0
  115. package/types/ToastAnnounce.d.ts.map +1 -0
  116. package/types/ToastImpl.d.ts +267 -0
  117. package/types/ToastImpl.d.ts.map +1 -0
  118. package/types/ToastProvider.d.ts +80 -0
  119. package/types/ToastProvider.d.ts.map +1 -0
  120. package/types/ToastViewport.d.ts +115 -0
  121. package/types/ToastViewport.d.ts.map +1 -0
  122. package/types/constants.d.ts +2 -0
  123. package/types/constants.d.ts.map +1 -0
  124. package/types/createNativeToast.d.ts +3 -0
  125. package/types/createNativeToast.d.ts.map +1 -0
  126. package/types/createNativeToast.native.d.ts +3 -0
  127. package/types/createNativeToast.native.d.ts.map +1 -0
  128. package/types/createToast.d.ts +42 -0
  129. package/types/createToast.d.ts.map +1 -0
  130. package/types/types.d.ts +17 -0
  131. package/types/types.d.ts.map +1 -0
@@ -0,0 +1,254 @@
1
+ import { AnimatePresence } from "@tamagui/animate-presence";
2
+ import { useComposedRefs } from "@tamagui/compose-refs";
3
+ import { isWeb, styled } from "@tamagui/core";
4
+ import { PortalHost } from "@tamagui/portal";
5
+ import { YStack } from "@tamagui/stacks";
6
+ import { VisuallyHidden } from "@tamagui/visually-hidden";
7
+ import * as React from "react";
8
+ import {
9
+ Collection,
10
+ useCollection,
11
+ useToastProviderContext
12
+ } from "./ToastProvider";
13
+ const VIEWPORT_NAME = "ToastViewport";
14
+ const VIEWPORT_DEFAULT_HOTKEY = ["F8"];
15
+ const VIEWPORT_PAUSE = "toast.viewportPause";
16
+ const VIEWPORT_RESUME = "toast.viewportResume";
17
+ const ToastViewportWrapperFrame = styled(YStack, {
18
+ name: "ViewportWrapper",
19
+ pointerEvents: "box-none",
20
+ top: 0,
21
+ bottom: 0,
22
+ left: 0,
23
+ right: 0,
24
+ // @ts-ignore
25
+ position: isWeb ? "fixed" : "absolute",
26
+ maxWidth: "100%",
27
+ tabIndex: 0,
28
+ zIndex: 1e5
29
+ });
30
+ const ToastViewportFrame = styled(YStack, {
31
+ name: VIEWPORT_NAME,
32
+ pointerEvents: "box-none",
33
+ // @ts-ignore
34
+ position: isWeb ? "fixed" : "absolute",
35
+ maxWidth: "100%"
36
+ });
37
+ const ToastViewport = React.forwardRef(
38
+ (props, forwardedRef) => {
39
+ const {
40
+ __scopeToast,
41
+ hotkey = VIEWPORT_DEFAULT_HOTKEY,
42
+ label = "Notifications ({hotkey})",
43
+ name,
44
+ ...viewportProps
45
+ } = props;
46
+ const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast);
47
+ const getItems = useCollection(__scopeToast);
48
+ const headFocusProxyRef = React.useRef(null);
49
+ const tailFocusProxyRef = React.useRef(null);
50
+ const wrapperRef = React.useRef(null);
51
+ const ref = React.useRef(null);
52
+ const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange);
53
+ const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
54
+ const hasToasts = context.toastCount > 0;
55
+ React.useEffect(() => {
56
+ if (!isWeb)
57
+ return;
58
+ const handleKeyDown = (event) => {
59
+ const isHotkeyPressed = hotkey.every(
60
+ (key) => event[key] || event.code === key
61
+ );
62
+ if (isHotkeyPressed)
63
+ ref.current?.focus();
64
+ console.log(isHotkeyPressed);
65
+ };
66
+ document.addEventListener("keydown", handleKeyDown);
67
+ return () => {
68
+ document.removeEventListener("keydown", handleKeyDown);
69
+ };
70
+ }, [hotkey]);
71
+ React.useEffect(() => {
72
+ if (!isWeb)
73
+ return;
74
+ const wrapper = wrapperRef.current;
75
+ const viewport = ref.current;
76
+ if (hasToasts && wrapper && viewport) {
77
+ const handlePause = () => {
78
+ if (!context.isClosePausedRef.current) {
79
+ const pauseEvent = new CustomEvent(VIEWPORT_PAUSE);
80
+ viewport.dispatchEvent(pauseEvent);
81
+ context.isClosePausedRef.current = true;
82
+ }
83
+ };
84
+ const handleResume = () => {
85
+ if (context.isClosePausedRef.current) {
86
+ const resumeEvent = new CustomEvent(VIEWPORT_RESUME);
87
+ viewport.dispatchEvent(resumeEvent);
88
+ context.isClosePausedRef.current = false;
89
+ }
90
+ };
91
+ const handleFocusOutResume = (event) => {
92
+ const isFocusMovingOutside = !wrapper.contains(
93
+ event.relatedTarget
94
+ );
95
+ if (isFocusMovingOutside)
96
+ handleResume();
97
+ };
98
+ const handlePointerLeaveResume = () => {
99
+ const isFocusInside = wrapper.contains(document.activeElement);
100
+ if (!isFocusInside)
101
+ handleResume();
102
+ };
103
+ wrapper.addEventListener("focusin", handlePause);
104
+ wrapper.addEventListener("focusout", handleFocusOutResume);
105
+ wrapper.addEventListener("pointermove", handlePause);
106
+ wrapper.addEventListener("pointerleave", handlePointerLeaveResume);
107
+ window.addEventListener("blur", handlePause);
108
+ window.addEventListener("focus", handleResume);
109
+ return () => {
110
+ wrapper.removeEventListener("focusin", handlePause);
111
+ wrapper.removeEventListener("focusout", handleFocusOutResume);
112
+ wrapper.removeEventListener("pointermove", handlePause);
113
+ wrapper.removeEventListener("pointerleave", handlePointerLeaveResume);
114
+ window.removeEventListener("blur", handlePause);
115
+ window.removeEventListener("focus", handleResume);
116
+ };
117
+ }
118
+ }, [hasToasts, context.isClosePausedRef]);
119
+ const getSortedTabbableCandidates = React.useCallback(
120
+ ({ tabbingDirection }) => {
121
+ const toastItems = getItems();
122
+ const tabbableCandidates = toastItems.map((toastItem) => {
123
+ const toastNode = toastItem.ref.current;
124
+ const toastTabbableCandidates = [toastNode, ...getTabbableCandidates(toastNode)];
125
+ return tabbingDirection === "forwards" ? toastTabbableCandidates : toastTabbableCandidates.reverse();
126
+ });
127
+ return (tabbingDirection === "forwards" ? tabbableCandidates.reverse() : tabbableCandidates).flat();
128
+ },
129
+ [getItems]
130
+ );
131
+ React.useEffect(() => {
132
+ if (!isWeb)
133
+ return;
134
+ const viewport = ref.current;
135
+ if (viewport) {
136
+ const handleKeyDown = (event) => {
137
+ const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
138
+ const isTabKey = event.key === "Tab" && !isMetaKey;
139
+ if (isTabKey) {
140
+ const focusedElement = document.activeElement;
141
+ const isTabbingBackwards = event.shiftKey;
142
+ const targetIsViewport = event.target === viewport;
143
+ if (targetIsViewport && isTabbingBackwards) {
144
+ headFocusProxyRef.current?.focus();
145
+ return;
146
+ }
147
+ const tabbingDirection = isTabbingBackwards ? "backwards" : "forwards";
148
+ const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection });
149
+ const index = sortedCandidates.findIndex(
150
+ (candidate) => candidate === focusedElement
151
+ );
152
+ if (focusFirst(sortedCandidates.slice(index + 1))) {
153
+ event.preventDefault();
154
+ } else {
155
+ isTabbingBackwards ? headFocusProxyRef.current?.focus() : tailFocusProxyRef.current?.focus();
156
+ }
157
+ }
158
+ };
159
+ viewport.addEventListener("keydown", handleKeyDown);
160
+ return () => viewport.removeEventListener("keydown", handleKeyDown);
161
+ }
162
+ }, [getItems, getSortedTabbableCandidates]);
163
+ return <ToastViewportWrapperFrame
164
+ ref={wrapperRef}
165
+ role="region"
166
+ aria-label={label.replace("{hotkey}", hotkeyLabel)}
167
+ tabIndex={-1}
168
+ >
169
+ {hasToasts && <FocusProxy
170
+ ref={headFocusProxyRef}
171
+ onFocusFromOutsideViewport={() => {
172
+ const tabbableCandidates = getSortedTabbableCandidates({
173
+ tabbingDirection: "forwards"
174
+ });
175
+ focusFirst(tabbableCandidates);
176
+ }}
177
+ />}
178
+ <Collection.Slot scope={__scopeToast}><ToastViewportFrame ref={composedRefs} {...viewportProps}><PortalHost
179
+ render={(children) => <AnimatePresence>{children}</AnimatePresence>}
180
+ name={name ?? "default"}
181
+ /></ToastViewportFrame></Collection.Slot>
182
+ {hasToasts && <FocusProxy
183
+ ref={tailFocusProxyRef}
184
+ onFocusFromOutsideViewport={() => {
185
+ const tabbableCandidates = getSortedTabbableCandidates({
186
+ tabbingDirection: "backwards"
187
+ });
188
+ focusFirst(tabbableCandidates);
189
+ }}
190
+ />}
191
+ </ToastViewportWrapperFrame>;
192
+ }
193
+ );
194
+ ToastViewport.displayName = VIEWPORT_NAME;
195
+ const FOCUS_PROXY_NAME = "ToastFocusProxy";
196
+ const FocusProxy = React.forwardRef(
197
+ (props, forwardedRef) => {
198
+ const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props;
199
+ const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast);
200
+ return <VisuallyHidden
201
+ aria-hidden
202
+ tabIndex={0}
203
+ {...proxyProps}
204
+ ref={forwardedRef}
205
+ position={isWeb ? "fixed" : "absolute"}
206
+ onFocus={(event) => {
207
+ if (!isWeb)
208
+ return;
209
+ const prevFocusedElement = event.relatedTarget;
210
+ const isFocusFromOutsideViewport = !context.viewport?.contains(
211
+ prevFocusedElement
212
+ );
213
+ if (isFocusFromOutsideViewport)
214
+ onFocusFromOutsideViewport();
215
+ }}
216
+ />;
217
+ }
218
+ );
219
+ FocusProxy.displayName = FOCUS_PROXY_NAME;
220
+ function focusFirst(candidates) {
221
+ if (!isWeb)
222
+ return;
223
+ const previouslyFocusedElement = document.activeElement;
224
+ return candidates.some((candidate) => {
225
+ if (candidate === previouslyFocusedElement)
226
+ return true;
227
+ candidate.focus();
228
+ return document.activeElement !== previouslyFocusedElement;
229
+ });
230
+ }
231
+ function getTabbableCandidates(container) {
232
+ if (!isWeb)
233
+ return [];
234
+ const containerHtml = container;
235
+ const nodes = [];
236
+ const walker = document.createTreeWalker(containerHtml, NodeFilter.SHOW_ELEMENT, {
237
+ acceptNode: (node) => {
238
+ const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
239
+ if (node.disabled || node.hidden || isHiddenInput)
240
+ return NodeFilter.FILTER_SKIP;
241
+ return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
242
+ }
243
+ });
244
+ while (walker.nextNode())
245
+ nodes.push(walker.currentNode);
246
+ return nodes;
247
+ }
248
+ export {
249
+ ToastViewport,
250
+ VIEWPORT_DEFAULT_HOTKEY,
251
+ VIEWPORT_PAUSE,
252
+ VIEWPORT_RESUME
253
+ };
254
+ //# sourceMappingURL=ToastViewport.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/ToastViewport.tsx"],
4
+ "sourcesContent": ["import { AnimatePresence } from '@tamagui/animate-presence'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport { GetProps, TamaguiElement, isWeb, styled } from '@tamagui/core'\nimport { PortalHost } from '@tamagui/portal'\nimport { YStack } from '@tamagui/stacks'\nimport { VisuallyHidden } from '@tamagui/visually-hidden'\nimport * as React from 'react'\n\nimport {\n Collection,\n ScopedProps,\n useCollection,\n useToastProviderContext,\n} from './ToastProvider'\n\nconst VIEWPORT_NAME = 'ToastViewport'\nconst VIEWPORT_DEFAULT_HOTKEY = ['F8']\nconst VIEWPORT_PAUSE = 'toast.viewportPause'\nconst VIEWPORT_RESUME = 'toast.viewportResume'\n\nconst ToastViewportWrapperFrame = styled(YStack, {\n name: 'ViewportWrapper',\n pointerEvents: 'box-none',\n top: 0,\n bottom: 0,\n left: 0,\n right: 0,\n // @ts-ignore\n position: isWeb ? 'fixed' : 'absolute',\n maxWidth: '100%',\n tabIndex: 0,\n zIndex: 100000,\n})\n\nconst ToastViewportFrame = styled(YStack, {\n name: VIEWPORT_NAME,\n pointerEvents: 'box-none',\n // @ts-ignore\n position: isWeb ? 'fixed' : 'absolute',\n maxWidth: '100%',\n})\ntype ToastViewportFrameProps = GetProps<typeof ToastViewportFrame>\ntype ToastViewportProps = ToastViewportFrameProps & {\n /**\n * The keys to use as the keyboard shortcut that will move focus to the toast viewport.\n * @defaultValue ['F8']\n */\n hotkey?: string[]\n /**\n * An author-localized label for the toast viewport to provide context for screen reader users\n * when navigating page landmarks. The available `{hotkey}` placeholder will be replaced for you.\n * @defaultValue 'Notifications ({hotkey})'\n */\n label?: string\n /**\n * Used to reference the viewport if you want to have multiple viewports in the same provider.\n */\n name?: string\n}\n\nconst ToastViewport = React.forwardRef<HTMLDivElement, ToastViewportProps>(\n (props: ScopedProps<ToastViewportProps>, forwardedRef) => {\n const {\n __scopeToast,\n hotkey = VIEWPORT_DEFAULT_HOTKEY,\n label = 'Notifications ({hotkey})',\n name,\n ...viewportProps\n } = props\n const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast)\n const getItems = useCollection(__scopeToast)\n const headFocusProxyRef = React.useRef<FocusProxyElement>(null)\n const tailFocusProxyRef = React.useRef<FocusProxyElement>(null)\n const wrapperRef = React.useRef<HTMLDivElement>(null)\n const ref = React.useRef<HTMLDivElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange)\n const hotkeyLabel = hotkey.join('+').replace(/Key/g, '').replace(/Digit/g, '')\n const hasToasts = context.toastCount > 0\n\n React.useEffect(() => {\n if (!isWeb) return\n const handleKeyDown = (event: KeyboardEvent) => {\n // we use `event.code` as it is consistent regardless of meta keys that were pressed.\n // for example, `event.key` for `Control+Alt+t` is `\u2020` and `t !== \u2020`\n const isHotkeyPressed = hotkey.every(\n (key) => (event as any)[key] || event.code === key\n )\n if (isHotkeyPressed) ref.current?.focus()\n console.log(isHotkeyPressed)\n }\n document.addEventListener('keydown', handleKeyDown)\n return () => {\n document.removeEventListener('keydown', handleKeyDown)\n }\n }, [hotkey])\n\n React.useEffect(() => {\n if (!isWeb) return\n const wrapper = wrapperRef.current\n const viewport = ref.current\n if (hasToasts && wrapper && viewport) {\n const handlePause = () => {\n if (!context.isClosePausedRef.current) {\n const pauseEvent = new CustomEvent(VIEWPORT_PAUSE)\n viewport.dispatchEvent(pauseEvent)\n context.isClosePausedRef.current = true\n }\n }\n\n const handleResume = () => {\n if (context.isClosePausedRef.current) {\n const resumeEvent = new CustomEvent(VIEWPORT_RESUME)\n viewport.dispatchEvent(resumeEvent)\n context.isClosePausedRef.current = false\n }\n }\n\n const handleFocusOutResume = (event: FocusEvent) => {\n const isFocusMovingOutside = !wrapper.contains(\n event.relatedTarget as HTMLElement\n )\n if (isFocusMovingOutside) handleResume()\n }\n\n const handlePointerLeaveResume = () => {\n const isFocusInside = wrapper.contains(document.activeElement)\n if (!isFocusInside) handleResume()\n }\n // Toasts are not in the viewport React tree so we need to bind DOM events\n\n wrapper.addEventListener('focusin', handlePause)\n wrapper.addEventListener('focusout', handleFocusOutResume)\n wrapper.addEventListener('pointermove', handlePause)\n wrapper.addEventListener('pointerleave', handlePointerLeaveResume)\n window.addEventListener('blur', handlePause)\n window.addEventListener('focus', handleResume)\n return () => {\n wrapper.removeEventListener('focusin', handlePause)\n wrapper.removeEventListener('focusout', handleFocusOutResume)\n wrapper.removeEventListener('pointermove', handlePause)\n wrapper.removeEventListener('pointerleave', handlePointerLeaveResume)\n window.removeEventListener('blur', handlePause)\n window.removeEventListener('focus', handleResume)\n }\n }\n }, [hasToasts, context.isClosePausedRef])\n\n const getSortedTabbableCandidates = React.useCallback(\n ({ tabbingDirection }: { tabbingDirection: 'forwards' | 'backwards' }) => {\n const toastItems = getItems()\n const tabbableCandidates = toastItems.map((toastItem) => {\n const toastNode = toastItem.ref.current!\n const toastTabbableCandidates = [toastNode, ...getTabbableCandidates(toastNode)]\n return tabbingDirection === 'forwards'\n ? toastTabbableCandidates\n : toastTabbableCandidates.reverse()\n })\n return (\n tabbingDirection === 'forwards'\n ? tabbableCandidates.reverse()\n : tabbableCandidates\n ).flat()\n },\n [getItems]\n )\n\n React.useEffect(() => {\n if (!isWeb) return\n const viewport = ref.current\n // We programmatically manage tabbing as we are unable to influence\n // the source order with portals, this allows us to reverse the\n // tab order so that it runs from most recent toast to least\n if (viewport) {\n const handleKeyDown = (event: KeyboardEvent) => {\n const isMetaKey = event.altKey || event.ctrlKey || event.metaKey\n const isTabKey = event.key === 'Tab' && !isMetaKey\n\n if (isTabKey) {\n const focusedElement = document.activeElement\n const isTabbingBackwards = event.shiftKey\n const targetIsViewport = event.target === viewport\n\n // If we're back tabbing after jumping to the viewport then we simply\n // proxy focus out to the preceding document\n if (targetIsViewport && isTabbingBackwards) {\n headFocusProxyRef.current?.focus()\n return\n }\n\n const tabbingDirection = isTabbingBackwards ? 'backwards' : 'forwards'\n const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection })\n const index = sortedCandidates.findIndex(\n (candidate) => candidate === focusedElement\n )\n if (focusFirst(sortedCandidates.slice(index + 1))) {\n event.preventDefault()\n } else {\n // If we can't focus that means we're at the edges so we\n // proxy to the corresponding exit point and let the browser handle\n // tab/shift+tab keypress and implicitly pass focus to the next valid element in the document\n isTabbingBackwards\n ? headFocusProxyRef.current?.focus()\n : tailFocusProxyRef.current?.focus()\n }\n }\n }\n\n // Toasts are not in the viewport React tree so we need to bind DOM events\n viewport.addEventListener('keydown', handleKeyDown)\n return () => viewport.removeEventListener('keydown', handleKeyDown)\n }\n }, [getItems, getSortedTabbableCandidates])\n\n return (\n <ToastViewportWrapperFrame\n ref={wrapperRef}\n role=\"region\"\n aria-label={label.replace('{hotkey}', hotkeyLabel)}\n // // Ensure virtual cursor from landmarks menus triggers focus/blur for pause/resume\n tabIndex={-1}\n // // incase list has size when empty (e.g. padding), we remove pointer events so\n // // it doesn't prevent interactions with page elements that it overlays\n // pointerEvents={hasToasts ? undefined : 'none'}\n >\n {hasToasts && (\n <FocusProxy\n ref={headFocusProxyRef}\n onFocusFromOutsideViewport={() => {\n const tabbableCandidates = getSortedTabbableCandidates({\n tabbingDirection: 'forwards',\n })\n focusFirst(tabbableCandidates)\n }}\n />\n )}\n {/**\n * tabindex on the the list so that it can be focused when items are removed. we focus\n * the list instead of the viewport so it announces number of items remaining.\n */}\n <Collection.Slot scope={__scopeToast}>\n <ToastViewportFrame ref={composedRefs} {...viewportProps}>\n <PortalHost\n // TODO: user should be able to control this prop to customize AnimatePresence\n render={(children) => <AnimatePresence>{children}</AnimatePresence>}\n name={name ?? 'default'}\n />\n </ToastViewportFrame>\n </Collection.Slot>\n {hasToasts && (\n <FocusProxy\n ref={tailFocusProxyRef}\n onFocusFromOutsideViewport={() => {\n const tabbableCandidates = getSortedTabbableCandidates({\n tabbingDirection: 'backwards',\n })\n focusFirst(tabbableCandidates)\n }}\n />\n )}\n </ToastViewportWrapperFrame>\n )\n }\n)\n\nToastViewport.displayName = VIEWPORT_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst FOCUS_PROXY_NAME = 'ToastFocusProxy'\n\ntype FocusProxyElement = React.ElementRef<typeof VisuallyHidden>\ntype VisuallyHiddenProps = GetProps<typeof VisuallyHidden>\ninterface FocusProxyProps extends VisuallyHiddenProps {\n onFocusFromOutsideViewport(): void\n}\n\nconst FocusProxy = React.forwardRef<FocusProxyElement, ScopedProps<FocusProxyProps>>(\n (props, forwardedRef) => {\n const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props\n const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast)\n\n return (\n <VisuallyHidden\n aria-hidden\n tabIndex={0}\n {...proxyProps}\n ref={forwardedRef}\n // Avoid page scrolling when focus is on the focus proxy\n position={isWeb ? ('fixed' as any) : 'absolute'}\n onFocus={(event) => {\n if (!isWeb) return\n const prevFocusedElement = event.relatedTarget as HTMLElement | null\n const isFocusFromOutsideViewport = !(context.viewport as HTMLElement)?.contains(\n prevFocusedElement\n )\n if (isFocusFromOutsideViewport) onFocusFromOutsideViewport()\n }}\n />\n )\n }\n)\n\nFocusProxy.displayName = FOCUS_PROXY_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction focusFirst(candidates: TamaguiElement[]) {\n if (!isWeb) return\n const previouslyFocusedElement = document.activeElement\n return candidates.some((candidate) => {\n // if focus is already where we want to go, we don't want to keep going through the candidates\n if (candidate === previouslyFocusedElement) return true\n candidate.focus()\n return document.activeElement !== previouslyFocusedElement\n })\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: TamaguiElement) {\n if (!isWeb) return []\n const containerHtml = container as HTMLElement\n const nodes: HTMLElement[] = []\n const walker = document.createTreeWalker(containerHtml, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden'\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP\n },\n })\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement)\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes\n}\n\nexport {\n ToastViewport,\n ToastViewportProps,\n VIEWPORT_DEFAULT_HOTKEY,\n VIEWPORT_PAUSE,\n VIEWPORT_RESUME,\n}\n"],
5
+ "mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,uBAAuB;AAChC,SAAmC,OAAO,cAAc;AACxD,SAAS,kBAAkB;AAC3B,SAAS,cAAc;AACvB,SAAS,sBAAsB;AAC/B,YAAY,WAAW;AAEvB;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AAEP,MAAM,gBAAgB;AACtB,MAAM,0BAA0B,CAAC,IAAI;AACrC,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AAExB,MAAM,4BAA4B,OAAO,QAAQ;AAAA,EAC/C,MAAM;AAAA,EACN,eAAe;AAAA,EACf,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AAAA;AAAA,EAEP,UAAU,QAAQ,UAAU;AAAA,EAC5B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,QAAQ;AACV,CAAC;AAED,MAAM,qBAAqB,OAAO,QAAQ;AAAA,EACxC,MAAM;AAAA,EACN,eAAe;AAAA;AAAA,EAEf,UAAU,QAAQ,UAAU;AAAA,EAC5B,UAAU;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,SAAS,kBAAkB;AAAA,EACpC,CAAC;AACH;AAYA,SAAS,sBAAsB,WAA2B;AACxD,MAAI,CAAC;AAAO,WAAO,CAAC;AACpB,QAAM,gBAAgB;AACtB,QAAM,QAAuB,CAAC;AAC9B,QAAM,SAAS,SAAS,iBAAiB,eAAe,WAAW,cAAc;AAAA,IAC/E,YAAY,CAAC,SAAc;AACzB,YAAM,gBAAgB,KAAK,YAAY,WAAW,KAAK,SAAS;AAChE,UAAI,KAAK,YAAY,KAAK,UAAU;AAAe,eAAO,WAAW;AAIrE,aAAO,KAAK,YAAY,IAAI,WAAW,gBAAgB,WAAW;AAAA,IACpE;AAAA,EACF,CAAC;AACD,SAAO,OAAO,SAAS;AAAG,UAAM,KAAK,OAAO,WAA0B;AAGtE,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,254 @@
1
+ import { AnimatePresence } from "@tamagui/animate-presence";
2
+ import { useComposedRefs } from "@tamagui/compose-refs";
3
+ import { isWeb, styled } from "@tamagui/core";
4
+ import { PortalHost } from "@tamagui/portal";
5
+ import { YStack } from "@tamagui/stacks";
6
+ import { VisuallyHidden } from "@tamagui/visually-hidden";
7
+ import * as React from "react";
8
+ import {
9
+ Collection,
10
+ useCollection,
11
+ useToastProviderContext
12
+ } from "./ToastProvider";
13
+ const VIEWPORT_NAME = "ToastViewport";
14
+ const VIEWPORT_DEFAULT_HOTKEY = ["F8"];
15
+ const VIEWPORT_PAUSE = "toast.viewportPause";
16
+ const VIEWPORT_RESUME = "toast.viewportResume";
17
+ const ToastViewportWrapperFrame = styled(YStack, {
18
+ name: "ViewportWrapper",
19
+ pointerEvents: "box-none",
20
+ top: 0,
21
+ bottom: 0,
22
+ left: 0,
23
+ right: 0,
24
+ // @ts-ignore
25
+ position: isWeb ? "fixed" : "absolute",
26
+ maxWidth: "100%",
27
+ tabIndex: 0,
28
+ zIndex: 1e5
29
+ });
30
+ const ToastViewportFrame = styled(YStack, {
31
+ name: VIEWPORT_NAME,
32
+ pointerEvents: "box-none",
33
+ // @ts-ignore
34
+ position: isWeb ? "fixed" : "absolute",
35
+ maxWidth: "100%"
36
+ });
37
+ const ToastViewport = React.forwardRef(
38
+ (props, forwardedRef) => {
39
+ const {
40
+ __scopeToast,
41
+ hotkey = VIEWPORT_DEFAULT_HOTKEY,
42
+ label = "Notifications ({hotkey})",
43
+ name,
44
+ ...viewportProps
45
+ } = props;
46
+ const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast);
47
+ const getItems = useCollection(__scopeToast);
48
+ const headFocusProxyRef = React.useRef(null);
49
+ const tailFocusProxyRef = React.useRef(null);
50
+ const wrapperRef = React.useRef(null);
51
+ const ref = React.useRef(null);
52
+ const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange);
53
+ const hotkeyLabel = hotkey.join("+").replace(/Key/g, "").replace(/Digit/g, "");
54
+ const hasToasts = context.toastCount > 0;
55
+ React.useEffect(() => {
56
+ if (!isWeb)
57
+ return;
58
+ const handleKeyDown = (event) => {
59
+ const isHotkeyPressed = hotkey.every(
60
+ (key) => event[key] || event.code === key
61
+ );
62
+ if (isHotkeyPressed)
63
+ ref.current?.focus();
64
+ console.log(isHotkeyPressed);
65
+ };
66
+ document.addEventListener("keydown", handleKeyDown);
67
+ return () => {
68
+ document.removeEventListener("keydown", handleKeyDown);
69
+ };
70
+ }, [hotkey]);
71
+ React.useEffect(() => {
72
+ if (!isWeb)
73
+ return;
74
+ const wrapper = wrapperRef.current;
75
+ const viewport = ref.current;
76
+ if (hasToasts && wrapper && viewport) {
77
+ const handlePause = () => {
78
+ if (!context.isClosePausedRef.current) {
79
+ const pauseEvent = new CustomEvent(VIEWPORT_PAUSE);
80
+ viewport.dispatchEvent(pauseEvent);
81
+ context.isClosePausedRef.current = true;
82
+ }
83
+ };
84
+ const handleResume = () => {
85
+ if (context.isClosePausedRef.current) {
86
+ const resumeEvent = new CustomEvent(VIEWPORT_RESUME);
87
+ viewport.dispatchEvent(resumeEvent);
88
+ context.isClosePausedRef.current = false;
89
+ }
90
+ };
91
+ const handleFocusOutResume = (event) => {
92
+ const isFocusMovingOutside = !wrapper.contains(
93
+ event.relatedTarget
94
+ );
95
+ if (isFocusMovingOutside)
96
+ handleResume();
97
+ };
98
+ const handlePointerLeaveResume = () => {
99
+ const isFocusInside = wrapper.contains(document.activeElement);
100
+ if (!isFocusInside)
101
+ handleResume();
102
+ };
103
+ wrapper.addEventListener("focusin", handlePause);
104
+ wrapper.addEventListener("focusout", handleFocusOutResume);
105
+ wrapper.addEventListener("pointermove", handlePause);
106
+ wrapper.addEventListener("pointerleave", handlePointerLeaveResume);
107
+ window.addEventListener("blur", handlePause);
108
+ window.addEventListener("focus", handleResume);
109
+ return () => {
110
+ wrapper.removeEventListener("focusin", handlePause);
111
+ wrapper.removeEventListener("focusout", handleFocusOutResume);
112
+ wrapper.removeEventListener("pointermove", handlePause);
113
+ wrapper.removeEventListener("pointerleave", handlePointerLeaveResume);
114
+ window.removeEventListener("blur", handlePause);
115
+ window.removeEventListener("focus", handleResume);
116
+ };
117
+ }
118
+ }, [hasToasts, context.isClosePausedRef]);
119
+ const getSortedTabbableCandidates = React.useCallback(
120
+ ({ tabbingDirection }) => {
121
+ const toastItems = getItems();
122
+ const tabbableCandidates = toastItems.map((toastItem) => {
123
+ const toastNode = toastItem.ref.current;
124
+ const toastTabbableCandidates = [toastNode, ...getTabbableCandidates(toastNode)];
125
+ return tabbingDirection === "forwards" ? toastTabbableCandidates : toastTabbableCandidates.reverse();
126
+ });
127
+ return (tabbingDirection === "forwards" ? tabbableCandidates.reverse() : tabbableCandidates).flat();
128
+ },
129
+ [getItems]
130
+ );
131
+ React.useEffect(() => {
132
+ if (!isWeb)
133
+ return;
134
+ const viewport = ref.current;
135
+ if (viewport) {
136
+ const handleKeyDown = (event) => {
137
+ const isMetaKey = event.altKey || event.ctrlKey || event.metaKey;
138
+ const isTabKey = event.key === "Tab" && !isMetaKey;
139
+ if (isTabKey) {
140
+ const focusedElement = document.activeElement;
141
+ const isTabbingBackwards = event.shiftKey;
142
+ const targetIsViewport = event.target === viewport;
143
+ if (targetIsViewport && isTabbingBackwards) {
144
+ headFocusProxyRef.current?.focus();
145
+ return;
146
+ }
147
+ const tabbingDirection = isTabbingBackwards ? "backwards" : "forwards";
148
+ const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection });
149
+ const index = sortedCandidates.findIndex(
150
+ (candidate) => candidate === focusedElement
151
+ );
152
+ if (focusFirst(sortedCandidates.slice(index + 1))) {
153
+ event.preventDefault();
154
+ } else {
155
+ isTabbingBackwards ? headFocusProxyRef.current?.focus() : tailFocusProxyRef.current?.focus();
156
+ }
157
+ }
158
+ };
159
+ viewport.addEventListener("keydown", handleKeyDown);
160
+ return () => viewport.removeEventListener("keydown", handleKeyDown);
161
+ }
162
+ }, [getItems, getSortedTabbableCandidates]);
163
+ return <ToastViewportWrapperFrame
164
+ ref={wrapperRef}
165
+ role="region"
166
+ aria-label={label.replace("{hotkey}", hotkeyLabel)}
167
+ tabIndex={-1}
168
+ >
169
+ {hasToasts && <FocusProxy
170
+ ref={headFocusProxyRef}
171
+ onFocusFromOutsideViewport={() => {
172
+ const tabbableCandidates = getSortedTabbableCandidates({
173
+ tabbingDirection: "forwards"
174
+ });
175
+ focusFirst(tabbableCandidates);
176
+ }}
177
+ />}
178
+ <Collection.Slot scope={__scopeToast}><ToastViewportFrame ref={composedRefs} {...viewportProps}><PortalHost
179
+ render={(children) => <AnimatePresence>{children}</AnimatePresence>}
180
+ name={name ?? "default"}
181
+ /></ToastViewportFrame></Collection.Slot>
182
+ {hasToasts && <FocusProxy
183
+ ref={tailFocusProxyRef}
184
+ onFocusFromOutsideViewport={() => {
185
+ const tabbableCandidates = getSortedTabbableCandidates({
186
+ tabbingDirection: "backwards"
187
+ });
188
+ focusFirst(tabbableCandidates);
189
+ }}
190
+ />}
191
+ </ToastViewportWrapperFrame>;
192
+ }
193
+ );
194
+ ToastViewport.displayName = VIEWPORT_NAME;
195
+ const FOCUS_PROXY_NAME = "ToastFocusProxy";
196
+ const FocusProxy = React.forwardRef(
197
+ (props, forwardedRef) => {
198
+ const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props;
199
+ const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast);
200
+ return <VisuallyHidden
201
+ aria-hidden
202
+ tabIndex={0}
203
+ {...proxyProps}
204
+ ref={forwardedRef}
205
+ position={isWeb ? "fixed" : "absolute"}
206
+ onFocus={(event) => {
207
+ if (!isWeb)
208
+ return;
209
+ const prevFocusedElement = event.relatedTarget;
210
+ const isFocusFromOutsideViewport = !context.viewport?.contains(
211
+ prevFocusedElement
212
+ );
213
+ if (isFocusFromOutsideViewport)
214
+ onFocusFromOutsideViewport();
215
+ }}
216
+ />;
217
+ }
218
+ );
219
+ FocusProxy.displayName = FOCUS_PROXY_NAME;
220
+ function focusFirst(candidates) {
221
+ if (!isWeb)
222
+ return;
223
+ const previouslyFocusedElement = document.activeElement;
224
+ return candidates.some((candidate) => {
225
+ if (candidate === previouslyFocusedElement)
226
+ return true;
227
+ candidate.focus();
228
+ return document.activeElement !== previouslyFocusedElement;
229
+ });
230
+ }
231
+ function getTabbableCandidates(container) {
232
+ if (!isWeb)
233
+ return [];
234
+ const containerHtml = container;
235
+ const nodes = [];
236
+ const walker = document.createTreeWalker(containerHtml, NodeFilter.SHOW_ELEMENT, {
237
+ acceptNode: (node) => {
238
+ const isHiddenInput = node.tagName === "INPUT" && node.type === "hidden";
239
+ if (node.disabled || node.hidden || isHiddenInput)
240
+ return NodeFilter.FILTER_SKIP;
241
+ return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP;
242
+ }
243
+ });
244
+ while (walker.nextNode())
245
+ nodes.push(walker.currentNode);
246
+ return nodes;
247
+ }
248
+ export {
249
+ ToastViewport,
250
+ VIEWPORT_DEFAULT_HOTKEY,
251
+ VIEWPORT_PAUSE,
252
+ VIEWPORT_RESUME
253
+ };
254
+ //# sourceMappingURL=ToastViewport.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/ToastViewport.tsx"],
4
+ "sourcesContent": ["import { AnimatePresence } from '@tamagui/animate-presence'\nimport { useComposedRefs } from '@tamagui/compose-refs'\nimport { GetProps, TamaguiElement, isWeb, styled } from '@tamagui/core'\nimport { PortalHost } from '@tamagui/portal'\nimport { YStack } from '@tamagui/stacks'\nimport { VisuallyHidden } from '@tamagui/visually-hidden'\nimport * as React from 'react'\n\nimport {\n Collection,\n ScopedProps,\n useCollection,\n useToastProviderContext,\n} from './ToastProvider'\n\nconst VIEWPORT_NAME = 'ToastViewport'\nconst VIEWPORT_DEFAULT_HOTKEY = ['F8']\nconst VIEWPORT_PAUSE = 'toast.viewportPause'\nconst VIEWPORT_RESUME = 'toast.viewportResume'\n\nconst ToastViewportWrapperFrame = styled(YStack, {\n name: 'ViewportWrapper',\n pointerEvents: 'box-none',\n top: 0,\n bottom: 0,\n left: 0,\n right: 0,\n // @ts-ignore\n position: isWeb ? 'fixed' : 'absolute',\n maxWidth: '100%',\n tabIndex: 0,\n zIndex: 100000,\n})\n\nconst ToastViewportFrame = styled(YStack, {\n name: VIEWPORT_NAME,\n pointerEvents: 'box-none',\n // @ts-ignore\n position: isWeb ? 'fixed' : 'absolute',\n maxWidth: '100%',\n})\ntype ToastViewportFrameProps = GetProps<typeof ToastViewportFrame>\ntype ToastViewportProps = ToastViewportFrameProps & {\n /**\n * The keys to use as the keyboard shortcut that will move focus to the toast viewport.\n * @defaultValue ['F8']\n */\n hotkey?: string[]\n /**\n * An author-localized label for the toast viewport to provide context for screen reader users\n * when navigating page landmarks. The available `{hotkey}` placeholder will be replaced for you.\n * @defaultValue 'Notifications ({hotkey})'\n */\n label?: string\n /**\n * Used to reference the viewport if you want to have multiple viewports in the same provider.\n */\n name?: string\n}\n\nconst ToastViewport = React.forwardRef<HTMLDivElement, ToastViewportProps>(\n (props: ScopedProps<ToastViewportProps>, forwardedRef) => {\n const {\n __scopeToast,\n hotkey = VIEWPORT_DEFAULT_HOTKEY,\n label = 'Notifications ({hotkey})',\n name,\n ...viewportProps\n } = props\n const context = useToastProviderContext(VIEWPORT_NAME, __scopeToast)\n const getItems = useCollection(__scopeToast)\n const headFocusProxyRef = React.useRef<FocusProxyElement>(null)\n const tailFocusProxyRef = React.useRef<FocusProxyElement>(null)\n const wrapperRef = React.useRef<HTMLDivElement>(null)\n const ref = React.useRef<HTMLDivElement>(null)\n const composedRefs = useComposedRefs(forwardedRef, ref, context.onViewportChange)\n const hotkeyLabel = hotkey.join('+').replace(/Key/g, '').replace(/Digit/g, '')\n const hasToasts = context.toastCount > 0\n\n React.useEffect(() => {\n if (!isWeb) return\n const handleKeyDown = (event: KeyboardEvent) => {\n // we use `event.code` as it is consistent regardless of meta keys that were pressed.\n // for example, `event.key` for `Control+Alt+t` is `\u2020` and `t !== \u2020`\n const isHotkeyPressed = hotkey.every(\n (key) => (event as any)[key] || event.code === key\n )\n if (isHotkeyPressed) ref.current?.focus()\n console.log(isHotkeyPressed)\n }\n document.addEventListener('keydown', handleKeyDown)\n return () => {\n document.removeEventListener('keydown', handleKeyDown)\n }\n }, [hotkey])\n\n React.useEffect(() => {\n if (!isWeb) return\n const wrapper = wrapperRef.current\n const viewport = ref.current\n if (hasToasts && wrapper && viewport) {\n const handlePause = () => {\n if (!context.isClosePausedRef.current) {\n const pauseEvent = new CustomEvent(VIEWPORT_PAUSE)\n viewport.dispatchEvent(pauseEvent)\n context.isClosePausedRef.current = true\n }\n }\n\n const handleResume = () => {\n if (context.isClosePausedRef.current) {\n const resumeEvent = new CustomEvent(VIEWPORT_RESUME)\n viewport.dispatchEvent(resumeEvent)\n context.isClosePausedRef.current = false\n }\n }\n\n const handleFocusOutResume = (event: FocusEvent) => {\n const isFocusMovingOutside = !wrapper.contains(\n event.relatedTarget as HTMLElement\n )\n if (isFocusMovingOutside) handleResume()\n }\n\n const handlePointerLeaveResume = () => {\n const isFocusInside = wrapper.contains(document.activeElement)\n if (!isFocusInside) handleResume()\n }\n // Toasts are not in the viewport React tree so we need to bind DOM events\n\n wrapper.addEventListener('focusin', handlePause)\n wrapper.addEventListener('focusout', handleFocusOutResume)\n wrapper.addEventListener('pointermove', handlePause)\n wrapper.addEventListener('pointerleave', handlePointerLeaveResume)\n window.addEventListener('blur', handlePause)\n window.addEventListener('focus', handleResume)\n return () => {\n wrapper.removeEventListener('focusin', handlePause)\n wrapper.removeEventListener('focusout', handleFocusOutResume)\n wrapper.removeEventListener('pointermove', handlePause)\n wrapper.removeEventListener('pointerleave', handlePointerLeaveResume)\n window.removeEventListener('blur', handlePause)\n window.removeEventListener('focus', handleResume)\n }\n }\n }, [hasToasts, context.isClosePausedRef])\n\n const getSortedTabbableCandidates = React.useCallback(\n ({ tabbingDirection }: { tabbingDirection: 'forwards' | 'backwards' }) => {\n const toastItems = getItems()\n const tabbableCandidates = toastItems.map((toastItem) => {\n const toastNode = toastItem.ref.current!\n const toastTabbableCandidates = [toastNode, ...getTabbableCandidates(toastNode)]\n return tabbingDirection === 'forwards'\n ? toastTabbableCandidates\n : toastTabbableCandidates.reverse()\n })\n return (\n tabbingDirection === 'forwards'\n ? tabbableCandidates.reverse()\n : tabbableCandidates\n ).flat()\n },\n [getItems]\n )\n\n React.useEffect(() => {\n if (!isWeb) return\n const viewport = ref.current\n // We programmatically manage tabbing as we are unable to influence\n // the source order with portals, this allows us to reverse the\n // tab order so that it runs from most recent toast to least\n if (viewport) {\n const handleKeyDown = (event: KeyboardEvent) => {\n const isMetaKey = event.altKey || event.ctrlKey || event.metaKey\n const isTabKey = event.key === 'Tab' && !isMetaKey\n\n if (isTabKey) {\n const focusedElement = document.activeElement\n const isTabbingBackwards = event.shiftKey\n const targetIsViewport = event.target === viewport\n\n // If we're back tabbing after jumping to the viewport then we simply\n // proxy focus out to the preceding document\n if (targetIsViewport && isTabbingBackwards) {\n headFocusProxyRef.current?.focus()\n return\n }\n\n const tabbingDirection = isTabbingBackwards ? 'backwards' : 'forwards'\n const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection })\n const index = sortedCandidates.findIndex(\n (candidate) => candidate === focusedElement\n )\n if (focusFirst(sortedCandidates.slice(index + 1))) {\n event.preventDefault()\n } else {\n // If we can't focus that means we're at the edges so we\n // proxy to the corresponding exit point and let the browser handle\n // tab/shift+tab keypress and implicitly pass focus to the next valid element in the document\n isTabbingBackwards\n ? headFocusProxyRef.current?.focus()\n : tailFocusProxyRef.current?.focus()\n }\n }\n }\n\n // Toasts are not in the viewport React tree so we need to bind DOM events\n viewport.addEventListener('keydown', handleKeyDown)\n return () => viewport.removeEventListener('keydown', handleKeyDown)\n }\n }, [getItems, getSortedTabbableCandidates])\n\n return (\n <ToastViewportWrapperFrame\n ref={wrapperRef}\n role=\"region\"\n aria-label={label.replace('{hotkey}', hotkeyLabel)}\n // // Ensure virtual cursor from landmarks menus triggers focus/blur for pause/resume\n tabIndex={-1}\n // // incase list has size when empty (e.g. padding), we remove pointer events so\n // // it doesn't prevent interactions with page elements that it overlays\n // pointerEvents={hasToasts ? undefined : 'none'}\n >\n {hasToasts && (\n <FocusProxy\n ref={headFocusProxyRef}\n onFocusFromOutsideViewport={() => {\n const tabbableCandidates = getSortedTabbableCandidates({\n tabbingDirection: 'forwards',\n })\n focusFirst(tabbableCandidates)\n }}\n />\n )}\n {/**\n * tabindex on the the list so that it can be focused when items are removed. we focus\n * the list instead of the viewport so it announces number of items remaining.\n */}\n <Collection.Slot scope={__scopeToast}>\n <ToastViewportFrame ref={composedRefs} {...viewportProps}>\n <PortalHost\n // TODO: user should be able to control this prop to customize AnimatePresence\n render={(children) => <AnimatePresence>{children}</AnimatePresence>}\n name={name ?? 'default'}\n />\n </ToastViewportFrame>\n </Collection.Slot>\n {hasToasts && (\n <FocusProxy\n ref={tailFocusProxyRef}\n onFocusFromOutsideViewport={() => {\n const tabbableCandidates = getSortedTabbableCandidates({\n tabbingDirection: 'backwards',\n })\n focusFirst(tabbableCandidates)\n }}\n />\n )}\n </ToastViewportWrapperFrame>\n )\n }\n)\n\nToastViewport.displayName = VIEWPORT_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst FOCUS_PROXY_NAME = 'ToastFocusProxy'\n\ntype FocusProxyElement = React.ElementRef<typeof VisuallyHidden>\ntype VisuallyHiddenProps = GetProps<typeof VisuallyHidden>\ninterface FocusProxyProps extends VisuallyHiddenProps {\n onFocusFromOutsideViewport(): void\n}\n\nconst FocusProxy = React.forwardRef<FocusProxyElement, ScopedProps<FocusProxyProps>>(\n (props, forwardedRef) => {\n const { __scopeToast, onFocusFromOutsideViewport, ...proxyProps } = props\n const context = useToastProviderContext(FOCUS_PROXY_NAME, __scopeToast)\n\n return (\n <VisuallyHidden\n aria-hidden\n tabIndex={0}\n {...proxyProps}\n ref={forwardedRef}\n // Avoid page scrolling when focus is on the focus proxy\n position={isWeb ? ('fixed' as any) : 'absolute'}\n onFocus={(event) => {\n if (!isWeb) return\n const prevFocusedElement = event.relatedTarget as HTMLElement | null\n const isFocusFromOutsideViewport = !(context.viewport as HTMLElement)?.contains(\n prevFocusedElement\n )\n if (isFocusFromOutsideViewport) onFocusFromOutsideViewport()\n }}\n />\n )\n }\n)\n\nFocusProxy.displayName = FOCUS_PROXY_NAME\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction focusFirst(candidates: TamaguiElement[]) {\n if (!isWeb) return\n const previouslyFocusedElement = document.activeElement\n return candidates.some((candidate) => {\n // if focus is already where we want to go, we don't want to keep going through the candidates\n if (candidate === previouslyFocusedElement) return true\n candidate.focus()\n return document.activeElement !== previouslyFocusedElement\n })\n}\n\n/**\n * Returns a list of potential tabbable candidates.\n *\n * NOTE: This is only a close approximation. For example it doesn't take into account cases like when\n * elements are not visible. This cannot be worked out easily by just reading a property, but rather\n * necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.\n *\n * See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker\n * Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1\n */\nfunction getTabbableCandidates(container: TamaguiElement) {\n if (!isWeb) return []\n const containerHtml = container as HTMLElement\n const nodes: HTMLElement[] = []\n const walker = document.createTreeWalker(containerHtml, NodeFilter.SHOW_ELEMENT, {\n acceptNode: (node: any) => {\n const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden'\n if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP\n // `.tabIndex` is not the same as the `tabindex` attribute. It works on the\n // runtime's understanding of tabbability, so this automatically accounts\n // for any kind of element that could be tabbed to.\n return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP\n },\n })\n while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement)\n // we do not take into account the order of nodes with positive `tabIndex` as it\n // hinders accessibility to have tab order different from visual order.\n return nodes\n}\n\nexport {\n ToastViewport,\n ToastViewportProps,\n VIEWPORT_DEFAULT_HOTKEY,\n VIEWPORT_PAUSE,\n VIEWPORT_RESUME,\n}\n"],
5
+ "mappings": "AAAA,SAAS,uBAAuB;AAChC,SAAS,uBAAuB;AAChC,SAAmC,OAAO,cAAc;AACxD,SAAS,kBAAkB;AAC3B,SAAS,cAAc;AACvB,SAAS,sBAAsB;AAC/B,YAAY,WAAW;AAEvB;AAAA,EACE;AAAA,EAEA;AAAA,EACA;AAAA,OACK;AAEP,MAAM,gBAAgB;AACtB,MAAM,0BAA0B,CAAC,IAAI;AACrC,MAAM,iBAAiB;AACvB,MAAM,kBAAkB;AAExB,MAAM,4BAA4B,OAAO,QAAQ;AAAA,EAC/C,MAAM;AAAA,EACN,eAAe;AAAA,EACf,KAAK;AAAA,EACL,QAAQ;AAAA,EACR,MAAM;AAAA,EACN,OAAO;AAAA;AAAA,EAEP,UAAU,QAAQ,UAAU;AAAA,EAC5B,UAAU;AAAA,EACV,UAAU;AAAA,EACV,QAAQ;AACV,CAAC;AAED,MAAM,qBAAqB,OAAO,QAAQ;AAAA,EACxC,MAAM;AAAA,E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AAc;AAEpC,QAAI,cAAc;AAA0B,aAAO;AACnD,cAAU,MAAM;AAChB,WAAO,SAAS,kBAAkB;AAAA,EACpC,CAAC;AACH;AAYA,SAAS,sBAAsB,WAA2B;AACxD,MAAI,CAAC;AAAO,WAAO,CAAC;AACpB,QAAM,gBAAgB;AACtB,QAAM,QAAuB,CAAC;AAC9B,QAAM,SAAS,SAAS,iBAAiB,eAAe,WAAW,cAAc;AAAA,IAC/E,YAAY,CAAC,SAAc;AACzB,YAAM,gBAAgB,KAAK,YAAY,WAAW,KAAK,SAAS;AAChE,UAAI,KAAK,YAAY,KAAK,UAAU;AAAe,eAAO,WAAW;AAIrE,aAAO,KAAK,YAAY,IAAI,WAAW,gBAAgB,WAAW;AAAA,IACpE;AAAA,EACF,CAAC;AACD,SAAO,OAAO,SAAS;AAAG,UAAM,KAAK,OAAO,WAA0B;AAGtE,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ const TOAST_NAME = "Toast";
2
+ export {
3
+ TOAST_NAME
4
+ };
5
+ //# sourceMappingURL=constants.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/constants.ts"],
4
+ "sourcesContent": ["export const TOAST_NAME = 'Toast'\n"],
5
+ "mappings": "AAAO,MAAM,aAAa;",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ const TOAST_NAME = "Toast";
2
+ export {
3
+ TOAST_NAME
4
+ };
5
+ //# sourceMappingURL=constants.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/constants.ts"],
4
+ "sourcesContent": ["export const TOAST_NAME = 'Toast'\n"],
5
+ "mappings": "AAAO,MAAM,aAAa;",
6
+ "names": []
7
+ }
@@ -0,0 +1,25 @@
1
+ const createNativeToast = (title, { message }) => {
2
+ if (!("Notification" in window)) {
3
+ throw Error("This browser does not support desktop notification");
4
+ }
5
+ if (Notification.permission === "denied")
6
+ return {};
7
+ const showNotification = () => {
8
+ new Notification(title, {
9
+ body: message
10
+ });
11
+ };
12
+ if (Notification.permission === "granted") {
13
+ showNotification();
14
+ } else {
15
+ Notification.requestPermission().then((permission) => {
16
+ if (permission === "granted") {
17
+ showNotification();
18
+ }
19
+ });
20
+ }
21
+ };
22
+ export {
23
+ createNativeToast
24
+ };
25
+ //# sourceMappingURL=createNativeToast.js.map