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

Introduce unstyled prop #187

Merged
merged 2 commits into from
Oct 15, 2023
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
3 changes: 2 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface ToastProps {
style?: React.CSSProperties;
duration?: number;
className?: string;
unstyled?: boolean;
descriptionClassName?: string;
}

Expand Down Expand Up @@ -213,7 +214,7 @@ const Toast = (props: ToastProps) => {
ref={toastRef}
className={className + ' ' + toastClassname}
data-sonner-toast=""
data-styled={!Boolean(toast.jsx)}
data-styled={!Boolean(toast.jsx || toast.unstyled)}
data-mounted={mounted}
data-promise={Boolean(toast.promise)}
data-removed={removed}
Expand Down
64 changes: 13 additions & 51 deletions src/state.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import React from 'react';
import type {
ExternalToast,
ToastT,
PromiseData,
PromiseT,
ToastToDismiss,
ToastTypes,
} from './types';
import type { ExternalToast, ToastT, PromiseData, PromiseT, ToastToDismiss, ToastTypes } from './types';

let toastsCounter = 1;

Expand All @@ -20,9 +13,7 @@ class Observer {
}

// We use arrow functions to maintain the correct `this` reference
subscribe = (
subscriber: (toast: ToastT | ToastToDismiss) => void,
) => {
subscribe = (subscriber: (toast: ToastT | ToastToDismiss) => void) => {
this.subscribers.push(subscriber);

return () => {
Expand All @@ -48,15 +39,11 @@ class Observer {
},
) => {
const { message, ...rest } = data;
const id =
typeof data?.id === 'number' || data.id?.length > 0
? data.id
: toastsCounter++;
const id = typeof data?.id === 'number' || data.id?.length > 0 ? data.id : toastsCounter++;
const alreadyExists = this.toasts.find((toast) => {
return toast.id === id;
});
const dismissible =
data.dismissible === undefined ? true : data.dismissible;
const dismissible = data.dismissible === undefined ? true : data.dismissible;

if (alreadyExists) {
this.toasts = this.toasts.map((toast) => {
Expand All @@ -83,50 +70,31 @@ class Observer {
dismiss = (id?: number | string) => {
if (!id) {
this.toasts.forEach((toast) => {
this.subscribers.forEach((subscriber) =>
subscriber({ id: toast.id, dismiss: true }),
);
this.subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
});
}

this.subscribers.forEach((subscriber) =>
subscriber({ id, dismiss: true }),
);
this.subscribers.forEach((subscriber) => subscriber({ id, dismiss: true }));
return id;
};

message = (
message: string | React.ReactNode,
data?: ExternalToast,
) => {
message = (message: string | React.ReactNode, data?: ExternalToast) => {
return this.create({ ...data, message });
};

error = (
message: string | React.ReactNode,
data?: ExternalToast,
) => {
error = (message: string | React.ReactNode, data?: ExternalToast) => {
return this.create({ ...data, message, type: 'error' });
};

success = (
message: string | React.ReactNode,
data?: ExternalToast,
) => {
success = (message: string | React.ReactNode, data?: ExternalToast) => {
return this.create({ ...data, type: 'success', message });
};

loading = (
message: string | React.ReactNode,
data?: ExternalToast,
) => {
loading = (message: string | React.ReactNode, data?: ExternalToast) => {
return this.create({ ...data, type: 'loading', message });
};

promise = <ToastData>(
promise: PromiseT<ToastData>,
data?: PromiseData<ToastData>,
) => {
promise = <ToastData>(promise: PromiseT<ToastData>, data?: PromiseData<ToastData>) => {
if (!data) {
// Nothing to show
return;
Expand Down Expand Up @@ -174,10 +142,7 @@ class Observer {
};

// We can't provide the toast we just created as a prop as we didn't create 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 | string) => React.ReactElement,
data?: ExternalToast,
) => {
custom = (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => {
const id = data?.id || toastsCounter++;
this.publish({ jsx: jsx(id), id, ...data });
};
Expand All @@ -186,10 +151,7 @@ class Observer {
export const ToastState = new Observer();

// bind this to the toast function
const toastFunction = (
message: string | React.ReactNode,
data?: ExternalToast,
) => {
const toastFunction = (message: string | React.ReactNode, data?: ExternalToast) => {
const id = data?.id || toastsCounter++;

ToastState.addToast({
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface ToastT {
onAutoClose?: (toast: ToastT) => void;
promise?: PromiseT;
style?: React.CSSProperties;
unstyled?: boolean;
className?: string;
descriptionClassName?: string;
position?: Position;
Expand All @@ -51,6 +52,7 @@ interface ToastOptions {
descriptionClassName?: string;
style?: React.CSSProperties;
duration?: number;
unstyled?: boolean;
}

export interface ToasterProps {
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/toast.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,4 @@ toast.dismiss(toastId);
| id | Custom id for the toast. | `-` |
| onDismiss | The function gets called when either the close button is clicked, or the toast is swiped. | `-` |
| onAutoClose | Function that gets called when the toast disappears automatically after it's timeout (duration` prop). | `-` |
| unstyled | Removes the default styling, which allows for easier customization. | `false` |