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