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: warning variant #40

Merged
merged 3 commits into from
Sep 8, 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
2 changes: 1 addition & 1 deletion docs/docs/sonner.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
| Headless | ❌ | ✅ |
| Dynamic position | ❌ | ✅ |
| important | ❌ | ✅ |
| warning variant | | ✅ |
| warning variant | | ✅ |

## Toaster:

Expand Down
8 changes: 8 additions & 0 deletions docs/docs/toast.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ Use the toast.error() function to display an error message. By default, it rende
toast.error('My error toast');
```

### Warning

The toast.warning() function can be used to display a warning message. By default, it renders a warning icon in front of the message.

```jsx
toast.warning('My warning toast');
```

### Action

Renders an action button with a callback function. The action object should contain a label and an `onClick` function. The action and its label can be customized with the `actionButtonStyles` and `actionButtonTextStyles` params, respectively.
Expand Down
5 changes: 5 additions & 0 deletions example/src/ToastDemo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ export const ToastDemo: React.FC = () => {
title="Loading variant"
onPress={() => toast.loading('Loading...')}
/>
<Button
title="Dynamic position"
onPress={() => toast('Dynamic position', { position: 'bottom-center' })}
/>
<Button title="Warning toast" onPress={() => toast.warning('Warning')} />
</ScrollView>
);
};
Expand Down
8 changes: 8 additions & 0 deletions src/toast-fns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ toast.error = (title: string, options = {}) => {
});
};

toast.warning = (title: string, options = {}) => {
return getToastContext().addToast({
...options,
title,
variant: 'warning',
});
};

toast.info = (title: string, options = {}) => {
return getToastContext().addToast({
title,
Expand Down
10 changes: 9 additions & 1 deletion src/toast.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { CircleCheck, CircleX, Info, X } from 'lucide-react-native';
import {
CircleCheck,
CircleX,
Info,
TriangleAlert,
X,
} from 'lucide-react-native';
import * as React from 'react';
import { ActivityIndicator, Pressable, Text, View } from 'react-native';
import Animated from 'react-native-reanimated';
Expand Down Expand Up @@ -405,6 +411,8 @@ export const ToastIcon: React.FC<Pick<ToastProps, 'variant'>> = ({
return <CircleCheck size={20} color={colors.success} />;
case 'error':
return <CircleX size={20} color={colors.error} />;
case 'warning':
return <TriangleAlert size={20} color={colors.warning} />;
default:
case 'info':
return <Info size={20} color={colors.info} />;
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type ToastTheme = 'light' | 'dark' | 'system';

export type ToastSwipeDirection = 'left' | 'up';

export type ToastVariant = 'success' | 'error' | 'info' | 'loading';
export type ToastVariant = 'success' | 'error' | 'warning' | 'info' | 'loading';

export type ToastAction = {
label: string;
Expand Down Expand Up @@ -106,6 +106,7 @@ export type ToasterProps = StyleProps & {
icons?: {
success?: React.ReactNode;
error?: React.ReactNode;
warning?: React.ReactNode;
info?: React.ReactNode;
loading?: React.ReactNode;
};
Expand Down Expand Up @@ -145,6 +146,7 @@ export declare const toast: ((
success: (message: string, data?: ExternalToast) => string | number;
info: (message: string, data?: ExternalToast) => string | number;
error: (message: string, data?: ExternalToast) => string | number;
warning: (message: string, data?: ExternalToast) => string | number;
custom: (jsx: React.ReactElement, data?: ExternalToast) => string | number;
promise: <T>(
promise: Promise<T>,
Expand Down
2 changes: 2 additions & 0 deletions src/use-colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const light = {
'border-secondary': '#e6e3e3',
'success': '#3c8643',
'error': '#ff3a41',
'warning': '#e37a00',
'info': '#286efa',
};

Expand All @@ -22,6 +23,7 @@ const dark: typeof light = {
'border-secondary': '#302B2B',
'success': '#9ED397',
'error': '#FF999D',
'warning': '#ffd089',
'info': '#B3CDFF',
};

Expand Down
Loading