Open History to see the last commit
1"use client";
2
3import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
4
5import { cn } from "~/lib/utils";
6
7const PopoverCreateHandle = PopoverPrimitive.createHandle;
8
9const Popover = PopoverPrimitive.Root;
10
11function PopoverTrigger({ className, children, ...props }: PopoverPrimitive.Trigger.Props) {
12 return (
13 <PopoverPrimitive.Trigger className={className} data-slot="popover-trigger" {...props}>
14 {children}
15 </PopoverPrimitive.Trigger>
16 );
17}
18
19// Popovers hold prose and forms, so a width is fixed rather than a minimum,
20// and every width is capped to the viewport.
21const popoverPopupWidthClassName = {
22 auto: "",
23 sm: "w-64",
24 md: "w-80",
25 lg: "w-96",
26} as const;
27
28function PopoverPopup({
29 children,
30 className,
31 viewportClassName,
32 width = "auto",
33 side = "bottom",
34 align = "center",
35 sideOffset = 4,
36 alignOffset = 0,
37 tooltipStyle = false,
38 keepMounted = false,
39 anchor,
40 ...props
41}: PopoverPrimitive.Popup.Props & {
42 viewportClassName?: string;
43 side?: PopoverPrimitive.Positioner.Props["side"];
44 align?: PopoverPrimitive.Positioner.Props["align"];
45 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
46 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
47 tooltipStyle?: boolean;
48 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
49 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
50 width?: keyof typeof popoverPopupWidthClassName;
51}) {
52 // Viewport rekeys its children when the active trigger clears on close. Persistent
53 // single-trigger forms need a stable container to retain drafts and submit guards.
54 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
55 return (
56 <PopoverPrimitive.Portal keepMounted={keepMounted}>
57 <PopoverPrimitive.Positioner
58 align={align}
59 alignOffset={alignOffset}
60 anchor={anchor}
61 className="z-[130] h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none"
62 data-slot="popover-positioner"
63 side={side}
64 sideOffset={sideOffset}
65 >
66 <PopoverPrimitive.Popup
67 className={cn(
68 "dropdown-glass relative flex h-(--popup-height,auto) w-(--popup-width,auto) origin-(--transform-origin) rounded-lg text-popover-foreground outline-none transition-[width,height,scale,opacity] before:pointer-events-none before:absolute before:inset-0 before:rounded-[calc(var(--radius-lg)-1px)] before:shadow-[0_1px_--theme(--color-black/4%)] has-data-[slot=calendar]:rounded-xl has-data-[slot=calendar]:before:rounded-[calc(var(--radius-xl)-1px)] data-starting-style:scale-98 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
69 tooltipStyle &&
70 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
71 !tooltipStyle &&
72 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
73 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
74 className,
75 )}
76 data-slot="popover-popup"
77 {...props}
78 >
79 <Viewport
80 className={cn(
81 "relative size-full max-h-(--available-height) overflow-clip px-(--viewport-inline-padding) py-4 [--viewport-inline-padding:--spacing(4)] has-data-[slot=calendar]:p-2 data-instant:transition-none **:data-current:data-ending-style:opacity-0 **:data-current:data-starting-style:opacity-0 **:data-previous:data-ending-style:opacity-0 **:data-previous:data-starting-style:opacity-0 **:data-current:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-previous:w-[calc(var(--popup-width)-2*var(--viewport-inline-padding)-2px)] **:data-current:opacity-100 **:data-previous:opacity-100 **:data-current:transition-opacity **:data-previous:transition-opacity",
82 tooltipStyle
83 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
84 : "not-data-transitioning:overflow-y-auto",
85 viewportClassName,
86 )}
87 data-slot="popover-viewport"
88 >
89 {children}
90 </Viewport>
91 </PopoverPrimitive.Popup>
92 </PopoverPrimitive.Positioner>
93 </PopoverPrimitive.Portal>
94 );
95}
96
97function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
98 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
99}
100
101function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
102 return (
103 <PopoverPrimitive.Title
104 className={cn("font-semibold text-sm leading-none", className)}
105 data-slot="popover-title"
106 {...props}
107 />
108 );
109}
110
111export {
112 PopoverCreateHandle,
113 Popover,
114 PopoverTrigger,
115 PopoverPopup,
116 PopoverPopup as PopoverContent,
117 PopoverTitle,
118 PopoverClose,
119};