An opinionated toast component for React.
Update examples
emilkowalskidcdebffec6c1
diff --git a/src/index.tsx b/src/index.tsxindex 3ba803b..a2796e7 100644--- a/src/index.tsx+++ b/src/index.tsx@@ -288,7 +288,7 @@ const Toast = (props: ToastProps) => {toastRef.current?.style.setProperty('--swipe-amount', `${yPosition}px`);}}>- {closeButton ? (+ {closeButton && !toast.jsx ? (<buttonaria-label="Close toast"data-disabled={disabled}diff --git a/src/state.ts b/src/state.tsindex 49a2fa6..0655d08 100644--- a/src/state.ts+++ b/src/state.ts@@ -54,9 +54,9 @@ class Observer {};// 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()?- custom = (jsx: (id: number) => React.ReactElement) => {+ custom = (jsx: (id: number) => React.ReactElement, data?: ExternalToast) => {const id = toastsCounter++;- this.publish({ jsx: jsx(id), id });+ this.publish({ jsx: jsx(id), id, ...data });};}diff --git a/src/styles.css b/src/styles.cssindex 0f2c45c..000556b 100644--- a/src/styles.css+++ b/src/styles.css@@ -247,7 +247,7 @@transition: opacity 400ms;}-[data-sonner-toast][data-expanded='false'][data-front='false'] > * {+[data-sonner-toast][data-expanded='false'][data-front='false'][data-custom='false'] > * {opacity: 0;}diff --git a/website/src/components/Other/Other.tsx b/website/src/components/Other/Other.tsxindex 29a07b2..0284f29 100644--- a/website/src/components/Other/Other.tsx+++ b/website/src/components/Other/Other.tsx@@ -2,6 +2,7 @@ import React from 'react';import { useMemo } from 'react';import { toast } from 'sonner';import { CodeBlock } from '../CodeBlock';+import styles from './other.module.css';export const Other = ({setRichColors,@@ -32,7 +33,7 @@ export const Other = ({name: 'Close Button',snippet: `toast('Event has been created', {description: 'Monday, January 3rd at 6:00pm',-}),`,+})`,action: () => {toast('Event has been created', {description: 'Monday, January 3rd at 6:00pm',@@ -40,6 +41,32 @@ export const Other = ({setCloseButton((t) => !t);},},+ {+ name: 'Headless',+ snippet: `toast.custom((t) => (+ <div>+ <h1>Custom toast</h1>+ <button onClick={() => toast.dismiss(t)}>Dismiss</button>+ </div>+));`,+ action: () => {+ toast.custom(+ (t) => (+ <div className={styles.headless}>+ <p className={styles.headlessTitle}>Event Created</p>+ <p className={styles.headlessDescription}>Today at 4:00pm - "Louvre Museum"</p>+ <button className={styles.headlessClose} onClick={() => toast.dismiss(t)}>+ <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">+ <path d="M2.96967 2.96967C3.26256 2.67678 3.73744 2.67678 4.03033 2.96967L8 6.939L11.9697 2.96967C12.2626 2.67678 12.7374 2.67678 13.0303 2.96967C13.3232 3.26256 13.3232 3.73744 13.0303 4.03033L9.061 8L13.0303 11.9697C13.2966 12.2359 13.3208 12.6526 13.1029 12.9462L13.0303 13.0303C12.7374 13.3232 12.2626 13.3232 11.9697 13.0303L8 9.061L4.03033 13.0303C3.73744 13.3232 3.26256 13.3232 2.96967 13.0303C2.67678 12.7374 2.67678 12.2626 2.96967 11.9697L6.939 8L2.96967 4.03033C2.7034 3.76406 2.6792 3.3474 2.89705 3.05379L2.96967 2.96967Z"></path>+ </svg>+ </button>+ </div>+ ),+ { duration: 999999 },+ );+ setCloseButton((t) => !t);+ },+ },],[setRichColors],);diff --git a/website/src/components/Other/other.module.css b/website/src/components/Other/other.module.cssnew file mode 100644index 0000000..169ea7e--- /dev/null+++ b/website/src/components/Other/other.module.css@@ -0,0 +1,45 @@+.headless {+ padding: 16px;+ width: 356px;+ box-sizing: border-box;+ border-radius: 8px;+ background: var(--gray1);+ border: 1px solid var(--gray4);+ position: relative;+}++.headlessDescription {+ margin: 0;+ color: var(--gray10);+ font-size: 14px;+ line-height: 1;+}++.headlessTitle {+ font-size: 14px;+ margin: 0 0 8px;+ color: var(--gray12);+ font-weight: 500;+ line-height: 1;+}++.headlessClose {+ position: absolute;+ cursor: pointer;+ top: 6px;+ height: 24px;+ width: 24px;+ display: flex;+ justify-content: center;+ align-items: center;+ right: 6px;+ color: var(--gray10);+ padding: 0;+ background: transparent;+ border: none;+ transition: color 200ms;+}++.headlessClose:hover {+ color: var(--gray12);+}