Last active
February 1, 2024 04:23
-
-
Save brandonpittman/3a869011ba67f16bde8de67ef299e0ed to your computer and use it in GitHub Desktop.
`usePresence` with Tailwind animation utilities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 →</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