A
A
Alexander2021-04-18 00:52:16
React
Alexander, 2021-04-18 00:52:16

How to save ReactJS modal fade animation?

How can I keep the modal's fading animation if the check is very necessary, but everything works fine without it?

export const ModalBlock: React.FC<ModalBlockProps> = ({
  title,
  onClose,
  isOpen = false,
  children,
}: ModalBlockProps): React.ReactElement | null => {
  if (!isOpen) {
    return null
  }
  return (
    <Dialog TransitionComponent={Transition} open={isOpen} aria-labelledby='form-dialog-title'>
      <DialogTitle id='form-dialog-title'>
        <IconButton onClick={onClose} color='secondary' aria-label='close'>
          <CloseIcon style={{ fontSize: 26 }} color='secondary' />
        </IconButton>
        {title}
      </DialogTitle>
      <DialogContent>{children}</DialogContent>
    </Dialog>
  )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-04-18
@axrising

export const ModalBlock: React.FC<ModalBlockProps> = ({
  title,
  onClose,
  isOpen = false,
  children,
}: ModalBlockProps): React.ReactElement | null => {
  const [closing, setClosing] = useState(false);
  const handleClose = () => setClosing(true);
  
  useEffect(() => {
    if (isOpen) {
      setClosing(false);
    }
  }, [isOpen]);

  if (!isOpen) {
    return null;
  }

  return (
    <Dialog TransitionComponent={Transition} open={!closing && isOpen} onExited={onClose} aria-labelledby='form-dialog-title'>
      <DialogTitle id='form-dialog-title'>
        <IconButton onClick={handleClose} color='secondary' aria-label='close'>
          <CloseIcon style={{ fontSize: 26 }} color='secondary' />
        </IconButton>
        {title}
      </DialogTitle>
      <DialogContent>{children}</DialogContent>
    </Dialog>
  );
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question