The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
"use client";
import { cn } from "@dub/utils";
import * as PopoverPrimitive from "@radix-ui/react-popover";
import {
PropsWithChildren,
ReactNode,
RefObject,
WheelEventHandler,
} from "react";
import { createPortal } from "react-dom";
import { Drawer } from "vaul";
import { useMediaQuery } from "./hooks";
export type PopoverProps = PropsWithChildren<{
content: ReactNode | string;
align?: "center" | "start" | "end";
side?: "bottom" | "top" | "left" | "right";
openPopover: boolean;
setOpenPopover: (open: boolean) => void;
mobileOnly?: boolean;
forceDropdown?: boolean;
popoverContentClassName?: string;
onOpenAutoFocus?: PopoverPrimitive.PopoverContentProps["onOpenAutoFocus"];
onCloseAutoFocus?: PopoverPrimitive.PopoverContentProps["onCloseAutoFocus"];
collisionBoundary?: Element | Element[];
sticky?: "partial" | "always";
onEscapeKeyDown?: (event: KeyboardEvent) => void;
onWheel?: WheelEventHandler;
sideOffset?: number;
collisionPadding?: PopoverPrimitive.PopoverContentProps["collisionPadding"];
anchor?: ReactNode;
virtualAnchorRef?: RefObject<HTMLElement | null>;
}>;
export function Popover({
children,
content,
align = "center",
side = "bottom",
openPopover,
setOpenPopover,
mobileOnly,
forceDropdown,
popoverContentClassName,
onOpenAutoFocus,
onCloseAutoFocus,
collisionBoundary,
sticky,
onEscapeKeyDown,
onWheel,
sideOffset = 8,
collisionPadding,
anchor,
virtualAnchorRef,
}: PopoverProps) {
const { isMobile } = useMediaQuery();
if (!forceDropdown && (mobileOnly || isMobile)) {
return (
<Drawer.Root open={openPopover} onOpenChange={setOpenPopover}>
<Drawer.Trigger className="sm:hidden" asChild>
{children}
</Drawer.Trigger>
<Drawer.Portal>
<Drawer.Overlay className="bg-bg-subtle fixed inset-0 z-50 bg-opacity-10 backdrop-blur" />
<Drawer.Content
className="border-border-subtle bg-bg-default fixed bottom-0 left-0 right-0 z-50 mt-24 rounded-t-[10px] border-t"
onEscapeKeyDown={onEscapeKeyDown}
onPointerDownOutside={(e) => {
// Prevent dismissal when clicking inside a toast
if (
e.target instanceof Element &&
e.target.closest("[data-sonner-toast]")
) {
e.preventDefault();
}
}}
>
<div className="sticky top-0 z-20 flex w-full items-center justify-center rounded-t-[10px] bg-inherit">
<div className="bg-border-default my-3 h-1 w-12 rounded-full" />
</div>
<div className="bg-bg-default flex w-full items-center justify-center overflow-hidden pb-4 align-middle shadow-xl">
{content}
</div>
</Drawer.Content>
<Drawer.Overlay />
</Drawer.Portal>
</Drawer.Root>
);
}
return (
<PopoverPrimitive.Root open={openPopover} onOpenChange={setOpenPopover}>
{anchor &&
typeof document !== "undefined" &&
createPortal(
<PopoverPrimitive.Anchor asChild>{anchor}</PopoverPrimitive.Anchor>,
document.body,
)}
{virtualAnchorRef && (
<PopoverPrimitive.Anchor
virtualRef={
virtualAnchorRef as unknown as RefObject<{
getBoundingClientRect(): DOMRect;
}>
}
/>
)}
<PopoverPrimitive.Trigger className="sm:inline-flex" asChild>
{children}
</PopoverPrimitive.Trigger>
<PopoverPrimitive.Portal>
<PopoverPrimitive.Content
sideOffset={sideOffset}
align={align}
side={side}
className={cn(
"animate-slide-up-fade border-border-subtle bg-bg-default z-50 items-center rounded-lg border drop-shadow-lg sm:block",
popoverContentClassName,
)}
sticky={sticky}
collisionBoundary={collisionBoundary}
collisionPadding={collisionPadding}
onOpenAutoFocus={onOpenAutoFocus}
onCloseAutoFocus={onCloseAutoFocus}
onEscapeKeyDown={onEscapeKeyDown}
onWheel={onWheel}
>
{content}
</PopoverPrimitive.Content>
</PopoverPrimitive.Portal>
</PopoverPrimitive.Root>
);
}