Open History to see the last commit
1import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
2
3import { cn } from "~/lib/utils";
4
5import { renderClosingPositioner } from "./closing-positioner";
6import { OpenPopupRegistration, PopupActionsProvider, usePopupActionsRef } from "./open-popups";
7
8const PopoverCreateHandle = PopoverPrimitive.createHandle;
9
10function Popover<Payload = unknown>(props: PopoverPrimitive.Root.Props<Payload>) {
11 const actionsRef = usePopupActionsRef(props.actionsRef);
12 return (
13 <PopupActionsProvider value={actionsRef}>
14 <PopoverPrimitive.Root {...props} actionsRef={actionsRef} />
15 </PopupActionsProvider>
16 );
17}
18
19function PopoverTrigger({ className, children, ...props }: PopoverPrimitive.Trigger.Props) {
20 return (
21 <PopoverPrimitive.Trigger className={className} data-slot="popover-trigger" {...props}>
22 {children}
23 </PopoverPrimitive.Trigger>
24 );
25}
26
27const popoverPopupWidthClassName = {
28 auto: "",
29 sm: "w-64",
30 md: "w-80",
31 lg: "w-96",
32} as const;
33
34const popoverViewportPaddingClassName = {
35 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
36 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
37 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
38} as const;
39
40function PopoverPopup({
41 children,
42 className,
43 padding = "default",
44 variant = "default",
45 width = "auto",
46 side = "bottom",
47 align = "center",
48 sideOffset = 4,
49 alignOffset = 0,
50 collisionAvoidance,
51 tooltipStyle = false,
52 keepMounted = false,
53 anchor,
54 touchBackdrop = false,
55 ...props
56}: PopoverPrimitive.Popup.Props & {
57 padding?: keyof typeof popoverViewportPaddingClassName;
58 variant?: "default" | "panel";
59 side?: PopoverPrimitive.Positioner.Props["side"];
60 align?: PopoverPrimitive.Positioner.Props["align"];
61 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
62 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
63 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
64 tooltipStyle?: boolean;
65 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
66 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
67 width?: keyof typeof popoverPopupWidthClassName;
68 touchBackdrop?: boolean;
69}) {
70 // Viewport rekeys on close; persistent single-trigger forms need stable containers for drafts and submit
71 // guards.
72 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
73 return (
74 <PopoverPrimitive.Portal keepMounted={keepMounted}>
75 {/* A kept-mounted portal stays mounted while closed, so it cannot tell when it is open. */}
76 {keepMounted ? null : <OpenPopupRegistration />}
77 {touchBackdrop ? (
78 // On touch, the backdrop consumes outside taps so closing cannot also activate the control beneath.
79 <PopoverPrimitive.Backdrop
80 className="fixed inset-0 z-[130] hidden pointer-coarse:block"
81 data-slot="popover-backdrop"
82 />
83 ) : null}
84 <PopoverPrimitive.Positioner
85 align={align}
86 alignOffset={alignOffset}
87 anchor={anchor}
88 collisionAvoidance={collisionAvoidance}
89 collisionPadding={variant === "panel" ? 0 : undefined}
90 className={cn(
91 "h-(--positioner-height) max-h-(--available-height) w-(--positioner-width) max-w-(--available-width)",
92 // Kept-mounted positioners first paint at the window origin, so a transform transition slides them in from there.
93 !keepMounted && "transition-transform data-instant:transition-none",
94 // A panel opened as a popover joins the popover tier (50), so a resized preview
95 // mini-player (47–49) never paints over it.
96 variant === "panel"
97 ? "z-50 w-[min(calc(var(--thread-details-panel-width)+--spacing(4)),calc(var(--anchor-width)---spacing(2)))] transition-[top] duration-150 ease-out data-instant:transition-[top] motion-reduce:transition-none motion-reduce:data-instant:transition-none"
98 : "z-[130]",
99 )}
100 data-slot="popover-positioner"
101 render={renderClosingPositioner}
102 side={side}
103 sideOffset={sideOffset}
104 >
105 <PopoverPrimitive.Popup
106 className={cn(
107 "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-ending-style:scale-98 data-starting-style:scale-98 data-ending-style:opacity-0 data-starting-style:opacity-0 dark:before:shadow-[0_-1px_--theme(--color-white/6%)]",
108 variant !== "panel" && "dropdown-glass",
109 // Without it, Chromium re-snaps text after the scale-in at fractional zoom, so hovered rows jump a pixel.
110 "will-change-[scale]",
111 // Electron drag hit-testing ignores z-index; popups over a drag region would swallow hover.
112 "[-webkit-app-region:no-drag]",
113 tooltipStyle &&
114 "w-fit rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
115 !tooltipStyle &&
116 variant !== "panel" &&
117 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
118 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
119 variant === "panel" &&
120 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
121 className,
122 )}
123 data-slot="popover-popup"
124 {...props}
125 >
126 <Viewport
127 className={cn(
128 "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",
129 tooltipStyle && padding === "default"
130 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
131 : popoverViewportPaddingClassName[padding],
132 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
133 variant === "panel" &&
134 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
135 )}
136 data-slot="popover-viewport"
137 >
138 {children}
139 </Viewport>
140 </PopoverPrimitive.Popup>
141 </PopoverPrimitive.Positioner>
142 </PopoverPrimitive.Portal>
143 );
144}
145
146function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
147 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
148}
149
150function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
151 return (
152 <PopoverPrimitive.Title
153 className={cn("font-semibold text-sm leading-none", className)}
154 data-slot="popover-title"
155 {...props}
156 />
157 );
158}
159
160export {
161 PopoverCreateHandle,
162 Popover,
163 PopoverTrigger,
164 PopoverPopup,
165 PopoverPopup as PopoverContent,
166 PopoverTitle,
167 PopoverClose,
168};