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
28// The inset around the content. "compact" suits dense content (a list, a code excerpt, a
29// row of reactions); "none" is for content that draws its own frame edge to edge.
30const popoverViewportPaddingClassName = {
31 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
32 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
33 // Rounded to the popup so edge-to-edge content clips to its corners.
34 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
35} as const;
36
37function PopoverPopup({
38 children,
39 className,
40 padding = "default",
41 variant = "default",
42 width = "auto",
43 side = "bottom",
44 align = "center",
45 sideOffset = 4,
46 alignOffset = 0,
47 collisionAvoidance,
48 tooltipStyle = false,
49 keepMounted = false,
50 anchor,
51 ...props
52}: PopoverPrimitive.Popup.Props & {
53 padding?: keyof typeof popoverViewportPaddingClassName;
54 variant?: "default" | "panel";
55 side?: PopoverPrimitive.Positioner.Props["side"];
56 align?: PopoverPrimitive.Positioner.Props["align"];
57 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
58 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
59 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
60 tooltipStyle?: boolean;
61 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
62 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
63 width?: keyof typeof popoverPopupWidthClassName;
64}) {
65 // Viewport rekeys its children when the active trigger clears on close. Persistent
66 // single-trigger forms need a stable container to retain drafts and submit guards.
67 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
68 return (
69 <PopoverPrimitive.Portal keepMounted={keepMounted}>
70 <PopoverPrimitive.Positioner
71 align={align}
72 alignOffset={alignOffset}
73 anchor={anchor}
74 collisionAvoidance={collisionAvoidance}
75 className={cn(
76 "z-[130] h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
77 variant === "panel" &&
78 "w-[min(var(--thread-details-panel-width),var(--anchor-width))] transition-none",
79 )}
80 data-slot="popover-positioner"
81 side={side}
82 sideOffset={sideOffset}
83 >
84 <PopoverPrimitive.Popup
85 className={cn(
86 "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%)]",
87 variant !== "panel" && "dropdown-glass",
88 tooltipStyle &&
89 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
90 !tooltipStyle &&
91 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
92 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
93 variant === "panel" &&
94 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
95 className,
96 )}
97 data-slot="popover-popup"
98 {...props}
99 >
100 <Viewport
101 className={cn(
102 "relative size-full max-h-(--available-height) overflow-clip px-(--viewport-inline-padding) 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",
103 tooltipStyle && padding === "default"
104 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
105 : popoverViewportPaddingClassName[padding],
106 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
107 variant === "panel" &&
108 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
109 )}
110 data-slot="popover-viewport"
111 >
112 {children}
113 </Viewport>
114 </PopoverPrimitive.Popup>
115 </PopoverPrimitive.Positioner>
116 </PopoverPrimitive.Portal>
117 );
118}
119
120function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
121 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
122}
123
124function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
125 return (
126 <PopoverPrimitive.Title
127 className={cn("font-semibold text-sm leading-none", className)}
128 data-slot="popover-title"
129 {...props}
130 />
131 );
132}
133
134export {
135 PopoverCreateHandle,
136 Popover,
137 PopoverTrigger,
138 PopoverPopup,
139 PopoverPopup as PopoverContent,
140 PopoverTitle,
141 PopoverClose,
142};