@hanzogui/toast 2.0.0-rc.41-hanzoai.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/Toast.cjs +178 -0
- package/dist/cjs/Toast.native.js +181 -0
- package/dist/cjs/Toast.native.js.map +1 -0
- package/dist/cjs/ToastAnnounce.cjs +103 -0
- package/dist/cjs/ToastAnnounce.native.js +111 -0
- package/dist/cjs/ToastAnnounce.native.js.map +1 -0
- package/dist/cjs/ToastComposable.cjs +933 -0
- package/dist/cjs/ToastComposable.native.js +1025 -0
- package/dist/cjs/ToastComposable.native.js.map +1 -0
- package/dist/cjs/ToastImperative.cjs +119 -0
- package/dist/cjs/ToastImperative.native.js +130 -0
- package/dist/cjs/ToastImperative.native.js.map +1 -0
- package/dist/cjs/ToastImpl.cjs +354 -0
- package/dist/cjs/ToastImpl.native.js +364 -0
- package/dist/cjs/ToastImpl.native.js.map +1 -0
- package/dist/cjs/ToastItemFrame.cjs +143 -0
- package/dist/cjs/ToastItemFrame.native.js +148 -0
- package/dist/cjs/ToastItemFrame.native.js.map +1 -0
- package/dist/cjs/ToastPortal.cjs +49 -0
- package/dist/cjs/ToastPortal.native.js +52 -0
- package/dist/cjs/ToastPortal.native.js.map +1 -0
- package/dist/cjs/ToastProvider.cjs +150 -0
- package/dist/cjs/ToastProvider.native.js +161 -0
- package/dist/cjs/ToastProvider.native.js.map +1 -0
- package/dist/cjs/ToastState.cjs +311 -0
- package/dist/cjs/ToastState.native.js +324 -0
- package/dist/cjs/ToastState.native.js.map +1 -0
- package/dist/cjs/ToastViewport.cjs +319 -0
- package/dist/cjs/ToastViewport.native.js +343 -0
- package/dist/cjs/ToastViewport.native.js.map +1 -0
- package/dist/cjs/Toaster.cjs +87 -0
- package/dist/cjs/Toaster.native.js +91 -0
- package/dist/cjs/Toaster.native.js.map +1 -0
- package/dist/cjs/constants.cjs +30 -0
- package/dist/cjs/constants.native.js +33 -0
- package/dist/cjs/constants.native.js.map +1 -0
- package/dist/cjs/createNativeToast.cjs +59 -0
- package/dist/cjs/createNativeToast.native.js +60 -0
- package/dist/cjs/createNativeToast.native.js.map +1 -0
- package/dist/cjs/dispatchNativeToast.cjs +47 -0
- package/dist/cjs/dispatchNativeToast.native.js +52 -0
- package/dist/cjs/dispatchNativeToast.native.js.map +1 -0
- package/dist/cjs/index.cjs +20 -0
- package/dist/cjs/index.native.js +23 -0
- package/dist/cjs/index.native.js.map +1 -0
- package/dist/cjs/types.cjs +18 -0
- package/dist/cjs/types.native.js +21 -0
- package/dist/cjs/types.native.js.map +1 -0
- package/dist/cjs/useAnimatedDragGesture.cjs +214 -0
- package/dist/cjs/useAnimatedDragGesture.native.js +224 -0
- package/dist/cjs/useAnimatedDragGesture.native.js.map +1 -0
- package/dist/cjs/useReducedMotion.cjs +74 -0
- package/dist/cjs/useReducedMotion.native.js +83 -0
- package/dist/cjs/useReducedMotion.native.js.map +1 -0
- package/dist/cjs/useToastAnimations.cjs +294 -0
- package/dist/cjs/useToastAnimations.native.js +304 -0
- package/dist/cjs/useToastAnimations.native.js.map +1 -0
- package/dist/cjs/v2.cjs +36 -0
- package/dist/cjs/v2.native.js +39 -0
- package/dist/cjs/v2.native.js.map +1 -0
- package/dist/esm/Toast.mjs +137 -0
- package/dist/esm/Toast.mjs.map +1 -0
- package/dist/esm/Toast.native.js +137 -0
- package/dist/esm/Toast.native.js.map +1 -0
- package/dist/esm/ToastAnnounce.mjs +66 -0
- package/dist/esm/ToastAnnounce.mjs.map +1 -0
- package/dist/esm/ToastAnnounce.native.js +71 -0
- package/dist/esm/ToastAnnounce.native.js.map +1 -0
- package/dist/esm/ToastComposable.mjs +895 -0
- package/dist/esm/ToastComposable.mjs.map +1 -0
- package/dist/esm/ToastComposable.native.js +984 -0
- package/dist/esm/ToastComposable.native.js.map +1 -0
- package/dist/esm/ToastImperative.mjs +80 -0
- package/dist/esm/ToastImperative.mjs.map +1 -0
- package/dist/esm/ToastImperative.native.js +88 -0
- package/dist/esm/ToastImperative.native.js.map +1 -0
- package/dist/esm/ToastImpl.mjs +316 -0
- package/dist/esm/ToastImpl.mjs.map +1 -0
- package/dist/esm/ToastImpl.native.js +323 -0
- package/dist/esm/ToastImpl.native.js.map +1 -0
- package/dist/esm/ToastItemFrame.mjs +114 -0
- package/dist/esm/ToastItemFrame.mjs.map +1 -0
- package/dist/esm/ToastItemFrame.native.js +116 -0
- package/dist/esm/ToastItemFrame.native.js.map +1 -0
- package/dist/esm/ToastPortal.mjs +24 -0
- package/dist/esm/ToastPortal.mjs.map +1 -0
- package/dist/esm/ToastPortal.native.js +24 -0
- package/dist/esm/ToastPortal.native.js.map +1 -0
- package/dist/esm/ToastProvider.mjs +110 -0
- package/dist/esm/ToastProvider.mjs.map +1 -0
- package/dist/esm/ToastProvider.native.js +118 -0
- package/dist/esm/ToastProvider.native.js.map +1 -0
- package/dist/esm/ToastState.mjs +285 -0
- package/dist/esm/ToastState.mjs.map +1 -0
- package/dist/esm/ToastState.native.js +295 -0
- package/dist/esm/ToastState.native.js.map +1 -0
- package/dist/esm/ToastViewport.mjs +280 -0
- package/dist/esm/ToastViewport.mjs.map +1 -0
- package/dist/esm/ToastViewport.native.js +301 -0
- package/dist/esm/ToastViewport.native.js.map +1 -0
- package/dist/esm/Toaster.mjs +51 -0
- package/dist/esm/Toaster.mjs.map +1 -0
- package/dist/esm/Toaster.native.js +52 -0
- package/dist/esm/Toaster.native.js.map +1 -0
- package/dist/esm/constants.mjs +4 -0
- package/dist/esm/constants.mjs.map +1 -0
- package/dist/esm/constants.native.js +4 -0
- package/dist/esm/constants.native.js.map +1 -0
- package/dist/esm/createNativeToast.mjs +32 -0
- package/dist/esm/createNativeToast.mjs.map +1 -0
- package/dist/esm/createNativeToast.native.js +30 -0
- package/dist/esm/createNativeToast.native.js.map +1 -0
- package/dist/esm/dispatchNativeToast.mjs +22 -0
- package/dist/esm/dispatchNativeToast.mjs.map +1 -0
- package/dist/esm/dispatchNativeToast.native.js +24 -0
- package/dist/esm/dispatchNativeToast.native.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/index.mjs.map +1 -0
- package/dist/esm/index.native.js +2 -0
- package/dist/esm/index.native.js.map +1 -0
- package/dist/esm/types.mjs +2 -0
- package/dist/esm/types.mjs.map +1 -0
- package/dist/esm/types.native.js +2 -0
- package/dist/esm/types.native.js.map +1 -0
- package/dist/esm/useAnimatedDragGesture.mjs +178 -0
- package/dist/esm/useAnimatedDragGesture.mjs.map +1 -0
- package/dist/esm/useAnimatedDragGesture.native.js +185 -0
- package/dist/esm/useAnimatedDragGesture.native.js.map +1 -0
- package/dist/esm/useReducedMotion.mjs +38 -0
- package/dist/esm/useReducedMotion.mjs.map +1 -0
- package/dist/esm/useReducedMotion.native.js +44 -0
- package/dist/esm/useReducedMotion.native.js.map +1 -0
- package/dist/esm/useToastAnimations.mjs +258 -0
- package/dist/esm/useToastAnimations.mjs.map +1 -0
- package/dist/esm/useToastAnimations.native.js +265 -0
- package/dist/esm/useToastAnimations.native.js.map +1 -0
- package/dist/esm/v2.mjs +6 -0
- package/dist/esm/v2.mjs.map +1 -0
- package/dist/esm/v2.native.js +6 -0
- package/dist/esm/v2.native.js.map +1 -0
- package/dist/jsx/Toast.mjs +137 -0
- package/dist/jsx/Toast.mjs.map +1 -0
- package/dist/jsx/Toast.native.js +181 -0
- package/dist/jsx/Toast.native.js.map +1 -0
- package/dist/jsx/ToastAnnounce.mjs +66 -0
- package/dist/jsx/ToastAnnounce.mjs.map +1 -0
- package/dist/jsx/ToastAnnounce.native.js +111 -0
- package/dist/jsx/ToastAnnounce.native.js.map +1 -0
- package/dist/jsx/ToastComposable.mjs +895 -0
- package/dist/jsx/ToastComposable.mjs.map +1 -0
- package/dist/jsx/ToastComposable.native.js +1025 -0
- package/dist/jsx/ToastComposable.native.js.map +1 -0
- package/dist/jsx/ToastImperative.mjs +80 -0
- package/dist/jsx/ToastImperative.mjs.map +1 -0
- package/dist/jsx/ToastImperative.native.js +130 -0
- package/dist/jsx/ToastImperative.native.js.map +1 -0
- package/dist/jsx/ToastImpl.mjs +316 -0
- package/dist/jsx/ToastImpl.mjs.map +1 -0
- package/dist/jsx/ToastImpl.native.js +364 -0
- package/dist/jsx/ToastImpl.native.js.map +1 -0
- package/dist/jsx/ToastItemFrame.mjs +114 -0
- package/dist/jsx/ToastItemFrame.mjs.map +1 -0
- package/dist/jsx/ToastItemFrame.native.js +148 -0
- package/dist/jsx/ToastItemFrame.native.js.map +1 -0
- package/dist/jsx/ToastPortal.mjs +24 -0
- package/dist/jsx/ToastPortal.mjs.map +1 -0
- package/dist/jsx/ToastPortal.native.js +52 -0
- package/dist/jsx/ToastPortal.native.js.map +1 -0
- package/dist/jsx/ToastProvider.mjs +110 -0
- package/dist/jsx/ToastProvider.mjs.map +1 -0
- package/dist/jsx/ToastProvider.native.js +161 -0
- package/dist/jsx/ToastProvider.native.js.map +1 -0
- package/dist/jsx/ToastState.mjs +285 -0
- package/dist/jsx/ToastState.mjs.map +1 -0
- package/dist/jsx/ToastState.native.js +324 -0
- package/dist/jsx/ToastState.native.js.map +1 -0
- package/dist/jsx/ToastViewport.mjs +280 -0
- package/dist/jsx/ToastViewport.mjs.map +1 -0
- package/dist/jsx/ToastViewport.native.js +343 -0
- package/dist/jsx/ToastViewport.native.js.map +1 -0
- package/dist/jsx/Toaster.mjs +51 -0
- package/dist/jsx/Toaster.mjs.map +1 -0
- package/dist/jsx/Toaster.native.js +91 -0
- package/dist/jsx/Toaster.native.js.map +1 -0
- package/dist/jsx/constants.mjs +4 -0
- package/dist/jsx/constants.mjs.map +1 -0
- package/dist/jsx/constants.native.js +33 -0
- package/dist/jsx/constants.native.js.map +1 -0
- package/dist/jsx/createNativeToast.mjs +32 -0
- package/dist/jsx/createNativeToast.mjs.map +1 -0
- package/dist/jsx/createNativeToast.native.js +60 -0
- package/dist/jsx/createNativeToast.native.js.map +1 -0
- package/dist/jsx/dispatchNativeToast.mjs +22 -0
- package/dist/jsx/dispatchNativeToast.mjs.map +1 -0
- package/dist/jsx/dispatchNativeToast.native.js +52 -0
- package/dist/jsx/dispatchNativeToast.native.js.map +1 -0
- package/dist/jsx/index.js +2 -0
- package/dist/jsx/index.js.map +1 -0
- package/dist/jsx/index.mjs +2 -0
- package/dist/jsx/index.mjs.map +1 -0
- package/dist/jsx/index.native.js +23 -0
- package/dist/jsx/index.native.js.map +1 -0
- package/dist/jsx/types.mjs +2 -0
- package/dist/jsx/types.mjs.map +1 -0
- package/dist/jsx/types.native.js +21 -0
- package/dist/jsx/types.native.js.map +1 -0
- package/dist/jsx/useAnimatedDragGesture.mjs +178 -0
- package/dist/jsx/useAnimatedDragGesture.mjs.map +1 -0
- package/dist/jsx/useAnimatedDragGesture.native.js +224 -0
- package/dist/jsx/useAnimatedDragGesture.native.js.map +1 -0
- package/dist/jsx/useReducedMotion.mjs +38 -0
- package/dist/jsx/useReducedMotion.mjs.map +1 -0
- package/dist/jsx/useReducedMotion.native.js +83 -0
- package/dist/jsx/useReducedMotion.native.js.map +1 -0
- package/dist/jsx/useToastAnimations.mjs +258 -0
- package/dist/jsx/useToastAnimations.mjs.map +1 -0
- package/dist/jsx/useToastAnimations.native.js +304 -0
- package/dist/jsx/useToastAnimations.native.js.map +1 -0
- package/dist/jsx/v2.mjs +6 -0
- package/dist/jsx/v2.mjs.map +1 -0
- package/dist/jsx/v2.native.js +39 -0
- package/dist/jsx/v2.native.js.map +1 -0
- package/package.json +83 -0
- package/src/Toast.tsx +219 -0
- package/src/ToastAnnounce.tsx +103 -0
- package/src/ToastComposable.tsx +1380 -0
- package/src/ToastImperative.tsx +192 -0
- package/src/ToastImpl.tsx +514 -0
- package/src/ToastItemFrame.tsx +136 -0
- package/src/ToastPortal.tsx +19 -0
- package/src/ToastProvider.tsx +204 -0
- package/src/ToastState.ts +398 -0
- package/src/ToastViewport.tsx +430 -0
- package/src/Toaster.tsx +181 -0
- package/src/constants.ts +2 -0
- package/src/createNativeToast.native.tsx +33 -0
- package/src/createNativeToast.tsx +42 -0
- package/src/dispatchNativeToast.ts +43 -0
- package/src/index.ts +3 -0
- package/src/types.ts +71 -0
- package/src/useAnimatedDragGesture.native.ts +255 -0
- package/src/useAnimatedDragGesture.ts +319 -0
- package/src/useReducedMotion.ts +59 -0
- package/src/useToastAnimations.ts +377 -0
- package/src/v2.ts +31 -0
- package/types/Toast.d.ts +84 -0
- package/types/Toast.d.ts.map +1 -0
- package/types/ToastAnnounce.d.ts +18 -0
- package/types/ToastAnnounce.d.ts.map +1 -0
- package/types/ToastComposable.d.ts +199 -0
- package/types/ToastComposable.d.ts.map +1 -0
- package/types/ToastImperative.d.ts +95 -0
- package/types/ToastImperative.d.ts.map +1 -0
- package/types/ToastImpl.d.ts +109 -0
- package/types/ToastImpl.d.ts.map +1 -0
- package/types/ToastItemFrame.d.ts +25 -0
- package/types/ToastItemFrame.d.ts.map +1 -0
- package/types/ToastPortal.d.ts +8 -0
- package/types/ToastPortal.d.ts.map +1 -0
- package/types/ToastProvider.d.ts +92 -0
- package/types/ToastProvider.d.ts.map +1 -0
- package/types/ToastState.d.ts +179 -0
- package/types/ToastState.d.ts.map +1 -0
- package/types/ToastViewport.d.ts +75 -0
- package/types/ToastViewport.d.ts.map +1 -0
- package/types/Toaster.d.ts +112 -0
- package/types/Toaster.d.ts.map +1 -0
- package/types/constants.d.ts +3 -0
- package/types/constants.d.ts.map +1 -0
- package/types/createNativeToast.d.ts +9 -0
- package/types/createNativeToast.d.ts.map +1 -0
- package/types/createNativeToast.native.d.ts +5 -0
- package/types/createNativeToast.native.d.ts.map +1 -0
- package/types/dispatchNativeToast.d.ts +12 -0
- package/types/dispatchNativeToast.d.ts.map +1 -0
- package/types/index.d.ts +4 -0
- package/types/index.d.ts.map +1 -0
- package/types/types.d.ts +61 -0
- package/types/types.d.ts.map +1 -0
- package/types/useAnimatedDragGesture.d.ts +33 -0
- package/types/useAnimatedDragGesture.d.ts.map +1 -0
- package/types/useAnimatedDragGesture.native.d.ts +33 -0
- package/types/useAnimatedDragGesture.native.d.ts.map +1 -0
- package/types/useReducedMotion.d.ts +6 -0
- package/types/useReducedMotion.d.ts.map +1 -0
- package/types/useToastAnimations.d.ts +50 -0
- package/types/useToastAnimations.d.ts.map +1 -0
- package/types/v2.d.ts +10 -0
- package/types/v2.d.ts.map +1 -0
- package/v2/index.cjs +2 -0
- package/v2/index.js +2 -0
- package/v2/index.native.cjs +2 -0
- package/v2/index.native.js +2 -0
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { AnimatePresence } from '@hanzogui/animate-presence'
|
|
2
|
+
import { useComposedRefs } from '@hanzogui/compose-refs'
|
|
3
|
+
import { isWeb } from '@hanzogui/constants'
|
|
4
|
+
import type { GetProps, HanzoguiElement } from '@hanzogui/core'
|
|
5
|
+
import { styled } from '@hanzogui/core'
|
|
6
|
+
import { PortalHost } from '@hanzogui/portal'
|
|
7
|
+
import { YStack } from '@hanzogui/stacks'
|
|
8
|
+
import { VisuallyHidden } from '@hanzogui/visually-hidden'
|
|
9
|
+
import * as React from 'react'
|
|
10
|
+
import { TOAST_CONTEXT } from './constants'
|
|
11
|
+
import { ToastPortal } from './ToastPortal'
|
|
12
|
+
import type { ScopedProps, ToastProviderContextValue } from './ToastProvider'
|
|
13
|
+
import { Collection, useCollection, useToastProviderContext } from './ToastProvider'
|
|
14
|
+
|
|
15
|
+
const VIEWPORT_NAME = 'ToastViewport'
|
|
16
|
+
const VIEWPORT_DEFAULT_HOTKEY = ['F8']
|
|
17
|
+
const VIEWPORT_PAUSE = 'toast.viewportPause'
|
|
18
|
+
const VIEWPORT_RESUME = 'toast.viewportResume'
|
|
19
|
+
|
|
20
|
+
const ToastViewportWrapperFrame = styled(YStack, {
|
|
21
|
+
name: 'ViewportWrapper',
|
|
22
|
+
|
|
23
|
+
variants: {
|
|
24
|
+
unstyled: {
|
|
25
|
+
false: {
|
|
26
|
+
pointerEvents: 'box-none',
|
|
27
|
+
top: 0,
|
|
28
|
+
bottom: 0,
|
|
29
|
+
left: 0,
|
|
30
|
+
right: 0,
|
|
31
|
+
position: isWeb ? ('fixed' as any) : 'absolute',
|
|
32
|
+
maxWidth: '100%',
|
|
33
|
+
tabIndex: 0,
|
|
34
|
+
zIndex: 100000,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
} as const,
|
|
38
|
+
|
|
39
|
+
defaultVariants: {
|
|
40
|
+
unstyled: process.env.HANZOGUI_HEADLESS === '1',
|
|
41
|
+
},
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const ToastViewportFrame = styled(YStack, {
|
|
45
|
+
name: VIEWPORT_NAME,
|
|
46
|
+
|
|
47
|
+
variants: {
|
|
48
|
+
unstyled: {
|
|
49
|
+
false: {
|
|
50
|
+
pointerEvents: 'box-none',
|
|
51
|
+
position: isWeb ? ('fixed' as any) : 'absolute',
|
|
52
|
+
maxWidth: '100%',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
} as const,
|
|
56
|
+
|
|
57
|
+
defaultVariants: {
|
|
58
|
+
unstyled: process.env.HANZOGUI_HEADLESS === '1',
|
|
59
|
+
},
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
type ToastViewportFrameProps = GetProps<typeof ToastViewportFrame>
|
|
63
|
+
type ToastViewportProps = ToastViewportFrameProps & {
|
|
64
|
+
/**
|
|
65
|
+
* The keys to use as the keyboard shortcut that will move focus to the toast viewport.
|
|
66
|
+
* @defaultValue ['F8']
|
|
67
|
+
*/
|
|
68
|
+
hotkey?: string[]
|
|
69
|
+
/**
|
|
70
|
+
* An author-localized label for the toast viewport to provide context for screen reader users
|
|
71
|
+
* when navigating page landmarks. The available `{hotkey}` placeholder will be replaced for you.
|
|
72
|
+
* @defaultValue 'Notifications ({hotkey})'
|
|
73
|
+
*/
|
|
74
|
+
label?: string
|
|
75
|
+
/**
|
|
76
|
+
* Used to reference the viewport if you want to have multiple viewports in the same provider.
|
|
77
|
+
*/
|
|
78
|
+
name?: string
|
|
79
|
+
/**
|
|
80
|
+
* Pass this when you want to have multiple/duplicated toasts.
|
|
81
|
+
*/
|
|
82
|
+
multipleToasts?: boolean
|
|
83
|
+
/**
|
|
84
|
+
* When true, uses a portal to render at the very top of the root HanzoguiProvider.
|
|
85
|
+
*/
|
|
86
|
+
portalToRoot?: boolean
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const ToastViewport = React.memo(
|
|
90
|
+
React.forwardRef<HTMLDivElement, ToastViewportProps>(
|
|
91
|
+
(props: ScopedProps<ToastViewportProps>, forwardedRef) => {
|
|
92
|
+
const {
|
|
93
|
+
scope,
|
|
94
|
+
hotkey = VIEWPORT_DEFAULT_HOTKEY,
|
|
95
|
+
label = 'Notifications ({hotkey})',
|
|
96
|
+
name = 'default',
|
|
97
|
+
multipleToasts,
|
|
98
|
+
zIndex,
|
|
99
|
+
portalToRoot,
|
|
100
|
+
...viewportProps
|
|
101
|
+
} = props
|
|
102
|
+
const context = useToastProviderContext(scope)
|
|
103
|
+
const getItems = useCollection(scope || TOAST_CONTEXT)
|
|
104
|
+
const headFocusProxyRef = React.useRef<FocusProxyElement>(null)
|
|
105
|
+
const tailFocusProxyRef = React.useRef<FocusProxyElement>(null)
|
|
106
|
+
const wrapperRef = React.useRef<HTMLDivElement>(null)
|
|
107
|
+
const ref = React.useRef<HTMLDivElement>(null)
|
|
108
|
+
const onViewportChange = React.useCallback(
|
|
109
|
+
(el: HanzoguiElement) => {
|
|
110
|
+
if (context.viewports[name] !== el) context.onViewportChange(name, el)
|
|
111
|
+
},
|
|
112
|
+
[name, context.viewports]
|
|
113
|
+
)
|
|
114
|
+
// @ts-ignore TODO react 19 type needs fix
|
|
115
|
+
const composedRefs = useComposedRefs(forwardedRef, ref, onViewportChange)
|
|
116
|
+
const hotkeyLabel = hotkey.join('+').replace(/Key/g, '').replace(/Digit/g, '')
|
|
117
|
+
const hasToasts = context.toastCount > 0
|
|
118
|
+
|
|
119
|
+
React.useEffect(() => {
|
|
120
|
+
if (!isWeb) return
|
|
121
|
+
if (context.toastCount === 0) return
|
|
122
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
123
|
+
// we use `event.code` as it is consistent regardless of meta keys that were pressed.
|
|
124
|
+
// for example, `event.key` for `Control+Alt+t` is `†` and `t !== †`
|
|
125
|
+
const isHotkeyPressed = hotkey.every(
|
|
126
|
+
(key) => (event as any)[key] || event.code === key
|
|
127
|
+
)
|
|
128
|
+
if (isHotkeyPressed) ref.current?.focus()
|
|
129
|
+
}
|
|
130
|
+
document.addEventListener('keydown', handleKeyDown)
|
|
131
|
+
return () => {
|
|
132
|
+
document.removeEventListener('keydown', handleKeyDown)
|
|
133
|
+
}
|
|
134
|
+
}, [hotkey, context.toastCount])
|
|
135
|
+
|
|
136
|
+
React.useEffect(() => {
|
|
137
|
+
if (!isWeb) return
|
|
138
|
+
if (context.toastCount === 0) return
|
|
139
|
+
const wrapper = wrapperRef.current
|
|
140
|
+
const viewport = ref.current
|
|
141
|
+
if (hasToasts && wrapper && viewport) {
|
|
142
|
+
const handlePause = () => {
|
|
143
|
+
if (!context.isClosePausedRef.current) {
|
|
144
|
+
const pauseEvent = new CustomEvent(VIEWPORT_PAUSE)
|
|
145
|
+
viewport.dispatchEvent(pauseEvent)
|
|
146
|
+
context.isClosePausedRef.current = true
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const handleResume = () => {
|
|
151
|
+
if (context.isClosePausedRef.current) {
|
|
152
|
+
const resumeEvent = new CustomEvent(VIEWPORT_RESUME)
|
|
153
|
+
viewport.dispatchEvent(resumeEvent)
|
|
154
|
+
context.isClosePausedRef.current = false
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const handleFocusOutResume = (event: FocusEvent) => {
|
|
159
|
+
const isFocusMovingOutside = !wrapper.contains(
|
|
160
|
+
event.relatedTarget as HTMLElement
|
|
161
|
+
)
|
|
162
|
+
if (isFocusMovingOutside) handleResume()
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const handlePointerLeaveResume = () => {
|
|
166
|
+
const isFocusInside = wrapper.contains(document.activeElement)
|
|
167
|
+
if (!isFocusInside) handleResume()
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Toasts are not in the viewport React tree so we need to bind DOM events
|
|
171
|
+
wrapper.addEventListener('focusin', handlePause)
|
|
172
|
+
wrapper.addEventListener('focusout', handleFocusOutResume)
|
|
173
|
+
wrapper.addEventListener('pointermove', handlePause)
|
|
174
|
+
wrapper.addEventListener('pointerleave', handlePointerLeaveResume)
|
|
175
|
+
window.addEventListener('blur', handlePause)
|
|
176
|
+
window.addEventListener('focus', handleResume)
|
|
177
|
+
return () => {
|
|
178
|
+
wrapper.removeEventListener('focusin', handlePause)
|
|
179
|
+
wrapper.removeEventListener('focusout', handleFocusOutResume)
|
|
180
|
+
wrapper.removeEventListener('pointermove', handlePause)
|
|
181
|
+
wrapper.removeEventListener('pointerleave', handlePointerLeaveResume)
|
|
182
|
+
window.removeEventListener('blur', handlePause)
|
|
183
|
+
window.removeEventListener('focus', handleResume)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}, [hasToasts, context.isClosePausedRef, context.toastCount])
|
|
187
|
+
|
|
188
|
+
const getSortedTabbableCandidates = React.useCallback(
|
|
189
|
+
({ tabbingDirection }: { tabbingDirection: 'forwards' | 'backwards' }) => {
|
|
190
|
+
const toastItems = getItems()
|
|
191
|
+
const tabbableCandidates = toastItems.map((toastItem) => {
|
|
192
|
+
const toastNode = toastItem.ref.current!
|
|
193
|
+
const toastTabbableCandidates = [
|
|
194
|
+
toastNode,
|
|
195
|
+
...getTabbableCandidates(toastNode),
|
|
196
|
+
]
|
|
197
|
+
return tabbingDirection === 'forwards'
|
|
198
|
+
? toastTabbableCandidates
|
|
199
|
+
: toastTabbableCandidates.reverse()
|
|
200
|
+
})
|
|
201
|
+
return (
|
|
202
|
+
tabbingDirection === 'forwards'
|
|
203
|
+
? tabbableCandidates.reverse()
|
|
204
|
+
: tabbableCandidates
|
|
205
|
+
).flat()
|
|
206
|
+
},
|
|
207
|
+
[getItems]
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
React.useEffect(() => {
|
|
211
|
+
if (!isWeb) return
|
|
212
|
+
if (context.toastCount === 0) return
|
|
213
|
+
|
|
214
|
+
const viewport = ref.current
|
|
215
|
+
// We programmatically manage tabbing as we are unable to influence
|
|
216
|
+
// the source order with portals, this allows us to reverse the
|
|
217
|
+
// tab order so that it runs from most recent toast to least
|
|
218
|
+
if (viewport) {
|
|
219
|
+
const handleKeyDown = (event: KeyboardEvent) => {
|
|
220
|
+
const isMetaKey = event.altKey || event.ctrlKey || event.metaKey
|
|
221
|
+
const isTabKey = event.key === 'Tab' && !isMetaKey
|
|
222
|
+
|
|
223
|
+
if (isTabKey) {
|
|
224
|
+
const focusedElement = document.activeElement
|
|
225
|
+
const isTabbingBackwards = event.shiftKey
|
|
226
|
+
const targetIsViewport = event.target === viewport
|
|
227
|
+
|
|
228
|
+
// If we're back tabbing after jumping to the viewport then we simply
|
|
229
|
+
// proxy focus out to the preceding document
|
|
230
|
+
if (targetIsViewport && isTabbingBackwards) {
|
|
231
|
+
// @ts-ignore ali TODO type
|
|
232
|
+
headFocusProxyRef.current?.focus()
|
|
233
|
+
return
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
const tabbingDirection = isTabbingBackwards ? 'backwards' : 'forwards'
|
|
237
|
+
const sortedCandidates = getSortedTabbableCandidates({ tabbingDirection })
|
|
238
|
+
const index = sortedCandidates.findIndex(
|
|
239
|
+
(candidate) => candidate === focusedElement
|
|
240
|
+
)
|
|
241
|
+
if (focusFirst(sortedCandidates.slice(index + 1) as any)) {
|
|
242
|
+
event.preventDefault()
|
|
243
|
+
} else {
|
|
244
|
+
// If we can't focus that means we're at the edges so we
|
|
245
|
+
// proxy to the corresponding exit point and let the browser handle
|
|
246
|
+
// tab/shift+tab keypress and implicitly pass focus to the next valid element in the document
|
|
247
|
+
isTabbingBackwards
|
|
248
|
+
? // @ts-ignore ali TODO type
|
|
249
|
+
headFocusProxyRef.current?.focus()
|
|
250
|
+
: // @ts-ignore ali TODO type
|
|
251
|
+
tailFocusProxyRef.current?.focus()
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Toasts are not in the viewport React tree so we need to bind DOM events
|
|
257
|
+
viewport.addEventListener('keydown', handleKeyDown)
|
|
258
|
+
return () => viewport.removeEventListener('keydown', handleKeyDown)
|
|
259
|
+
}
|
|
260
|
+
}, [getItems, getSortedTabbableCandidates, context.toastCount])
|
|
261
|
+
|
|
262
|
+
const contents = (
|
|
263
|
+
<ToastViewportWrapperFrame
|
|
264
|
+
ref={wrapperRef as any}
|
|
265
|
+
// biome-ignore lint/a11y/useSemanticElements: <explanation>
|
|
266
|
+
role="region"
|
|
267
|
+
aria-label={label.replace('{hotkey}', hotkeyLabel)}
|
|
268
|
+
// // Ensure virtual cursor from landmarks menus triggers focus/blur for pause/resume
|
|
269
|
+
tabIndex={-1}
|
|
270
|
+
// // incase list has size when empty (e.g. padding), we remove pointer events so
|
|
271
|
+
// // it doesn't prevent interactions with page elements that it overlays
|
|
272
|
+
// pointerEvents={hasToasts ? undefined : 'none'}
|
|
273
|
+
>
|
|
274
|
+
{hasToasts && (
|
|
275
|
+
<FocusProxy
|
|
276
|
+
context={context}
|
|
277
|
+
viewportName={name}
|
|
278
|
+
ref={headFocusProxyRef}
|
|
279
|
+
onFocusFromOutsideViewport={() => {
|
|
280
|
+
const tabbableCandidates = getSortedTabbableCandidates({
|
|
281
|
+
tabbingDirection: 'forwards',
|
|
282
|
+
})
|
|
283
|
+
focusFirst(tabbableCandidates as any)
|
|
284
|
+
}}
|
|
285
|
+
/>
|
|
286
|
+
)}
|
|
287
|
+
{/**
|
|
288
|
+
* tabindex on the the list so that it can be focused when items are removed. we focus
|
|
289
|
+
* the list instead of the viewport so it announces number of items remaining.
|
|
290
|
+
*/}
|
|
291
|
+
<Collection.Slot scope={context.toastScope}>
|
|
292
|
+
<ToastViewportFrame
|
|
293
|
+
focusable={context.toastCount > 0}
|
|
294
|
+
ref={composedRefs as any}
|
|
295
|
+
{...viewportProps}
|
|
296
|
+
>
|
|
297
|
+
<PortalHost
|
|
298
|
+
render={(children) => (
|
|
299
|
+
<AnimatePresence exitBeforeEnter={!multipleToasts}>
|
|
300
|
+
{children}
|
|
301
|
+
</AnimatePresence>
|
|
302
|
+
)}
|
|
303
|
+
name={name ?? 'default'}
|
|
304
|
+
/>
|
|
305
|
+
</ToastViewportFrame>
|
|
306
|
+
</Collection.Slot>
|
|
307
|
+
{hasToasts && (
|
|
308
|
+
<FocusProxy
|
|
309
|
+
context={context}
|
|
310
|
+
viewportName={name}
|
|
311
|
+
ref={tailFocusProxyRef}
|
|
312
|
+
onFocusFromOutsideViewport={() => {
|
|
313
|
+
const tabbableCandidates = getSortedTabbableCandidates({
|
|
314
|
+
tabbingDirection: 'backwards',
|
|
315
|
+
})
|
|
316
|
+
focusFirst(tabbableCandidates as any)
|
|
317
|
+
}}
|
|
318
|
+
/>
|
|
319
|
+
)}
|
|
320
|
+
</ToastViewportWrapperFrame>
|
|
321
|
+
)
|
|
322
|
+
|
|
323
|
+
if (portalToRoot) {
|
|
324
|
+
return (
|
|
325
|
+
<ToastPortal
|
|
326
|
+
context={context}
|
|
327
|
+
{...(typeof zIndex === 'number' ? { zIndex } : {})}
|
|
328
|
+
>
|
|
329
|
+
{contents}
|
|
330
|
+
</ToastPortal>
|
|
331
|
+
)
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
return contents
|
|
335
|
+
}
|
|
336
|
+
)
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
ToastViewport.displayName = VIEWPORT_NAME
|
|
340
|
+
|
|
341
|
+
/* -----------------------------------------------------------------------------------------------*/
|
|
342
|
+
|
|
343
|
+
const FOCUS_PROXY_NAME = 'ToastFocusProxy'
|
|
344
|
+
|
|
345
|
+
type FocusProxyElement = React.ElementRef<typeof VisuallyHidden>
|
|
346
|
+
|
|
347
|
+
type VisuallyHiddenProps = GetProps<typeof VisuallyHidden>
|
|
348
|
+
|
|
349
|
+
interface FocusProxyProps extends VisuallyHiddenProps {
|
|
350
|
+
onFocusFromOutsideViewport(): void
|
|
351
|
+
viewportName: string
|
|
352
|
+
context: ToastProviderContextValue
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const FocusProxy = React.forwardRef<FocusProxyElement, ScopedProps<FocusProxyProps>>(
|
|
356
|
+
(props, forwardedRef) => {
|
|
357
|
+
const { onFocusFromOutsideViewport, viewportName, context, ...proxyProps } = props
|
|
358
|
+
const viewport = context.viewports[viewportName] as HTMLElement
|
|
359
|
+
|
|
360
|
+
return (
|
|
361
|
+
<VisuallyHidden
|
|
362
|
+
aria-hidden
|
|
363
|
+
tabIndex={0}
|
|
364
|
+
{...proxyProps}
|
|
365
|
+
ref={forwardedRef}
|
|
366
|
+
// Avoid page scrolling when focus is on the focus proxy
|
|
367
|
+
position={isWeb ? ('fixed' as any) : 'absolute'}
|
|
368
|
+
onFocus={(event) => {
|
|
369
|
+
if (!isWeb) return
|
|
370
|
+
const prevFocusedElement = event.relatedTarget as HTMLElement | null
|
|
371
|
+
const isFocusFromOutsideViewport = !viewport?.contains(prevFocusedElement)
|
|
372
|
+
if (isFocusFromOutsideViewport) onFocusFromOutsideViewport()
|
|
373
|
+
}}
|
|
374
|
+
/>
|
|
375
|
+
)
|
|
376
|
+
}
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
FocusProxy.displayName = FOCUS_PROXY_NAME
|
|
380
|
+
|
|
381
|
+
/* -----------------------------------------------------------------------------------------------*/
|
|
382
|
+
|
|
383
|
+
function focusFirst(candidates: HanzoguiElement[]) {
|
|
384
|
+
if (!isWeb) return
|
|
385
|
+
const previouslyFocusedElement = document.activeElement
|
|
386
|
+
return candidates.some((candidate) => {
|
|
387
|
+
// if focus is already where we want to go, we don't want to keep going through the candidates
|
|
388
|
+
if (candidate === previouslyFocusedElement) return true
|
|
389
|
+
candidate.focus()
|
|
390
|
+
return document.activeElement !== previouslyFocusedElement
|
|
391
|
+
})
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/**
|
|
395
|
+
* Returns a list of potential tabbable candidates.
|
|
396
|
+
*
|
|
397
|
+
* NOTE: This is only a close approximation. For example it doesn't take into account cases like when
|
|
398
|
+
* elements are not visible. This cannot be worked out easily by just reading a property, but rather
|
|
399
|
+
* necessitate runtime knowledge (computed styles, etc). We deal with these cases separately.
|
|
400
|
+
*
|
|
401
|
+
* See: https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker
|
|
402
|
+
* Credit: https://github.com/discord/focus-layers/blob/master/src/util/wrapFocus.tsx#L1
|
|
403
|
+
*/
|
|
404
|
+
function getTabbableCandidates(container: HanzoguiElement) {
|
|
405
|
+
if (!isWeb) return []
|
|
406
|
+
const containerHtml = container as HTMLElement
|
|
407
|
+
const nodes: HTMLElement[] = []
|
|
408
|
+
const walker = document.createTreeWalker(containerHtml, NodeFilter.SHOW_ELEMENT, {
|
|
409
|
+
acceptNode: (node: any) => {
|
|
410
|
+
const isHiddenInput = node.tagName === 'INPUT' && node.type === 'hidden'
|
|
411
|
+
if (node.disabled || node.hidden || isHiddenInput) return NodeFilter.FILTER_SKIP
|
|
412
|
+
// `.tabIndex` is not the same as the `tabindex` attribute. It works on the
|
|
413
|
+
// runtime's understanding of tabbability, so this automatically accounts
|
|
414
|
+
// for any kind of element that could be tabbed to.
|
|
415
|
+
return node.tabIndex >= 0 ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_SKIP
|
|
416
|
+
},
|
|
417
|
+
})
|
|
418
|
+
while (walker.nextNode()) nodes.push(walker.currentNode as HTMLElement)
|
|
419
|
+
// we do not take into account the order of nodes with positive `tabIndex` as it
|
|
420
|
+
// hinders accessibility to have tab order different from visual order.
|
|
421
|
+
return nodes
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
export {
|
|
425
|
+
ToastViewport,
|
|
426
|
+
VIEWPORT_DEFAULT_HOTKEY,
|
|
427
|
+
VIEWPORT_PAUSE,
|
|
428
|
+
VIEWPORT_RESUME,
|
|
429
|
+
type ToastViewportProps,
|
|
430
|
+
}
|
package/src/Toaster.tsx
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Toaster — drop-in all-in-one toast component.
|
|
3
|
+
* Thin wrapper around Toast + Toast.Viewport + Toast.List (composable API).
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { HanzoguiElement } from '@hanzogui/core'
|
|
7
|
+
import * as React from 'react'
|
|
8
|
+
import { Toast } from './ToastComposable'
|
|
9
|
+
import type { ToastPosition } from './ToastComposable'
|
|
10
|
+
import type { SwipeDirection } from './ToastProvider'
|
|
11
|
+
import type { ExternalToast } from './ToastState'
|
|
12
|
+
import type { BurntToastOptions } from './types'
|
|
13
|
+
|
|
14
|
+
export type ToasterPosition = ToastPosition
|
|
15
|
+
|
|
16
|
+
export interface ToasterProps {
|
|
17
|
+
/**
|
|
18
|
+
* Position of the toasts on screen
|
|
19
|
+
* @default 'bottom-right'
|
|
20
|
+
*/
|
|
21
|
+
position?: ToasterPosition
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Expand toasts on hover to show all
|
|
25
|
+
* @default false
|
|
26
|
+
*/
|
|
27
|
+
expand?: boolean
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Number of toasts visible at once
|
|
31
|
+
* @default 4
|
|
32
|
+
*/
|
|
33
|
+
visibleToasts?: number
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Gap between toasts in pixels
|
|
37
|
+
* @default 14
|
|
38
|
+
*/
|
|
39
|
+
gap?: number
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Default duration for toasts in ms
|
|
43
|
+
* @default 4000
|
|
44
|
+
*/
|
|
45
|
+
duration?: number
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Offset from screen edge in pixels
|
|
49
|
+
* @default 24
|
|
50
|
+
*/
|
|
51
|
+
offset?: number | { top?: number; right?: number; bottom?: number; left?: number }
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Hotkey to focus toast viewport
|
|
55
|
+
* @default ['altKey', 'KeyT']
|
|
56
|
+
*/
|
|
57
|
+
hotkey?: string[]
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Direction(s) toasts can be swiped to dismiss.
|
|
61
|
+
* 'auto' detects based on position (swipe toward nearest edge).
|
|
62
|
+
* @default 'auto'
|
|
63
|
+
*/
|
|
64
|
+
swipeDirection?: SwipeDirection
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Distance in pixels swipe must pass to dismiss
|
|
68
|
+
* @default 50
|
|
69
|
+
*/
|
|
70
|
+
swipeThreshold?: number
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Show close button on toasts
|
|
74
|
+
* @default false
|
|
75
|
+
*/
|
|
76
|
+
closeButton?: boolean
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Theme for toasts (auto-detected if not set)
|
|
80
|
+
*/
|
|
81
|
+
theme?: 'light' | 'dark' | 'system'
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Custom icons for toast types
|
|
85
|
+
*/
|
|
86
|
+
icons?: {
|
|
87
|
+
success?: React.ReactNode
|
|
88
|
+
error?: React.ReactNode
|
|
89
|
+
warning?: React.ReactNode
|
|
90
|
+
info?: React.ReactNode
|
|
91
|
+
loading?: React.ReactNode
|
|
92
|
+
close?: React.ReactNode
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Default toast options
|
|
97
|
+
*/
|
|
98
|
+
toastOptions?: ExternalToast
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Container aria label for screen readers
|
|
102
|
+
* @default 'Notifications'
|
|
103
|
+
*/
|
|
104
|
+
containerAriaLabel?: string
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* When true, uses burnt native OS toasts on mobile instead of RN views.
|
|
108
|
+
* @default false
|
|
109
|
+
*/
|
|
110
|
+
native?: boolean
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Options for burnt native toasts on mobile
|
|
114
|
+
*/
|
|
115
|
+
burntOptions?: Omit<BurntToastOptions, 'title' | 'message' | 'duration'>
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Options for web Notification API
|
|
119
|
+
*/
|
|
120
|
+
notificationOptions?: NotificationOptions
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Force reduced motion mode (disables animations)
|
|
124
|
+
*/
|
|
125
|
+
reducedMotion?: boolean
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const Toaster = React.forwardRef<HanzoguiElement, ToasterProps>(
|
|
129
|
+
function Toaster(props, ref) {
|
|
130
|
+
const {
|
|
131
|
+
position = 'bottom-right',
|
|
132
|
+
expand = false,
|
|
133
|
+
visibleToasts,
|
|
134
|
+
gap,
|
|
135
|
+
duration,
|
|
136
|
+
offset,
|
|
137
|
+
hotkey,
|
|
138
|
+
swipeDirection,
|
|
139
|
+
swipeThreshold,
|
|
140
|
+
closeButton,
|
|
141
|
+
theme,
|
|
142
|
+
icons,
|
|
143
|
+
toastOptions,
|
|
144
|
+
containerAriaLabel = 'Notifications',
|
|
145
|
+
native,
|
|
146
|
+
burntOptions,
|
|
147
|
+
notificationOptions,
|
|
148
|
+
reducedMotion,
|
|
149
|
+
} = props
|
|
150
|
+
|
|
151
|
+
return (
|
|
152
|
+
<Toast
|
|
153
|
+
position={position}
|
|
154
|
+
expand={expand}
|
|
155
|
+
visibleToasts={visibleToasts}
|
|
156
|
+
gap={gap}
|
|
157
|
+
duration={toastOptions?.duration ?? duration}
|
|
158
|
+
swipeDirection={swipeDirection}
|
|
159
|
+
swipeThreshold={swipeThreshold}
|
|
160
|
+
closeButton={closeButton}
|
|
161
|
+
theme={theme}
|
|
162
|
+
icons={icons}
|
|
163
|
+
native={native}
|
|
164
|
+
burntOptions={burntOptions}
|
|
165
|
+
notificationOptions={notificationOptions}
|
|
166
|
+
reducedMotion={reducedMotion}
|
|
167
|
+
>
|
|
168
|
+
<Toast.Viewport
|
|
169
|
+
ref={ref}
|
|
170
|
+
offset={offset}
|
|
171
|
+
hotkey={hotkey}
|
|
172
|
+
label={containerAriaLabel}
|
|
173
|
+
>
|
|
174
|
+
<Toast.List />
|
|
175
|
+
</Toast.Viewport>
|
|
176
|
+
</Toast>
|
|
177
|
+
)
|
|
178
|
+
}
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
Toaster.displayName = 'Toaster'
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { getBurnt } from '@hanzogui/native'
|
|
2
|
+
import type { CreateNativeToastsFn, HideNativeToastsFn } from './types'
|
|
3
|
+
|
|
4
|
+
export const createNativeToast: CreateNativeToastsFn = (
|
|
5
|
+
title,
|
|
6
|
+
{ message, duration, burntOptions }
|
|
7
|
+
) => {
|
|
8
|
+
const burnt = getBurnt()
|
|
9
|
+
if (!burnt.isEnabled) {
|
|
10
|
+
console.warn(
|
|
11
|
+
`Warning: Must call import '@hanzogui/native/setup-burnt' at your app entry point to use native toasts`
|
|
12
|
+
)
|
|
13
|
+
return false
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
burnt.state.toast!({
|
|
17
|
+
title,
|
|
18
|
+
message,
|
|
19
|
+
duration: duration ? duration / 1000 : undefined,
|
|
20
|
+
...burntOptions,
|
|
21
|
+
})
|
|
22
|
+
return true
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const hideNativeToast: HideNativeToastsFn = () => {
|
|
26
|
+
const burnt = getBurnt()
|
|
27
|
+
if (!burnt.isEnabled) return
|
|
28
|
+
burnt.state.dismissAllAlerts!()
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function requestNotificationPermission(): Promise<null> {
|
|
32
|
+
return null
|
|
33
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CreateNativeToastsFn, HideNativeToastsFn } from './types'
|
|
2
|
+
|
|
3
|
+
export const createNativeToast: CreateNativeToastsFn = (
|
|
4
|
+
title,
|
|
5
|
+
{ message, notificationOptions }
|
|
6
|
+
) => {
|
|
7
|
+
if (!('Notification' in window)) {
|
|
8
|
+
return false
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (Notification.permission !== 'granted') return false
|
|
12
|
+
|
|
13
|
+
const notification = new Notification(title, {
|
|
14
|
+
body: message,
|
|
15
|
+
...notificationOptions,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
nativeToastRef: notification,
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const hideNativeToast: HideNativeToastsFn = (ref) => {
|
|
24
|
+
if (!('Notification' in window)) {
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (ref) {
|
|
29
|
+
ref.close()
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Request notification permission from the browser.
|
|
35
|
+
* Must be called from a user gesture (click/tap handler).
|
|
36
|
+
*/
|
|
37
|
+
export async function requestNotificationPermission(): Promise<NotificationPermission | null> {
|
|
38
|
+
if (!('Notification' in window)) return null
|
|
39
|
+
if (Notification.permission === 'granted') return 'granted'
|
|
40
|
+
if (Notification.permission === 'denied') return 'denied'
|
|
41
|
+
return Notification.requestPermission()
|
|
42
|
+
}
|