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.

Use transitions for nav page changes

Tim Wilsonfbe72940e3f7
diff --git a/apps/web/ui/layout/sidebar/sidebar-nav.tsx b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
index 22a1757ccd..b83b3dce03 100644
--- a/apps/web/ui/layout/sidebar/sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/sidebar-nav.tsx
@@ -1,11 +1,18 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { Wordmark } from "@dub/ui";
import { cn } from "@dub/utils";
-import { AnimatePresence, motion } from "framer-motion";
import { ChevronLeft } from "lucide-react";
import Link from "next/link";
import { useParams, usePathname } from "next/navigation";
-import { ReactNode, Suspense, useMemo, useState } from "react";
+import {
+ PropsWithChildren,
+ ReactNode,
+ Suspense,
+ useEffect,
+ useMemo,
+ useRef,
+ useState,
+} from "react";
import { ITEMS, type NavItem } from "./items";
import UserDropdown from "./user-dropdown";
import { WorkspaceDropdown } from "./workspace-dropdown";
@@ -25,94 +32,74 @@ export function SidebarNav({ toolContent }: { toolContent?: ReactNode }) {
: "default";
}, [slug, pathname]);
+ const [previousArea, setPreviousArea] = useState<
+ (typeof AREAS)[number] | null
+ >(currentArea);
+
+ useEffect(() => {
+ setPreviousArea(currentArea);
+ }, [currentArea]);
+
return (
<div className="relative p-3 text-gray-500">
<div className="relative flex items-start justify-between gap-1 pb-3">
- <AnimatePresence initial={false}>
- {AREAS.map((area) =>
- area === currentArea ? (
- <motion.div
- key={area}
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- exit={{
- opacity: 0,
- position: "absolute",
- }}
- transition={{ duration: 0.15, ease: "easeInOut" }}
- className="left-0 top-0"
- >
- <Link href={slug ? `/${slug}` : "/"}>
- {area === "default" ? (
- <div className="pb-1">
- <Wordmark className="ml-1 h-6" />
- </div>
- ) : (
- <div className="py group -my-1 flex items-center gap-2 py-2 text-sm font-medium text-neutral-900">
- <ChevronLeft className="size-4 text-neutral-500 transition-transform duration-100 group-hover:-translate-x-0.5" />
- Settings
- </div>
- )}
- </Link>
- </motion.div>
- ) : null,
- )}
- </AnimatePresence>
+ {AREAS.map((area) => (
+ <Link
+ href={slug ? `/${slug}` : "/"}
+ className={cn(
+ "transition-opacity",
+ area === currentArea
+ ? "relative opacity-100"
+ : "pointer-events-none absolute opacity-0",
+ )}
+ aria-hidden={area !== currentArea}
+ >
+ {area === "default" ? (
+ <div className="pb-1">
+ <Wordmark className="ml-1 h-6" />
+ </div>
+ ) : (
+ <div className="py group -my-1 flex items-center gap-2 py-2 text-sm font-medium text-neutral-900">
+ <ChevronLeft className="size-4 text-neutral-500 transition-transform duration-100 group-hover:-translate-x-0.5" />
+ Settings
+ </div>
+ )}
+ </Link>
+ ))}
<div className="hidden items-center gap-3 md:flex">
<Suspense fallback={null}>{toolContent}</Suspense>
<UserDropdown />
</div>
</div>
<div className="relative w-full">
- <AnimatePresence initial={false}>
- {AREAS.map((area) =>
- area === currentArea ? (
- <motion.div
- key={area}
- className="relative left-0 top-0 w-full"
- initial={{
- opacity: 0,
- x: area === "default" ? "-100%" : "100%",
- }}
- animate={{ opacity: 1, x: 0 }}
- exit={{
- opacity: 0,
- position: "absolute",
- x: area === "default" ? "-100%" : "100%",
- }}
- transition={{ duration: 0.15, ease: "easeInOut" }}
- >
- {area === "default" && (
- <div className="pt-2">
- <WorkspaceDropdown />
- </div>
- )}
+ {AREAS.map((area) => (
+ <Area
+ key={area}
+ visible={area === currentArea}
+ direction={area === "default" ? "left" : "right"}
+ >
+ {area === "default" && (
+ <div className="pt-2">
+ <WorkspaceDropdown />
+ </div>
+ )}
- <div className="flex flex-col gap-4 pt-4">
- {ITEMS[area].map(({ name, items }, idx) => (
- <div
- key={`${name}-${idx}`}
- className="flex flex-col gap-0.5"
- >
- {name && (
- <div className="mb-2 pl-1 text-sm text-neutral-500">
- {name}
- </div>
- )}
- {items({ slug: slug || "", flags }).map((item) => (
- <NavItem
- key={item.name}
- pathname={pathname}
- item={item}
- />
- ))}
+ <div className="flex flex-col gap-4 pt-4">
+ {ITEMS[area].map(({ name, items }, idx) => (
+ <div key={`${name}-${idx}`} className="flex flex-col gap-0.5">
+ {name && (
+ <div className="mb-2 pl-1 text-sm text-neutral-500">
+ {name}
</div>
+ )}
+ {items({ slug: slug || "", flags }).map((item) => (
+ <NavItem key={item.name} pathname={pathname} item={item} />
))}
</div>
- </motion.div>
- ) : null,
- )}
- </AnimatePresence>
+ ))}
+ </div>
+ </Area>
+ ))}
</div>
</div>
);
@@ -146,3 +133,29 @@ function NavItem({ pathname, item }: { pathname: string; item: NavItem }) {
</Link>
);
}
+
+export function Area({
+ visible,
+ direction,
+ children,
+}: PropsWithChildren<{ visible: boolean; direction: "left" | "right" }>) {
+ const ref = useRef<HTMLDivElement>(null);
+
+ return (
+ <div
+ ref={ref}
+ className={cn(
+ "left-0 top-0 w-full transition-[opacity,transform] duration-300",
+ visible
+ ? "opacity-1 relative"
+ : cn(
+ "pointer-events-none absolute opacity-0",
+ direction === "left" ? "-translate-x-full" : "translate-x-full",
+ ),
+ )}
+ area-hidden={!visible}
+ >
+ {children}
+ </div>
+ );
+}