forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import React from 'react'
2import {View} from 'react-native'
3import {nanoid} from 'nanoid/non-secure'
4import {toast as sonner, Toaster} from 'sonner-native'
5
6import {atoms as a} from '#/alf'
7import {DURATION} from '#/components/Toast/const'
8import {
9 Icon as ToastIcon,
10 Outer as BaseOuter,
11 Text as ToastText,
12 ToastConfigProvider,
13} from '#/components/Toast/Toast'
14import {type BaseToastOptions} from '#/components/Toast/types'
15
16export {DURATION} from '#/components/Toast/const'
17export {Action, Icon, Text, ToastConfigProvider} from '#/components/Toast/Toast'
18export {type ToastType} from '#/components/Toast/types'
19
20/**
21 * Toasts are rendered in a global outlet, which is placed at the top of the
22 * component tree.
23 */
24export function ToastOutlet() {
25 return <Toaster pauseWhenPageIsHidden gap={a.gap_sm.gap} />
26}
27
28export function Outer({children}: {children: React.ReactNode}) {
29 return (
30 <View style={[a.px_xl, a.w_full]}>
31 <BaseOuter>{children}</BaseOuter>
32 </View>
33 )
34}
35
36/**
37 * Access the full Sonner API
38 */
39export const api = sonner
40
41/**
42 * Our base toast API, using the `Toast` export of this file.
43 */
44export function show(
45 content: React.ReactNode,
46 {type = 'default', ...options}: BaseToastOptions = {},
47) {
48 const id = nanoid()
49
50 if (typeof content === 'string') {
51 sonner.custom(
52 <ToastConfigProvider id={id} type={type}>
53 <Outer>
54 <ToastIcon />
55 <ToastText>{content}</ToastText>
56 </Outer>
57 </ToastConfigProvider>,
58 {
59 ...options,
60 id,
61 duration: options?.duration ?? DURATION,
62 },
63 )
64 } else if (React.isValidElement(content)) {
65 sonner.custom(
66 <ToastConfigProvider id={id} type={type}>
67 {content}
68 </ToastConfigProvider>,
69 {
70 ...options,
71 id,
72 duration: options?.duration ?? DURATION,
73 },
74 )
75 } else {
76 throw new Error(
77 `Toast can be a string or a React element, got ${typeof content}`,
78 )
79 }
80}