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

[core] Add useTransitionStatus and useExecuteIfNotAnimated Hooks #396

Merged
merged 10 commits into from May 23, 2024

Conversation

atomiks
Copy link
Contributor

@atomiks atomiks commented May 7, 2024

@atomiks atomiks added the core Infrastructure work going on behind the scenes label May 7, 2024
* @param isRendered - a boolean that determines if the component is rendered.
* @ignore - internal hook.
*/
export function useTransitionStatus(isRendered: boolean) {
Copy link
Member

Choose a reason for hiding this comment

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

How do you feel about returning props from this hook as well (onTransitionEnd and onAnimationEnd)? We may be able to omit setMounted from the returned object.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel that should be an implementation detail of the components that use this hook instead. This keeps its responsibility quite minimal.

@atomiks atomiks changed the title [core] Add useTransitionStatus Hook [core] Add useTransitionStatus and useAnimationUnmount Hooks May 22, 2024
// Notes:
// - A single `requestAnimationFrame` is sometimes unreliable.
// - `queueMicrotask` does not work.
frame1Ref.current = requestAnimationFrame(() => {
Copy link
Member

Choose a reason for hiding this comment

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

I'm wondering if two animation frames are guaranteed to be reliable.
If we detect an animation where there isn't one (the [data-instant] example), we may end up with an element waiting indefinitely to be unmounted, breaking the user interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Playing around with the transitions experiments demo with [data-instant], I wasn't ever able to reproduce it not unmounting. If there isn't any animation at all (no [data-instant] or anything), this won't be an issue at all.

* @ignore - internal hook.
*/
export function useAnimationUnmount(
getElement: () => Element | null | undefined,
Copy link
Member

Choose a reason for hiding this comment

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

Why is this a function and not a ref object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of the wrapper in anchored popups, it's the firstElementChild of the floating element.

The usage is:

const unmount = useAnimationUnmount(
  () => popupEl?.firstElementChild,
  () => setMounted(false)
);

* Unmounts the supplied element only if it has no animations or transitions.
* @ignore - internal hook.
*/
export function useAnimationUnmount(
Copy link
Member

Choose a reason for hiding this comment

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

I find it quite hard to understand the purpose of this function when reading the code. It doesn't really unmount anything (as the name would suggest), but it's a means of detecting if the element has any animation defined in its styles. Renaming it could make it easier to grasp.

Copy link
Contributor Author

@atomiks atomiks May 23, 2024

Choose a reason for hiding this comment

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

Yeah, I didn't really know what to call it without it being really long. It returns an unmount function, but it's simply guarded to only run if the element has no animations/transitions.

  • useUnmountIfNoAnimations
  • useCallIfNoAnimations (well, since the supplied function can be anything)

return React.useMemo(
() => ({
mounted,
setMounted,
Copy link
Member

Choose a reason for hiding this comment

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

Does it make sense to call setMounted(true) from outside of this hook? If not, we could return an onAnimationEnd callback that will call setMounted(false) (and maybe also setTransitionStatus(undefined)?)

Copy link
Contributor Author

@atomiks atomiks May 23, 2024

Choose a reason for hiding this comment

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

setMounted gives them the freedom to perform more complex checks and keeps its responsibility minimal. For example, due to the wrapper in anchored popups, the checks in animationend/transitionend may differ. This gives full control.

Example:

      function handleEnd({ target }: React.SyntheticEvent) {
        const popupElement = refs.floating.current?.firstElementChild;
        if (target === popupElement && setMounted) {
          setMounted((prevMounted) => (prevMounted ? false : prevMounted));
        }
      }

@atomiks atomiks changed the title [core] Add useTransitionStatus and useAnimationUnmount Hooks [core] Add useTransitionStatus and useExecuteIfNotAnimated Hooks May 23, 2024
Copy link
Member

@michaldudak michaldudak left a comment

Choose a reason for hiding this comment

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

All good!

@atomiks atomiks merged commit bf8fca5 into mui:master May 23, 2024
17 checks passed
@atomiks atomiks deleted the feat/useTransitionStatus branch May 23, 2024 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Infrastructure work going on behind the scenes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants