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.

Update `@dub/ui` `Nav`

Tim Wilsonb1c54a06b687
diff --git a/packages/ui/src/nav/nav.tsx b/packages/ui/src/nav/nav.tsx
index ad94a209f4..9ec2000c6f 100644
--- a/packages/ui/src/nav/nav.tsx
+++ b/packages/ui/src/nav/nav.tsx
@@ -2,12 +2,13 @@
import { APP_DOMAIN, cn, createHref, fetcher } from "@dub/utils";
import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";
-import { cva } from "class-variance-authority";
+import { LayoutGroup, motion } from "framer-motion";
import Cookies from "js-cookie";
import Link from "next/link";
-import { useParams, useSelectedLayoutSegment } from "next/navigation";
-import { PropsWithChildren, createContext } from "react";
+import { useParams, usePathname } from "next/navigation";
+import { PropsWithChildren, createContext, useId } from "react";
import useSWR from "swr";
+import { buttonVariants } from "../button";
import { FEATURES_LIST, RESOURCES } from "../content";
import { useScroll } from "../hooks";
import { MaxWidthWrapper } from "../max-width-wrapper";
@@ -26,56 +27,50 @@ export const navItems = [
name: "Product",
content: ProductContent,
childItems: FEATURES_LIST,
+ segments: ["/", "/home", "/features"],
},
{
name: "Resources",
content: ResourcesContent,
childItems: RESOURCES,
+ segments: ["/solutions", "/blog", "/changelog", "/docs", "/help", "/brand"],
},
{
name: "Enterprise",
href: "/enterprise",
- },
- {
- name: "Customers",
- href: "/customers",
+ segments: ["/enterprise"],
},
{
name: "Pricing",
href: "/pricing",
+ segments: ["/pricing"],
},
];
-const navItemStyles = cva(
- [
- "block rounded-md px-3 py-1.5 text-sm text-gray-900/60 hover:text-gray-900/80",
- "dark:text-white/70 dark:hover:text-white",
- "transition-colors ease-out",
- ],
- {
- variants: {
- isActive: {
- true: "text-gray-900/80 dark:text-white",
- },
- },
- },
+const navItemClassName = cn(
+ "relative block rounded-md px-4 py-2 text-sm font-medium text-neutral-700 hover:text-neutral-900",
+ "dark:text-white/90 dark:hover:text-white",
+ "transition-colors",
);
export function Nav({
theme = "light",
staticDomain,
+ maxWidthWrapperClassName,
}: {
theme?: NavTheme;
staticDomain?: string;
+ maxWidthWrapperClassName?: string;
}) {
let { domain = "dub.co" } = useParams() as { domain: string };
if (staticDomain) {
domain = staticDomain;
}
- const scrolled = useScroll(80);
- const scrolledHalf = useScroll(40);
- const selectedLayout = useSelectedLayoutSegment();
+ const layoutGroupId = useId();
+
+ const scrolled = useScroll(40);
+ const pathname = usePathname();
const { data: session, isLoading } = useSWR(
domain.endsWith("dub.co") && "/api/auth/session",
fetcher,
@@ -92,130 +87,143 @@ export function Nav({
return (
<NavContext.Provider value={{ theme }}>
- <div
- className={cn(
- `sticky inset-x-0 top-0 z-30 w-full transition-all`,
- theme === "dark" && "dark",
- )}
- >
- {/* Mobile background */}
+ <LayoutGroup id={layoutGroupId}>
<div
className={cn(
- "absolute inset-0 block transition-all lg:hidden",
- scrolledHalf && "bg-white/75 backdrop-blur-md dark:bg-black/75",
+ `sticky inset-x-0 top-0 z-30 w-full transition-all`,
+ theme === "dark" && "dark",
)}
- />
- <MaxWidthWrapper className="relative">
- <div className="flex h-14 items-center justify-between">
- <Link
- className="grow basis-0"
- href={createHref("/home", domain, {
- utm_source: "Custom Domain",
- utm_medium: "Navbar",
- utm_campaign: domain,
- utm_content: "Logo",
- })}
- >
- <NavWordmark />
- </Link>
- <NavigationMenuPrimitive.Root
- delayDuration={0}
- className="relative hidden lg:block"
- >
- <NavigationMenuPrimitive.List className="relative flex flex-row gap-2 px-2 py-0.5">
- {/* Desktop background */}
- <div className="absolute inset-0 -z-[1]">
- <div
+ >
+ {/* Scrolled background */}
+ <div
+ className={cn(
+ "absolute inset-0 block border-b border-transparent transition-all",
+ scrolled &&
+ "border-neutral-100 bg-white/75 backdrop-blur-lg dark:border-white/10 dark:bg-black/75",
+ )}
+ />
+ <MaxWidthWrapper className={cn("relative", maxWidthWrapperClassName)}>
+ <div className="flex h-14 items-center justify-between">
+ <Link
+ className="grow basis-0"
+ href={createHref("/home", domain, {
+ utm_source: "Custom Domain",
+ utm_medium: "Navbar",
+ utm_campaign: domain,
+ utm_content: "Logo",
+ })}
+ >
+ <NavWordmark />
+ </Link>
+ <NavigationMenuPrimitive.Root
+ delayDuration={0}
+ className="relative hidden lg:block"
+ >
+ <NavigationMenuPrimitive.List className="relative z-0 flex">
+ {navItems.map(
+ ({ name, href, segments, content: Content }) => {
+ console.log(pathname, segments);
+ const isActive = segments.some((segment) =>
+ segment === "/"
+ ? pathname === segment
+ : pathname?.startsWith(segment),
+ );
+ return (
+ <NavigationMenuPrimitive.Item key={name}>
+ <WithTrigger trigger={!!Content}>
+ {href !== undefined ? (
+ <Link
+ id={`nav-${href}`}
+ href={createHref(href, domain, {
+ utm_source: "Custom Domain",
+ utm_medium: "Navbar",
+ utm_campaign: domain,
+ utm_content: name,
+ })}
+ className={navItemClassName}
+ >
+ {name}
+ {isActive && <Indicator />}
+ </Link>
+ ) : (
+ <button className={navItemClassName}>
+ {name}
+ {isActive && <Indicator />}
+ </button>
+ )}
+ </WithTrigger>
+
+ {Content && (
+ <NavigationMenuPrimitive.Content className="data-[motion=from-start]:animate-enter-from-left data-[motion=from-end]:animate-enter-from-right data-[motion=to-start]:animate-exit-to-left data-[motion=to-end]:animate-exit-to-right absolute left-0 top-0">
+ <Content domain={domain} />
+ </NavigationMenuPrimitive.Content>
+ )}
+ </NavigationMenuPrimitive.Item>
+ );
+ },
+ )}
+ </NavigationMenuPrimitive.List>
+
+ <div className="absolute left-1/2 top-full mt-3 -translate-x-1/2">
+ <NavigationMenuPrimitive.Viewport
className={cn(
- "absolute left-1/2 top-1/2 h-full w-full -translate-x-1/2 -translate-y-1/2 rounded-full border border-gray-200 bg-white/75 drop-shadow-sm backdrop-blur-lg transition-all dark:border-white/10 dark:bg-black/75",
- scrolled && "h-14 w-screen rounded-none drop-shadow-none",
+ "relative flex origin-[top_center] justify-start overflow-hidden rounded-[20px] border border-gray-200 bg-white shadow-md dark:border-white/[0.15] dark:bg-black",
+ "data-[state=closed]:animate-scale-out-content data-[state=open]:animate-scale-in-content",
+ "h-[var(--radix-navigation-menu-viewport-height)] w-[var(--radix-navigation-menu-viewport-width)] transition-[width,height]",
)}
/>
</div>
- {navItems.map(({ name, href, content: Content }) => {
- const isActive =
- href === `/${selectedLayout}` ||
- (href === "/" && selectedLayout === null);
- return (
- <NavigationMenuPrimitive.Item key={name}>
- <WithTrigger trigger={!!Content}>
- {href !== undefined ? (
- <Link
- id={`nav-${href}`}
- href={createHref(href, domain, {
- utm_source: "Custom Domain",
- utm_medium: "Navbar",
- utm_campaign: domain,
- utm_content: name,
- })}
- className={navItemStyles({ isActive })}
- >
- {name}
- </Link>
- ) : (
- <button className={navItemStyles({ isActive })}>
- {name}
- </button>
- )}
- </WithTrigger>
-
- {Content && (
- <NavigationMenuPrimitive.Content className="data-[motion=from-start]:animate-enter-from-left data-[motion=from-end]:animate-enter-from-right data-[motion=to-start]:animate-exit-to-left data-[motion=to-end]:animate-exit-to-right absolute left-0 top-0">
- <Content domain={domain} />
- </NavigationMenuPrimitive.Content>
- )}
- </NavigationMenuPrimitive.Item>
- );
- })}
- </NavigationMenuPrimitive.List>
-
- <div className="absolute left-1/2 top-full mt-3 -translate-x-1/2">
- <NavigationMenuPrimitive.Viewport
- className={cn(
- "relative flex origin-[top_center] justify-start overflow-hidden rounded-[20px] border border-gray-200 bg-white shadow-md dark:border-white/[0.15] dark:bg-black",
- "data-[state=closed]:animate-scale-out-content data-[state=open]:animate-scale-in-content",
- "h-[var(--radix-navigation-menu-viewport-height)] w-[var(--radix-navigation-menu-viewport-width)] transition-[width,height]",
- )}
- />
- </div>
- </NavigationMenuPrimitive.Root>
+ </NavigationMenuPrimitive.Root>
- <div className="hidden grow basis-0 justify-end lg:flex">
- {session && Object.keys(session).length > 0 ? (
- <Link
- href={APP_DOMAIN}
- className="animate-fade-in rounded-full border border-black bg-black px-4 py-1.5 text-sm text-white transition-all hover:bg-gray-800 hover:ring-4 hover:ring-gray-200 dark:border-white dark:bg-white dark:text-gray-600 dark:hover:bg-white dark:hover:text-gray-800 dark:hover:hover:shadow-[0_0_25px_5px_rgba(256,256,256,0.2)] dark:hover:ring-0"
- >
- Dashboard
- </Link>
- ) : !isLoading ? (
- <>
+ <div className="hidden grow basis-0 justify-end gap-2 lg:flex">
+ {session && Object.keys(session).length > 0 ? (
<Link
- href={
- hasDubCookie
- ? "https://app.dub.co/login"
- : "https://d.to/login"
- }
- className="animate-fade-in rounded-full px-4 py-1.5 text-sm font-medium text-gray-500 transition-colors ease-out hover:text-black dark:text-white dark:hover:text-white/70"
- >
- Log in
- </Link>
- <Link
- href={
- hasDubCookie
- ? "https://app.dub.co/register"
- : "https://d.to/register"
- }
- className="animate-fade-in rounded-full border border-black bg-black px-4 py-1.5 text-sm text-white transition-all hover:bg-gray-800 hover:ring-4 hover:ring-gray-200 dark:border-white dark:bg-white dark:text-gray-600 dark:hover:bg-white dark:hover:text-gray-800 dark:hover:hover:shadow-[0_0_25px_5px_rgba(256,256,256,0.2)] dark:hover:ring-0"
+ href={APP_DOMAIN}
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "flex h-8 items-center rounded-lg border px-4 text-sm",
+ "dark:border-white dark:bg-white dark:text-black dark:hover:bg-neutral-50 dark:hover:ring-white/10",
+ )}
>
- Sign Up
+ Dashboard
</Link>
- </>
- ) : null}
+ ) : !isLoading ? (
+ <>
+ <Link
+ href={
+ hasDubCookie
+ ? "https://app.dub.co/login"
+ : "https://d.to/login"
+ }
+ className={cn(
+ buttonVariants({ variant: "secondary" }),
+ "flex h-8 items-center rounded-lg border px-4 text-sm",
+ "dark:border-white/10 dark:bg-black dark:text-white dark:hover:bg-neutral-900",
+ )}
+ >
+ Log in
+ </Link>
+ <Link
+ href={
+ hasDubCookie
+ ? "https://app.dub.co/register"
+ : "https://d.to/register"
+ }
+ className={cn(
+ buttonVariants({ variant: "primary" }),
+ "flex h-8 items-center rounded-lg border px-4 text-sm",
+ "dark:border-white dark:bg-white dark:text-black dark:hover:bg-neutral-50 dark:hover:ring-white/10",
+ )}
+ >
+ Sign up
+ </Link>
+ </>
+ ) : null}
+ </div>
</div>
- </div>
- </MaxWidthWrapper>
- </div>
+ </MaxWidthWrapper>
+ </div>
+ </LayoutGroup>
</NavContext.Provider>
);
}
@@ -232,3 +240,15 @@ function WithTrigger({
children
);
}
+
+function Indicator() {
+ return (
+ <motion.div
+ layoutId="indicator"
+ transition={{
+ duration: 0.1,
+ }}
+ className="pointer-events-none absolute inset-0 -z-[1] rounded-lg bg-neutral-900/5 dark:bg-white/10"
+ />
+ );
+}