Skip to content

Commit

Permalink
fix: incorrect stacking (#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilkowalski authored Nov 2, 2024
1 parent 8d33b51 commit 51ed654
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ const Toast = (props: ToastProps) => {
setMounted(true);
}, []);

React.useEffect(() => {
const toastNode = toastRef.current;
if (toastNode) {
const height = toastNode.getBoundingClientRect().height;
// Add toast height tot heights array after the toast is mounted
setInitialHeight(height);
setHeights((h) => [{ toastId: toast.id, height, position: toast.position }, ...h]);
return () => setHeights((h) => h.filter((height) => height.toastId !== toast.id));
}
}, [setHeights, toast.id]);

React.useLayoutEffect(() => {
if (!mounted) return;
const toastNode = toastRef.current;
Expand Down Expand Up @@ -219,15 +230,21 @@ const Toast = (props: ToastProps) => {
function getLoadingIcon() {
if (icons?.loading) {
return (
<div className={cn(classNames?.loader, toast?.classNames?.loader, "sonner-loader")} data-visible={toastType === 'loading'}>
<div
className={cn(classNames?.loader, toast?.classNames?.loader, 'sonner-loader')}
data-visible={toastType === 'loading'}
>
{icons.loading}
</div>
);
}

if (loadingIconProp) {
return (
<div className={cn(classNames?.loader, toast?.classNames?.loader, "sonner-loader")} data-visible={toastType === 'loading'}>
<div
className={cn(classNames?.loader, toast?.classNames?.loader, 'sonner-loader')}
data-visible={toastType === 'loading'}
>
{loadingIconProp}
</div>
);
Expand Down Expand Up @@ -569,26 +586,26 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
try {
// Chrome & Firefox
darkMediaQuery.addEventListener('change', ({ matches }) => {
if (matches) {
setActualTheme('dark');
} else {
setActualTheme('light');
}
});
if (matches) {
setActualTheme('dark');
} else {
setActualTheme('light');
}
});
} catch (error) {
// Safari < 14
darkMediaQuery.addListener( ({ matches }) => {
darkMediaQuery.addListener(({ matches }) => {
try {
if (matches) {
setActualTheme('dark');
} else {
setActualTheme('light');
}
} catch(e) {
console.error(e)
} catch (e) {
console.error(e);
}
});
}
}
}, [theme]);

React.useEffect(() => {
Expand Down

0 comments on commit 51ed654

Please sign in to comment.