Skip to content

Commit ce01433

Browse files
committed
feat(toast): handle component render function
1 parent 76e118e commit ce01433

File tree

3 files changed

+55
-23
lines changed

3 files changed

+55
-23
lines changed

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { Button, Toast, useToast } from 'heroui-native';
1+
import {
2+
Button,
3+
Toast,
4+
useToast,
5+
type ToastComponentProps,
6+
} from 'heroui-native';
7+
import { useCallback } from 'react';
28
import { View } from 'react-native';
39
import type { UsageVariant } from '../../../components/component-presentation/types';
410
import { UsageVariantFlatList } from '../../../components/component-presentation/usage-variant-flatlist';
@@ -68,31 +74,40 @@ import { UsageVariantFlatList } from '../../../components/component-presentation
6874

6975
// ------------------------------------------------------------------------------
7076

77+
const MyToast = ({ id }: ToastComponentProps) => {
78+
console.log('🔴 🔴', id); // VS remove
79+
const toast = useToast();
80+
81+
return (
82+
<Toast variant="accent" className="flex-row items-center gap-3">
83+
<View className="flex-1">
84+
<Toast.Label>{id}</Toast.Label>
85+
<Toast.Description>
86+
Use buttons below to control this toast
87+
</Toast.Description>
88+
</View>
89+
<Toast.Action onPress={() => toast.hide(id)}>Close</Toast.Action>
90+
</Toast>
91+
);
92+
};
93+
7194
const InteractiveDemoContent = () => {
7295
const toast = useToast();
7396

97+
const _renderToast = useCallback(
98+
({ id }: ToastComponentProps) => <MyToast id={id} />,
99+
[]
100+
);
101+
74102
return (
75103
<View className="flex-1 px-5">
76104
<View className="flex-1 justify-center gap-3">
77105
<Button
78106
onPress={() => {
79-
const id = toast.show({
107+
toast.show({
80108
id: 'my-toast',
81-
component: (
82-
<Toast variant="accent" className="flex-row items-center gap-3">
83-
<View className="flex-1">
84-
<Toast.Label>Interactive Toast</Toast.Label>
85-
<Toast.Description>
86-
Use buttons below to control this toast
87-
</Toast.Description>
88-
</View>
89-
<Toast.Action onPress={() => toast.hide('my-toast')}>
90-
Close
91-
</Toast.Action>
92-
</Toast>
93-
),
109+
component: _renderToast,
94110
});
95-
console.log('Toast shown with ID:', id);
96111
}}
97112
variant="primary"
98113
>

src/providers/toast/provider.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,12 @@ export function ToastProvider({ insets, children }: ToastProviderProps) {
8181
<InsetsContainer insets={insets}>
8282
<View className="flex-1">
8383
{toasts.map((toastItem) => {
84-
return (
85-
<Fragment key={toastItem.id}>{toastItem.component}</Fragment>
86-
);
84+
const content =
85+
typeof toastItem.component === 'function'
86+
? toastItem.component({ id: toastItem.id })
87+
: toastItem.component;
88+
89+
return <Fragment key={toastItem.id}>{content}</Fragment>;
8790
})}
8891
</View>
8992
</InsetsContainer>

src/providers/toast/types.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export interface ToastProviderProps {
4444
children?: React.ReactNode;
4545
}
4646

47+
/**
48+
* Props passed to the toast component function
49+
*/
50+
export interface ToastComponentProps {
51+
/**
52+
* The unique ID of the toast
53+
*/
54+
id: ToastId;
55+
}
56+
4757
/**
4858
* Options for showing a toast
4959
*/
@@ -54,9 +64,11 @@ export interface ToastShowOptions {
5464
*/
5565
id?: ToastId;
5666
/**
57-
* The React element to render
67+
* The React element to render, or a function that receives toast props
5868
*/
59-
component: React.ReactElement;
69+
component:
70+
| React.ReactElement
71+
| ((props: ToastComponentProps) => React.ReactElement);
6072
}
6173

6274
/**
@@ -68,9 +80,11 @@ export interface ToastItem {
6880
*/
6981
id: ToastId;
7082
/**
71-
* The React element to render
83+
* The React element to render, or a function that receives toast props
7284
*/
73-
component: React.ReactElement;
85+
component:
86+
| React.ReactElement
87+
| ((props: ToastComponentProps) => React.ReactElement);
7488
}
7589

7690
/**

0 commit comments

Comments
 (0)