question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component Unmount: Allow non-duration based outro animations

See original GitHub issue

I am struggling to use a spring-based animation for component outros (unmounting the DOM element). As far as I can tell, the only way to defer/delay the unmount/outro of a component is to use the transition or out directive and return an transition object with a specified duration:

{
  duration: 400,
  easing: ...
}

This works great for easing functions, but I cannot use Svelte’s spring() function for the outro transition, because it has no fixed duration.


Proposal/Potential solution

My proposed solution is for the transition, in and out directives to accept a function that optionally returns a Promise instead of a Transition object. Once the Promise fulfills (or rejects), Svelte can remove the element from the DOM.

Example REPL for illustration: https://svelte.dev/repl/cc1467f851284051a11ca0325c2efd97?version=3.12.1

Alternative I have considered

One alternative I’ve considered, is pre-calculating a duration for the spring and using that as the duration value in the transition object. This approach has one disadvantage: springs are inherently dynamic, so the animation distance changes how long the spring takes to finish (its duration), which makes a static pre-calculation (i.e. one pre-calculated duration per spring-config) of the spring not possible.

It may be be possible to predict the duration relatively accurately based on the spring config (damping, stiffness, mass, …) and the travel distance, but that is a considerable amount of work for something which Just Works™ when using normal easing transitions.

Additional context

Svelte puts a lot of focus into “making UI interactions and animations as easy as possible”. For that matter, Svelte even provides its own Spring-based animation API. So it would make sense to allow to use Svelte’s spring() function (or third-party ones) for outro transitions.

If this addition is feasible and wanted, it may also make sense to revisit some of the built in animation functions like fade and slide to make it possible to use them also with a spring.

How important is this feature to me? Right now, I am trying to evaluate to port an existing project to Svelte. This existing project has spring-based outro animations in a few places (pages, menus). It’s not a dealbreaker, but seems like an unfortunate limitation.

In my opinion, spring interactions and animations are superior to fixed (duration based) easings and feel more intuitive.

Similar “defered unmount” discussion over at React: https://github.com/reactjs/rfcs/issues/128

Potential drawback: One drawback of the Promise based approach as I proposed it above is, that the author has to handle the animation themselves. I.e. I have to get a reference to the DOM element and do stuff with it, instead of letting Svelte handle it. But this may be necessary nontheless when the author wants to use a third-party library, such as Popmotion.

What about Intro transitions: In general, my proposal is also about intro animations. But it is not such a big of a problem, because you can roll your own intro animation logic within an onMount() handler. In the case of the outro animation, there is no real workaround that I am aware of, which is why I primarily focused on that.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
pushkinecommented, May 7, 2020

@russellgoldenberg it will come built in to #4742

3reactions
stale[bot]commented, Dec 28, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React - animate mount and unmount of a single component
Here is my solution using the new hooks API (with TypeScript), based on this post, for delaying the component's unmount phase:
Read more >
How to animate mount and unmount of a react component ...
In this video we add animations to the mount and unmount events of a react component using the react-spring animation library and hooks, ......
Read more >
Super easy react mount/unmount animations with hooks
As soon as the animation finishes, unmount the component. I want to show you the simplest way to accomplish this using pure CSS...
Read more >
Apple - Filter (19782 patents) - PatentInspiration
A user interface and methods for using a user interface for controlling processing of time-based media files. In one exemplary method, ...
Read more >
Sibelius Reference (version 8.3) - Avid Technology
reading, allowing you to jump to any chapter, topic, or even sub-heading in ... Convert chord symbol text updates text-based chord symbols from...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found