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

feat: add closeIcon option #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions docs/src/content/docs/toast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ The `toast.variant` function accepts the following options:
| `text` | Notification title | `string` | ✅ |
| `description` | Toast's description | `string` | - |
| `icon` | Icon to display in the toast | `ReactNode` | - |
| `closeIcon` | Icon to change Close text | `ReactNode` | - |
| `delayDuration` | Duration before the toast disappears | `number` (default: `4000`) | - |
| `theme` | Theme of the toast | `Theme` (default: `system`): `light`, `dark` or `system` | - |
| `action` | Show a _Action_ button and execute a function | `label` (default text: `action`) & `onClick`: `() => void` or `Promise<void>` | - |
17 changes: 16 additions & 1 deletion examples/nextjs/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { testAction, testErrorAction } from '@/actions/testAction';
import { toast } from '@pheralb/toast';
import { PartyPopperIcon } from 'lucide-react';
import { PartyPopperIcon, XCircle } from 'lucide-react';
import { useTheme } from 'next-themes';
import { useState } from 'react';

Expand All @@ -20,6 +20,15 @@ export default function Home() {
});
};

const renderToastWithCloseIcon = () => {
toast.default({
closeIcon: <XCircle width={18} height={18} />,
text: 'Rendered toast!',
description: 'This is a toast with close icon!',
delayDuration: duration,
});
};

const renderToastWithIcon = () => {
toast.default({
text: 'Rendered toast without icon',
Expand Down Expand Up @@ -111,6 +120,12 @@ export default function Home() {
<button className={buttonStyles} onClick={() => testLoading()}>
Test Loading with Next.js Server Action
</button>

<button className={buttonStyles} onClick={() => renderToastWithCloseIcon()}>
Test with close icon
</button>


</>
);
}
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pheralb/toast",
"version": "0.2.3",
"version": "0.2.4",
"author": "@pheralb_",
"keywords": [
"react",
Expand Down
12 changes: 9 additions & 3 deletions library/src/components/toast.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect, useState, type FC } from 'react';
import '../styles/toast-component.css';
import type {
Position,
ToastPropsWithLoading,
Variant,
} from '../types/toast.types';
import '../styles/toast-component.css';

import { Error, Info, Loading, Success, Warning } from '../icons';
import { useTimeout } from '../hooks/useTimeout';
import { Error, Info, Loading, Success, Warning } from '../icons';
import { classNames, prefersReducedMotion } from '../utils';

const icons: Record<Variant, FC<React.SVGProps<SVGSVGElement>>> = {
Expand All @@ -16,6 +16,7 @@ const icons: Record<Variant, FC<React.SVGProps<SVGSVGElement>>> = {
warning: Warning,
info: Info,
loading: Loading,

};

const iconsColors: Record<Variant, string> = {
Expand All @@ -24,6 +25,7 @@ const iconsColors: Record<Variant, string> = {
warning: '#eab308',
info: '#3b82f6',
loading: 'currentColor',

};

interface ToastComponentProps extends ToastPropsWithLoading {
Expand Down Expand Up @@ -174,7 +176,11 @@ const Toast = (props: ToastComponentProps) => {
</button>
)}
<button onClick={handleCloseToast} title="Close toast">
Close
{props.closeIcon ?
props.closeIcon
: 'Close'
}

</button>
</div>
</div>
Expand Down
12 changes: 11 additions & 1 deletion library/src/icons/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ const Loading: FC<ComponentProps<'svg'>> = (props) => (
</svg>
);

export { Success, Warning, Error, Info, Loading };

const Close: FC<ComponentProps<'svg'>> = (props) => (
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" {...props}>
<path fill="currentColor" d="M10.03 8.97a.75.75 0 0 0-1.06 1.06L10.94 12l-1.97 1.97a.75.75 0 1 0 1.06 1.06L12 13.06l1.97 1.97a.75.75 0 0 0 1.06-1.06L13.06 12l1.97-1.97a.75.75 0 1 0-1.06-1.06L12 10.94z"></path><path fill="currentColor" fillRule="evenodd" d="M12 1.25C6.063 1.25 1.25 6.063 1.25 12S6.063 22.75 12 22.75S22.75 17.937 22.75 12S17.937 1.25 12 1.25M2.75 12a9.25 9.25 0 1 1 18.5 0a9.25 9.25 0 0 1-18.5 0" clipRule="evenodd"></path>
</svg>
);



export { Close, Error, Info, Loading, Success, Warning };

9 changes: 5 additions & 4 deletions library/src/types/toast.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ export type ToastProps = {
text: string;
description?: string;
icon?: ReactNode;
closeIcon?: ReactNode;
delayDuration?: number;
theme?: Theme;
action?: Action;
};

export interface LoadingType {
promise:
| (() => Promise<void>)
| Promise<void>
| (() => Promise<any>)
| Promise<unknown>;
| (() => Promise<void>)
| Promise<void>
| (() => Promise<any>)
| Promise<unknown>;
success: string;
error: string;
autoDismiss: boolean;
Expand Down