Skip to content

Instantly share code, notes, and snippets.

@brandonpittman
Last active February 1, 2024 04:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brandonpittman/3a869011ba67f16bde8de67ef299e0ed to your computer and use it in GitHub Desktop.
Save brandonpittman/3a869011ba67f16bde8de67ef299e0ed to your computer and use it in GitHub Desktop.
`usePresence` with Tailwind animation utilities
import { AnimatePresence, usePresence } from "framer-motion";
import { classNames } from "~/lib/utils/class-names";
import { useCounter } from "@kyleshevlin/use-common";
import { useCallback } from "react";
const Box = ({ count }: { count: number }) => {
const [isPresent, safeToRemove] = usePresence();
const onAnimationEnd = useCallback(() => {
if (!isPresent) safeToRemove();
}, [isPresent, safeToRemove]);
return (
<p
className={classNames(isPresent ? "animate-fade-in" : "animate-fade-out")}
onAnimationEnd={onAnimationEnd}
>
Hello, there! I'm #{count}.
</p>
);
};
export default function Playground() {
const [count, { inc }] = useCounter(1);
return (
<div className="flex flex-col justify-center items-center w-full gap-16">
<button onClick={inc}>Next &rarr;</button>
<AnimatePresence exitBeforeEnter>
<Box key={count} count={count} />
</AnimatePresence>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment