Skip to content

Commit 44908b8

Browse files
Toast Example
1 parent 79c031f commit 44908b8

File tree

1 file changed

+85
-0
lines changed
  • apps/nextjs-nativewind/src/components/ui

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'use client';
2+
3+
import * as React from 'react';
4+
import { Pressable, Text, View } from 'react-native';
5+
import * as ToastPrimitive from '@method-inc/toast';
6+
7+
const Toast = () => {
8+
const [open, setOpen] = React.useState(false);
9+
const [seconds, setSeconds] = React.useState(3);
10+
11+
React.useEffect(() => {
12+
let interval: ReturnType<typeof setInterval> | null = null;
13+
14+
if (open) {
15+
interval = setInterval(() => {
16+
setSeconds((prevSeconds) => {
17+
if (prevSeconds <= 1) {
18+
setOpen(false);
19+
if (interval) {
20+
clearInterval(interval);
21+
}
22+
return 3;
23+
}
24+
return prevSeconds - 1;
25+
});
26+
}, 1000);
27+
} else {
28+
if (interval) {
29+
clearInterval(interval);
30+
}
31+
setSeconds(3);
32+
}
33+
34+
if (interval && !open) {
35+
clearInterval(interval);
36+
}
37+
38+
return () => {
39+
if (interval) {
40+
clearInterval(interval);
41+
}
42+
};
43+
}, [open, seconds]);
44+
45+
return (
46+
<>
47+
{open && (
48+
<View className='px-4 absolute w-full'>
49+
<ToastPrimitive.Root
50+
type='foreground'
51+
open={open}
52+
onOpenChange={setOpen}
53+
className='opacity-95 bg-secondary border-border flex-row justify-between items-center p-4 rounded-xl'
54+
>
55+
<View className='gap-1.5'>
56+
<ToastPrimitive.Title className='text-foreground text-3xl'>
57+
Here is a toast
58+
</ToastPrimitive.Title>
59+
<ToastPrimitive.Description className='text-foreground text-lg'>
60+
It will disappear in {seconds} seconds
61+
</ToastPrimitive.Description>
62+
</View>
63+
<View className='gap-2'>
64+
<ToastPrimitive.Action className='border border-primary px-4 py-2'>
65+
<Text className='text-foreground'>Action</Text>
66+
</ToastPrimitive.Action>
67+
<ToastPrimitive.Close className='border border-primary px-4 py-2'>
68+
<Text className='text-foreground'>Close</Text>
69+
</ToastPrimitive.Close>
70+
</View>
71+
</ToastPrimitive.Root>
72+
</View>
73+
)}
74+
<View className='flex-1 justify-center items-center p-6 gap-12'>
75+
<Pressable onPress={() => setOpen((prev) => !prev)}>
76+
<Text className='text-foreground text-xl'>Show Toast</Text>
77+
</Pressable>
78+
</View>
79+
</>
80+
);
81+
};
82+
83+
Toast.displayName = 'Toast';
84+
85+
export { Toast };

0 commit comments

Comments
 (0)