rodal icon indicating copy to clipboard operation
rodal copied to clipboard

Feature request: unmountOnExit

Open NeXTs opened this issue 4 years ago • 1 comments

Hello

For now, the content of the modal is rendered in the DOM even when it is hidden. To get around this, I have to add an extra condition, but it breaks the closing animation (closes instantly).

{isVisible && <SomeModal
  visible={isVisible}
  hide={this.hideModal}
/>}

How about adding the unmountOnExit option, as in react-transition-group?

<SomeModal
  visible={isVisible}
  hide={this.hideModal}
  unmountOnExit={true}
/>

NeXTs avatar Jul 21 '20 12:07 NeXTs

I found a proper solution for this case and works great Just use this hook useDelayedUnmount

useDelayedUnmount.tsx

import { useEffect, useState } from "react";

export function useDelayUnmount(isMounted: boolean, delayTime: number) {
  const [shouldRender, setShouldRender] = useState(false);

  useEffect(() => {
    let timeoutId: number;
    if (isMounted && !shouldRender) {
      setShouldRender(true);
    } else if (!isMounted && shouldRender) {
      timeoutId = setTimeout(() => setShouldRender(false), delayTime);
    }
    return () => clearTimeout(timeoutId);
  }, [isMounted, delayTime, shouldRender]);
  
  return shouldRender;
}

Then in your component

import { useDelayedUnmount } from "@Hooks/useDelayedUnmount"
// ...
const [isVisibile, setIsVisible] = useState(false);
const shouldRenderPopup = useDelayUnmount(isModalOpen, 500);
// ...
{shouldRenderPopup && <SomeModal
  visible={isVisible}
  hide={this.hideModal}
/>}

Migggz avatar Nov 23 '20 11:11 Migggz