Skip to content

Request: mode for useTransition to wait for old values to leave before it inserts new values #583

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

Closed
chuckdries opened this issue Mar 7, 2019 · 5 comments
Labels
kind: request New feature or request that should be actioned

Comments

@chuckdries
Copy link

I'm basically asking for Vue's transition out-in mode, where the leaving state finishes before the entering value exists at all.

Toggle Between Components

Take the following example lifted from the useTransition docs:

const [toggle, set] = useState(false)
const transitions = useTransition(toggle, null, {
  from: { position: 'absolute', opacity: 0 },
  enter: { opacity: 1 },
  leave: { opacity: 0 },
})
return transitions.map(({ item, key, props }) => 
  item
    ? <animated.div style={props}>😄</animated.div>
    : <animated.div style={props}>🤪</animated.div>
)

when toggle flips from false to true, the transitions array looks like this over time:

1. [
     { item: false, state: 'leaving'},
     { item: true, state: 'entering'},
   ] 
2. [
     { item: true, state: 'updated'},
   ] 

With my hypothetical out-in mode, new values don't enter until old values leave. The transitions array would look like this:

1. [
     { item: false, state: 'leaving'},
   ]
2. [
     { item: true, state: 'entering'},
   ] 
3. [
     { item: true, state: 'updated'},
   ] 

In this example, only one item ever exists in the array at a time, but that's probably not true of all situations where this flag would work

Real life

My use case is a button which displays a call to action, then after clicked it displays a success confirmation. I want the states to fade, but I don't want them to fade over each other, I want the call to action to fade out, then the confirmation to fade in.

Below is a demonstration of the behavior I previously made by applying CSS classes with timeouts:

button

I ended up emulating this functionality multi stage transitions, but I had to add an unused stage to delay the enter, and interpolate a nonsense variable in that stage because if a stage doesn't need to change anything it ends immediately.

https://codesandbox.io/s/34q346788p?fontsize=14

I'm wondering if there's a better approach with the APIs we have today, but I would still like this hypothetical in-out mode as this behavior is one I frequently want.

Thank you for all of your hard work on this incredible library!

@chuckdries chuckdries changed the title Request: flag for useTransition to wait for leaves to finish before it inserts and starts enters P: flag for useTransition to wait for old values to leave before it inserts new values Mar 7, 2019
@chuckdries chuckdries changed the title P: flag for useTransition to wait for old values to leave before it inserts new values Request: mode for useTransition to wait for old values to leave before it inserts new values Mar 7, 2019
@drcmda
Copy link
Member

drcmda commented Mar 7, 2019

have you tried trail?

trail: 100,
order: ['leave', 'enter', 'update],

please let me know how it's going ...

@aleclarson aleclarson added kind: request New feature or request that should be actioned and removed question labels Jul 13, 2019
@aleclarson aleclarson reopened this Jul 13, 2019
@montogeek
Copy link

@chuckdries Did you found a solution? I have the same problem when umounting/fade left and mounting/fade in components, right now they overlap because the new one doesn't wait until the previous one fades out to fade in.

@aleclarson
Copy link
Contributor

aleclarson commented Sep 30, 2019

I don't think there's a solution for this yet. 😢

If anyone wants to get a PoC going, please build on top of #808 instead of master or v9.

@RemyMachado
Copy link

RemyMachado commented Feb 28, 2021

have you tried trail?

trail: 100,
order: ['leave', 'enter', 'update],

please let me know how it's going ...

I used your workaround (trail + order properties) and it solved the problem. I couldn't find this order property in the documentation though.

It's a shame that this workaround wouldn't work without a duration config that is equal to the trail config (the opposite of what this library intends to do with physic based animations).

My transition values sample in case it helps someone:
https://codesandbox.io/s/react-spring-single-component-transition-no-overlap-ou0xe?file=/src/App.js

{
    from: { fontSize: 0 },
    enter: { fontSize: 100 },
    leave: { fontSize: 0 },
    trail: ANIMATION_DURATION,
    config: { duration: ANIMATION_DURATION },
    order: ["leave", "enter", "update"]
}

Anyway, thanks.

@joshuaellis
Copy link
Member

Closing in favour of #1064 as that contains a POC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: request New feature or request that should be actioned
Projects
None yet
Development

No branches or pull requests

6 participants