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

Handle dynamic change to toast height #75

Open
wants to merge 2 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
13 changes: 8 additions & 5 deletions src/lib/components/ToastWrapper.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
export let toast: DOMToast;
export let setHeight: (height: number) => void;

let wrapperEl: HTMLElement;
onMount(() => {
setHeight(wrapperEl.getBoundingClientRect().height);
});
let clientHeight: number;

function onHeightChange(clientHeight: number) {
if (clientHeight === undefined) return;
setHeight(clientHeight);
}

$: onHeightChange(clientHeight);
$: top = toast.position?.includes('top') ? 0 : null;
$: bottom = toast.position?.includes('bottom') ? 0 : null;
$: factor = toast.position?.includes('top') ? 1 : -1;
Expand All @@ -24,7 +27,7 @@
</script>

<div
bind:this={wrapperEl}
bind:clientHeight
class="wrapper"
class:active={toast.visible}
class:transition={!prefersReducedMotion()}
Expand Down
4 changes: 2 additions & 2 deletions src/lib/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const clearFromRemoveQueue = (toastId: string) => {
}
};

export function update(toast: Partial<Toast>) {
if (toast.id) {
export function update(toast: Partial<Toast>, clearTimeout = true) {
if (clearTimeout && toast.id) {
clearFromRemoveQueue(toast.id);
}
toasts.update(($toasts) => $toasts.map((t) => (t.id === toast.id ? { ...t, ...toast } : t)));
Expand Down
2 changes: 1 addition & 1 deletion src/lib/core/use-toaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const handlers = {
_endPause(Date.now());
},
updateHeight: (toastId: string, height: number) => {
update({ id: toastId, height });
update({ id: toastId, height }, false);
},
calculateOffset
};
Expand Down