Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to customize icons from toaster #325

Merged
merged 2 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const Toast = (props: ToastProps) => {
loadingIcon: loadingIconProp,
expandByDefault,
classNames,
icons,
closeButtonAriaLabel = 'Close toast',
pauseWhenPageIsHidden,
} = props;
Expand Down Expand Up @@ -213,6 +214,14 @@ const Toast = (props: ToastProps) => {
}, [deleteToast, toast.delete]);

function getLoadingIcon() {
if (icons?.loading) {
return (
<div className="loader" data-visible={toastType === 'loading'}>
{icons.loading}
</div>
);
}

if (loadingIconProp) {
return (
<div className="sonner-loader" data-visible={toastType === 'loading'}>
Expand All @@ -235,6 +244,7 @@ const Toast = (props: ToastProps) => {
toastClassname,
classNames?.toast,
toast?.classNames?.toast,
classNames?.default,
classNames?.[toastType],
toast?.classNames?.[toastType],
)}
Expand Down Expand Up @@ -352,8 +362,9 @@ const Toast = (props: ToastProps) => {
<>
{toastType || toast.icon || toast.promise ? (
<div data-icon="">
{(toast.promise || toast.type === 'loading') && !toast.icon ? getLoadingIcon() : null}
{toast.icon || getAsset(toastType)}
{toast.promise || toastType === 'loading'
? toast.icon || icons?.loading || getLoadingIcon()
: toast.icon || icons?.[toastType] || getAsset(toastType)}
</div>
) : null}

Expand Down Expand Up @@ -443,6 +454,7 @@ const Toaster = (props: ToasterProps) => {
dir = getDocumentDirection(),
gap,
loadingIcon,
icons,
containerAriaLabel = 'Notifications',
pauseWhenPageIsHidden,
} = props;
Expand All @@ -459,10 +471,10 @@ const Toaster = (props: ToasterProps) => {
theme !== 'system'
? theme
: typeof window !== 'undefined'
? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'
: 'light',
? window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light'
: 'light',
);

const listRef = React.useRef<HTMLOListElement>(null);
Expand Down Expand Up @@ -642,6 +654,7 @@ const Toaster = (props: ToasterProps) => {
.map((toast, index) => (
<Toast
key={toast.id}
icons={icons}
index={index}
toast={toast}
duration={toastOptions?.duration ?? duration}
Expand All @@ -658,8 +671,8 @@ const Toaster = (props: ToasterProps) => {
cancelButtonStyle={toastOptions?.cancelButtonStyle}
actionButtonStyle={toastOptions?.actionButtonStyle}
removeToast={removeToast}
toasts={toasts.filter(t => t.position == toast.position)}
heights={heights.filter(h => h.position == toast.position)}
toasts={toasts.filter((t) => t.position == toast.position)}
heights={heights.filter((h) => h.position == toast.position)}
setHeights={setHeights}
expandByDefault={expand}
gap={gap}
Expand Down
18 changes: 18 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export interface ToastClassnames {
default?: string;
}

export interface ToastIcons {
success?: React.ReactNode;
info?: React.ReactNode;
warning?: React.ReactNode;
error?: React.ReactNode;
loading?: React.ReactNode;
}

export interface ToastT {
id: number | string;
title?: string | React.ReactNode;
Expand Down Expand Up @@ -99,7 +107,16 @@ export interface ToasterProps {
style?: React.CSSProperties;
offset?: string | number;
dir?: 'rtl' | 'ltr' | 'auto';
/**
* @deprecated Please use the `icons` prop instead:
* ```jsx
* <Toaster
* icons={{ loading: <LoadingIcon /> }}
* />
* ```
*/
loadingIcon?: React.ReactNode;
icons?: ToastIcons;
containerAriaLabel?: string;
pauseWhenPageIsHidden?: boolean;
}
Expand Down Expand Up @@ -128,6 +145,7 @@ export interface ToastProps {
descriptionClassName?: string;
loadingIcon?: React.ReactNode;
classNames?: ToastClassnames;
icons?: ToastIcons;
closeButtonAriaLabel?: string;
pauseWhenPageIsHidden: boolean;
}
Expand Down
24 changes: 24 additions & 0 deletions website/src/pages/styling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,27 @@ Styling per toast type is also possible.
}}
/>
```

## Changing Icons

You can change the default icons using the `icons` prop:

```jsx
<Toaster
icons={{
success: <SuccessIcon />,
info: <InfoIcon />,
warning: <WarningIcon />,
error: <ErrorIcon />,
loading: <LoadingIcon />,
}}
/>
```

You can also set an icon for each toast:

```jsx
toast('Hello World', {
icon: <Icon />,
});
```
1 change: 1 addition & 0 deletions website/src/pages/toaster.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ Changes the directionality of the toast's text.
| gap | Gap between toasts when expanded | `14` |
| loadingIcon | Changes the default loading icon | `-` |
| pauseWhenPageIsHidden | Pauses toast timers when the page is hidden, e.g., when the tab is backgrounded, the browser is minimized, or the OS is locked. | `false` |
| icons | Changes the default icons | `-` |
Loading