Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: wrap Toaster with the ToasterOverlayWrapper prop #73

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions docs/docs/Toaster.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ You can provide default styles for all toasts by passing `style` and `className`
/>
```

### Usage with react-native-z-view

Use the `ToasterOverlayWrapper` prop to wrap the Toaster component with a custom component. This is useful when using `react-native-z-view` to render the toasts.

sonner-native uses `FullWindowOverlay` from react-native-screens by default on iOS and `View` on Android.

```tsx
import { ZView } from 'react-native-z-view';

<Toaster
ToasterOverlayWrapper={ZView}
toastOptions={{
style: { backgroundColor: 'red' },
className: 'bg-red-500',
}}
/>;
```

## API Reference

| Property | Description | Default |
Expand All @@ -69,3 +87,8 @@ You can provide default styles for all toasts by passing `style` and `className`
| pauseWhenPageIsHidden | Pauses toast timers when the app enters background. | `{}` |
| `swipToDismissDirection` | Swipe direction to dismiss (`left`, `up`). | `up` |
| cn | Custom function for constructing/merging classes. | `filter(Boolean).join(' ')` |
|  ToasterOverlayWrapper |  Custom component to wrap the Toaster. | `div` |

```

```
11 changes: 10 additions & 1 deletion src/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ import {
let addToastHandler: AddToastContextHandler;
let dismissToastHandler: typeof toast.dismiss;

export const Toaster: React.FC<ToasterProps> = (props) => {
export const Toaster: React.FC<ToasterProps> = ({
ToasterOverlayWrapper,
...props
}) => {
if (ToasterOverlayWrapper) {
return (
<ToasterOverlayWrapper>{<ToasterUI {...props} />}</ToasterOverlayWrapper>
);
}

if (Platform.OS === 'ios') {
return (
<FullWindowOverlay>
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export type ToasterProps = StyleProps & {
swipToDismissDirection?: ToastSwipeDirection;
pauseWhenPageIsHidden?: boolean;
cn?: (...classes: Array<string | undefined>) => string;
ToasterOverlayWrapper?: React.ComponentType<{ children: React.ReactNode }>;
};

export type AddToastContextHandler = (
Expand Down
Loading