Open History to see the last commit
1"use client";
2
3import { Popover as PopoverPrimitive } from "@base-ui/react/popover";
4
5import { usePortalContainer } from "~/lib/portalContainer";
6import { cn } from "~/lib/utils";
7
8const PopoverCreateHandle = PopoverPrimitive.createHandle;
9
10const Popover = PopoverPrimitive.Root;
11
12function PopoverTrigger({ className, children, ...props }: PopoverPrimitive.Trigger.Props) {
13 return (
14 <PopoverPrimitive.Trigger className={className} data-slot="popover-trigger" {...props}>
15 {children}
16 </PopoverPrimitive.Trigger>
17 );
18}
19
20const popoverPopupWidthClassName = {
21 auto: "",
22 sm: "w-64",
23 md: "w-80",
24 lg: "w-96",
25} as const;
26
27const popoverViewportPaddingClassName = {
28 default: "py-4 [--viewport-inline-padding:--spacing(4)]",
29 compact: "py-2 [--viewport-inline-padding:--spacing(3)]",
30 none: "rounded-[calc(var(--radius-lg)-1px)] py-0 [--viewport-inline-padding:0px]",
31} as const;
32
33function PopoverPopup({
34 children,
35 className,
36 padding = "default",
37 variant = "default",
38 width = "auto",
39 side = "bottom",
40 align = "center",
41 sideOffset = 4,
42 alignOffset = 0,
43 collisionAvoidance,
44 tooltipStyle = false,
45 keepMounted = false,
46 anchor,
47 touchBackdrop = false,
48 ...props
49}: PopoverPrimitive.Popup.Props & {
50 padding?: keyof typeof popoverViewportPaddingClassName;
51 variant?: "default" | "panel";
52 side?: PopoverPrimitive.Positioner.Props["side"];
53 align?: PopoverPrimitive.Positioner.Props["align"];
54 sideOffset?: PopoverPrimitive.Positioner.Props["sideOffset"];
55 alignOffset?: PopoverPrimitive.Positioner.Props["alignOffset"];
56 collisionAvoidance?: PopoverPrimitive.Positioner.Props["collisionAvoidance"];
57 tooltipStyle?: boolean;
58 keepMounted?: PopoverPrimitive.Portal.Props["keepMounted"];
59 anchor?: PopoverPrimitive.Positioner.Props["anchor"];
60 width?: keyof typeof popoverPopupWidthClassName;
61 touchBackdrop?: boolean;
62}) {
63 // Viewport rekeys its children when the active trigger clears on close. Persistent
64 // single-trigger forms need a stable container to retain drafts and submit guards.
65 const Viewport = keepMounted ? "div" : PopoverPrimitive.Viewport;
66 return (
67 <PopoverPrimitive.Portal container={usePortalContainer()} keepMounted={keepMounted}>
68 {touchBackdrop ? (
69 // A non-modal popover closes on an outside tap and lets that tap reach the
70 // control under the finger. On touch screens the backdrop takes the tap, so
71 // tapping away only closes the popover, as it does for modal menus and selects.
72 <PopoverPrimitive.Backdrop
73 className="fixed inset-0 z-[130] hidden pointer-coarse:block"
74 data-slot="popover-backdrop"
75 />
76 ) : null}
77 <PopoverPrimitive.Positioner
78 align={align}
79 alignOffset={alignOffset}
80 anchor={anchor}
81 collisionAvoidance={collisionAvoidance}
82 collisionPadding={variant === "panel" ? 0 : undefined}
83 className={cn(
84 "h-(--positioner-height) w-(--positioner-width) max-w-(--available-width) transition-transform data-instant:transition-none",
85 variant === "panel"
86 ? "z-(--z-sheet) 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"
87 : "z-[130]",
88 )}
89 data-slot="popover-positioner"
90 side={side}
91 sideOffset={sideOffset}
92 >
93 <PopoverPrimitive.Popup
94 className={cn(
95 "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%)]",
96 variant !== "panel" && "dropdown-glass",
97 tooltipStyle &&
98 "w-fit text-balance rounded-md text-xs shadow-md/5 before:rounded-[calc(var(--radius-md)-1px)]",
99 !tooltipStyle &&
100 variant !== "panel" &&
101 "shadow-[0_16px_40px_-18px_rgb(0_0_0/55%)] dark:shadow-[0_18px_44px_-18px_rgb(0_0_0/80%)]",
102 width !== "auto" && ["max-w-[calc(100vw-2rem)]", popoverPopupWidthClassName[width]],
103 variant === "panel" &&
104 "w-full overflow-visible rounded-none border-0 bg-transparent shadow-none before:hidden [backdrop-filter:none] [-webkit-backdrop-filter:none]",
105 className,
106 )}
107 data-slot="popover-popup"
108 {...props}
109 >
110 <Viewport
111 className={cn(
112 "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",
113 tooltipStyle && padding === "default"
114 ? "py-1 [--viewport-inline-padding:--spacing(2)]"
115 : popoverViewportPaddingClassName[padding],
116 !tooltipStyle && "not-data-transitioning:overflow-y-auto",
117 variant === "panel" &&
118 "overflow-visible py-2 [--viewport-inline-padding:--spacing(2)]",
119 )}
120 data-slot="popover-viewport"
121 >
122 {children}
123 </Viewport>
124 </PopoverPrimitive.Popup>
125 </PopoverPrimitive.Positioner>
126 </PopoverPrimitive.Portal>
127 );
128}
129
130function PopoverClose({ ...props }: PopoverPrimitive.Close.Props) {
131 return <PopoverPrimitive.Close data-slot="popover-close" {...props} />;
132}
133
134function PopoverTitle({ className, ...props }: PopoverPrimitive.Title.Props) {
135 return (
136 <PopoverPrimitive.Title
137 className={cn("font-semibold text-sm leading-none", className)}
138 data-slot="popover-title"
139 {...props}
140 />
141 );
142}
143
144export {
145 PopoverCreateHandle,
146 Popover,
147 PopoverTrigger,
148 PopoverPopup,
149 PopoverPopup as PopoverContent,
150 PopoverTitle,
151 PopoverClose,
152};