Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski committed Feb 23, 2024
1 parent 24aa153 commit 4588e14
Showing 1 changed file with 7 additions and 38 deletions.
45 changes: 7 additions & 38 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,33 +437,6 @@ function getDocumentDirection(): ToasterProps['dir'] {
return dirAttribute as ToasterProps['dir'];
}

function useSonner() {
const [activeToasts, setActiveToasts] = React.useState<ToastT[]>([]);

React.useEffect(() => {
return ToastState.subscribe((toast) => {
setActiveToasts((currentToasts) => {
if ('dismiss' in toast && toast.dismiss) {
return currentToasts.filter((t) => t.id !== toast.id);
}

const existingToastIndex = currentToasts.findIndex((t) => t.id === toast.id);
if (existingToastIndex !== -1) {
const updatedToasts = [...currentToasts];
updatedToasts[existingToastIndex] = { ...updatedToasts[existingToastIndex], ...toast };
return updatedToasts;
} else {
return [toast, ...currentToasts];
}
});
});
}, []);

return {
toasts: activeToasts,
};
}

const Toaster = (props: ToasterProps) => {
const {
invert,
Expand Down Expand Up @@ -500,10 +473,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 All @@ -512,12 +485,8 @@ const Toaster = (props: ToasterProps) => {
const isFocusWithinRef = React.useRef(false);

const removeToast = React.useCallback(
(toastToRemove: ToastT) => {
if (!toasts.find((toast) => toast.id === toastToRemove.id)?.delete) {
ToastState.dismiss(toastToRemove.id);
}
},
[toasts],
(toast: ToastT) => setToasts((toasts) => toasts.filter(({ id }) => id !== toast.id)),
[],
);

React.useEffect(() => {
Expand Down Expand Up @@ -721,4 +690,4 @@ const Toaster = (props: ToasterProps) => {
</section>
);
};
export { toast, Toaster, type ToastT, type ExternalToast, useSonner };
export { toast, Toaster, type ToastT, type ExternalToast };

0 comments on commit 4588e14

Please sign in to comment.