You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DialogTransition.tsx 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { ReactElement, useEffect, useState } from 'react';
  2. export const DialogTransitionContext = React.createContext({ isUnmounting: false });
  3. type TimeoutType = ReturnType<typeof setTimeout>;
  4. const DialogTransition = ({ children }: { children: ReactElement | null; }) => {
  5. const [ childrenToRender, setChildrenToRender ] = useState(children);
  6. const [ isUnmounting, setIsUnmounting ] = useState(false);
  7. const [ timeoutID, setTimeoutID ] = useState<TimeoutType | undefined>(undefined);
  8. useEffect(() => {
  9. if (children === null) {
  10. setIsUnmounting(true);
  11. if (typeof timeoutID === 'undefined') {
  12. setTimeoutID(setTimeout(() => {
  13. setChildrenToRender(children);
  14. setIsUnmounting(false);
  15. setTimeoutID(undefined);
  16. }, 150));
  17. }
  18. } else {
  19. if (typeof timeoutID !== 'undefined') {
  20. clearTimeout(timeoutID);
  21. setTimeoutID(undefined);
  22. setIsUnmounting(false);
  23. }
  24. setChildrenToRender(children);
  25. }
  26. }, [ children ]);
  27. return (
  28. <DialogTransitionContext.Provider value = {{ isUnmounting }}>
  29. {childrenToRender}
  30. </DialogTransitionContext.Provider>
  31. );
  32. };
  33. export default DialogTransition;