@loke/design-system
Version:
A design system with individually importable components
182 lines (181 loc) • 7.09 kB
text/typescript
import React from "react";
import type { ToastToDismiss } from ".";
import type { PromiseT, ToastT, ToastTypes } from "./toast";
/**
* Type definition for toast titles
* Allows for both static React nodes and functions that return React nodes
*/
type titleT = (() => React.ReactNode) | React.ReactNode;
/**
* Extended promise result type for complex toast results
* Can be either a direct result object or a function that takes data and returns a result
*
* @template Data - The type of data that the promise resolves with
*/
type PromiseTExtendedResult<Data = unknown> = PromiseIExtendedResult | ((data: Data) => PromiseIExtendedResult | Promise<PromiseIExtendedResult>);
/**
* Promise result type for toast content
* Can be a string, React node, or a function that takes data and returns content
*
* @template Data - The type of data that the promise resolves with
*/
type PromiseTResult<Data = unknown> = string | React.ReactNode | ((data: Data) => React.ReactNode | string | Promise<React.ReactNode | string>);
/**
* Promise external toast type without description
* Used to allow overriding all properties except description
*/
type PromiseExternalToast = Omit<ExternalToast, "description">;
/**
* Configuration for toast notifications that display promise states
*/
type PromiseData<ToastData = unknown> = PromiseExternalToast & {
/**
* Content to display while the promise is loading
*/
loading?: string | React.ReactNode;
/**
* Content to display when the promise resolves successfully
*/
success?: PromiseTResult<ToastData> | PromiseTExtendedResult<ToastData>;
/**
* Content to display when the promise rejects
*/
error?: PromiseTResult | PromiseTExtendedResult;
/**
* Additional description content
*/
description?: PromiseTResult;
/**
* Function to execute after the promise settles
*/
finally?: () => void | Promise<void>;
};
/**
* External toast configuration type
* Represents the user-facing API for creating toasts with optional ID
*/
type ExternalToast = Omit<ToastT, "id" | "type" | "title" | "jsx" | "delete" | "promise"> & {
id?: number | string;
};
/**
* Interface for extended promise results
* Used to provide additional configuration options along with the message
*
* @property {React.ReactNode} message - The main content to display
*/
interface PromiseIExtendedResult extends ExternalToast {
message: React.ReactNode;
}
/**
* Observer class for managing toast notifications
* Implements the observer pattern to allow components to subscribe to toast updates
*/
declare class Observer {
/**
* List of functions that will be called when a toast is added or dismissed
*/
subscribers: Array<(toastData: ExternalToast | ToastToDismiss) => void>;
/**
* Collection of all toasts, including those that have been dismissed
*/
toasts: Array<ToastT | ToastToDismiss>;
/**
* Set of IDs for toasts that have been dismissed
*/
dismissedToasts: Set<string | number>;
/**
* Add a flag to track if a Toaster is mounted
*/
isToasterMounted: boolean;
/**
* Creates a new Observer instance and initializes collections
*/
constructor();
/**
* Mark a Toaster as mounted
*/
markToasterMounted: () => void;
/**
* Mark a Toaster as unmounted
*/
markToasterUnmounted: () => void;
/**
* Check if a Toaster is mounted before executing toast functions
*/
checkToasterMounted: () => boolean;
/**
* Registers a subscriber function to be called when toasts are added or dismissed
*/
subscribe: (subscriber: (toastData: ExternalToast | ToastToDismiss) => void) => () => void;
/**
* Publishes a toast notification to all subscribers
*/
publish: (data: ToastT) => void;
/**
* Adds a toast to the collection and publishes it to all subscribers
*/
addToast: (data: ToastT) => void;
/**
* Creates a new toast or updates an existing one with the same ID
*/
create: (data: ExternalToast & {
message?: titleT;
type?: ToastTypes;
promise?: PromiseT;
jsx?: React.ReactElement;
}) => string | number;
/**
* Dismisses a toast by ID or all toasts if no ID is provided
*/
dismiss: (id?: number | string) => string | number | undefined;
message: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
error: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
success: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
info: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
warning: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
loading: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
/**
* Creates a toast that updates based on the state of a promise
*/
promise: <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>) => (string & {
unwrap: () => Promise<ToastData>;
}) | (number & {
unwrap: () => Promise<ToastData>;
}) | {
unwrap: () => Promise<ToastData>;
} | undefined;
custom: (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => string | number;
/**
* Gets all toasts that haven't been dismissed
*/
getActiveToasts: () => (ToastT | ToastToDismiss)[];
}
/**
* Singleton instance of the Observer class to manage toast state globally
*/
export declare const ToastState: Observer;
/**
* Main toast API that combines all toast creation methods and utilities
* We use `Object.assign` to maintain the correct types as we would lose them otherwise
*/
export declare const toast: ((message: titleT, data?: ExternalToast) => string | number | undefined) & {
custom: (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => string | number;
dismiss: (id?: number | string) => string | number | undefined;
error: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
info: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
loading: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
message: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
promise: <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData> | undefined) => (string & {
unwrap: () => Promise<ToastData>;
}) | (number & {
unwrap: () => Promise<ToastData>;
}) | {
unwrap: () => Promise<ToastData>;
} | undefined;
success: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
warning: (message: titleT | React.ReactNode, data?: ExternalToast) => string | number;
} & {
getHistory: () => (ToastT | ToastToDismiss)[];
getToasts: () => (ToastT | ToastToDismiss)[];
};
export type { ExternalToast };