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

Conversation

BlueGreenMagick
Copy link

Fixes #51

Change toast's height property when toast wrapper div's height changes.

Note that we cannot use below one liner:

$: clientHeight !== undefined && setHeight(clientHeight);

because setHeight is re-created whenever toasts is modified.

In Action

Screen.Recording.2024-08-13.at.9.51.43.PM.mov

You can test it with following example code:

toast.promise(promise, {
  loading: 'Saving...',
  success: `Settings saved! And some additional text that is very long and takes up multiple lines`,
  error: `Could not save. An enormous wall of error message is to be placed here, making the toast taller`
});

Copy link

vercel bot commented Aug 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
svelte-french-toast ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 13, 2024 0:59am

Copy link
Owner

@kbrgl kbrgl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your PR!

Curious—what happens on mount? Is there a flash of unstyled content if the initial height is undefined?

@BlueGreenMagick
Copy link
Author

BlueGreenMagick commented Aug 15, 2024

TLDR: There shouldn't be any observable difference for toasts whose height don't change.

  1. It is very likely that <div bind:clientHeight /> sets clientHeight in the same JS task as mount, right after onMount() is run. So there shouldn't be any observable difference for toasts whose height don't change.

    If we run the below code, we can observe that "heightChange" is printed between "mount" and "next microtask". So either onHeightChange() is run after onMount() synchronously, or its microtask is already queued at the time onMount() is run.

    Therefore, there is no browser paint after component mount, but before height is set.

function onHeightChange(clientHeight: number) {
  console.log("heightChange", clientHeight)
  if (clientHeight === undefined) return;
  setHeight(clientHeight);
}

onMount(async () => {
  console.log("mount");
  await Promise.resolve();
  console.log("next microtask")	
})
  1. Even if the browser actually paints a frame when the toast's height is not set, toasts are set to transparent if their height is falsy. So it doesn't cause jitters or flashes.

    class="base {toast.height ? animation : 'transparent'} {toast.className || ''}"

    The other place that uses .height property is in calculateOffset(), which skips over toasts whose height is falsy.
    So having toasts with undefined height does not cause any issues.

The only difference from this PR that I can think of for fixed-height toast, is when a toast is queued while executing a long-running task. Before this PR, toast's duration is measured from mount. With this PR, toast's duration is measured from add, so toasts may be destroyed before it has a chance to render. Would that be an issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Toast offset should update when toast updates
2 participants