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

Add animation complete callbacks #7630

Open
cprussin opened this issue Jan 17, 2025 · 5 comments · May be fixed by #7633
Open

Add animation complete callbacks #7630

cprussin opened this issue Jan 17, 2025 · 5 comments · May be fixed by #7633
Labels
enhancement New feature or request

Comments

@cprussin
Copy link

Provide a general summary of the feature here

I'd like a callback that I can execute when animations end for things like Modal in react-aria-components

🤔 Expected Behavior?

Ideal would be a set of props like onEnterComplete and onExitComplete which would accept functions and call them when the relevant animations are finished.

Barring that, a stable / documented way to hook into the animationend event; I can't seem to find a great way to do it due to how the components are getting unmounted inside of react-aria-components.

😯 Current Behavior

As far as I know there's no way to trigger such events aside from just setting a timeout equal to the length of the animation.

💁 Possible Solution

No response

🔦 Context

I have a few use cases that this would help with:

  1. When clicking on a link in an overlay modal, I want to close the menu first, and THEN trigger the route change
  2. I have some page styling that I want to apply whenever the overlay is visible at all, including when it's animating in AND out

💻 Examples

No response

🧢 Your Company/Team

Douro Labs

🕷 Tracking Issue

No response

@nwidynski
Copy link
Contributor

The implementation around animations was recently changed to support CSS transitions, hence why you won't find references to the animationend event anymore. If you are looking to hook into that event exclusively, you can refer to the previous implementation here. These hooks are meant to run in the (inner) component that is getting (un-)mounted.

That being said, if you want to contribute, it should be fairly trivial to add something like onEnterEnd and onExitEnd by chaining the callback to the onEnd argument of useAnimation here and here.

cprussin added a commit to cprussin/react-spectrum that referenced this issue Jan 18, 2025
This PR adds `onEnterComplete` and `onExitComplete` callbacks to components in
`react-aria-components` which have enter and exit animations.

Closes adobe#7630.
@cprussin cprussin linked a pull request Jan 18, 2025 that will close this issue
5 tasks
@cprussin
Copy link
Author

@nwidynski thanks for the pointers, contributed in #7633. Let me know if there's anything I need to do to get that to be accepted and merged!

@nwidynski
Copy link
Contributor

@cprussin Thank you very much 👍

It's likely the core team will ask you to move the AnimationProps inside @react-aria/utils or @react-types/utils and I also believe adding onEnd as the 3rd argument instead of the 2nd would be more inline with useAnimations's signature while also making sure this change doesn't break existing implementations.

For additional feedback I'm sure the core maintainers will get back to you, as they decide whether these changes make it 🚀

cprussin added a commit to cprussin/react-spectrum that referenced this issue Jan 19, 2025
This PR adds `onEnterComplete` and `onExitComplete` callbacks to components in
`react-aria-components` which have enter and exit animations.

Closes adobe#7630.
@cprussin
Copy link
Author

Great suggestions, thanks @nwidynski ! I implemented both; I'll hold tight and see what the core maintainers have to say.

Thanks again!

@LFDanLu LFDanLu added the enhancement New feature or request label Jan 21, 2025
@snowystinger
Copy link
Member

Can you use the web apis which returns a promise when the animation completes?
You can use ref.current.getAnimations
https://developer.mozilla.org/en-US/docs/Web/API/Element/getAnimations#examples

In our animation hooks, we think the callback would fire a bit too early, as it'd happen after only one frame. We could consider a convenience API for this, but we'd need to listen for the same thing I mentioned in the place where 'onEnd' is fired in your PR, then once the promise resolves is when we'd actually call the prop.
It may also be a bit different for keyframe animations, adding some complexity.

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

Successfully merging a pull request may close this issue.

4 participants