An opinionated toast component for React.
Programmatically remove toast
emilkowalskib4c8f34aba4c
diff --git a/package.json b/package.jsonindex 3e92886..ce7e14b 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "sonner",- "version": "0.1.5",+ "version": "0.1.1","description": "An opinionated toast component for React.","main": "dist/index.js","module": "dist/index.mjs",diff --git a/src/index.tsx b/src/index.tsxindex e0f188b..f5b5526 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -4,7 +4,7 @@ import React from 'react';import './styles.css';import { getAsset, Loader } from './assets';-import { HeightT, Position, ToastT } from './types';+import { HeightT, Position, ToastT, ToastToDismiss } from './types';import { ToastState, toast } from './state';// Visible toasts amount@@ -318,13 +318,10 @@ const Toast = (props: ToastProps) => {<div data-content=""><div data-title="">{toast.title ?? promiseTitle}</div>{toast.description ? (- <div- data-description=""- className={descriptionClassName + toastDescriptionClassname}- >+ <div data-description="" className={descriptionClassName + toastDescriptionClassname}>{toast.description}</div>- ) : null}+ ) : null}</div>{toast.cancel ? (<button@@ -410,6 +407,13 @@ const Toaster = (props: ToasterProps) => {React.useEffect(() => {return ToastState.subscribe((toast) => {+ if ((toast as ToastToDismiss).dismiss) {+ console.log(toast.id);++ removeToast(toast);+ return;+ }+setToasts((toasts) => [toast, ...toasts]);});}, []);diff --git a/src/state.ts b/src/state.tsindex 51039da..49a2fa6 100644--- a/src/state.ts+++ b/src/state.ts@@ -1,17 +1,17 @@import React from 'react';-import { ExternalToast, ToastT, PromiseData, PromiseT } from './types';+import { ExternalToast, ToastT, PromiseData, PromiseT, ToastToDismiss } from './types';let toastsCounter = 0;class Observer {- subscribers: Array<(toast: ExternalToast) => void>;+ subscribers: Array<(toast: ExternalToast | ToastToDismiss) => void>;constructor() {this.subscribers = [];}// We use arrow functions to maintain the correct `this` reference- subscribe = (subscriber: (toast: ToastT) => void) => {+ subscribe = (subscriber: (toast: ToastT | ToastToDismiss) => void) => {this.subscribers.push(subscriber);return () => {@@ -24,20 +24,33 @@ class Observer {this.subscribers.forEach((subscriber) => subscriber(data));};+ dismiss = (id: number) => {+ this.subscribers.forEach((subscriber) => subscriber({ id, dismiss: true }));+ return id;+ };+message = (message: string, data?: ExternalToast) => {- this.publish({ ...data, id: toastsCounter++, title: message });+ const id = toastsCounter++;+ this.publish({ ...data, id, title: message });+ return id;};error = (message: string, data?: ExternalToast) => {- this.publish({ ...data, id: toastsCounter++, type: 'error', title: message });+ const id = toastsCounter++;+ this.publish({ ...data, id, type: 'error', title: message });+ return id;};success = (message: string, data?: ExternalToast) => {- this.publish({ ...data, id: toastsCounter++, type: 'success', title: message });+ const id = toastsCounter++;+ this.publish({ ...data, id, type: 'success', title: message });+ return id;};promise = (promise: PromiseT, data?: PromiseData) => {- this.publish({ promiseData: data, promise, id: toastsCounter++ });+ const id = toastsCounter++;+ this.publish({ promiseData: data, promise, id });+ return id;};// We can't provide the toast we just created as a prop as we didn't creat it yet, so we can create a default toast object, I just don't know how to use function in argument when calling()?@@ -51,11 +64,13 @@ export const ToastState = new Observer();// bind this to the toast functionconst toastFunction = (message: string, data?: ExternalToast) => {+ const id = toastsCounter++;ToastState.publish({title: message,...data,- id: toastsCounter++,+ id,});+ return id;};const basicToast = toastFunction;@@ -67,4 +82,5 @@ export const toast = Object.assign(basicToast, {custom: ToastState.custom,message: ToastState.message,promise: ToastState.promise,+ dismiss: ToastState.dismiss,});diff --git a/src/types.ts b/src/types.tsindex d309986..9775edd 100644--- a/src/types.ts+++ b/src/types.ts@@ -49,4 +49,9 @@ export enum SwipeStateTypes {export type Theme = 'light' | 'dark';+export interface ToastToDismiss {+ id: number;+ dismiss: boolean;+}+export type ExternalToast = Omit<ToastT, 'id' | 'type' | 'title'>;diff --git a/website/src/components/Hero/index.tsx b/website/src/components/Hero/index.tsxindex 28c77b9..ab75a6b 100644--- a/website/src/components/Hero/index.tsx+++ b/website/src/components/Hero/index.tsx@@ -13,6 +13,17 @@ export const Hero = () => {<h1 className={styles.heading}>Sonner</h1><p style={{ marginTop: 0, fontSize: 18 }}>An opinionated toast component for React.</p><div className={styles.buttons}>+ <button+ onClick={() => {+ const toastId = toast('hello');+ console.log(toastId);+ setTimeout(() => {+ toast.dismiss(toastId);+ }, 200);+ }}+ >+ dismiss+ </button><buttondata-primary=""onClick={() => {