Skip to content

Commit

Permalink
feat: add support for custom close icon (#481)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandernanberg authored Sep 10, 2024
1 parent ee55353 commit 8fe7045
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
17 changes: 17 additions & 0 deletions src/assets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,20 @@ const ErrorIcon = (
/>
</svg>
);

export const CloseIcon = (
<svg
xmlns="http://www.w3.org/2000/svg"
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
);
17 changes: 2 additions & 15 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import React from 'react';
import ReactDOM from 'react-dom';

import { getAsset, Loader } from './assets';
import { CloseIcon, getAsset, Loader } from './assets';
import { useIsDocumentHidden } from './hooks';
import { toast, ToastState } from './state';
import './styles.css';
Expand Down Expand Up @@ -358,20 +358,7 @@ const Toast = (props: ToastProps) => {
}
className={cn(classNames?.closeButton, toast?.classNames?.closeButton)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
{icons?.close ?? CloseIcon}
</button>
) : null}
{toast.jsx || React.isValidElement(toast.title) ? (
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface ToastIcons {
warning?: React.ReactNode;
error?: React.ReactNode;
loading?: React.ReactNode;
close?: React.ReactNode;
}

export interface Action {
Expand Down
26 changes: 26 additions & 0 deletions test/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ export default function Home({ searchParams }: any) {
>
ReactNode Description
</button>
<button
data-testid="close-button"
className="button"
onClick={() => toast('Toast with close button', { closeButton: true })}
>
Render close button
</button>
{showAutoClose ? <div data-testid="auto-close-el" /> : null}
{showDismiss ? <div data-testid="dismiss-el" /> : null}
<Toaster
Expand All @@ -179,6 +186,25 @@ export default function Home({ searchParams }: any) {
}}
theme={theme}
dir={searchParams.dir || 'auto'}
icons={{
close:
searchParams.customCloseIcon === '' ? (
<svg
xmlns="http://www.w3.org/2000/svg"
width="12"
height="12"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="3"
strokeLinecap="round"
strokeLinejoin="round"
>
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
) : undefined,
}}
/>
</>
);
Expand Down

0 comments on commit 8fe7045

Please sign in to comment.