Skip to content

Commit

Permalink
Merge pull request #83 from xxmichas/main
Browse files Browse the repository at this point in the history
fix: blurry toasts, heights store
  • Loading branch information
wobsoriano authored Mar 30, 2024
2 parents ebc807c + e3ec6c7 commit a6cc46a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/dirty-pigs-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"svelte-sonner": patch
---

fix: blurry toasts, heights store
16 changes: 9 additions & 7 deletions src/lib/Toast.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
loading: ''
};
const { toasts, heights, removeHeight, addHeight, dismiss } = toastState;
const { toasts, heights, removeHeight, setHeight, dismiss } = toastState;
export let toast: $$Props['toast'];
export let index: $$Props['index'];
Expand Down Expand Up @@ -88,9 +88,8 @@
$: invert = toast.invert || invert;
$: disabled = toastType === 'loading';
$: {
offset = heightIndex * GAP + toastsHeightBefore;
}
// Sometimes toasts are blurry when offset isn't an int
$: offset = Math.round(heightIndex * GAP + toastsHeightBefore);
// Listen to height changes
async function updateHeights() {
Expand All @@ -109,10 +108,13 @@
initialHeight = newHeight;
addHeight({ toastId: toast.id, height: newHeight });
setHeight({ toastId: toast.id, height: newHeight });
}
$: mounted, toast.title, toast.description, updateHeights()
$: title = toast.title;
$: description = toast.description;
$: mounted, title, description, updateHeights();
function deleteToast() {
removed = true;
Expand Down Expand Up @@ -192,7 +194,7 @@
// Add toast height tot heights array after the toast is mounted
initialHeight = height;
addHeight({ toastId: toast.id, height });
setHeight({ toastId: toast.id, height });
return () => removeHeight(toast.id);
});
Expand Down
30 changes: 26 additions & 4 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ function createToastState() {
toasts.update((prev) =>
prev.map((toast) => {
if (toast.id === id) {
return { ...toast, ...data, id, title: message, dismissable, type, updated: true };
return {
...toast,
...data,
id,
title: message,
dismissable,
type,
updated: true
};
}
return {
...toast,
Expand Down Expand Up @@ -163,8 +171,22 @@ function createToastState() {
heights.update((prev) => prev.filter((height) => height.toastId !== id));
}

function addHeight(height: HeightT) {
heights.update((prev) => [height, ...prev]);
function setHeight(data: HeightT) {
const exists = get(heights).find((el) => el.toastId === data.toastId);
if (exists === undefined) {
heights.update((prev) => [data, ...prev]);
return;
}

heights.update((prev) =>
prev.map((el) => {
if (el.toastId === data.toastId) {
return data;
} else {
return el;
}
})
);
}

function reset() {
Expand All @@ -186,7 +208,7 @@ function createToastState() {
promise,
custom,
removeHeight,
addHeight,
setHeight,
reset,
// stores
toasts,
Expand Down

0 comments on commit a6cc46a

Please sign in to comment.