@pheralb/toast
Version:
A lightweight, customizable, and dependency-free notification library for React.
77 lines (72 loc) • 2.56 kB
text/typescript
import { ReactNode } from 'react';
import * as react_jsx_runtime from 'react/jsx-runtime';
type Variant = "success" | "error" | "warning" | "info" | "loading";
type Position = "top-left" | "top-right" | "top-center" | "bottom-left" | "bottom-right" | "bottom-center";
type Theme = "light" | "dark" | "system";
interface Action {
content?: string | ReactNode;
onClick: () => void | (() => Promise<void>);
}
type ToastIcons = Record<Variant, ReactNode>;
type ToastProps = {
id?: number;
text: string;
description?: string;
icon?: ReactNode;
delayDuration?: number;
theme?: Theme;
action?: Action;
};
interface LoadingType<T = unknown> {
promise: (() => Promise<T>) | Promise<T>;
success: string;
error: string;
autoDismiss: boolean;
onSuccess?: (data: T) => void;
onError?: (error: Error) => void;
}
interface ToastActionsCustomClassnames {
container: string;
closeBtn: string;
actionBtn: string;
}
interface ToastClassnames {
toast?: string;
container?: string;
icon?: string;
content?: string;
actions?: ToastActionsCustomClassnames;
}
type ToastAnimations = "slide" | "swipe";
type ToastOptions = {
animationOnClose?: ToastAnimations;
font?: string;
icons?: ToastIcons;
headless?: boolean;
classNames?: ToastClassnames;
defaultActionContent?: string | ReactNode;
defaultCloseContent?: string | ReactNode;
};
type ToasterProperties = {
theme?: Theme;
maxToasts?: number;
position?: Position;
toastOptions?: ToastOptions;
};
interface ToastPropsWithVariant extends ToastProps {
variant?: Variant;
}
interface ToastPropsWithLoading extends ToastPropsWithVariant {
options?: LoadingType;
}
interface ToastFunctions {
default: (data: ToastPropsWithVariant) => ToastPropsWithVariant;
success: (data: ToastPropsWithVariant) => ToastPropsWithVariant;
error: (data: ToastPropsWithVariant) => ToastPropsWithVariant;
warning: (data: ToastPropsWithVariant) => ToastPropsWithVariant;
info: (data: ToastPropsWithVariant) => ToastPropsWithVariant;
loading: (data: ToastPropsWithLoading) => ToastPropsWithLoading;
}
declare const toast: ToastFunctions;
declare const Toaster: ({ maxToasts, position, theme, toastOptions, }: ToasterProperties) => false | react_jsx_runtime.JSX.Element;
export { type ToastAnimations, type ToastOptions, type Position as ToastPosition, type ToastProps as ToastProperties, type Theme as ToastTheme, type Variant as ToastVariant, Toaster, type ToasterProperties, toast };