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 toast type function (success, danger, warning) #191

Open
wants to merge 3 commits into
base: master
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ toast.show("Task finished successfully", {
});
```

### success(), danger(), warning()
```js
toast.success("success");
toast.danger("danger");
toast.warning("warning");
```

### update()

```js
Expand Down
2 changes: 1 addition & 1 deletion src/hook/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import ToastContainer from "../toast-container";

export type ToastType = Pick<
ToastContainer,
"show" | "update" | "hide" | "hideAll" | "isOpen"
"show" | "success" | "danger" | "warning" | "update" | "hide" | "hideAll" | "isOpen"
>;

const ToastContext = React.createContext({} as ToastType);
Expand Down
65 changes: 41 additions & 24 deletions src/toast-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,22 @@ class ToastContainer extends Component<Props, State> {
/**
* Shows a new toast. Returns id
*/
show = (message: string | JSX.Element, toastOptions?: ToastOptions) => {
let id = toastOptions?.id || Math.random().toString();
const onDestroy = () => {
toastOptions?.onClose && toastOptions?.onClose();
this.setState({ toasts: this.state.toasts.filter((t) => t.id !== id) });
};
show = (message: string | JSX.Element, toastOptions?: ToastOptions) => this.renderToast(message, toastOptions)

requestAnimationFrame(() => {
this.setState({
toasts: [
{
id,
onDestroy,
message,
open: true,
onHide: () => this.hide(id),
...this.props,
...toastOptions,
},
...this.state.toasts.filter((t) => t.open),
],
});
});
/**
* Shows a new success toast. Returns id
*/
success = (message: string | JSX.Element, toastOptions?: ToastOptions) => this.renderToast(message, { type: "success", ...toastOptions })

return id;
};
/**
* Shows a new danger toast. Returns id
*/
danger = (message: string | JSX.Element, toastOptions?: ToastOptions) => this.renderToast(message, { type: "danger", ...toastOptions })

/**
* Shows a new warning toast. Returns id
*/
warning = (message: string | JSX.Element, toastOptions?: ToastOptions) => this.renderToast(message, { type: "warning", ...toastOptions })

/**
* Updates a toast, To use this create you must pass an id to show method first, then pass it here to update the toast.
Expand Down Expand Up @@ -109,6 +99,33 @@ class ToastContainer extends Component<Props, State> {
return this.state.toasts.some((t) => t.id === id && t.open);
}

renderToast(message: string | JSX.Element, toastOptions?: ToastOptions) {
let id = toastOptions?.id || Math.random().toString();
const onDestroy = () => {
toastOptions?.onClose && toastOptions?.onClose();
this.setState({ toasts: this.state.toasts.filter((t) => t.id !== id) });
};

requestAnimationFrame(() => {
this.setState({
toasts: [
{
id,
onDestroy,
message,
open: true,
onHide: () => this.hide(id),
...this.props,
...toastOptions,
},
...this.state.toasts.filter((t) => t.open),
],
});
});

return id;
}

renderBottomToasts() {
const { toasts } = this.state;
let { offset, offsetBottom } = this.props;
Expand Down