use-animate-presence icon indicating copy to clipboard operation
use-animate-presence copied to clipboard

Ability to call show/hide as well as togglePresence

Open bravokiloecho opened this issue 4 years ago • 4 comments

It would be useful to be able to call a function that would trigger the exit animation (eg, animatedDiv.hidePresence()) or the appear animation (eg, animatedDiv.showPresence()) as well as the animatedDiv.togglePresence()

Really nice library otherwise!

bravokiloecho avatar Sep 02 '20 15:09 bravokiloecho

Are there any use cases where it's too hard to just use togglePresence ?

Really nice library otherwise!

Thanks!

jlkiri avatar Sep 03 '20 13:09 jlkiri

Are there any use cases where it's too hard to just use togglePresence ?

I needed it to show/hide a component based on some state, where that state is not triggered by button click. For example (a simplified example)...

const showMessage = fieldOne && fieldTwo

React.useEffect(() => {
  if (showMessage) {
    showPresence()
    return
  }
  hidePresence()
}, [showMessage])

bravokiloecho avatar Sep 04 '20 07:09 bravokiloecho

I see! It can be pretty easily done with togglePresence but yeah maybe hide/show is a more intuitive API?

const SomeComponentWithProps = (props) => {
  const isFirstRender = React.useRef(true);
  const popup = useAnimatePresence({
    variants: popupVariants,
    initial: props.showPopup ? "visible" : "hidden",
    options: {
      stiffness: 200,
      mass: 3,
      damping: 30
    }
  });

  React.useEffect(() => {
    if (isFirstRender.current) {
      isFirstRender.current = false;
      return;
    }

    popup.togglePresence();
  }, [props.showPopup]);

  return popup.isRendered && <Toast ref={popup.ref} />;
};

Here's the working example: https://codesandbox.io/s/animate-popup-based-on-state-seu4r

jlkiri avatar Sep 04 '20 08:09 jlkiri

+1

jacobclyne avatar Jan 02 '21 16:01 jacobclyne