The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
fixed nav bugs
Steven Tey16b2ef21b97b
diff --git a/packages/ui/package.json b/packages/ui/package.jsonindex e6243d6e89..78626b06f9 100644--- a/packages/ui/package.json+++ b/packages/ui/package.json@@ -1,7 +1,7 @@{"name": "@dub/ui","description": "UI components for Dub",- "version": "0.3.14",+ "version": "0.3.17","sideEffects": false,"main": "./dist/index.js","module": "./dist/index.mjs",diff --git a/packages/ui/src/nav/nav.tsx b/packages/ui/src/nav/nav.tsxindex b2780742c3..18f4f74a8c 100644--- a/packages/ui/src/nav/nav.tsx+++ b/packages/ui/src/nav/nav.tsx@@ -2,7 +2,7 @@import { APP_DOMAIN, cn, createHref, fetcher } from "@dub/utils";import * as NavigationMenuPrimitive from "@radix-ui/react-navigation-menu";-import { LayoutGroup, motion } from "motion/react";+import { motion } from "motion/react";import Link from "next/link";import { useParams, usePathname } from "next/navigation";import {@@ -10,7 +10,6 @@ import {ReactNode,SVGProps,createContext,- useId,useRef,useState,} from "react";@@ -96,10 +95,30 @@ export const navItems = [];const navItemClassName = cn(- "relative group/item flex items-center rounded-md px-3 h-8 text-sm rounded-lg font-medium text-neutral-700 hover:text-neutral-900 transition-colors",+ "relative group/item flex items-center rounded-md px-3 h-8 text-sm rounded-lg font-medium text-neutral-700 hover:text-neutral-900 transition-[color]","dark:text-white/90 dark:hover:text-white",+ // Idle active state — hidden whenever the floating pill is showing+ "data-[active=true]:bg-neutral-900/5 dark:data-[active=true]:bg-white/10",+ "group-data-[pill=true]/nav:data-[active=true]:bg-transparent",);+function getHoverStyle(list: HTMLElement, item: HTMLElement) {+ const listRect = list.getBoundingClientRect();+ const itemRect = item.getBoundingClientRect();+ return {+ left: itemRect.left - listRect.left,+ width: itemRect.width,+ opacity: 1,+ };+}++const pillSpring = {+ type: "spring" as const,+ stiffness: 400,+ damping: 30,+ opacity: { duration: 0 },+};+export function Nav({theme = "light",staticDomain,@@ -116,7 +135,6 @@ export function Nav({domain = staticDomain;}- const layoutGroupId = useId();const navListRef = useRef<HTMLUListElement>(null);const [hoverStyle, setHoverStyle] = useState({left: 0,@@ -134,191 +152,180 @@ export function Nav({},);- const moveHoverTo = (el: HTMLElement) => {+ const showHover = (item: HTMLElement) => {const list = navListRef.current;if (!list) return;- const listRect = list.getBoundingClientRect();- const itemRect = el.getBoundingClientRect();- setHoverStyle({- left: itemRect.left - listRect.left,- width: itemRect.width,- opacity: 1,- });+ setHoverStyle(getHoverStyle(list, item));};- const clearHover = () => {+ const hideHover = () => {setHoverStyle((style) => ({ ...style, opacity: 0 }));};- const handleNavMouseLeave = () => {- const openTrigger =- navListRef.current?.querySelector<HTMLElement>("[data-state=open]");- if (openTrigger) {- moveHoverTo(openTrigger);- } else {- clearHover();- }- };-return (<NavContext.Provider value={{ theme }}>- <LayoutGroup id={layoutGroupId}>+ <div+ className={cn(+ `sticky inset-x-0 top-0 z-30 w-full transition-all`,+ theme === "dark" && "dark",+ )}+ >+ {/* Scrolled background */}<divclassName={cn(- `sticky inset-x-0 top-0 z-30 w-full transition-all`,- theme === "dark" && "dark",+ "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",)}- >- {/* 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">- <div className="grow basis-0">- {logo ?? (- <Link- className="block w-fit py-2 pr-2"- href={createHref("/home", domain, {- utm_source: "Custom Domain",- utm_medium: "Navbar",- utm_campaign: domain,- utm_content: "Logo",- })}- >- <NavWordmark />- </Link>- )}- </div>+ />+ <MaxWidthWrapper className={cn("relative", maxWidthWrapperClassName)}>+ <div className="flex h-14 items-center justify-between">+ <div className="grow basis-0">+ {logo ?? (+ <Link+ className="block w-fit py-2 pr-2"+ href={createHref("/home", domain, {+ utm_source: "Custom Domain",+ utm_medium: "Navbar",+ utm_campaign: domain,+ utm_content: "Logo",+ })}+ >+ <NavWordmark />+ </Link>+ )}+ </div>- <NavigationMenuPrimitive.Root- delayDuration={0}- className="hidden lg:block"- onValueChange={(value) => {- if (!value) clearHover();+ <NavigationMenuPrimitive.Root+ delayDuration={0}+ className="hidden lg:block"+ onValueChange={(value) => {+ // Closing a dropdown while moving to another nav item shouldn't clear the pill+ if (!value && !navListRef.current?.matches(":hover")) {+ hideHover();+ }+ }}+ >+ <NavigationMenuPrimitive.List+ ref={navListRef}+ data-pill={hoverStyle.opacity > 0 ? "true" : undefined}+ className="group/nav relative flex"+ onMouseLeave={() => {+ const list = navListRef.current;+ const openTrigger = list?.querySelector<HTMLElement>(+ "[data-state=open]",+ );+ // Keep the pill on the open trigger so it doesn't flicker into the dropdown+ if (list && openTrigger) {+ setHoverStyle(getHoverStyle(list, openTrigger));+ } else {+ hideHover();+ }}}>- <NavigationMenuPrimitive.List- ref={navListRef}- className="relative flex"- onMouseLeave={handleNavMouseLeave}- >- <motion.div- className="pointer-events-none absolute top-0 h-8 rounded-lg bg-neutral-900/5 dark:bg-white/10"- animate={hoverStyle}- transition={{- type: "spring",- stiffness: 400,- damping: 30,- }}- />- {navItems- .filter(({ mobileOnly }) => !mobileOnly)- .map(({ name, href, segments, content: Content }) => {- const isActive = (segments ?? []).some((segment) =>- pathname?.startsWith(segment),- );- return (- <NavigationMenuPrimitive.Item key={name} value={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}- data-active={isActive}- onMouseEnter={(e) =>- moveHoverTo(e.currentTarget)- }- >- {name}- </Link>- ) : (- <button- className={navItemClassName}- data-active={isActive}- onMouseEnter={(e) =>- moveHoverTo(e.currentTarget)- }- >- {name}- <AnimatedChevron className="ml-1.5 size-2.5 text-neutral-700" />- </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>+ <motion.div+ className="pointer-events-none absolute top-0 h-8 rounded-lg bg-neutral-900/5 dark:bg-white/10"+ animate={hoverStyle}+ transition={pillSpring}+ />+ {navItems+ .filter(({ mobileOnly }) => !mobileOnly)+ .map(({ name, href, segments, content: Content }) => {+ const isActive = (segments ?? []).some((segment) =>+ pathname?.startsWith(segment),+ );+ return (+ <NavigationMenuPrimitive.Item key={name} value={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}+ data-active={isActive}+ onMouseEnter={(e) => showHover(e.currentTarget)}+ >+ {name}+ </Link>+ ) : (+ <button+ className={navItemClassName}+ data-active={isActive}+ onMouseEnter={(e) => showHover(e.currentTarget)}+ >+ {name}+ <AnimatedChevron className="ml-1.5 size-2.5 text-neutral-700" />+ </button>)}- </NavigationMenuPrimitive.Item>- );- })}- </NavigationMenuPrimitive.List>+ </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>- {/* Positioned against MaxWidthWrapper so the dropdown centers on the page */}- <div className="absolute left-1/2 top-full mt-3 -translate-x-1/2">- <NavigationMenuPrimitive.Viewport+ {/* Positioned against MaxWidthWrapper so the dropdown centers on the page */}+ <div className="absolute left-1/2 top-full mt-3 -translate-x-1/2">+ <NavigationMenuPrimitive.Viewport+ className={cn(+ "relative flex origin-top justify-start overflow-hidden rounded-[20px] border border-neutral-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>++ <div className="hidden grow basis-0 justify-end gap-2 lg:flex">+ {session && Object.keys(session).length > 0 ? (+ <Link+ 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",+ )}+ >+ Dashboard+ </Link>+ ) : !isLoading ? (+ <>+ <Link+ href="https://app.dub.co/login"className={cn(- "relative flex origin-top justify-start overflow-hidden rounded-[20px] border border-neutral-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]",+ 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",)}- />- </div>- </NavigationMenuPrimitive.Root>-- <div className="hidden grow basis-0 justify-end gap-2 lg:flex">- {session && Object.keys(session).length > 0 ? (+ >+ Log in+ </Link><Link- href={APP_DOMAIN}+ href="https://app.dub.co/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",)}>- Dashboard+ Sign up</Link>- ) : !isLoading ? (- <>- <Link- href="https://app.dub.co/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="https://app.dub.co/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>+ </>+ ) : null}</div>- </MaxWidthWrapper>- </div>- </LayoutGroup>+ </div>+ </MaxWidthWrapper>+ </div></NavContext.Provider>);}