Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

936 B
Raw Blame
import { AnimatePresence, motion } from "motion/react";
import { ReactNode, useEffect, useState } from "react";

export const ClientOnly = ({
  children,
  fallback,
  fadeInDuration = 0.5,
  className,
}: {
  children: ReactNode;
  fallback?: ReactNode;
  fadeInDuration?: number;
  className?: string;
}) => {
  const [clientReady, setClientReady] = useState<boolean>(false);

  useEffect(() => {
    setClientReady(true);
  }, []);

  const Comp = fadeInDuration ? motion.div : "div";

  return (
    <AnimatePresence>
      {clientReady ? (
        <Comp
          {...(fadeInDuration
            ? {
                initial: { opacity: 0 },
                animate: { opacity: 1 },
                transition: { duration: fadeInDuration },
              }
            : {})}
          className={className}
        >
          {children}
        </Comp>
      ) : (
        fallback || null
      )}
    </AnimatePresence>
  );
};