Skip to content

Commit b1612a9

Browse files
committed
feat(toast): add InsetsContainer component
1 parent ee6e14e commit b1612a9

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

example/src/app/(home)/components/toast.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ const MultipleToastsContent = () => {
202202
// ------------------------------------------------------------------------------
203203

204204
const TOAST_VARIANTS: UsageVariant[] = [
205-
// {
206-
// value: 'all-variants',
207-
// label: 'All variants',
208-
// content: <AllVariantsContent />,
209-
// },
205+
{
206+
value: 'all-variants',
207+
label: 'All variants',
208+
content: <AllVariantsContent />,
209+
},
210210
{
211211
value: 'interactive-demo',
212212
label: 'Interactive Demo',
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { ReactNode } from 'react';
2+
import { useMemo } from 'react';
3+
import { View } from 'react-native';
4+
import { useSafeAreaInsets } from 'react-native-safe-area-context';
5+
import type { ToastInsets } from './types';
6+
7+
interface InsetsContainerProps {
8+
/**
9+
* Optional inset values for all edges
10+
* If not provided, defaults to safe area insets + 12px
11+
*/
12+
insets?: ToastInsets;
13+
/**
14+
* Children to render inside the container
15+
*/
16+
children: ReactNode;
17+
}
18+
19+
/**
20+
* Container component that applies inset padding to position toasts
21+
* away from screen edges and safe areas
22+
*
23+
* Combines custom insets with safe area insets:
24+
* - If custom inset is provided, it overrides safe area + default padding
25+
* - If not provided, uses safe area inset + 12px default padding
26+
*/
27+
export function InsetsContainer({ insets, children }: InsetsContainerProps) {
28+
const safeAreaInsets = useSafeAreaInsets();
29+
30+
const finalInsets = useMemo(() => {
31+
return {
32+
top: insets?.top ?? safeAreaInsets.top + 12,
33+
bottom: insets?.bottom ?? safeAreaInsets.bottom + 12,
34+
left: insets?.left ?? safeAreaInsets.left + 12,
35+
right: insets?.right ?? safeAreaInsets.right + 12,
36+
};
37+
}, [safeAreaInsets, insets]);
38+
39+
return (
40+
<View
41+
className="absolute inset-0 pointer-events-box-none"
42+
style={{
43+
paddingTop: finalInsets.top,
44+
paddingBottom: finalInsets.bottom,
45+
paddingLeft: finalInsets.left,
46+
paddingRight: finalInsets.right,
47+
}}
48+
>
49+
{children}
50+
</View>
51+
);
52+
}

src/providers/toast/provider.tsx

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import {
33
Fragment,
44
useCallback,
55
useContext,
6-
useEffect,
76
useMemo,
87
useReducer,
98
useRef,
109
} from 'react';
1110
import { View } from 'react-native';
12-
import { useSafeAreaInsets } from 'react-native-safe-area-context';
11+
import { InsetsContainer } from './insets-container';
1312
import { toastReducer } from './reducer';
1413
import type {
1514
ToasterContextValue,
@@ -30,23 +29,8 @@ const ToasterContext = createContext<ToasterContextValue | null>(null);
3029
export function ToastProvider({ insets, children }: ToastProviderProps) {
3130
const [toasts, dispatch] = useReducer(toastReducer, []);
3231

33-
useEffect(() => {
34-
console.log('🔴 🔴', toasts); // VS remove
35-
}, [toasts]);
36-
37-
const safeAreaInsets = useSafeAreaInsets();
38-
3932
const idCounter = useRef(0);
4033

41-
const finalInsets = useMemo(() => {
42-
return {
43-
top: insets?.top ?? safeAreaInsets.top + 12,
44-
bottom: insets?.bottom ?? safeAreaInsets.bottom + 12,
45-
left: insets?.left ?? safeAreaInsets.left + 12,
46-
right: insets?.right ?? safeAreaInsets.right + 12,
47-
};
48-
}, [safeAreaInsets, insets]);
49-
5034
/**
5135
* Show a toast
5236
*/
@@ -92,23 +76,15 @@ export function ToastProvider({ insets, children }: ToastProviderProps) {
9276
return (
9377
<ToasterContext.Provider value={contextValue}>
9478
{children}
95-
<View
96-
className="absolute inset-0 pointer-events-box-none"
97-
style={{
98-
paddingTop: finalInsets.top,
99-
paddingBottom: finalInsets.bottom,
100-
paddingLeft: finalInsets.left,
101-
paddingRight: finalInsets.right,
102-
}}
103-
>
79+
<InsetsContainer insets={insets}>
10480
<View className="flex-1">
10581
{toasts.map((toastItem) => {
10682
return (
10783
<Fragment key={toastItem.id}>{toastItem.component}</Fragment>
10884
);
10985
})}
11086
</View>
111-
</View>
87+
</InsetsContainer>
11288
</ToasterContext.Provider>
11389
);
11490
}

0 commit comments

Comments
 (0)