@abisheks238/react-toaster
Version:
An advanced, customizable toast notification system for React with smart queue, swipe support, and theming
99 lines (94 loc) • 3.06 kB
TypeScript
import React, { ReactNode, CSSProperties } from 'react';
type ToastPosition = 'top-right' | 'top-left' | 'top-center' | 'bottom-right' | 'bottom-left' | 'bottom-center' | 'center';
type ToastType = 'success' | 'error' | 'info' | 'warning' | 'loading' | 'custom';
type ToastTheme = 'light' | 'dark' | 'auto';
type ToastPriority = 'low' | 'normal' | 'high' | 'urgent';
interface ToastAction {
label: string;
onClick: (id: string) => void;
style?: 'primary' | 'secondary' | 'danger';
loading?: boolean;
}
interface SwipeConfig {
enabled: boolean;
threshold: number;
velocity: number;
}
interface ToastConfig {
position?: ToastPosition;
duration?: number;
priority?: ToastPriority;
dismissible?: boolean;
pauseOnHover?: boolean;
pauseOnFocusLoss?: boolean;
swipe?: SwipeConfig;
icon?: ReactNode | boolean;
sound?: boolean;
vibrate?: boolean;
actions?: ToastAction[];
onClose?: () => void;
onUndo?: (() => void) | null;
onRetry?: (() => void) | null;
className?: string;
style?: CSSProperties;
groupId?: string;
stackable?: boolean;
}
interface RequiredToastConfig extends Omit<Required<ToastConfig>, 'onUndo' | 'onRetry'> {
onUndo: (() => void) | null;
onRetry: (() => void) | null;
}
interface Toast {
id: string;
content: ReactNode;
type: ToastType;
config: RequiredToastConfig;
timestamp: number;
isExiting: boolean;
isPaused: boolean;
remainingTime: number;
swipeProgress: number;
retryCount: number;
}
interface ToastQueue {
maxToasts: number;
strategy: 'fifo' | 'lifo' | 'priority';
grouping: boolean;
maxPerGroup: number;
}
interface ToasterContextValue {
toasts: Toast[];
theme: ToastTheme;
queue: ToastQueue;
success: (content: ReactNode, config?: ToastConfig) => string;
error: (content: ReactNode, config?: ToastConfig) => string;
info: (content: ReactNode, config?: ToastConfig) => string;
warning: (content: ReactNode, config?: ToastConfig) => string;
loading: (content: ReactNode, config?: ToastConfig) => string;
custom: (content: ReactNode, config?: ToastConfig) => string;
update: (id: string, content: ReactNode, config?: Partial<ToastConfig>) => void;
dismiss: (id: string) => void;
dismissAll: () => void;
dismissGroup: (groupId: string) => void;
pause: (id: string) => void;
resume: (id: string) => void;
retry: (id: string) => void;
undo: (id: string) => void;
setTheme: (theme: ToastTheme) => void;
setQueue: (queue: Partial<ToastQueue>) => void;
}
interface ToasterOptions {
position?: ToastPosition;
theme?: ToastTheme;
duration?: number;
richColors?: boolean;
closeButton?: boolean;
className?: string;
style?: CSSProperties;
onClose?: () => void;
}
declare const ToasterProvider: React.FC<{
children: ReactNode;
}>;
declare const useToaster: () => ToasterContextValue;
export { Toast, ToastPosition, ToastType, ToasterOptions, ToasterProvider, useToaster };